mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Merge branch 'signature-app/OP#767-block-external-send-when-no-document' into 'signature-app-master'
Add workflow guard to block external send when the handler does not provide a public view See merge request Chill-Projet/chill-bundles!748
This commit is contained in:
commit
00408b91a9
@ -0,0 +1,106 @@
|
||||
<?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\EventSubscriber;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowHandlerInterface;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowMarkingStore;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowWithPublicViewInterface;
|
||||
use Chill\MainBundle\Workflow\EventSubscriber\EntityWorkflowGuardSendExternalIfNoPublicView;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class EntityWorkflowGuardSendExternalIfNoPublicViewTest extends TestCase
|
||||
{
|
||||
public function testGuardSendExternalIfNoStoredObjectWithNoStoredObject()
|
||||
{
|
||||
$entityWorkflow = new EntityWorkflow();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
|
||||
$handler = $this->createMock(EntityWorkflowHandlerInterface::class);
|
||||
$handler->method('supports')->willReturn(true);
|
||||
|
||||
$registry = $this->buildRegistry($handler);
|
||||
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
|
||||
self::assertFalse($workflow->can($entityWorkflow, 'to_send_external'));
|
||||
self::assertTrue($workflow->can($entityWorkflow, 'to_other'));
|
||||
}
|
||||
|
||||
public function testGuardSendExternalIfNoStoredObjectWithStoredObject()
|
||||
{
|
||||
$entityWorkflow = new EntityWorkflow();
|
||||
$entityWorkflow->setWorkflowName('dummy');
|
||||
|
||||
$handler = $this->createMockForIntersectionOfInterfaces(
|
||||
[EntityWorkflowHandlerInterface::class, EntityWorkflowWithPublicViewInterface::class]
|
||||
);
|
||||
$handler->method('supports')->willReturn(true);
|
||||
|
||||
$registry = $this->buildRegistry($handler);
|
||||
|
||||
$workflow = $registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
|
||||
self::assertTrue($workflow->can($entityWorkflow, 'to_send_external'));
|
||||
self::assertTrue($workflow->can($entityWorkflow, 'to_other'));
|
||||
}
|
||||
|
||||
private function buildRegistry(EntityWorkflowHandlerInterface $handler): Registry
|
||||
{
|
||||
$builder = new DefinitionBuilder();
|
||||
|
||||
$builder->addPlace('initial');
|
||||
$builder->addPlace('send_external');
|
||||
$builder->addPlace('other');
|
||||
$builder->addTransition(new Transition('to_send_external', 'initial', 'send_external'));
|
||||
$builder->addTransition(new Transition('to_other', 'initial', 'other'));
|
||||
$builder->setMetadataStore(
|
||||
new InMemoryMetadataStore(
|
||||
placesMetadata: [
|
||||
'send_external' => [
|
||||
'isSentExternal' => true,
|
||||
],
|
||||
]
|
||||
)
|
||||
);
|
||||
$definition = $builder->build();
|
||||
$workflow = new Workflow($definition, new EntityWorkflowMarkingStore(), $eventDispatcher = new EventDispatcher(), 'dummy');
|
||||
$registry = new Registry();
|
||||
$registry->addWorkflow($workflow, new class () implements WorkflowSupportStrategyInterface {
|
||||
public function supports(\Symfony\Component\Workflow\WorkflowInterface $workflow, object $subject): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
$eventSubscriber = new EntityWorkflowGuardSendExternalIfNoPublicView(
|
||||
$registry,
|
||||
new EntityWorkflowManager([$handler], $registry)
|
||||
);
|
||||
$eventDispatcher->addSubscriber($eventSubscriber);
|
||||
|
||||
return $registry;
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?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\EventSubscriber;
|
||||
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowManager;
|
||||
use Chill\MainBundle\Workflow\EntityWorkflowWithPublicViewInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Workflow\Event\GuardEvent;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\TransitionBlocker;
|
||||
|
||||
final readonly class EntityWorkflowGuardSendExternalIfNoPublicView implements EventSubscriberInterface
|
||||
{
|
||||
public function __construct(private Registry $registry, private EntityWorkflowManager $entityWorkflowManager) {}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return ['workflow.guard' => [
|
||||
['guardSendExternalIfNoStoredObject', 0],
|
||||
]];
|
||||
}
|
||||
|
||||
public function guardSendExternalIfNoStoredObject(GuardEvent $event)
|
||||
{
|
||||
$entityWorkflow = $event->getSubject();
|
||||
if (!$entityWorkflow instanceof EntityWorkflow) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
|
||||
|
||||
foreach ($event->getTransition()->getTos() as $to) {
|
||||
$metadata = $workflow->getMetadataStore()->getPlaceMetadata($to);
|
||||
if (true === ($metadata['isSentExternal'] ?? false)
|
||||
&& !$this->entityWorkflowManager->getHandler($entityWorkflow) instanceof EntityWorkflowWithPublicViewInterface) {
|
||||
$event->addTransitionBlocker(
|
||||
new TransitionBlocker(
|
||||
'No document associated with this entityWorkflow, not able to send external',
|
||||
'a95e57d8-9136-11ef-a208-43b111cfc66d'
|
||||
)
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user