From 451f7f4230d907928966c05fc6e62fca96d9fd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 27 Sep 2022 09:41:41 +0200 Subject: [PATCH] [person][Search] Feature: use the current center history when searching for person --- .../Repository/PersonACLAwareRepository.php | 8 +- .../PersonACLAwareRepositoryTest.php | 79 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Repository/PersonACLAwareRepositoryTest.php diff --git a/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php b/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php index d5dc3cd7d..96662b66c 100644 --- a/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php @@ -16,6 +16,7 @@ use Chill\MainBundle\Repository\CountryRepository; use Chill\MainBundle\Search\ParsingException; use Chill\MainBundle\Search\SearchApiQuery; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Security\Authorization\PersonVoter; use DateTimeInterface; @@ -34,7 +35,7 @@ use function implode; final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterface { - private AuthorizationHelper $authorizationHelper; + private AuthorizationHelperInterface $authorizationHelper; private CountryRepository $countryRepository; @@ -46,7 +47,7 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac Security $security, EntityManagerInterface $em, CountryRepository $countryRepository, - AuthorizationHelper $authorizationHelper + AuthorizationHelperInterface $authorizationHelper ) { $this->security = $security; $this->em = $em; @@ -310,9 +311,10 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac } return $query + ->setFromClause($query->getFromClause() . ' JOIN view_chill_person_person_center_history_current vcppchc ON vcppchc.person_id = person.id', $query->getFromParams()) ->andWhereClause( strtr( - 'person.center_id IN ({{ center_ids }})', + 'vcppchc.center_id IN ({{ center_ids }})', [ '{{ center_ids }}' => implode( ', ', diff --git a/src/Bundle/ChillPersonBundle/Tests/Repository/PersonACLAwareRepositoryTest.php b/src/Bundle/ChillPersonBundle/Tests/Repository/PersonACLAwareRepositoryTest.php new file mode 100644 index 000000000..4540e3716 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Repository/PersonACLAwareRepositoryTest.php @@ -0,0 +1,79 @@ +entityManager = self::$container->get(EntityManagerInterface::class); + $this->countryRepository = self::$container->get(CountryRepository::class); + $this->centerRepository = self::$container->get(CenterRepositoryInterface::class); + + } + + public function testCountByCriteria() + { + $user = new User(); + + $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class); + $authorizationHelper->getReachableCenters(Argument::exact($user), Argument::exact(PersonVoter::SEE)) + ->willReturn($this->centerRepository->findAll()); + + $security = $this->prophesize(Security::class); + $security->getUser()->willReturn($user); + + $repository = new PersonACLAwareRepository($security->reveal(), $this->entityManager, $this->countryRepository, + $authorizationHelper->reveal()); + + $number = $repository->countBySearchCriteria('diallo'); + + $this->assertGreaterThan(0, $number); + } + + public function testFindByCriteria() + { + $user = new User(); + + $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class); + $authorizationHelper->getReachableCenters(Argument::exact($user), Argument::exact(PersonVoter::SEE)) + ->willReturn($this->centerRepository->findAll()); + + $security = $this->prophesize(Security::class); + $security->getUser()->willReturn($user); + + $repository = new PersonACLAwareRepository($security->reveal(), $this->entityManager, $this->countryRepository, + $authorizationHelper->reveal()); + + $results = $repository->findBySearchCriteria(0, 5, false, 'diallo'); + + $this->assertGreaterThan(0, count($results)); + $this->assertContainsOnlyInstancesOf(Person::class, $results); + foreach ($results as $person) { + $this->assertStringContainsString('diallo', strtolower($person->getFirstName() . ' ' . $person->getLastName())); + } + } +}