mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix person view test
This commit is contained in:
parent
690697cb35
commit
1b8acfab24
@ -51,7 +51,7 @@ final class PersonControllerUpdateTest extends WebTestCase
|
||||
/**
|
||||
* @var list<array<class-string, int>>
|
||||
*/
|
||||
private static $toDelete = [];
|
||||
private static array $toDelete = [];
|
||||
|
||||
/**
|
||||
* Prepare client and create a random person.
|
||||
|
@ -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<array<class-string, int>>
|
||||
*/
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user