From e6c60e66fc4676f305a5828643fc2d0e8f2f7aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 7 Dec 2021 18:05:05 +0100 Subject: [PATCH] fix validator for taking ancestors into account --- .../Entity/SocialWork/SocialIssue.php | 21 +++++++++++++++++++ .../Entity/SocialWork/SocialIssueTest.php | 16 ++++++++++++++ .../AccompanyingPeriodValidityValidator.php | 15 +++++++------ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index 84d47796b..b8a9141e4 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -121,6 +121,27 @@ class SocialIssue return $ancestors; } + /** + * get all the ancestors of the social issue + * + * @param bool $includeThis if the array in the result must include the present SocialIssue + */ + public function getAncestors(bool $includeThis = true): array + { + $ancestors = []; + + if ($includeThis) { + $ancestors[] = $this; + } + + $current = $this; + while ($current->hasParent()) { + $ancestors[] = $current = $current->getParent(); + } + + return $ancestors; + } + /** * @return Collection|self[] */ diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php index f8f2e3b89..491a2fffb 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php @@ -61,4 +61,20 @@ final class SocialIssueTest extends TestCase $this->assertFalse($child->isDescendantOf($grandChild)); } + + public function testGetAncestors() + { + $parent = new SocialIssue(); + $child = (new SocialIssue())->setParent($parent); + $grandChild = (new SocialIssue())->setParent($child); + $grandGrandChild = (new SocialIssue())->setParent($grandChild); + $unrelated = new SocialIssue(); + + $this->assertContains($parent, $grandGrandChild->getAncestors(true)); + $this->assertContains($child, $grandGrandChild->getAncestors(true)); + $this->assertContains($grandChild, $grandGrandChild->getAncestors(true)); + $this->assertContains($grandGrandChild, $grandGrandChild->getAncestors(true)); + $this->assertNotContains($grandGrandChild, $grandGrandChild->getAncestors(false)); + $this->assertCount(0, $unrelated->getAncestors(false)); + } } diff --git a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php index 8fed065ce..d3df79c38 100644 --- a/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php +++ b/src/Bundle/ChillPersonBundle/Validator/Constraints/AccompanyingPeriod/AccompanyingPeriodValidityValidator.php @@ -61,14 +61,17 @@ class AccompanyingPeriodValidityValidator extends ConstraintValidator $socialIssuesByKey[$si->getId()] = $si; } - $periodIssuesKeys = $period->getSocialIssues()->map(function (SocialIssue $si) { return $si->getId();}) - ->toArray(); - - dump($socialIssuesByKey); - dump($periodIssuesKeys); + $periodIssuesWithAncestors = []; + foreach ($period->getSocialIssues() as $si) { + /** @var SocialIssue $si */ + $periodIssuesWithAncestors = array_merge($periodIssuesWithAncestors, \array_map( + function (SocialIssue $si) { return $si->getId(); }, + $si->getAncestors(true)) + ); + } foreach ($socialIssuesByKey as $key => $si) { - if (!in_array($key, $periodIssuesKeys)) { + if (!in_array($key, $periodIssuesWithAncestors)) { $this->context ->buildViolation( $constraint->messageSocialIssueCannotBeDeleted