From 646ddc657848b30a16e0a6766b099b89a4376634 Mon Sep 17 00:00:00 2001 From: Thomas Nabord Date: Mon, 16 Dec 2024 11:17:21 +0000 Subject: [PATCH] Move validation of value in TaskType --- classes/Task/TaskType.php | 14 ++++++++++++ .../AbstractPageWithStepController.php | 1 - .../admin/self-managed/LogsController.php | 16 +++----------- tests/unit/Task/TaskTypeTest.php | 22 +++++++++++++++++++ 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 tests/unit/Task/TaskTypeTest.php diff --git a/classes/Task/TaskType.php b/classes/Task/TaskType.php index b37c6b8cf7..93a5a77d89 100644 --- a/classes/Task/TaskType.php +++ b/classes/Task/TaskType.php @@ -27,6 +27,8 @@ namespace PrestaShop\Module\AutoUpgrade\Task; +use InvalidArgumentException; + class TaskType { const TASK_TYPE_BACKUP = 'backup'; @@ -38,4 +40,16 @@ class TaskType self::TASK_TYPE_UPDATE, self::TASK_TYPE_RESTORE, ]; + + /** + * @return self::TASK_TYPE_* + */ + public static function fromString(string $type) + { + if (!in_array($type, TaskType::ALL_TASKS)) { + throw new InvalidArgumentException('Unknown log type ' . $type); + } + + return $type; + } } diff --git a/controllers/admin/self-managed/AbstractPageWithStepController.php b/controllers/admin/self-managed/AbstractPageWithStepController.php index a7b22e4e23..6a9e009b7e 100644 --- a/controllers/admin/self-managed/AbstractPageWithStepController.php +++ b/controllers/admin/self-managed/AbstractPageWithStepController.php @@ -29,7 +29,6 @@ use PrestaShop\Module\AutoUpgrade\AjaxResponseBuilder; use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors; -use PrestaShop\Module\AutoUpgrade\Twig\UpdateSteps; use Symfony\Component\HttpFoundation\Response; abstract class AbstractPageWithStepController extends AbstractPageController diff --git a/controllers/admin/self-managed/LogsController.php b/controllers/admin/self-managed/LogsController.php index ac2527a851..76cedfb5ff 100644 --- a/controllers/admin/self-managed/LogsController.php +++ b/controllers/admin/self-managed/LogsController.php @@ -27,7 +27,6 @@ namespace PrestaShop\Module\AutoUpgrade\Controller; -use InvalidArgumentException; use PrestaShop\Module\AutoUpgrade\AjaxResponseBuilder; use PrestaShop\Module\AutoUpgrade\Task\TaskType; use PrestaShop\Module\AutoUpgrade\Twig\PageSelectors; @@ -37,8 +36,9 @@ class LogsController extends AbstractGlobalController { public function getDownloadLogsButton(): JsonResponse { - $type = $this->request->request->get('download-logs-type'); - $this->validateLogsType($type); + $type = TaskType::fromString( + $this->request->request->get('download-logs-type') + ); return AjaxResponseBuilder::hydrationResponse( PageSelectors::DOWNLOAD_LOGS_PARENT_ID, @@ -48,14 +48,4 @@ public function getDownloadLogsButton(): JsonResponse ) ); } - - /** - * @throws InvalidArgumentException - */ - private function validateLogsType(string $type): void - { - if (!in_array($type, TaskType::ALL_TASKS)) { - throw new InvalidArgumentException('Unknown log type ' . $type); - } - } } diff --git a/tests/unit/Task/TaskTypeTest.php b/tests/unit/Task/TaskTypeTest.php new file mode 100644 index 0000000000..9d94a2b071 --- /dev/null +++ b/tests/unit/Task/TaskTypeTest.php @@ -0,0 +1,22 @@ +assertSame(TaskType::TASK_TYPE_BACKUP, TaskType::fromString('backup')); + $this->assertSame(TaskType::TASK_TYPE_UPDATE, TaskType::fromString('update')); + $this->assertSame(TaskType::TASK_TYPE_RESTORE, TaskType::fromString('restore')); + } + + public function testFromStringThrowsExceptionForInvalidTaskType() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Unknown log type invalid_task'); + + TaskType::fromString('invalid_task'); + } +}