Files
ephub/tests/Controller/AccessCodeControllerTest.php
T
2026-10-05 17:10:02 +02:00

223 lines
8.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller;
use App\Entity\AccessCode;
use App\Entity\User;
use App\Enum\CodeVisibility;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* The quick look: one card per door with the code that works today.
*
* Most of what is asserted here is what the page does *not* show -- a code that is not in force
* is not an answer to "what do I type", and a code somebody may not read must not reach them at
* all. The full list is the admin table's job and is tested there.
*/
class AccessCodeControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $em;
protected function setUp(): void
{
$this->client = self::createClient(server: ['HTTPS' => true]);
$this->client->disableReboot();
$this->em = self::getContainer()->get(EntityManagerInterface::class);
$this->em->getConnection()->beginTransaction();
}
protected function tearDown(): void
{
$this->em->getConnection()->rollBack();
parent::tearDown();
}
public function testItIsBehindTheFirewall(): void
{
$this->client->request('GET', '/access-codes');
self::assertResponseRedirects();
}
public function testAnEmployeeSeesACardPerDoor(): void
{
$this->code('Aufzug', '4711', description: 'Kabine im Erdgeschoss');
$this->code('Schranke', '1234');
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$cards = $this->client->request('GET', '/access-codes')->filter('[data-access-code]');
self::assertResponseIsSuccessful();
self::assertCount(2, $cards);
self::assertStringContainsString('Aufzug', $cards->eq(0)->text());
self::assertStringContainsString('Kabine im Erdgeschoss', $cards->eq(0)->text());
self::assertSame('4711', $cards->eq(0)->filter('[data-code]')->text());
self::assertSame('1234', $cards->eq(1)->filter('[data-code]')->text());
}
/**
* The restriction is a filter on the query, not a hidden card: a code an employee may not
* read must not be in the response at all.
*/
public function testAnAdminOnlyCodeIsNotSentToAnEmployee(): void
{
$this->code('Serverraum', '9999', visibility: CodeVisibility::ADMIN);
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$crawler = $this->client->request('GET', '/access-codes');
self::assertCount(0, $crawler->filter('[data-access-code]'));
self::assertStringNotContainsString('9999', (string) $this->client->getResponse()->getContent());
$this->client->loginUser($this->user('[email protected]', ['ROLE_ADMIN']));
$card = $this->client->request('GET', '/access-codes')->filter('[data-access-code]');
self::assertCount(1, $card);
self::assertSame('9999', $card->filter('[data-code]')->text());
self::assertStringContainsString('Administration', $card->text());
}
/**
* A supervisor code is read by supervisors and by administrators, who hold no
* ROLE_SUPERVISOR of their own -- role_hierarchy does not reach the stored roles the query
* filters on. And a supervisor is not an administrator: the admin-only code stays out of
* their response.
*/
public function testASupervisorCodeIsReadBySupervisorsAndAdminsOnly(): void
{
$this->code('Management office', '5555', visibility: CodeVisibility::SUPERVISOR);
$this->code('Serverraum', '9999', visibility: CodeVisibility::ADMIN);
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$this->client->request('GET', '/access-codes');
self::assertStringNotContainsString('5555', (string) $this->client->getResponse()->getContent());
$this->client->loginUser($this->user('[email protected]', ['ROLE_EMPLOYEE', 'ROLE_SUPERVISOR']));
$card = $this->client->request('GET', '/access-codes')->filter('[data-access-code]');
self::assertCount(1, $card);
self::assertSame('5555', $card->filter('[data-code]')->text());
// The visibility caption the page shows, which is German like all user-facing text.
self::assertStringContainsString('Leitung', $card->text());
self::assertStringNotContainsString('9999', (string) $this->client->getResponse()->getContent());
$this->client->loginUser($this->user('[email protected]', ['ROLE_ADMIN']));
self::assertCount(2, $this->client->request('GET', '/access-codes')->filter('[data-access-code]'));
}
/**
* A supervisor reads codes but does not manage them.
*/
public function testASupervisorIsNotOfferedTheTable(): void
{
$this->client->loginUser($this->user('[email protected]', ['ROLE_EMPLOYEE', 'ROLE_SUPERVISOR']));
$crawler = $this->client->request('GET', '/access-codes');
self::assertCount(0, $crawler->filter('a[href="/admin/access-codes"]'));
}
/**
* Rotating a code. Only the one in force is shown: its predecessor is history, and its
* successor does not open anything yet -- both would only be another number to mistype.
*/
public function testOnlyTheCodeInForceIsShown(): void
{
$this->code('Aufzug', 'OLD', validFrom: '-2 months');
$this->code('Aufzug', 'NOW', validFrom: '-1 day');
$this->code('Aufzug', 'NEXT', validFrom: '+1 month');
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$cards = $this->client->request('GET', '/access-codes')->filter('[data-access-code]');
self::assertCount(1, $cards);
self::assertSame('NOW', $cards->filter('[data-code]')->text());
$body = (string) $this->client->getResponse()->getContent();
self::assertStringNotContainsString('OLD', $body);
self::assertStringNotContainsString('NEXT', $body);
}
/**
* A code that starts on a date starts that morning, not the day after.
*/
public function testACodeStartingTodayIsInForce(): void
{
$this->code('Aufzug', '4711', validFrom: 'today');
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$cards = $this->client->request('GET', '/access-codes')->filter('[data-access-code]');
self::assertCount(1, $cards);
self::assertSame('4711', $cards->filter('[data-code]')->text());
}
/**
* A door that only has a code for next week is simply not on the page yet.
*/
public function testADoorWithNoCodeInForceIsAbsent(): void
{
$this->code('Schranke', 'SOON', validFrom: '+1 week');
$this->em->flush();
$this->client->loginUser($this->user('[email protected]'));
$crawler = $this->client->request('GET', '/access-codes');
self::assertCount(0, $crawler->filter('[data-access-code]'));
self::assertStringContainsString('Zurzeit gilt kein Code', $crawler->filter('body')->text());
}
public function testOnlyAnAdminIsOfferedTheTable(): void
{
$this->client->loginUser($this->user('[email protected]'));
self::assertCount(0, $this->client->request('GET', '/access-codes')->filter('a[href="/admin/access-codes"]'));
$this->client->loginUser($this->user('[email protected]', ['ROLE_ADMIN']));
self::assertCount(1, $this->client->request('GET', '/access-codes')->filter('a[href="/admin/access-codes"]'));
}
public function testTheDashboardLinksHereForEveryone(): void
{
$this->client->loginUser($this->user('[email protected]'));
self::assertCount(1, $this->client->request('GET', '/')->filter('a[href="/access-codes"]'));
}
private function code(
string $label,
string $value,
string $validFrom = 'today',
?string $description = null,
CodeVisibility $visibility = CodeVisibility::EVERYONE,
): AccessCode {
$code = new AccessCode($label, $value, new \DateTimeImmutable($validFrom), $description, $visibility);
$this->em->persist($code);
return $code;
}
/**
* @param list<string> $roles
*/
private function user(string $email, array $roles = ['ROLE_EMPLOYEE']): User
{
$existing = $this->em->getRepository(User::class)->findOneBy(['email' => $email]);
if (null !== $existing) {
return $existing;
}
$user = new User($email, 'Test');
$user->setRoles($roles);
$this->em->persist($user);
$this->em->flush();
return $user;
}
}