fix validator for taking ancestors into account

This commit is contained in:
Julien Fastré 2021-12-07 18:05:05 +01:00
parent b1b9acc686
commit e6c60e66fc
3 changed files with 46 additions and 6 deletions

View File

@ -121,6 +121,27 @@ class SocialIssue
return $ancestors; 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[] * @return Collection|self[]
*/ */

View File

@ -61,4 +61,20 @@ final class SocialIssueTest extends TestCase
$this->assertFalse($child->isDescendantOf($grandChild)); $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));
}
} }

View File

@ -61,14 +61,17 @@ class AccompanyingPeriodValidityValidator extends ConstraintValidator
$socialIssuesByKey[$si->getId()] = $si; $socialIssuesByKey[$si->getId()] = $si;
} }
$periodIssuesKeys = $period->getSocialIssues()->map(function (SocialIssue $si) { return $si->getId();}) $periodIssuesWithAncestors = [];
->toArray(); foreach ($period->getSocialIssues() as $si) {
/** @var SocialIssue $si */
dump($socialIssuesByKey); $periodIssuesWithAncestors = array_merge($periodIssuesWithAncestors, \array_map(
dump($periodIssuesKeys); function (SocialIssue $si) { return $si->getId(); },
$si->getAncestors(true))
);
}
foreach ($socialIssuesByKey as $key => $si) { foreach ($socialIssuesByKey as $key => $si) {
if (!in_array($key, $periodIssuesKeys)) { if (!in_array($key, $periodIssuesWithAncestors)) {
$this->context $this->context
->buildViolation( ->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted $constraint->messageSocialIssueCannotBeDeleted