mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix person update controller tests
This commit is contained in:
parent
5314e7b501
commit
690697cb35
@ -12,10 +12,13 @@ declare(strict_types=1);
|
||||
namespace Chill\PersonBundle\Tests\Controller;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Repository\CenterRepositoryInterface;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Repository\PersonRepository;
|
||||
use Closure;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Framework\Constraint\StringContains;
|
||||
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
@ -40,13 +43,16 @@ final class PersonControllerUpdateTest extends WebTestCase
|
||||
|
||||
private ?object $em = null;
|
||||
|
||||
private ?\Chill\PersonBundle\Entity\Person $person = null;
|
||||
|
||||
/**
|
||||
* @var string The url using for seeing the person's information
|
||||
*/
|
||||
private string $viewUrl;
|
||||
|
||||
/**
|
||||
* @var list<array<class-string, int>>
|
||||
*/
|
||||
private static $toDelete = [];
|
||||
|
||||
/**
|
||||
* Prepare client and create a random person.
|
||||
*/
|
||||
@ -54,76 +60,77 @@ final class PersonControllerUpdateTest extends WebTestCase
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
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 the edit page of a given person are not accessible for an
|
||||
* administrative user.
|
||||
*
|
||||
* @dataProvider providePerson
|
||||
*/
|
||||
public function testEditPageDeniedForUnauthorizedInsideCenter()
|
||||
public function testEditPageDeniedForUnauthorizedInsideCenter(int $personId)
|
||||
{
|
||||
$client = self::createClient([], [
|
||||
'PHP_AUTH_USER' => 'center a_administrative',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
]);
|
||||
|
||||
$client->request('GET', $this->editUrl);
|
||||
$this->assertEquals(403, $client->getResponse()->getStatusCode());
|
||||
$client->request('GET', $this->makeEditPath($personId));
|
||||
|
||||
self::assertResponseStatusCodeSame(403);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the edit page of a given person is not accessible for a user
|
||||
* of another center of the person.
|
||||
*
|
||||
* @dataProvider providePerson
|
||||
*/
|
||||
public function testEditPageDeniedForUnauthorizedOutsideCenter()
|
||||
public function testEditPageDeniedForUnauthorizedOutsideCenter(int $personId)
|
||||
{
|
||||
$client = self::createClient([], [
|
||||
'PHP_AUTH_USER' => 'center b_social',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
]);
|
||||
|
||||
$client->request('GET', $this->editUrl);
|
||||
$this->assertEquals(
|
||||
$client->request('GET', $this->makeEditPath($personId));
|
||||
|
||||
self::assertResponseStatusCodeSame(
|
||||
403,
|
||||
$client->getResponse()->getStatusCode(),
|
||||
'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.
|
||||
*
|
||||
* @dataProvider providePerson *
|
||||
*/
|
||||
public function testEditPageIsSuccessful()
|
||||
public function testEditPageIsSuccessful(int $personId)
|
||||
{
|
||||
$client = $this->getClientAuthenticated();
|
||||
|
||||
$crawler = $client->request('GET', $this->editUrl);
|
||||
$crawler = $client->request('GET', $this->makeEditPath($personId));
|
||||
|
||||
self::assertResponseIsSuccessful('The person edit form is accessible');
|
||||
|
||||
@ -133,18 +140,21 @@ final class PersonControllerUpdateTest extends WebTestCase
|
||||
|
||||
$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();
|
||||
|
||||
$crawler = $client->request('GET', $this->editUrl);
|
||||
$crawler = $client->request('GET', $this->makeEditPath($personId));
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 makeEditPath(int $personId): string
|
||||
{
|
||||
return "/fr/person/{$personId}/general/edit";
|
||||
}
|
||||
|
||||
private function makeViewPath(int $personId): string
|
||||
{
|
||||
return "/fr/person/{$personId}/general";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user