diff --git a/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/UnknownStepRuleTest.php b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/UnknownStepRuleTest.php new file mode 100644 index 0000000000..7f1a07ac3d --- /dev/null +++ b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/UnknownStepRuleTest.php @@ -0,0 +1,106 @@ +getWorkflow(); + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage("Modification of step 'core:root' of type 'root' with ID 'root' is not supported when the related plugin is not active."); + (new WorkflowWalker())->walk($workflow, [$this->getRule()]); + } + + public function testItDetectsAddedStep(): void { + $workflow = $this->getWorkflow(); + $existingWorkflow = $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [], + ]); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage("Modification of step 'core:root' of type 'root' with ID 'root' is not supported when the related plugin is not active."); + (new WorkflowWalker())->walk($workflow, [$this->getRule($existingWorkflow)]); + } + + public function testItDetectsChangedStep(): void { + $workflow = $this->getWorkflow(); + $existingWorkflow = $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [ + 'root' => $this->createStep('root', Step::TYPE_ROOT, ['next-step-id']), + ], + ]); + + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage("Modification of step 'core:root' of type 'root' with ID 'root' is not supported when the related plugin is not active."); + (new WorkflowWalker())->walk($workflow, [$this->getRule($existingWorkflow)]); + } + + + public function testItPassesWithDeletedStep(): void { + $workflow = $this->getWorkflow(); + $existingWorkflow = $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [ + 'root' => new Step('root', 'root', 'core:root', [], []), + 'abc' => $this->createStep('abc', Step::TYPE_TRIGGER, []), + ], + ]); + (new WorkflowWalker())->walk($workflow, [$this->getRule($existingWorkflow)]); + } + + public function testItPassesWithoutChanges(): void { + $workflow = $this->getWorkflow(); + (new WorkflowWalker())->walk($workflow, [$this->getRule($workflow)]); + } + + public function testItPassesWithExistingRegistryStep(): void { + $workflow = $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [ + 'root' => new Step('root', 'root', 'core:root', [], []), + ], + ]); + + $existingWorkflow = $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [ + 'root' => new Step('root', 'root', 'core:root', ['key' => 'value'], []), + ], + ]); + (new WorkflowWalker())->walk($workflow, [$this->getRule($existingWorkflow, [new RootStep()])]); + } + + private function getRule(Workflow $existingWorkflow = null, array $steps = []): UnknownStepRule { + $stepMap = []; + foreach ($steps as $step) { + $stepMap[$step->getKey()] = $step; + } + $registry = $this->make(Registry::class, [ + 'steps' => $stepMap, + ]); + $storage = $this->make(WorkflowStorage::class, [ + 'getWorkflow' => $existingWorkflow, + ]); + return new UnknownStepRule($registry, $storage); + } + + private function getWorkflow(): Workflow { + return $this->make(Workflow::class, [ + 'getId' => 1, + 'getSteps' => [ + 'root' => new Step('root', 'root', 'core:root', [], []), + ], + ]); + } +} diff --git a/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRuleTest.php b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRuleTest.php new file mode 100644 index 0000000000..58687e001c --- /dev/null +++ b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/ValidStepArgsRuleTest.php @@ -0,0 +1,48 @@ +make(Registry::class, [ + 'steps' => ['core:root' => new RootStep()], + ]); + + $validator = $this->make(Validator::class, [ + 'validate' => Expected::once(), + ]); + + $rule = new ValidStepArgsRule($registry, $validator); + $workflow = $this->getWorkflow(); + (new WorkflowWalker())->walk($workflow, [$rule]); + } + + public function testItSkipsArgsValidationForNonExistentStep(): void { + $registry = $this->make(Registry::class); + $validator = $this->make(Validator::class, [ + 'validate' => Expected::never(), + ]); + + $rule = new ValidStepArgsRule($registry, $validator); + $workflow = $this->getWorkflow(); + (new WorkflowWalker())->walk($workflow, [$rule]); + } + + private function getWorkflow(): Workflow { + return $this->make(Workflow::class, [ + 'getSteps' => [ + 'root' => new Step('root', 'root', 'core:root', [], []), + ], + ]); + } +}