check for ancestors when validating the presence of accompanying

period's social issues
This commit is contained in:
Julien Fastré 2021-12-07 18:17:22 +01:00
parent e6c60e66fc
commit 4e4add3cc1
4 changed files with 29 additions and 23 deletions

View File

@ -31,7 +31,8 @@ class ActivityRepository extends ServiceEntityRepository
}
/**
* @deprecated use @link{ActivityACLAwareRepositoryInterface::findByAccompanyingPeriod}
* @deprecated use @see{ActivityACLAwareRepositoryInterface::findByAccompanyingPeriod}
*
* @return Activity[]
*/
public function findByAccompanyingPeriod(AccompanyingPeriod $period, array $scopes, ?bool $allowNullScope = false, ?int $limit = 100, ?int $offset = 0, array $orderBy = ['date' => 'desc']): array

View File

@ -122,7 +122,7 @@ class SocialIssue
}
/**
* get all the ancestors of the social issue
* get all the ancestors of the social issue.
*
* @param bool $includeThis if the array in the result must include the present SocialIssue
*/
@ -135,6 +135,7 @@ class SocialIssue
}
$current = $this;
while ($current->hasParent()) {
$ancestors[] = $current = $current->getParent();
}

View File

@ -39,6 +39,22 @@ final class SocialIssueTest extends TestCase
$this->assertContains($grandChild, $ancestors);
}
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));
}
public function testIsDescendantOf()
{
$parent = new SocialIssue();
@ -61,20 +77,4 @@ 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));
}
}

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod;
use Chill\ActivityBundle\Repository\ActivityACLAwareRepository;
use Chill\ActivityBundle\Repository\ActivityRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\SocialWork\SocialIssue;
@ -57,21 +56,26 @@ class AccompanyingPeriodValidityValidator extends ConstraintValidator
}
$socialIssuesByKey = [];
foreach ($socialIssues as $si) {
$socialIssuesByKey[$si->getId()] = $si;
}
$periodIssuesWithAncestors = [];
foreach ($period->getSocialIssues() as $si) {
/** @var SocialIssue $si */
$periodIssuesWithAncestors = array_merge($periodIssuesWithAncestors, \array_map(
function (SocialIssue $si) { return $si->getId(); },
$si->getAncestors(true))
$periodIssuesWithAncestors = array_merge(
$periodIssuesWithAncestors,
array_map(
static function (SocialIssue $si) { return $si->getId(); },
$si->getAncestors(true)
)
);
}
foreach ($socialIssuesByKey as $key => $si) {
if (!in_array($key, $periodIssuesWithAncestors)) {
if (!in_array($key, $periodIssuesWithAncestors, true)) {
$this->context
->buildViolation(
$constraint->messageSocialIssueCannotBeDeleted