From 2d9af8f8c034d1a1d487a31122faad03eadb1762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 12:12:30 +0200 Subject: [PATCH] period: fix method hasPreviousUser and create method isChangedUser, and use it in Notification --- .../Events/UserRefEventSubscriber.php | 2 +- .../Entity/AccompanyingPeriod.php | 18 +++++++++++++- .../Tests/Entity/AccompanyingPeriodTest.php | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php index 45fe4db70..aacb283c3 100644 --- a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php @@ -58,7 +58,7 @@ class UserRefEventSubscriber implements EventSubscriberInterface public function postUpdate(AccompanyingPeriod $period, LifecycleEventArgs $args): void { - if ($period->hasPreviousUser() + if ($period->isChangedUser() && $period->getUser() !== $this->security->getUser() && null !== $period->getUser() && $period->getStep() !== AccompanyingPeriod::STEP_DRAFT diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index ff26d7b20..7615e59fe 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -361,6 +361,8 @@ class AccompanyingPeriod implements */ private Collection $userHistories; + private bool $userIsChanged = false; + /** * Temporary field, which is filled when the user is changed. * @@ -975,7 +977,12 @@ class AccompanyingPeriod implements public function hasPreviousUser(): bool { - return $this->user !== $this->userPrevious; + return null !== $this->userPrevious; + } + + public function isChangedUser(): bool + { + return $this->userIsChanged && $this->user !== $this->userPrevious; } /** @@ -1103,6 +1110,14 @@ class AccompanyingPeriod implements $this->setStep(AccompanyingPeriod::STEP_CONFIRMED); } + public function resetPreviousUser(): self + { + $this->userPrevious = null; + $this->userIsChanged = false; + + return $this; + } + /** * @Groups({"write"}) */ @@ -1329,6 +1344,7 @@ class AccompanyingPeriod implements { if ($this->user !== $user) { $this->userPrevious = $this->user; + $this->userIsChanged = true; foreach ($this->userHistories as $history) { if (null === $history->getEndDate()) { diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php index 0b66d8648..542144bc2 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Tests\Entity; use ArrayIterator; use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; @@ -62,6 +63,29 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase $this->assertFalse($period->isClosingAfterOpening()); } + public function testHasChangedUser() + { + $period = new AccompanyingPeriod(); + + $this->assertFalse($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->setUser($user1 = new User()); + + $this->assertTrue($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->resetPreviousUser(); + $this->assertFalse($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->setUser($user2 = new User()); + + $this->assertTrue($period->isChangedUser()); + $this->assertTrue($period->hasPreviousUser()); + $this->assertSame($user1, $period->getPreviousUser()); + } + public function testHistoryLocation() { $period = new AccompanyingPeriod();