mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-19 08:44:24 +00:00
Create a "do not exclude" validation constraint for user groups
This commit is contained in:
parent
0bc6e62d4d
commit
9f355032a8
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Tests\Validation\Validator;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Chill\MainBundle\Validation\Validator\UserGroupDoNotExclude;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class UserGroupDoNotExcludeTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
protected function createValidator()
|
||||
{
|
||||
return new UserGroupDoNotExclude(
|
||||
new class () implements TranslatableStringHelperInterface {
|
||||
public function localize(array $translatableStrings): ?string
|
||||
{
|
||||
return $translatableStrings['fr'];
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyArrayIsValid(): void
|
||||
{
|
||||
$this->validator->validate([], new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude());
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testMixedUserGroupAndUsersIsValid(): void
|
||||
{
|
||||
$this->validator->validate(
|
||||
[new User(), new UserGroup()],
|
||||
new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
|
||||
);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testDifferentExcludeKeysIsValid(): void
|
||||
{
|
||||
$this->validator->validate(
|
||||
[(new UserGroup())->setExcludeKey('A'), (new UserGroup())->setExcludeKey('B')],
|
||||
new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
|
||||
);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testMultipleGroupsWithEmptyExcludeKeyIsValid(): void
|
||||
{
|
||||
$this->validator->validate(
|
||||
[(new UserGroup())->setExcludeKey(''), (new UserGroup())->setExcludeKey('')],
|
||||
new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
|
||||
);
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testSameExclusionKeyWillRaiseError(): void
|
||||
{
|
||||
$this->validator->validate(
|
||||
[
|
||||
(new UserGroup())->setExcludeKey('A')->setLabel(['fr' => 'Group 1']),
|
||||
(new UserGroup())->setExcludeKey('A')->setLabel(['fr' => 'Group 2']),
|
||||
],
|
||||
new \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude()
|
||||
);
|
||||
|
||||
$this->buildViolation('The groups {{ excluded_groups }} do exclude themselves. Please choose one between them')
|
||||
->setParameter('excluded_groups', 'Group 1, Group 2')
|
||||
->setCode('e16c8226-0090-11ef-8560-f7239594db09')
|
||||
->assertRaised();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class UserGroupDoNotExclude extends Constraint
|
||||
{
|
||||
public string $message = 'The groups {{ excluded_groups }} do exclude themselves. Please choose one between them';
|
||||
public string $code = 'e16c8226-0090-11ef-8560-f7239594db09';
|
||||
|
||||
public function getTargets()
|
||||
{
|
||||
return [self::PROPERTY_CONSTRAINT];
|
||||
}
|
||||
|
||||
public function validatedBy()
|
||||
{
|
||||
return \Chill\MainBundle\Validation\Validator\UserGroupDoNotExclude::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Validation\Validator;
|
||||
|
||||
use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
|
||||
final class UserGroupDoNotExclude extends ConstraintValidator
|
||||
{
|
||||
public function __construct(private readonly TranslatableStringHelperInterface $translatableStringHelper) {}
|
||||
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof \Chill\MainBundle\Validation\Constraint\UserGroupDoNotExclude) {
|
||||
throw new UnexpectedTypeException($constraint, UserGroupDoNotExclude::class);
|
||||
}
|
||||
|
||||
if (null === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_iterable($value)) {
|
||||
throw new UnexpectedValueException($value, 'iterable');
|
||||
}
|
||||
|
||||
$groups = [];
|
||||
|
||||
foreach ($value as $gr) {
|
||||
if ($gr instanceof UserGroup) {
|
||||
$groups[$gr->getExcludeKey()][] = $gr;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($groups as $excludeKey => $groupByKey) {
|
||||
if ('' === $excludeKey) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (1 < count($groupByKey)) {
|
||||
$excludedGroups = implode(
|
||||
', ',
|
||||
array_map(
|
||||
fn (UserGroup $group) => $this->translatableStringHelper->localize($group->getLabel()),
|
||||
$groupByKey
|
||||
)
|
||||
);
|
||||
|
||||
$this->context
|
||||
->buildViolation($constraint->message)
|
||||
->setCode($constraint->code)
|
||||
->setParameters(['excluded_groups' => $excludedGroups])
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user