Update configuration arguments and invalidate 2 years of waiting

[MAILPOET-4418]
This commit is contained in:
David Remer
2022-07-28 09:49:02 +03:00
committed by Veljko V
parent a9a35333c4
commit 83c7f7bc7f
2 changed files with 136 additions and 2 deletions

View File

@ -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;
}
}
}

View File

@ -0,0 +1,118 @@
<?php
namespace MailPoet\Test\Automation\Integrations\Core\Actions;
use MailPoet\Automation\Engine\Control\ActionScheduler;
use MailPoet\Automation\Engine\Hooks;
use MailPoet\Automation\Engine\Workflows\Step;
use MailPoet\Automation\Engine\Workflows\Workflow;
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
use MailPoet\Automation\Integrations\Core\Actions\DelayAction;
class DelayActionTest extends \MailPoetTest {
/**
* @dataProvider dataForTestItCalculatesDelayTypesCorrectly
*/
public function testItCalculatesDelayTypesCorrectly(int $delay, string $type, int $expectation) {
$step = new Step("1", 'core:delay', 'core:delay', 'next-step', [
'delay' => $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,
],
];
}
}