mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add TransitionHasDestineeIfIsSentExternal validator
This commit introduces a new validator to ensure that transitions marked as 'sent' have a designated external recipient. It includes related tests for scenarios with and without recipients and covers integration with the workflow context.
This commit is contained in:
parent
071c5e3c55
commit
7cd638c5fc
@ -86,7 +86,6 @@ class WorkflowStepType extends AbstractType
|
|||||||
$builder
|
$builder
|
||||||
->add('transition', ChoiceType::class, [
|
->add('transition', ChoiceType::class, [
|
||||||
'label' => 'workflow.Next step',
|
'label' => 'workflow.Next step',
|
||||||
'mapped' => false,
|
|
||||||
'multiple' => false,
|
'multiple' => false,
|
||||||
'expanded' => true,
|
'expanded' => true,
|
||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
|
@ -0,0 +1,181 @@
|
|||||||
|
<?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\Workflow\EntityWorkflow;
|
||||||
|
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternal;
|
||||||
|
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternalValidator;
|
||||||
|
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
|
||||||
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||||
|
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 TransitionHasDestineeIfIsSentExternalValidatorTest extends ConstraintValidatorTestCase
|
||||||
|
{
|
||||||
|
private Transition $transitionToSent;
|
||||||
|
private Transition $transitionToNotSent;
|
||||||
|
|
||||||
|
private function buildRegistry(): Registry
|
||||||
|
{
|
||||||
|
$builder = new DefinitionBuilder(
|
||||||
|
['initial', 'sent', 'notSent'],
|
||||||
|
[
|
||||||
|
$this->transitionToSent = new Transition('send', 'initial', 'sent'),
|
||||||
|
$this->transitionToNotSent = new Transition('notSend', 'initial', 'notSent'),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$builder
|
||||||
|
->setInitialPlaces('initial')
|
||||||
|
->setMetadataStore(new InMemoryMetadataStore(
|
||||||
|
placesMetadata: [
|
||||||
|
'sent' => ['isSentExternal' => true],
|
||||||
|
]
|
||||||
|
))
|
||||||
|
;
|
||||||
|
|
||||||
|
$workflow = new Workflow($builder->build(), name: 'dummy');
|
||||||
|
$registry = new Registry();
|
||||||
|
$registry->addWorkflow($workflow, new class () implements WorkflowSupportStrategyInterface {
|
||||||
|
public function supports(WorkflowInterface $workflow, object $subject): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return $registry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToSentPlaceWithoutDestineeAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToSent;
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::buildViolation('validation_message')
|
||||||
|
->setCode('d78ea142-819d-11ef-a459-b7009a3e4caf')
|
||||||
|
->atPath('property.path.futureDestineeThirdParties')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToSentPlaceWithDestineeThirdPartyDoesNotAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToSent;
|
||||||
|
$dto->futureDestineeThirdParties = [new ThirdParty()];
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToSentPlaceWithDestineeEmailDoesNotAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToSent;
|
||||||
|
$dto->futureDestineeEmails = ['test@example.com'];
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToNoSentPlaceWithNoDestineesDoesNotAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToNotSent;
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToNoSentPlaceWithDestineeThirdPartyAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToNotSent;
|
||||||
|
$dto->futureDestineeThirdParties = [new ThirdParty()];
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::buildViolation('validation_message')
|
||||||
|
->atPath('property.path.futureDestineeThirdParties')
|
||||||
|
->setCode('eb8051fc-8227-11ef-8c3b-7f2de85bdc5b')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToNoSentPlaceWithDestineeEmailAddViolation(): void
|
||||||
|
{
|
||||||
|
$entityWorkflow = new EntityWorkflow();
|
||||||
|
$entityWorkflow->setWorkflowName('dummy');
|
||||||
|
|
||||||
|
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||||
|
$dto->transition = $this->transitionToNotSent;
|
||||||
|
$dto->futureDestineeEmails = ['test@example.com'];
|
||||||
|
|
||||||
|
$constraint = new TransitionHasDestineeIfIsSentExternal();
|
||||||
|
$constraint->messageDestineeRequired = 'validation_message';
|
||||||
|
|
||||||
|
$this->validator->validate($dto, $constraint);
|
||||||
|
|
||||||
|
self::buildViolation('validation_message')
|
||||||
|
->atPath('property.path.futureDestineeEmails')
|
||||||
|
->setCode('eb8051fc-8227-11ef-8c3b-7f2de85bdc5b')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createValidator(): TransitionHasDestineeIfIsSentExternalValidator
|
||||||
|
{
|
||||||
|
return new TransitionHasDestineeIfIsSentExternalValidator($this->buildRegistry());
|
||||||
|
}
|
||||||
|
}
|
@ -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\Workflow\Validator;
|
||||||
|
|
||||||
|
use Symfony\Component\Validator\Constraint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check that a transition does have at least one external if the 'to' is 'isSentExternal'.
|
||||||
|
*/
|
||||||
|
#[\Attribute]
|
||||||
|
class TransitionHasDestineeIfIsSentExternal extends Constraint
|
||||||
|
{
|
||||||
|
public $messageDestineeRequired = 'workflow.transition_has_destinee_if_sent_external';
|
||||||
|
public $messageDestineeNotNecessary = 'workflow.transition_destinee_not_necessary';
|
||||||
|
public $codeNoNecessaryDestinee = 'd78ea142-819d-11ef-a459-b7009a3e4caf';
|
||||||
|
public $codeDestineeUnauthorized = 'eb8051fc-8227-11ef-8c3b-7f2de85bdc5b';
|
||||||
|
|
||||||
|
public function getTargets(): string
|
||||||
|
{
|
||||||
|
return self::CLASS_CONSTRAINT;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
<?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\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 TransitionHasDestineeIfIsSentExternalValidator extends ConstraintValidator
|
||||||
|
{
|
||||||
|
public function __construct(private readonly Registry $registry) {}
|
||||||
|
|
||||||
|
public function validate($value, Constraint $constraint)
|
||||||
|
{
|
||||||
|
if (!$constraint instanceof TransitionHasDestineeIfIsSentExternal) {
|
||||||
|
throw new UnexpectedTypeException($constraint, TransitionHasDestineeIfIsSentExternal::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());
|
||||||
|
$isSentExternal = false;
|
||||||
|
foreach ($value->transition->getTos() as $to) {
|
||||||
|
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
|
||||||
|
|
||||||
|
$isSentExternal = $isSentExternal ? true : $metadata['isSentExternal'] ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$isSentExternal) {
|
||||||
|
if (0 !== count($value->futureDestineeThirdParties)) {
|
||||||
|
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||||
|
->atPath('futureDestineeThirdParties')
|
||||||
|
->setCode($constraint->codeDestineeUnauthorized)
|
||||||
|
->addViolation();
|
||||||
|
}
|
||||||
|
if (0 !== count($value->futureDestineeEmails)) {
|
||||||
|
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||||
|
->atPath('futureDestineeEmails')
|
||||||
|
->setCode($constraint->codeDestineeUnauthorized)
|
||||||
|
->addViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 === count($value->futureDestineeEmails) && 0 === count($value->futureDestineeThirdParties)) {
|
||||||
|
$this->context->buildViolation($constraint->messageDestineeRequired)
|
||||||
|
->atPath('futureDestineeThirdParties')
|
||||||
|
->setCode($constraint->codeNoNecessaryDestinee)
|
||||||
|
->addViolation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,7 @@ namespace Chill\MainBundle\Workflow;
|
|||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
use Chill\MainBundle\Entity\UserGroup;
|
use Chill\MainBundle\Entity\UserGroup;
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||||
|
use Chill\MainBundle\Workflow\Validator\TransitionHasDestineeIfIsSentExternal;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||||
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
|
use Chill\ThirdPartyBundle\Validator\ThirdPartyHasEmail;
|
||||||
@ -24,6 +25,7 @@ use Symfony\Component\Workflow\Transition;
|
|||||||
/**
|
/**
|
||||||
* Context for a transition on an workflow entity.
|
* Context for a transition on an workflow entity.
|
||||||
*/
|
*/
|
||||||
|
#[TransitionHasDestineeIfIsSentExternal]
|
||||||
class WorkflowTransitionContextDTO
|
class WorkflowTransitionContextDTO
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@ -34,6 +34,8 @@ notification:
|
|||||||
workflow:
|
workflow:
|
||||||
You must add at least one dest user or email: Indiquez au moins un destinataire ou une adresse email
|
You must add at least one dest user or email: Indiquez au moins un destinataire ou une adresse email
|
||||||
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
|
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
|
||||||
|
|
||||||
rolling_date:
|
rolling_date:
|
||||||
When fixed date is selected, you must provide a date: Indiquez la date fixe choisie
|
When fixed date is selected, you must provide a date: Indiquez la date fixe choisie
|
||||||
|
Loading…
x
Reference in New Issue
Block a user