period: fix method hasPreviousUser and create method isChangedUser, and use it in Notification

This commit is contained in:
Julien Fastré 2022-04-22 12:12:30 +02:00
parent f7d9551dc1
commit 2d9af8f8c0
3 changed files with 42 additions and 2 deletions

View File

@ -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

View File

@ -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()) {

View File

@ -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();