From 83c7f7bc7fdca8cd45c44e5bf894151ec2882912 Mon Sep 17 00:00:00 2001 From: David Remer Date: Thu, 28 Jul 2022 09:49:02 +0300 Subject: [PATCH] Update configuration arguments and invalidate 2 years of waiting [MAILPOET-4418] --- .../Integrations/Core/Actions/DelayAction.php | 20 ++- .../Core/Actions/DelayActionTest.php | 118 ++++++++++++++++++ 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 mailpoet/tests/integration/Automation/Integrations/Core/Actions/DelayActionTest.php diff --git a/mailpoet/lib/Automation/Integrations/Core/Actions/DelayAction.php b/mailpoet/lib/Automation/Integrations/Core/Actions/DelayAction.php index 0495e8c81e..7f1c255b9c 100644 --- a/mailpoet/lib/Automation/Integrations/Core/Actions/DelayAction.php +++ b/mailpoet/lib/Automation/Integrations/Core/Actions/DelayAction.php @@ -28,7 +28,7 @@ class DelayAction implements Action { } public function run(Workflow $workflow, WorkflowRun $workflowRun, Step $step): void { - $this->actionScheduler->schedule(time() + $step->getArgs()['seconds'], Hooks::WORKFLOW_STEP, [ + $this->actionScheduler->schedule(time() + $this->calculateSeconds($step), Hooks::WORKFLOW_STEP, [ [ 'workflow_run_id' => $workflowRun->getId(), 'step_id' => $step->getNextStepId(), @@ -39,6 +39,22 @@ class DelayAction implements Action { } public function isValid(array $subjects, Step $step, Workflow $workflow): bool { - return (int)($step->getArgs()['seconds'] ?? null) > 0; + $seconds = $this->calculateSeconds($step); + + return $seconds > 0 && $seconds < 2 * YEAR_IN_SECONDS; + } + + private function calculateSeconds(Step $step): int { + $delay = (int)($step->getArgs()['delay'] ?? null); + switch ($step->getArgs()['delay_type']) { + case "HOURS": + return $delay * HOUR_IN_SECONDS; + case "DAYS": + return $delay * DAY_IN_SECONDS; + case "WEEKS": + return $delay * WEEK_IN_SECONDS; + default: + return 0; + } } } diff --git a/mailpoet/tests/integration/Automation/Integrations/Core/Actions/DelayActionTest.php b/mailpoet/tests/integration/Automation/Integrations/Core/Actions/DelayActionTest.php new file mode 100644 index 0000000000..562142dfc1 --- /dev/null +++ b/mailpoet/tests/integration/Automation/Integrations/Core/Actions/DelayActionTest.php @@ -0,0 +1,118 @@ + $delay, + 'delay_type' => $type, + ]); + $workflow = $this->createMock(Workflow::class); + $workflowRun = $this->createMock(WorkflowRun::class); + $workflowRun->expects($this->atLeastOnce())->method('getId')->willReturn(1); + + $actionScheduler = $this->createMock(ActionScheduler::class); + $actionScheduler->expects($this->once())->method('schedule')->with( + time() + $expectation, + Hooks::WORKFLOW_STEP, + [[ + 'workflow_run_id' => 1, + 'step_id' => 'next-step', + ]] + ); + $testee = new DelayAction($actionScheduler); + $testee->run( + $workflow, + $workflowRun, + $step + ); + } + + public function dataForTestItCalculatesDelayTypesCorrectly() : array { + return [ + '1_hour' => [ + 1, + "HOURS", + 3600, + ], + '3_hour' => [ + 3, + "HOURS", + 3*3600, + ], + '1_day' => [ + 1, + "DAYS", + 86400, + ], + '3_days' => [ + 3, + "DAYS", + 3*86400, + ], + '1_week' => [ + 1, + "WEEKS", + 604800, + ], + '3_weeks' => [ + 3, + "WEEKS", + 3*604800, + ], + ]; + } + + /** + * @dataProvider dataForTestDelayActionInvalidatesOutsideOfBoundaries + */ + public function testDelayActionInvalidatesOutsideOfBoundaries(int $delay, bool $expectation) { + + $step = new Step("1", 'core:delay', 'core:delay', 'next-step', [ + 'delay' => $delay, + 'delay_type' => "HOURS", + ]); + $workflow = $this->createMock(Workflow::class); + $actionScheduler = $this->createMock(ActionScheduler::class); + $testee = new DelayAction($actionScheduler); + $this->assertEquals($expectation, $testee->isValid([], $step, $workflow)); + } + + public function dataForTestDelayActionInvalidatesOutsideOfBoundaries() : array { + return [ + 'zero' => [ + 0, + false, + ], + 'minus_one' => [ + -1, + false, + ], + 'one' => [ + 1, + true, + ], + 'two_years' => [ + 2*8760, + false, + ], + 'below_two_years' => [ + 2*8760-1, + true, + ], + ]; + } +}