diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index e327efbab..5ecf1c0bd 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -98,6 +98,12 @@ class MembersEditor $participation->setEndDate($membership->getStartDate()); } } + } else { + // if a members is moved to the same household than the one he belongs to, + // we should make it leave the household + if ($person->getCurrentHousehold($date) === $this->household) { + $this->leaveMovement($date, $person); + } } $this->membershipsAffected[] = $membership; diff --git a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php index 65199dabf..f1a622f5f 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Household; use Chill\PersonBundle\Entity\Household\Household; +use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Household\Position; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; @@ -39,6 +40,46 @@ final class MembersEditorTest extends TestCase $this->factory = $this->buildMembersEditorFactory(); } + /** + * We test here a move for a person:. + * + * * which was in a position "sharing household" + * * which move to the same household, in a position "not sharing household" + */ + public function testMoveFromSharingHouseholdToNotSharingHousehouldInSamehousehold() + { + $person = new Person(); + $household = new Household(); + $positionSharing = (new Position())->setShareHousehold(true); + $positionNotSharing = (new Position())->setShareHousehold(false); + $factory = $this->buildMembersEditorFactory(); + $editor = $factory->createEditor($household); + + // we add the member to the household + $editor->addMovement(new DateTimeImmutable('1 month ago'), $person, $positionSharing); + + // double check that the person is in the household + $this->assertContains($person, $household->getCurrentPersons()); + + // we do the move to the position not sharing household + $editor = $factory->createEditor($household); + $editor->addMovement(new DateTimeImmutable('yesterday'), $person, $positionNotSharing); + + $sharings = $household->getCurrentMembers()->filter(static function (HouseholdMember $m) { + return $m->getShareHousehold(); + }); + $notSharing = $household->getCurrentMembers()->filter(static function (HouseholdMember $m) { + return !$m->getShareHousehold(); + }); + + $this->assertCount(1, $notSharing); + $this->assertCount(0, $sharings); + + $getPerson = static function (HouseholdMember $m) { return $m->getPerson(); }; + + $this->assertContains($person, $notSharing->map($getPerson)); + } + public function testMovePersonWithoutSharedHousehold() { $person = new Person();