Files
myep-team/tests/Controller/Administrative/System/StaffingRule/StaffingRuleControllerTest.php
T

193 lines
7.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace App\Tests\Controller\Administrative\System\StaffingRule;
use App\Entity\User;
use App\Repository\StaffingRuleRepository;
use App\Tests\Service\Assignment\StaffingFixturesTrait;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
class StaffingRuleControllerTest extends WebTestCase
{
use StaffingFixturesTrait;
private KernelBrowser $client;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
$this->client = static::createClient(server: ['HTTPS' => true]);
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->resetStaffingTables(self::getContainer()->get(Connection::class));
$this->entityManager->createQuery(sprintf('DELETE FROM %s', User::class))->execute();
$this->client->loginUser($this->user('ROLE_TEAM_ADMIN'));
}
public function testARuleIsCreatedWithItsCodeNormalized(): void
{
$this->submitCreate(' dkimp ');
$rules = $this->repository()->getList();
$this->assertCount(1, $rules);
$this->assertSame('DKIMP', $rules[0]->getProductCode());
$this->assertSame(2, $rules[0]->getGuides());
$this->assertTrue($rules[0]->isSkiBeginner());
$this->assertFalse($rules[0]->isEnabled());
$this->client->request('GET', '/administrative/system/staffing-rule');
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString('DKIMP', (string) $this->client->getResponse()->getContent());
}
public function testAManagerEditsTheMatrixToo(): void
{
$this->client->loginUser($this->user('ROLE_MANAGER'));
$this->submitCreate('DKIMP');
$this->assertCount(1, $this->repository()->getList());
$this->client->request('GET', '/administrative/system/staffing-rule/preview');
$this->assertResponseIsSuccessful();
}
public function testATeamerIsRefused(): void
{
$this->client->loginUser($this->user('ROLE_TEAMER'));
$this->client->request('GET', '/administrative/system/staffing-rule');
// the access-denied handler sends a teamer home instead of answering 403
$this->assertResponseRedirects('/teamer');
}
public function testASecondRowForTheSameProductIsRefused(): void
{
$this->submitCreate('DKIMP');
$this->submitCreate('dkimp');
$this->assertStringContainsString('Für dieses Produkt gibt es bereits eine Zeile', (string) $this->client->getResponse()->getContent());
$this->assertCount(1, $this->repository()->getList());
}
public function testADeletedRowFreesItsCode(): void
{
$this->submitCreate('DKIMP');
$this->repository()->getList()[0]->setDeleted();
$this->entityManager->flush();
$this->submitCreate('DKIMP');
$this->assertCount(1, $this->repository()->getList());
}
public function testThePreviewRendersWithoutCandidates(): void
{
$this->submitCreate('DKIMP');
$this->client->request('GET', '/administrative/system/staffing-rule/preview');
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString('Keine Termine', (string) $this->client->getResponse()->getContent());
}
public function testThePreviewShowsWhatWouldBeCreated(): void
{
$this->staffingRule($this->entityManager, 'DKIMP', enabled: false);
$this->staffingDestination($this->entityManager, 'DKIMP110127', 'DKIMP');
$this->entityManager->flush();
$this->client->request('GET', '/administrative/system/staffing-rule/preview');
$content = (string) $this->client->getResponse()->getContent();
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString('DKIMP110127', $content);
$this->assertStringContainsString('Zeile inaktiv', $content);
$this->assertSame(2, substr_count($content, '1× Reiseleitung'));
$this->assertSame(1, substr_count($content, 'Skigebietsguiding (pro Einheit)'));
$this->assertStringContainsString('Busbegleitung ab Göttingen, 22:00 Uhr', $content);
$this->assertStringContainsString('1× Kursleitung Ski Anfänger:innen', $content);
$this->assertStringContainsString('Ski-/Snowboardkurs (pro Stunde)', $content);
}
public function testThePreviewIsPaginated(): void
{
$this->staffingRule($this->entityManager, 'DKIMP');
$this->staffingRule($this->entityManager, 'DGSMP');
$this->staffingDestination($this->entityManager, 'DKIMP110127', 'DKIMP');
$this->staffingDestination($this->entityManager, 'DGSMP110127', 'DGSMP');
$this->entityManager->flush();
// same date: ordered by product code
$this->client->request('GET', '/administrative/system/staffing-rule/preview?limit=1&page=2');
$content = (string) $this->client->getResponse()->getContent();
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertStringContainsString('DKIMP110127', $content);
$this->assertStringNotContainsString('DGSMP110127', $content);
}
public function testThePreviewDownloadsAsXlsx(): void
{
$this->staffingRule($this->entityManager, 'DKIMP');
$this->staffingDestination($this->entityManager, 'DKIMP110127', 'DKIMP');
$this->entityManager->flush();
$this->client->request('GET', '/administrative/system/staffing-rule/export');
$this->assertSame(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$this->assertSame(
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
$this->client->getResponse()->headers->get('Content-Type'),
);
}
private function submitCreate(string $productCode): void
{
$crawler = $this->client->request('GET', '/administrative/system/staffing-rule/create');
$form = $crawler->filter('form')->form([
'staffing_rule[productCode]' => $productCode,
'staffing_rule[guides]' => '2',
]);
$form['staffing_rule[skiBeginner]']->tick();
$this->client->submit($form);
}
private function repository(): StaffingRuleRepository
{
$this->entityManager->clear();
return self::getContainer()->get(StaffingRuleRepository::class);
}
private function user(string $role): User
{
$user = (new User())
->setFirstName('Anna')
->setLastName('Admin')
->setEmail(sprintf('%[email protected]', strtolower($role)))
->setRoles([$role])
->setBusProAddressId(1)
->setBusProPersonId(1)
;
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
}