150 lines
5.0 KiB
PHP
150 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\BusProNet\XmlParser;
|
|
|
|
use App\BusProNet\Model\CrmAttributes;
|
|
use App\BusProNet\XmlCrawlerFactory;
|
|
use App\BusProNet\XmlParser\CrmAttributesResponseParser;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class CrmAttributesResponseParserTest extends TestCase
|
|
{
|
|
private const HOUSE_MANAGER_IDS = [
|
|
2001 => 'DKS',
|
|
2002 => 'XYZ',
|
|
2003 => 'ASB',
|
|
];
|
|
|
|
private CrmAttributesResponseParser $parser;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->parser = new CrmAttributesResponseParser(self::HOUSE_MANAGER_IDS);
|
|
}
|
|
|
|
public function testParseAssignsGroupsManagerRoleWhenSelected(): void
|
|
{
|
|
$roles = $this->parseRoles($this->selectionXml(1477, true));
|
|
|
|
self::assertContains('ROLE_GROUPS_MANAGER', $roles);
|
|
self::assertNotContains('ROLE_GROUPS_ADMIN', $roles);
|
|
}
|
|
|
|
public function testParseAssignsGroupsAdminRoleWhenSelected(): void
|
|
{
|
|
$roles = $this->parseRoles($this->selectionXml(1478, true));
|
|
|
|
self::assertContains('ROLE_GROUPS_ADMIN', $roles);
|
|
self::assertNotContains('ROLE_GROUPS_MANAGER', $roles);
|
|
}
|
|
|
|
public function testParseAssignsNoGroupsRolesWhenNotSelected(): void
|
|
{
|
|
$roles = $this->parseRoles($this->selectionXml(1477, false));
|
|
|
|
self::assertSame([], $roles, 'the parser reports what BusPro says and adds no fallback');
|
|
}
|
|
|
|
public function testParseStillAssignsExistingAdminManagerTeamerRoles(): void
|
|
{
|
|
$xmlContent = '<?xml version="1.0" encoding="utf-8"?>
|
|
<ergebnis>
|
|
<satz typ="KUNDENDATEN" />
|
|
<selektionsmerkmale>
|
|
<selektionsgruppe id="1" bezeichnung="Rollen">
|
|
<selektion id="1292" bezeichnung="Admin" aenderbar="0" auswahl="True" />
|
|
<selektion id="1293" bezeichnung="Manager" aenderbar="0" auswahl="True" />
|
|
<selektion id="1070" bezeichnung="Teamer" aenderbar="0" auswahl="True" />
|
|
</selektionsgruppe>
|
|
</selektionsmerkmale>
|
|
<crmaktionen></crmaktionen>
|
|
</ergebnis>';
|
|
|
|
$roles = $this->parseRoles($xmlContent);
|
|
|
|
self::assertContains('ROLE_ADMIN', $roles);
|
|
self::assertContains('ROLE_MANAGER', $roles);
|
|
self::assertContains('ROLE_TEAMER', $roles);
|
|
}
|
|
|
|
public function testParseCollectsTheHotelCodesOfEveryHausleitungSelection(): void
|
|
{
|
|
$attributes = $this->parse($this->hausleitungXml());
|
|
|
|
self::assertSame(['ROLE_HOUSE_MANAGER'], $attributes->roles);
|
|
self::assertSame(['DKS', 'ASB'], $attributes->hotelCodes);
|
|
}
|
|
|
|
public function testParseIgnoresHausleitungSelectionsThatAreNotMapped(): void
|
|
{
|
|
// A house that is deliberately left out of bpn_crm_house_manager_ids claims nothing —
|
|
// matching is by id, never by label.
|
|
$parser = new CrmAttributesResponseParser([]);
|
|
$attributes = $parser->parse(XmlCrawlerFactory::create($this->hausleitungXml())->filterXPath('//ergebnis'));
|
|
|
|
self::assertSame([], $attributes->roles);
|
|
self::assertSame([], $attributes->hotelCodes);
|
|
}
|
|
|
|
public function testParseAddsNoDefaultHotelCodeForAdmins(): void
|
|
{
|
|
// Hotel codes are synced on every login now, so a default would permanently grant a
|
|
// house to every administrator.
|
|
$attributes = $this->parse($this->selectionXml(1292, true));
|
|
|
|
self::assertSame(['ROLE_ADMIN'], $attributes->roles);
|
|
self::assertSame([], $attributes->hotelCodes);
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
private function parseRoles(string $xmlContent): array
|
|
{
|
|
return $this->parse($xmlContent)->roles;
|
|
}
|
|
|
|
private function parse(string $xmlContent): CrmAttributes
|
|
{
|
|
$crawler = XmlCrawlerFactory::create($xmlContent);
|
|
$resultNode = $crawler->filterXPath('//ergebnis');
|
|
|
|
return $this->parser->parse($resultNode);
|
|
}
|
|
|
|
private function hausleitungXml(): string
|
|
{
|
|
// Two groups, so the codes have to survive both closure levels of parse().
|
|
return '<?xml version="1.0" encoding="utf-8"?>
|
|
<ergebnis>
|
|
<satz typ="KUNDENDATEN" />
|
|
<selektionsmerkmale>
|
|
<selektionsgruppe id="1" bezeichnung="Rollen">
|
|
<selektion id="2001" bezeichnung="Hausleitung DKS" aenderbar="0" auswahl="True" />
|
|
<selektion id="2002" bezeichnung="Hausleitung XYZ" aenderbar="0" auswahl="False" />
|
|
</selektionsgruppe>
|
|
<selektionsgruppe id="2" bezeichnung="Häuser">
|
|
<selektion id="2003" bezeichnung="Hausleitung ASB" aenderbar="0" auswahl="True" />
|
|
</selektionsgruppe>
|
|
</selektionsmerkmale>
|
|
<crmaktionen></crmaktionen>
|
|
</ergebnis>';
|
|
}
|
|
|
|
private function selectionXml(int $id, bool $selected): string
|
|
{
|
|
return sprintf('<?xml version="1.0" encoding="utf-8"?>
|
|
<ergebnis>
|
|
<satz typ="KUNDENDATEN" />
|
|
<selektionsmerkmale>
|
|
<selektionsgruppe id="1" bezeichnung="Rollen">
|
|
<selektion id="%d" bezeichnung="Gruppenreisen" aenderbar="0" auswahl="%s" />
|
|
</selektionsgruppe>
|
|
</selektionsmerkmale>
|
|
<crmaktionen></crmaktionen>
|
|
</ergebnis>', $id, $selected ? 'True' : 'False');
|
|
}
|
|
}
|