Files
myep-team/tests/Controller/Administrative/Questionnaire/TestControllerTest.php
T

177 lines
6.8 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\Questionnaire;
use App\Config\QuestionnaireCatalog;
use App\Entity\Assignment;
use App\Entity\Destination;
use App\Entity\JobProfile;
use App\Entity\QuestionnaireResponse;
use App\Entity\User;
use App\Enum\QuestionType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Crawler;
/**
* The office's test mode: the real questionnaire on any assignment, and nothing saved, whichever
* button is pressed. Runs in a transaction that is rolled back; the client keeps its kernel so
* every request shares that connection.
*/
class TestControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $entityManager;
private Assignment $assignment;
protected function setUp(): void
{
$this->client = static::createClient(server: ['HTTPS' => true]);
$this->client->disableReboot();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->entityManager->getConnection()->beginTransaction();
$destination = (new Destination())
->setProduct('Davos Klosters - Schwendi')
->setHotel('Schwendi')
->setHotelCode('DKI')
->setHotelBusProId(1)
->setDateFrom(new \DateTimeImmutable('+1 month'))
->setDateTo(new \DateTimeImmutable('+1 month +7 days'))
->setPickups([])
;
// no escort and still ahead: the test mode does not care
$jobProfile = (new JobProfile())->setName('Reiseleitung')->setCategory(JobProfile::CATEGORY_GUIDE);
$this->assignment = (new Assignment())
->setDestination($destination)
->setJobProfile($jobProfile)
->setStatus(Assignment::STATUS_PUBLISHED)
;
$this->entityManager->persist($jobProfile);
$this->entityManager->persist($destination);
$this->entityManager->persist($this->assignment);
$this->entityManager->flush();
}
protected function tearDown(): void
{
$this->entityManager->getConnection()->rollBack();
parent::tearDown();
}
public function testTheOfficeSeesTheQuestionnaireForAnyAssignment(): void
{
$this->client->loginUser($this->user('ROLE_MANAGER'));
$crawler = $this->open();
$this->assertResponseIsSuccessful();
$this->assertStringContainsString('Testmodus', $crawler->text());
// the assignment has no escort, so the questionnaire comes without the bus part
$this->assertStringContainsString('Ohne die Fragen zur Busfahrt', $crawler->text());
$this->assertCount(0, $crawler->filter('[data-question-key="bus_company"]'));
$this->assertCount(1, $crawler->filter('form[data-controller="questionnaire"]'));
}
public function testAnIncompleteSubmitShowsTheErrorsAndSavesNothing(): void
{
$this->client->loginUser($this->user('ROLE_TEAM_ADMIN'));
$crawler = $this->client->submit($this->open()->selectButton('Absenden')->form());
// Symfony answers an invalid submitted form with 422, as it would for a teamer
$this->assertResponseStatusCodeSame(422);
$this->assertStringContainsString('Bitte beantworte diese Frage.', $crawler->text());
$this->assertNothingSaved();
}
public function testADraftKeepsTheAnswersInTheFormAndSavesNothing(): void
{
$this->client->loginUser($this->user('ROLE_TEAM_ADMIN'));
$form = $this->open()->selectButton('Entwurf speichern')->form();
$form['questionnaire[improvement][wished_differently]'] = 'Mehr Schnee';
$crawler = $this->client->submit($form);
$this->assertStringContainsString('Der Entwurf wäre jetzt gespeichert', $crawler->text());
$this->assertSame('Mehr Schnee', $crawler->filter('textarea[name="questionnaire[improvement][wished_differently]"]')->text());
$this->assertNothingSaved();
}
public function testACompleteSubmitShowsTheAnswersAndSavesNothing(): void
{
$this->client->loginUser($this->user('ROLE_TEAM_ADMIN'));
$form = $this->open()->selectButton('Absenden')->form();
$definition = self::getContainer()->get(QuestionnaireCatalog::class)->get(QuestionnaireCatalog::TEAMER_ASSIGNMENT)->forFlags([]);
foreach ($definition->sections as $section) {
foreach ($section->questions as $question) {
if (!$question->required || null !== $question->showIf) {
continue;
}
$form[sprintf('questionnaire[%s][%s]', $section->key, $question->key)] = match ($question->type) {
QuestionType::CHOICE => $question->options[0]->key,
QuestionType::RATING => '5',
default => 'Antwort',
};
}
}
$crawler = $this->client->submit($form);
$this->assertStringContainsString('So würde der abgeschickte Fragebogen gespeichert', $crawler->text());
$this->assertStringContainsString('Sehr gut – Probleme wurden schnell', $crawler->text());
$this->assertNothingSaved();
}
public function testTheBusPartCanBeTriedOnAnAssignmentWithoutEscort(): void
{
$this->client->loginUser($this->user('ROLE_TEAM_ADMIN'));
$crawler = $this->client->request('GET', sprintf('/administrative/questionnaire/test/%s?bus_escort=1', $this->assignment->getUuid()));
$this->assertStringContainsString('Mit den Fragen zur Busfahrt', $crawler->text());
$this->assertCount(1, $crawler->filter('[data-question-key="bus_company"]'));
}
/**
* The app answers a teamer in the office area with a redirect to their dashboard, not a 403.
*/
public function testATeamerHasNoAccess(): void
{
$this->client->loginUser($this->user('ROLE_TEAMER'));
$this->open();
$this->assertResponseRedirects('/teamer');
}
private function open(): Crawler
{
return $this->client->request('GET', sprintf('/administrative/questionnaire/test/%s', $this->assignment->getUuid()));
}
private function assertNothingSaved(): void
{
$this->assertSame(0, $this->entityManager->getRepository(QuestionnaireResponse::class)->count([]));
}
private function user(string $role): User
{
$user = (new User())
->setFirstName('Olga')
->setLastName('Office')
->setEmail(sprintf('olga.office.%[email protected]', strtolower($role)))
->setRoles([$role])
->setBusProAddressId(1)
->setBusProPersonId(1)
;
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
}