Ensure workflow was trashed before deleting it

[MAILPOET-4540]
This commit is contained in:
Jan Jakes
2022-10-14 10:01:32 +02:00
committed by David Remer
parent 1b20bf45dd
commit 506cb8b732
3 changed files with 30 additions and 0 deletions

View File

@ -21,6 +21,11 @@ class DeleteWorkflowController {
if (!$workflow) { if (!$workflow) {
throw Exceptions::workflowNotFound($id); throw Exceptions::workflowNotFound($id);
} }
if ($workflow->getStatus() !== Workflow::STATUS_TRASH) {
throw Exceptions::workflowNotTrashed($id);
}
$this->workflowStorage->deleteWorkflow($workflow); $this->workflowStorage->deleteWorkflow($workflow);
return $workflow; return $workflow;
} }

View File

@ -29,6 +29,7 @@ class Exceptions {
private const WORKFLOW_STEP_MODIFIED_WHEN_UNKNOWN = 'mailpoet_automation_workflow_step_modified_when_unknown'; 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 WORKFLOW_NOT_VALID = 'mailpoet_automation_workflow_not_valid';
private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects'; private const MISSING_REQUIRED_SUBJECTS = 'mailpoet_automation_missing_required_subjects';
private const WORKFLOW_NOT_TRASHED = 'mailpoet_automation_workflow_not_trashed';
public function __construct() { public function __construct() {
throw new InvalidStateException( 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));
}
} }

View File

@ -48,7 +48,24 @@ class WorkflowsDeleteEndpointTest extends AutomationTest {
$this->assertSame('Testing workflow', $workflow->getName()); $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 { 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())); $data = $this->delete(sprintf(self::ENDPOINT_PATH, $this->workflow->getId()));
$this->assertSame(['data' => null], $data); $this->assertSame(['data' => null], $data);