Fix person update controller tests

This commit is contained in:
Julien Fastré 2023-08-29 17:37:56 +02:00
parent 5314e7b501
commit 690697cb35
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -12,10 +12,13 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Controller; namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\MainBundle\Test\PrepareClientTrait; use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use Closure; use Closure;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Constraint\StringContains;
use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@ -40,13 +43,16 @@ final class PersonControllerUpdateTest extends WebTestCase
private ?object $em = null; private ?object $em = null;
private ?\Chill\PersonBundle\Entity\Person $person = null;
/** /**
* @var string The url using for seeing the person's information * @var string The url using for seeing the person's information
*/ */
private string $viewUrl; private string $viewUrl;
/**
* @var list<array<class-string, int>>
*/
private static $toDelete = [];
/** /**
* Prepare client and create a random person. * Prepare client and create a random person.
*/ */
@ -54,76 +60,77 @@ final class PersonControllerUpdateTest extends WebTestCase
{ {
self::bootKernel(); self::bootKernel();
$this->em = self::$container
->get(EntityManagerInterface::class);
$center = $this->em->getRepository(Center::class)
->findOneBy(['name' => 'Center A']);
$this->person = (new Person())
->setLastName('My Beloved')
->setFirstName('Jesus')
->setCenter($center)
->setGender(Person::MALE_GENDER);
$this->em->persist($this->person);
$this->em->flush();
$this->editUrl = '/fr/person/' . $this->person->getId() . '/general/edit';
$this->viewUrl = '/fr/person/' . $this->person->getId() . '/general';
$this->client = $this->getClientAuthenticated(); $this->client = $this->getClientAuthenticated();
} }
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 the edit page of a given person are not accessible for an * Test the edit page of a given person are not accessible for an
* administrative user. * administrative user.
*
* @dataProvider providePerson
*/ */
public function testEditPageDeniedForUnauthorizedInsideCenter() public function testEditPageDeniedForUnauthorizedInsideCenter(int $personId)
{ {
$client = self::createClient([], [ $client = self::createClient([], [
'PHP_AUTH_USER' => 'center a_administrative', 'PHP_AUTH_USER' => 'center a_administrative',
'PHP_AUTH_PW' => 'password', 'PHP_AUTH_PW' => 'password',
]); ]);
$client->request('GET', $this->editUrl); $client->request('GET', $this->makeEditPath($personId));
$this->assertEquals(403, $client->getResponse()->getStatusCode());
self::assertResponseStatusCodeSame(403);
} }
/** /**
* Test if the edit page of a given person is not accessible for a user * Test if the edit 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 testEditPageDeniedForUnauthorizedOutsideCenter() public function testEditPageDeniedForUnauthorizedOutsideCenter(int $personId)
{ {
$client = self::createClient([], [ $client = self::createClient([], [
'PHP_AUTH_USER' => 'center b_social', 'PHP_AUTH_USER' => 'center b_social',
'PHP_AUTH_PW' => 'password', 'PHP_AUTH_PW' => 'password',
]); ]);
$client->request('GET', $this->editUrl); $client->request('GET', $this->makeEditPath($personId));
$this->assertEquals(
self::assertResponseStatusCodeSame(
403, 403,
$client->getResponse()->getStatusCode(),
'The edit page of a person of a center A must not be accessible for user of center B' 'The edit page of a person of a center A must not be accessible for user of center B'
); );
} }
/** /**
* Test the edit page are accessible. * Test the edit page are accessible.
*
* @dataProvider providePerson *
*/ */
public function testEditPageIsSuccessful() public function testEditPageIsSuccessful(int $personId)
{ {
$client = $this->getClientAuthenticated(); $client = $this->getClientAuthenticated();
$crawler = $client->request('GET', $this->editUrl); $crawler = $client->request('GET', $this->makeEditPath($personId));
self::assertResponseIsSuccessful('The person edit form is accessible'); self::assertResponseIsSuccessful('The person edit form is accessible');
@ -133,18 +140,21 @@ final class PersonControllerUpdateTest extends WebTestCase
$client->submit($form); $client->submit($form);
self::assertResponseRedirects($this->viewUrl); self::assertResponseRedirects($this->makeViewPath($personId));
$this->refreshPerson(); $person = self::$container->get(PersonRepository::class)->find($personId);
self::assertEquals('tagada', $this->person->getFirstName()); self::assertEquals('tagada', $person->getFirstName());
} }
public function testEditPageWithErrorIsNotProcessable() /**
* @dataProvider providePerson
*/
public function testEditPageWithErrorIsNotProcessable(int $personId)
{ {
$client = $this->getClientAuthenticated(); $client = $this->getClientAuthenticated();
$crawler = $client->request('GET', $this->editUrl); $crawler = $client->request('GET', $this->makeEditPath($personId));
self::assertResponseIsSuccessful('The person edit form is accessible'); self::assertResponseIsSuccessful('The person edit form is accessible');
@ -161,12 +171,37 @@ final class PersonControllerUpdateTest extends WebTestCase
self::assertEquals(1, $alerts->count(), "there is an alert message displayed on the page"); self::assertEquals(1, $alerts->count(), "there is an alert message displayed on the page");
} }
/** 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 makeEditPath(int $personId): string
{
return "/fr/person/{$personId}/general/edit";
}
private function makeViewPath(int $personId): string
{
return "/fr/person/{$personId}/general";
} }
} }