From e5148f603b145d8c191ed83f66691e629492ca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 7 Nov 2024 19:55:09 +0100 Subject: [PATCH] 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. --- ...itionHasSignerIfSignatureValidatorTest.php | 175 ++++++++++++++++++ .../TransitionHasSignerIfSignature.php | 27 +++ ...ransitionHasSignerIfSignatureValidator.php | 68 +++++++ .../Workflow/WorkflowTransitionContextDTO.php | 2 + .../translations/validators.fr.yml | 1 + 5 files changed, 273 insertions(+) create mode 100644 src/Bundle/ChillMainBundle/Tests/Workflow/Validator/TransitionHasSignerIfSignatureValidatorTest.php create mode 100644 src/Bundle/ChillMainBundle/Workflow/Validator/TransitionHasSignerIfSignature.php create mode 100644 src/Bundle/ChillMainBundle/Workflow/Validator/TransitionHasSignerIfSignatureValidator.php diff --git a/src/Bundle/ChillMainBundle/Tests/Workflow/Validator/TransitionHasSignerIfSignatureValidatorTest.php b/src/Bundle/ChillMainBundle/Tests/Workflow/Validator/TransitionHasSignerIfSignatureValidatorTest.php new file mode 100644 index 000000000..9b385d4cb --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Workflow/Validator/TransitionHasSignerIfSignatureValidatorTest.php @@ -0,0 +1,175 @@ +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; + } +} diff --git a/src/Bundle/ChillMainBundle/Workflow/Validator/TransitionHasSignerIfSignature.php b/src/Bundle/ChillMainBundle/Workflow/Validator/TransitionHasSignerIfSignature.php new file mode 100644 index 000000000..87e98fd4e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Validator/TransitionHasSignerIfSignature.php @@ -0,0 +1,27 @@ +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(); + } + } +} diff --git a/src/Bundle/ChillMainBundle/Workflow/WorkflowTransitionContextDTO.php b/src/Bundle/ChillMainBundle/Workflow/WorkflowTransitionContextDTO.php index 324bef7e8..dcc4dbb69 100644 --- a/src/Bundle/ChillMainBundle/Workflow/WorkflowTransitionContextDTO.php +++ b/src/Bundle/ChillMainBundle/Workflow/WorkflowTransitionContextDTO.php @@ -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 { /** diff --git a/src/Bundle/ChillMainBundle/translations/validators.fr.yml b/src/Bundle/ChillMainBundle/translations/validators.fr.yml index 7e852177a..d5be5f7d3 100644 --- a/src/Bundle/ChillMainBundle/translations/validators.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/validators.fr.yml @@ -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