diff --git a/src/Bundle/ChillMainBundle/Tests/Workflow/Helper/OpenedEntityWorkflowHelperTest.php b/src/Bundle/ChillMainBundle/Tests/Workflow/Helper/OpenedEntityWorkflowHelperTest.php new file mode 100644 index 000000000..cc4827120 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Tests/Workflow/Helper/OpenedEntityWorkflowHelperTest.php @@ -0,0 +1,105 @@ +setInitialPlaces('initial') + ->addPlaces(['initial', 'final_positive', 'final_negative', 'final_no_data']) + ->setMetadataStore( + new InMemoryMetadataStore( + placesMetadata: [ + 'final_positive' => ['isFinalPositive' => true, 'isFinal' => true], + 'final_negative' => ['isFinalPositive' => false, 'isFinal' => true], + 'final_no_data' => ['isFinal' => true], + ] + ) + ); + + $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; + } + + public function testIsFinalPositive(): void + { + $entityWorkflow = new EntityWorkflow(); + $entityWorkflow->setWorkflowName('dummy'); + $dto = new WorkflowTransitionContextDTO($entityWorkflow); + $entityWorkflow->setStep('final_positive', $dto, 'fake', new \DateTimeImmutable()); + $entityWorkflow->getCurrentStep()->setIsFinal(true); + + $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); + + self::assertTrue($helper->isFinalPositive($entityWorkflow)); + self::assertFalse($helper->isFinalNegative($entityWorkflow)); + } + + public function testIsFinalNegative(): void + { + $entityWorkflow = new EntityWorkflow(); + $entityWorkflow->setWorkflowName('dummy'); + $dto = new WorkflowTransitionContextDTO($entityWorkflow); + $entityWorkflow->setStep('final_negative', $dto, 'fake', new \DateTimeImmutable()); + $entityWorkflow->getCurrentStep()->setIsFinal(true); + + $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); + + self::assertFalse($helper->isFinalPositive($entityWorkflow)); + self::assertTrue($helper->isFinalNegative($entityWorkflow)); + } + + public function testIsFinalNoInformation(): void + { + $entityWorkflow = new EntityWorkflow(); + $entityWorkflow->setWorkflowName('dummy'); + $dto = new WorkflowTransitionContextDTO($entityWorkflow); + $entityWorkflow->setStep('final_no_data', $dto, 'fake', new \DateTimeImmutable()); + $entityWorkflow->getCurrentStep()->setIsFinal(true); + + $helper = new OpenedEntityWorkflowHelper($this->buildRegistry()); + + self::assertFalse($helper->isFinalPositive($entityWorkflow)); + self::assertFalse($helper->isFinalNegative($entityWorkflow)); + } +} diff --git a/src/Bundle/ChillMainBundle/Workflow/Helper/OpenedEntityWorkflowHelper.php b/src/Bundle/ChillMainBundle/Workflow/Helper/OpenedEntityWorkflowHelper.php new file mode 100644 index 000000000..705210722 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Workflow/Helper/OpenedEntityWorkflowHelper.php @@ -0,0 +1,83 @@ +isFinal(); + } + + /** + * return true if the entity workflow has reached a final and positive step. + * + * If multiple steps, they are all taken into account: return true if at least one step has the metadata + * `isFinalPositive: true` + */ + public function isFinalPositive(EntityWorkflow $entityWorkflow): bool + { + if (!$this->isFinal($entityWorkflow)) { + return false; + } + + return $this->findFinalMark($entityWorkflow, true) ?? false; + } + + /** + * return true if the entity workflow has reached a final and negative step. + * + * If multiple steps, they are all taken into account: return true if at least one step has the metadata + * `isFinalPositive: false`. + */ + public function isFinalNegative(EntityWorkflow $entityWorkflow): bool + { + if (!$this->isFinal($entityWorkflow)) { + return false; + } + + return $this->findFinalMark($entityWorkflow, false) ?? false; + } + + private function findFinalMark(EntityWorkflow $entityWorkflow, bool $expectedMark): ?bool + { + $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); + $marking = $workflow->getMarkingStore()->getMarking($entityWorkflow); + + foreach ($marking->getPlaces() as $place => $key) { + $metadata = $workflow->getMetadataStore()->getPlaceMetadata($place); + + if (array_key_exists('isFinalPositive', $metadata)) { + if ($expectedMark === $metadata['isFinalPositive']) { + return true; + } + } + } + + return null; + } +}