Ensure at least one trigger is present and triggers are followed by actions

[MAILPOET-4700]
This commit is contained in:
David Remer
2022-10-21 12:10:16 +03:00
committed by Jan Jakeš
parent 8af01c3bcb
commit 4e1b1b3c27
5 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
class AtLeastOneTriggerRule implements WorkflowNodeVisitor {
public const RULE_ID = 'at-least-one-trigger';
public function initialize(Workflow $workflow): void {
foreach ($workflow->getSteps() as $step) {
if ($step->getType() === 'trigger') {
return;
}
}
throw Exceptions::workflowStructureNotValid(__('There must be at least one trigger in the workflow.', 'mailpoet'), self::RULE_ID);
}
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
}
public function complete(Workflow $workflow): void {
}
}

View File

@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
class TriggerNeedsToBeFollowedByActionRule implements WorkflowNodeVisitor {
public const RULE_ID = 'trigger-needs-to-be-followed-by-action';
public function initialize(Workflow $workflow): void {
}
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
$step = $node->getStep();
if ($step->getType() !== Step::TYPE_TRIGGER) {
return;
}
$nextSteps = $step->getNextSteps();
if (!count($nextSteps)) {
throw Exceptions::workflowStructureNotValid(__('A trigger needs to be followed by an action.', 'mailpoet'), self::RULE_ID);
}
foreach ($nextSteps as $step) {
$step = $workflow->getStep($step->getId());
if ($step && $step->getType() === Step::TYPE_ACTION) {
continue;
}
throw Exceptions::workflowStructureNotValid(__('A trigger needs to be followed by an action.', 'mailpoet'), self::RULE_ID);
}
}
public function complete(Workflow $workflow): void {
}
}

View File

@@ -4,12 +4,14 @@ namespace MailPoet\Automation\Engine\Validation;
use MailPoet\Automation\Engine\Data\Workflow; use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker; use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker;
use MailPoet\Automation\Engine\Validation\WorkflowRules\AtLeastOneTriggerRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\ConsistentStepMapRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\ConsistentStepMapRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\NoCycleRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\NoCycleRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\NoDuplicateEdgesRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\NoDuplicateEdgesRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\NoJoinRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\NoJoinRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\NoSplitRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\NoSplitRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\NoUnreachableStepsRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\NoUnreachableStepsRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\TriggerNeedsToBeFollowedByActionRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\TriggersUnderRootRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\TriggersUnderRootRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\UnknownStepRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\UnknownStepRule;
use MailPoet\Automation\Engine\Validation\WorkflowRules\ValidStepArgsRule; use MailPoet\Automation\Engine\Validation\WorkflowRules\ValidStepArgsRule;
@@ -53,6 +55,8 @@ class WorkflowValidator {
new ConsistentStepMapRule(), new ConsistentStepMapRule(),
new NoDuplicateEdgesRule(), new NoDuplicateEdgesRule(),
new TriggersUnderRootRule(), new TriggersUnderRootRule(),
new AtLeastOneTriggerRule(),
new TriggerNeedsToBeFollowedByActionRule(),
new NoCycleRule(), new NoCycleRule(),
new NoJoinRule(), new NoJoinRule(),
new NoSplitRule(), new NoSplitRule(),

View File

@@ -0,0 +1,36 @@
<?php
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker;
require_once __DIR__ . '/WorkflowRuleTest.php';
class AtLeastOnTriggerTest extends WorkflowRuleTest
{
public function testItPassesWhenTriggerExists(): void {
$steps = [
'root' => $this->createStep('root', Step::TYPE_ROOT, ['t']),
't' => $this->createStep('t', Step::TYPE_TRIGGER),
];
$workflow = $this->make(Workflow::class, ['getSteps' => $steps, 'getStep' => function($id) use ($steps) { return $steps[$id]??null; }]);
(new WorkflowWalker())->walk($workflow, [new AtLeastOneTriggerRule()]);
//no exception thrown.
}
public function testItFailsWhenNoTriggerExists(): void {
$steps = [
'root' => $this->createStep('root', Step::TYPE_ROOT)
];
$workflow = $this->make(Workflow::class, ['getSteps' => $steps, 'getStep' => function($id) use ($steps) { return $steps[$id]??null; }]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid workflow structure: There must be at least one trigger in the workflow.');
(new WorkflowWalker())->walk($workflow, [new AtLeastOneTriggerRule()]);
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
require_once __DIR__ . '/WorkflowRuleTest.php';
use MailPoet\Automation\Engine\Data\Step;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker;
class TriggerNeedsNextStepsRuleTest extends WorkflowRuleTest
{
public function testItPassesWhenActionFollows(): void {
$steps = [
'root' => $this->createStep('root', Step::TYPE_ROOT, ['t']),
't' => $this->createStep('t', Step::TYPE_TRIGGER, ['a']),
'a' => $this->createStep('a', Step::TYPE_ACTION, []),
];
$workflow = $this->make(Workflow::class, ['getSteps' => $steps, 'getStep' => function($id) use ($steps) { return $steps[$id]??null; }]);
(new WorkflowWalker())->walk($workflow, [new TriggerNeedsToBeFollowedByActionRule()]);
//no exception thrown.
}
public function testItFailsWhenNoActionIsFollowed(): void {
$steps = [
'root' => $this->createStep('root', Step::TYPE_ROOT, ['t']),
't' => $this->createStep('t', Step::TYPE_TRIGGER, []),
];
$workflow = $this->make(Workflow::class, ['getSteps' => $steps, 'getStep' => function($id) use ($steps) { return $steps[$id]??null; }]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid workflow structure: A trigger needs to be followed by an action.');
(new WorkflowWalker())->walk($workflow, [new TriggerNeedsToBeFollowedByActionRule()]);
}
public function testItFailsWhenFollowedByAStepNotBeingAnAction(): void {
$steps = [
'root' => $this->createStep('root', Step::TYPE_ROOT, ['t1']),
't1' => $this->createStep('t1', Step::TYPE_TRIGGER, ['a', 't2']),
'a' => $this->createStep('a', Step::TYPE_ACTION, []),
't2' => $this->createStep('t2', Step::TYPE_TRIGGER, ['a']),
];
$workflow = $this->make(Workflow::class, ['getSteps' => $steps, 'getStep' => function($id) use ($steps) { return $steps[$id]??null; }]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid workflow structure: A trigger needs to be followed by an action.');
(new WorkflowWalker())->walk($workflow, [new TriggerNeedsToBeFollowedByActionRule()]);
}
}