From 506cb8b732c73f2a75e154b0af942a6e2a80c1f3 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Fri, 14 Oct 2022 10:01:32 +0200 Subject: [PATCH] Ensure workflow was trashed before deleting it [MAILPOET-4540] --- .../Engine/Builder/DeleteWorkflowController.php | 5 +++++ mailpoet/lib/Automation/Engine/Exceptions.php | 8 ++++++++ .../Workflows/WorkflowsDeleteEndpointTest.php | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php b/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php index 3b26ce5d3c..8ca3b853c5 100644 --- a/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php +++ b/mailpoet/lib/Automation/Engine/Builder/DeleteWorkflowController.php @@ -21,6 +21,11 @@ class DeleteWorkflowController { if (!$workflow) { throw Exceptions::workflowNotFound($id); } + + if ($workflow->getStatus() !== Workflow::STATUS_TRASH) { + throw Exceptions::workflowNotTrashed($id); + } + $this->workflowStorage->deleteWorkflow($workflow); return $workflow; } diff --git a/mailpoet/lib/Automation/Engine/Exceptions.php b/mailpoet/lib/Automation/Engine/Exceptions.php index a8e62e4ffc..09df71b610 100644 --- a/mailpoet/lib/Automation/Engine/Exceptions.php +++ b/mailpoet/lib/Automation/Engine/Exceptions.php @@ -29,6 +29,7 @@ class Exceptions { private const WORKFLOW_STEP_MODIFIED_WHEN_UNKNOWN = 'mailpoet_automation_workflow_step_modified_when_unknown'; private const WORKFLOW_NOT_VALID = 'mailpoet_automation_workflow_not_valid'; private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects'; + private const WORKFLOW_NOT_TRASHED = 'mailpoet_automation_workflow_not_trashed'; public function __construct() { throw new InvalidStateException( @@ -204,4 +205,11 @@ class Exceptions { ) ); } + + public static function workflowNotTrashed(int $id): UnexpectedValueException { + return UnexpectedValueException::create() + ->withErrorCode(self::WORKFLOW_NOT_TRASHED) + // translators: %d is the ID of the workflow. + ->withMessage(sprintf(__("Can't delete workflow with ID '%d' because it was not trashed.", 'mailpoet'), $id)); + } } diff --git a/mailpoet/tests/integration/REST/Automation/Workflows/WorkflowsDeleteEndpointTest.php b/mailpoet/tests/integration/REST/Automation/Workflows/WorkflowsDeleteEndpointTest.php index c77bf2faf3..619fcbb8d1 100644 --- a/mailpoet/tests/integration/REST/Automation/Workflows/WorkflowsDeleteEndpointTest.php +++ b/mailpoet/tests/integration/REST/Automation/Workflows/WorkflowsDeleteEndpointTest.php @@ -48,7 +48,24 @@ class WorkflowsDeleteEndpointTest extends AutomationTest { $this->assertSame('Testing workflow', $workflow->getName()); } + public function testCantDeleteWorkflowWhenNotTrashed(): void { + $data = $this->delete(sprintf(self::ENDPOINT_PATH, $this->workflow->getId())); + + $this->assertSame([ + 'code' => 'mailpoet_automation_workflow_not_trashed', + 'message' => "Can't delete workflow with ID '{$this->workflow->getId()}' because it was not trashed.", + 'data' => ['status' => 400, 'errors' => []], + ], $data); + + $workflow = $this->workflowStorage->getWorkflow($this->workflow->getId()); + $this->assertInstanceOf(Workflow::class, $workflow); + $this->assertSame('Testing workflow', $workflow->getName()); + } + public function testItDeletesAWorkflow(): void { + $this->workflow->setStatus(Workflow::STATUS_TRASH); + $this->workflowStorage->updateWorkflow($this->workflow); + $data = $this->delete(sprintf(self::ENDPOINT_PATH, $this->workflow->getId())); $this->assertSame(['data' => null], $data);