From 1b8acfab244e19bedbc43db2591b554b9b0cf92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 30 Aug 2023 13:58:46 +0200 Subject: [PATCH] Fix person view test --- .../Controller/PersonControllerUpdateTest.php | 2 +- .../Controller/PersonControllerViewTest.php | 115 ++++++++++-------- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerUpdateTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerUpdateTest.php index 0e7bf2331..2d060249f 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerUpdateTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerUpdateTest.php @@ -51,7 +51,7 @@ final class PersonControllerUpdateTest extends WebTestCase /** * @var list> */ - private static $toDelete = []; + private static array $toDelete = []; /** * Prepare client and create a random person. diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerViewTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerViewTest.php index 3b5b3451a..1b087e151 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerViewTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/PersonControllerViewTest.php @@ -11,7 +11,10 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Controller; +use Chill\MainBundle\Repository\CenterRepositoryInterface; +use Chill\MainBundle\Test\PrepareClientTrait; use Chill\PersonBundle\Entity\Person; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; /** @@ -20,93 +23,99 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; */ final class PersonControllerViewTest extends WebTestCase { - private ?object $em = null; - - private ?\Chill\PersonBundle\Entity\Person $person = null; + use PrepareClientTrait; /** - * @var string The url to view the person details + * @var list> */ - private string $viewUrl; + private static array $toDelete = []; protected function setUp(): void { self::bootKernel(); - - $this->em = self::$kernel->getContainer() - ->get('doctrine.orm.entity_manager'); - - $center = $this->em->getRepository(\Chill\MainBundle\Entity\Center::class) - ->findOneBy(['name' => 'Center A']); - - $this->person = (new Person()) - ->setLastName('Tested Person') - ->setFirstName('Réginald') - ->setCenter($center) - ->setGender(Person::MALE_GENDER); - - $this->em->persist($this->person); - $this->em->flush(); - - $this->viewUrl = '/en/person/' . $this->person->getId() . '/general'; } - protected function tearDown(): void + public static function tearDownAfterClass(): void { - $this->refreshPerson(); - $this->em->remove($this->person); - $this->em->flush(); + self::bootKernel(); + + $em = self::$container->get(EntityManagerInterface::class); + + foreach (self::$toDelete as list($class, $id)) { + $entity = $em->find($class, $id); + + if (null === $entity) { + throw new \RuntimeException(sprintf('entity not found: %s, %d', $class, $id)); + } + + $em->remove($entity); + } + + $em->flush(); } /** * Test if the view page is accessible. * * @group configurable_fields + * + * @dataProvider providePerson */ - public function testViewPerson() + public function testViewPerson(int $personId): void { - $client = self::createClient([], [ - 'PHP_AUTH_USER' => 'center a_social', - 'PHP_AUTH_PW' => 'password', - ]); + $client = $this->getClientAuthenticated(); - $crawler = $client->request('GET', $this->viewUrl); + $crawler = $client->request('GET', $this->makeViewPath($personId)); self::assertResponseIsSuccessful(); - $this->assertGreaterThan(0, $crawler->filter('html:contains("TESTED PERSON")')->count()); - $this->assertGreaterThan(0, $crawler->filter('html:contains("Réginald")')->count()); - $this->assertStringContainsString('Email addresses', $crawler->text()); - $this->assertStringContainsString('Phonenumber', $crawler->text()); - $this->assertStringContainsString('Langues parlées', $crawler->text()); - $this->assertStringContainsString(/* Etat */ 'civil', $crawler->text()); + $this->assertGreaterThan(0, $crawler->filter('html:contains("Foo")')->count()); + $this->assertGreaterThan(0, $crawler->filter('html:contains("BAR")')->count()); } /** * Test if the view page of a given person is not accessible for a user * of another center of the person. + * + * @dataProvider providePerson */ - public function testViewPersonAccessDeniedForUnauthorized() + public function testViewPersonAccessDeniedForUnauthorized(int $personId): void { - $client = self::createClient([], [ - 'PHP_AUTH_USER' => 'center b_social', - 'PHP_AUTH_PW' => 'password', - ]); + $client = $this->getClientAuthenticated('center b_social'); - $client->request('GET', $this->viewUrl); - $this->assertEquals( - 403, - $client->getResponse()->getStatusCode(), + $client->request('GET', $this->makeViewPath($personId)); + + self::assertResponseStatusCodeSame(403, 'The view page of a person of a center A must not be accessible for user of center B' ); } - /** - * Reload the person from the db. - */ - protected function refreshPerson() + public static function providePerson(): iterable { - $this->person = $this->em->getRepository(\Chill\PersonBundle\Entity\Person::class) - ->find($this->person->getId()); + self::bootKernel(); + $centerRepository = self::$container->get(CenterRepositoryInterface::class); + $em = self::$container->get(EntityManagerInterface::class); + $center = $centerRepository->findOneBy(['name' => 'Center A']); + + $person = new Person(); + $person + ->setFirstName('Foo') + ->setLastName('Bar') + ->setBirthdate(new \DateTime('2017-09-30')) + ->setGender(Person::MALE_GENDER) + ->setCenter($center); + + $em->persist($person); + + self::$toDelete[] = [Person::class, $person->getId()]; + + $em->flush(); + + yield [$person->getId()]; + } + + private function makeViewPath(int $personId): string + { + return "/fr/person/{$personId}/general"; } }