mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-28 13:06:13 +00:00
Add TransitionHasSignerIfSignature validator
Introduced a new validator `TransitionHasSignerIfSignature` to enforce that a signature transition must have a designated signer. Included related tests, updated DTO annotations, and added translations for new validation messages.
This commit is contained in:
parent
b6c141a785
commit
e5148f603b
@ -0,0 +1,175 @@
|
||||
<?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\Workflow\Validator;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasSignerIfSignature;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasSignerIfSignatureValidator;
|
||||
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
use Symfony\Component\Workflow\DefinitionBuilder;
|
||||
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\SupportStrategy\WorkflowSupportStrategyInterface;
|
||||
use Symfony\Component\Workflow\Transition;
|
||||
use Symfony\Component\Workflow\Workflow;
|
||||
use Symfony\Component\Workflow\WorkflowInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class TransitionHasSignerIfSignatureValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
public function testMovingToNotSignatureDoesNotGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_not_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::assertNoViolation();
|
||||
}
|
||||
|
||||
public function testMovingToNotSignatureWithAPersonGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$dto->futurePersonSignatures = [new Person()];
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_not_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::buildViolation($constraint->messageShouldBeEmpty)
|
||||
->setCode('81d9e284-9d31-11ef-a078-6767f100370b')
|
||||
->atPath('property.path.futurePersonSignatures')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testMovingToNotSignatureWithAUserGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$dto->futureUserSignature = new User();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_not_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::buildViolation($constraint->messageShouldBeEmpty)
|
||||
->setCode('81d9e284-9d31-11ef-a078-6767f100370b')
|
||||
->atPath('property.path.futureUserSignature')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testStepMovingToSignatureWithoutDestineeDoesGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
$constraint->message = 'test';
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::buildViolation('test')
|
||||
->setCode('81d9e284-9d31-11ef-a078-6767f100370b')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testStepMovingToSignatureWithPersonDoesGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$dto->futurePersonSignatures = [new Person()];
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
$constraint->message = 'test';
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::assertNoViolation();
|
||||
}
|
||||
|
||||
public function testStepMovingToSignatureWithUserDoesGenerateViolation(): void
|
||||
{
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow = new EntityWorkflow());
|
||||
$dto->futureUserSignature = new User();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
$registry = $this->buildRegistry();
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
$dto->transition = $workflow->getEnabledTransition($entityWorkflow, 'to_signature');
|
||||
|
||||
$constraint = new TransitionHasSignerIfSignature();
|
||||
$constraint->message = 'test';
|
||||
|
||||
$this->validator->validate($dto, $constraint);
|
||||
|
||||
self::assertNoViolation();
|
||||
}
|
||||
|
||||
protected function createValidator()
|
||||
{
|
||||
return new TransitionHasSignerIfSignatureValidator($this->buildRegistry());
|
||||
}
|
||||
|
||||
private function buildRegistry(): Registry
|
||||
{
|
||||
$builder = new DefinitionBuilder();
|
||||
$builder
|
||||
->addPlaces(['initial', 'signature', 'not_signature'])
|
||||
->addTransition(new Transition('to_signature', 'initial', 'signature'))
|
||||
->addTransition(new Transition('to_not_signature', 'initial', 'not_signature'))
|
||||
->setMetadataStore(
|
||||
new InMemoryMetadataStore(
|
||||
placesMetadata: [
|
||||
'signature' => [
|
||||
'isSignature' => ['person', 'user'],
|
||||
],
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
$workflow = new Workflow($builder->build(), new EntityWorkflowMarkingStore(), name: 'dummy');
|
||||
$registry = new Registry();
|
||||
$registry->addWorkflow($workflow, new class () implements WorkflowSupportStrategyInterface {
|
||||
public function supports(WorkflowInterface $workflow, object $subject): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
return $registry;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?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\Workflow\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
#[\Attribute]
|
||||
class TransitionHasSignerIfSignature extends Constraint
|
||||
{
|
||||
public $message = 'workflow.You must add a destinee for signing';
|
||||
public $messageShouldBeEmpty = 'workflow.You must not add a destinee for signing';
|
||||
public $code = '81d9e284-9d31-11ef-a078-6767f100370b';
|
||||
|
||||
public function getTargets(): string
|
||||
{
|
||||
return self::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
<?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\Workflow\Validator;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
final class TransitionHasSignerIfSignatureValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Registry $registry,
|
||||
) {}
|
||||
|
||||
public function validate($value, Constraint $constraint): void
|
||||
{
|
||||
if (!$constraint instanceof TransitionHasSignerIfSignature) {
|
||||
throw new UnexpectedTypeException($constraint, TransitionHasSignerIfSignature::class);
|
||||
}
|
||||
|
||||
if (!$value instanceof WorkflowTransitionContextDTO) {
|
||||
throw new UnexpectedTypeException($value, WorkflowTransitionContextDTO::class);
|
||||
}
|
||||
|
||||
if (null === $value->transition) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workflow = $this->registry->get($value->entityWorkflow, $value->entityWorkflow->getWorkflowName());
|
||||
|
||||
foreach ($value->transition->getTos() as $to) {
|
||||
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
|
||||
|
||||
if ([] !== ($metadata['isSignature'] ?? [])) {
|
||||
if (null === $value->futureUserSignature && [] === $value->futurePersonSignatures) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->setCode($constraint->code)
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// at this step, there is no signature in the To, we must check there is not users or persons
|
||||
if ($value->futureUserSignature instanceof User || [] !== $value->futurePersonSignatures) {
|
||||
$this->context->buildViolation($constraint->messageShouldBeEmpty)
|
||||
->setCode($constraint->code)
|
||||
->atPath($value->futureUserSignature instanceof User ? 'futureUserSignature' : 'futurePersonSignatures')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ use Chill\MainBundle\Entity\UserGroup;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternal;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasDestUserIfRequired;
|
||||
use Chill\MainBundle\Workflow\Validator\TransitionHasSignerIfSignature;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
|
||||
@ -28,6 +29,7 @@ use Symfony\Component\Workflow\Transition;
|
||||
*/
|
||||
#[TransitionHasDestineeIfIsSentExternal]
|
||||
#[TransitionHasDestUserIfRequired]
|
||||
#[TransitionHasSignerIfSignature]
|
||||
class WorkflowTransitionContextDTO
|
||||
{
|
||||
/**
|
||||
|
@ -36,6 +36,7 @@ workflow:
|
||||
The user in cc cannot be a dest user in the same workflow step: L'utilisateur en copie ne peut pas être présent dans les utilisateurs qui valideront la prochaine étape
|
||||
transition_has_destinee_if_sent_external: Indiquez un destinataire de l'envoi externe
|
||||
transition_destinee_not_necessary: Pour cette transition, vous ne pouvez pas indiquer de destinataires externes
|
||||
You must add a destinee for signing: Indiquez un usager ou un utilisateur pour signature
|
||||
|
||||
rolling_date:
|
||||
When fixed date is selected, you must provide a date: Indiquez la date fixe choisie
|
||||
|
Loading…
x
Reference in New Issue
Block a user