Fix person view test

This commit is contained in:
Julien Fastré 2023-08-30 13:58:46 +02:00
parent 690697cb35
commit 1b8acfab24
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 63 additions and 54 deletions

View File

@ -51,7 +51,7 @@ final class PersonControllerUpdateTest extends WebTestCase
/** /**
* @var list<array<class-string, int>> * @var list<array<class-string, int>>
*/ */
private static $toDelete = []; private static array $toDelete = [];
/** /**
* Prepare client and create a random person. * Prepare client and create a random person.

View File

@ -11,7 +11,10 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Controller; namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/** /**
@ -20,93 +23,99 @@ use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
*/ */
final class PersonControllerViewTest extends WebTestCase final class PersonControllerViewTest extends WebTestCase
{ {
private ?object $em = null; use PrepareClientTrait;
private ?\Chill\PersonBundle\Entity\Person $person = null;
/** /**
* @var string The url to view the person details * @var list<array<class-string, int>>
*/ */
private string $viewUrl; private static array $toDelete = [];
protected function setUp(): void protected function setUp(): void
{ {
self::bootKernel(); 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(); self::bootKernel();
$this->em->remove($this->person);
$this->em->flush(); $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. * Test if the view page is accessible.
* *
* @group configurable_fields * @group configurable_fields
*
* @dataProvider providePerson
*/ */
public function testViewPerson() public function testViewPerson(int $personId): void
{ {
$client = self::createClient([], [ $client = $this->getClientAuthenticated();
'PHP_AUTH_USER' => 'center a_social',
'PHP_AUTH_PW' => 'password',
]);
$crawler = $client->request('GET', $this->viewUrl); $crawler = $client->request('GET', $this->makeViewPath($personId));
self::assertResponseIsSuccessful(); self::assertResponseIsSuccessful();
$this->assertGreaterThan(0, $crawler->filter('html:contains("TESTED PERSON")')->count()); $this->assertGreaterThan(0, $crawler->filter('html:contains("Foo")')->count());
$this->assertGreaterThan(0, $crawler->filter('html:contains("Réginald")')->count()); $this->assertGreaterThan(0, $crawler->filter('html:contains("BAR")')->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());
} }
/** /**
* Test if the view page of a given person is not accessible for a user * Test if the view page of a given person is not accessible for a user
* of another center of the person. * of another center of the person.
*
* @dataProvider providePerson
*/ */
public function testViewPersonAccessDeniedForUnauthorized() public function testViewPersonAccessDeniedForUnauthorized(int $personId): void
{ {
$client = self::createClient([], [ $client = $this->getClientAuthenticated('center b_social');
'PHP_AUTH_USER' => 'center b_social',
'PHP_AUTH_PW' => 'password',
]);
$client->request('GET', $this->viewUrl); $client->request('GET', $this->makeViewPath($personId));
$this->assertEquals(
403, self::assertResponseStatusCodeSame(403,
$client->getResponse()->getStatusCode(),
'The view page of a person of a center A must not be accessible for user of center B' 'The view page of a person of a center A must not be accessible for user of center B'
); );
} }
/** public static function providePerson(): iterable
* Reload the person from the db.
*/
protected function refreshPerson()
{ {
$this->person = $this->em->getRepository(\Chill\PersonBundle\Entity\Person::class) self::bootKernel();
->find($this->person->getId()); $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";
} }
} }