From 1658fee09024eebf65408c4bfa42bab630a14eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 15 Feb 2022 00:23:01 +0100 Subject: [PATCH] test on members editor post move + listen for event (wip) --- .../Events/PersonMoveEventSubscriber.php | 53 +++++++++++++++++++ .../Controller/HouseholdMemberController.php | 4 ++ .../ChillPersonBundle/Entity/Person.php | 2 + .../Household/MembersEditor.php | 1 + .../Household/MembersEditorFactory.php | 2 +- .../Tests/Household/MembersEditorTest.php | 51 +++++++++++++++++- .../translations/messages.fr.yml | 1 + 7 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonMoveEventSubscriber.php diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonMoveEventSubscriber.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonMoveEventSubscriber.php new file mode 100644 index 000000000..d788bd6e0 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonMoveEventSubscriber.php @@ -0,0 +1,53 @@ + 'resetPeriodLocation' + ]; + } + + public function resetPeriodLocation(PersonAddressMoveEvent $event) + { + if ($event->getPreviousAddress() !== $event->getNextAddress() + && null !== $event->getPreviousAddress() + ) { + $person = $event->getPerson(); + + foreach ($person->getCurrentAccompanyingPeriods() as $period) { + if ($period->getPersonLocation() === $person) { + $period->setPersonLocation(null); + $period->setAddressLocation($event->getPreviousAddress()); + + if (null !== $period->getUser() && $period->getUser() !== $this->security->getUser()) { + $notification = new Notification(); + $notification + ->addAddressee($period->getUser()) + ->setTitle($this->translator->trans()) + ->setRelatedEntityClass(AccompanyingPeriod::class) + ->setRelatedEntityId($period->getId()) + ; + + } + } + } + } + } + + +} diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php index 78234d114..849d411d7 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php @@ -180,6 +180,7 @@ class HouseholdMemberController extends ApiController public function move(Request $request, $_format): Response { try { + /** @var MembersEditor $editor */ $editor = $this->getSerializer() ->deserialize( $request->getContent(), @@ -199,6 +200,9 @@ class HouseholdMemberController extends ApiController return $this->json($errors, 422); } + // launch events on post move + $editor->postMove(); + $em = $this->getDoctrine()->getManager(); // if new household, persist it diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index e908dc489..49f7ae297 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -930,6 +930,8 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI /** * Get current accompanyingPeriods array. + * + * @return AccompanyingPeriod[]|array */ public function getCurrentAccompanyingPeriods(): array { diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index 07dc9612d..0fad51b7c 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -22,6 +22,7 @@ use LogicException; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use function in_array; use function spl_object_hash; diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php b/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php index 366b1cb4c..7d1b19d51 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditorFactory.php @@ -12,8 +12,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Household; use Chill\PersonBundle\Entity\Household\Household; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class MembersEditorFactory { diff --git a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php index f3e1424fb..c409cbb7b 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php @@ -14,10 +14,14 @@ namespace Chill\PersonBundle\Tests\Household; use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Position; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; use Chill\PersonBundle\Household\MembersEditorFactory; use DateTimeImmutable; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; use Symfony\Component\Validator\Validator\ValidatorInterface; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use function count; /** @@ -26,13 +30,56 @@ use function count; */ final class MembersEditorTest extends TestCase { + use ProphecyTrait; + private MembersEditorFactory $factory; protected function setUp(): void { - $validator = $this->createMock(ValidatorInterface::class); + $this->factory = $this->buildMembersEditorFactory(); + } - $this->factory = new MembersEditorFactory($validator); + private function buildMembersEditorFactory( + ?EventDispatcherInterface $eventDispatcher = null, + ?ValidatorInterface $validator = null + ) { + if ($eventDispatcher === null) { + $double = $this->getProphet()->prophesize(); + $double->willImplement(EventDispatcherInterface::class); + $double->dispatch(Argument::type(PersonAddressMoveEvent::class)); + $eventDispatcher = $double->reveal(); + } + + if ($validator === null) { + $double = $this->getProphet()->prophesize(); + $double->willImplement(ValidatorInterface::class); + $validator = $double->reveal(); + } + + return new MembersEditorFactory( + $eventDispatcher, $validator + ); + } + + public function testPostMove() + { + $person = new Person(); + $position = (new Position()) + ->setShareHousehold(false); + $household1 = new Household(); + $household2 = new Household(); + $eventDispatcher = $this->prophesize(EventDispatcherInterface::class); + $eventDispatcher + ->dispatch(Argument::type(PersonAddressMoveEvent::class)) + ->shouldBeCalled(); + $factory = $this->buildMembersEditorFactory( + $eventDispatcher->reveal(), null + ); + $editor = $factory->createEditor($household1); + + $editor->addMovement(new DateTimeImmutable('now'), $person, $position); + + $editor->postMove(); } public function testMovePersonWithoutSharedHousehold() diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 52b53553e..a52bcce60 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -547,6 +547,7 @@ period_notification: Persons are: Les usagers concernés sont les suivants Social issues are: Les problématiques sociales renseignées sont les suivantes See it online: Visualisez le parcours en ligne + Person locating period has moved: L'usager qui localise un parcours a déménagé You are getting a notification for a period which does not exists any more: Cette notification ne correspond pas à une période d'accompagnement valide. You are getting a notification for a period you are not allowed to see: La notification fait référence à une période d'accompagnement à laquelle vous n'avez pas accès.