mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fix validator for taking ancestors into account
This commit is contained in:
parent
b1b9acc686
commit
e6c60e66fc
@ -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[]
|
||||
*/
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user