diff --git a/classes/Backup/BackupFinder.php b/classes/Backup/BackupFinder.php index a19e5a8da..a1122ce87 100644 --- a/classes/Backup/BackupFinder.php +++ b/classes/Backup/BackupFinder.php @@ -165,7 +165,7 @@ public function sortBackupsByNewest(array &$backups): void private function getFormattedDatetime(int $timestamp): string { - setlocale(LC_TIME, 'UTC'); + setlocale(LC_TIME, ''); return strftime('%x %X', $timestamp); } diff --git a/classes/Services/LogsService.php b/classes/Services/LogsService.php index 1dc2ea8e8..fd17c2bdd 100644 --- a/classes/Services/LogsService.php +++ b/classes/Services/LogsService.php @@ -83,6 +83,8 @@ public function getLogsPath(string $task): ?string case TaskType::TASK_TYPE_UPDATE: $fileName = $this->state->getActiveUpdateLogFile(); break; + default: + $fileName = null; } if (!$fileName) { diff --git a/classes/State/BackupState.php b/classes/State/BackupState.php index ca98f6ba9..ea99a21a7 100644 --- a/classes/State/BackupState.php +++ b/classes/State/BackupState.php @@ -48,11 +48,11 @@ class BackupState extends AbstractState protected $backupDbFilename; /** - * @var int + * @var ?int */ protected $backupLoopLimit; /** - * @var string the table being synchronized, in case mutiple requests are needed to sync the whole table + * @var ?string the table being synchronized, in case mutiple requests are needed to sync the whole table */ protected $backupTable; @@ -75,6 +75,10 @@ public function initDefault(string $currentVersion): void $date = date('Ymd-His'); $backupName = 'V' . $currentVersion . '_' . $date . '-' . $rand; $this->setBackupName($backupName); + + $this->setBackupTable(null); + $this->setBackupLoopLimit(null); + $this->setDbStep(0); } public function getBackupName(): string diff --git a/classes/Task/AbstractTask.php b/classes/Task/AbstractTask.php index 1aea6f019..0a7a68d73 100644 --- a/classes/Task/AbstractTask.php +++ b/classes/Task/AbstractTask.php @@ -31,6 +31,7 @@ use PrestaShop\Module\AutoUpgrade\AjaxResponse; use PrestaShop\Module\AutoUpgrade\Analytics; use PrestaShop\Module\AutoUpgrade\Log\Logger; +use PrestaShop\Module\AutoUpgrade\Task\Runner\ChainedTasks; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator; @@ -120,20 +121,10 @@ public function getJsonResponse(): string */ public function getResponse(): AjaxResponse { - switch ($this::TASK_TYPE) { - case TaskType::TASK_TYPE_BACKUP: - $state = $this->container->getBackupState(); - break; - case TaskType::TASK_TYPE_RESTORE: - $state = $this->container->getRestoreState(); - break; - case TaskType::TASK_TYPE_UPDATE: - default: - $state = $this->container->getUpdateState(); - break; - } - - $response = new AjaxResponse($state, $this->logger); + $response = new AjaxResponse( + $this->container->getStateFromTaskType($this->getTaskType()), + $this->logger + ); return $response->setError($this->error) ->setStepDone($this->stepDone) @@ -142,6 +133,18 @@ public function getResponse(): AjaxResponse ->setUpgradeConfiguration($this->container->getUpgradeConfiguration()); } + /** + * @return TaskType::TASK_TYPE_* $task + */ + private function getTaskType(): string + { + if ($this instanceof ChainedTasks) { + return $this->stepClass::TASK_TYPE; + } + + return $this::TASK_TYPE; + } + private function checkTaskMayRun(): void { // PrestaShop demo mode diff --git a/classes/Task/Backup/BackupInitialization.php b/classes/Task/Backup/BackupInitialization.php index 2e706d98c..5fab74d48 100644 --- a/classes/Task/Backup/BackupInitialization.php +++ b/classes/Task/Backup/BackupInitialization.php @@ -33,6 +33,7 @@ use PrestaShop\Module\AutoUpgrade\Task\ExitCode; use PrestaShop\Module\AutoUpgrade\Task\TaskName; use PrestaShop\Module\AutoUpgrade\Task\TaskType; +use PrestaShop\Module\AutoUpgrade\UpgradeContainer; class BackupInitialization extends AbstractTask { @@ -43,6 +44,9 @@ class BackupInitialization extends AbstractTask */ public function run(): int { + $this->container->getBackupState()->initDefault( + $this->container->getProperty(UpgradeContainer::PS_VERSION) + ); $this->container->getBackupState()->setProgressPercentage( $this->container->getCompletionCalculator()->getBasePercentageOfTask(self::class) ); diff --git a/classes/Task/Runner/ChainedTasks.php b/classes/Task/Runner/ChainedTasks.php index b75ed3b03..b50240ad3 100644 --- a/classes/Task/Runner/ChainedTasks.php +++ b/classes/Task/Runner/ChainedTasks.php @@ -44,6 +44,11 @@ abstract class ChainedTasks extends AbstractTask */ protected $step; + /** + * @var string + */ + protected $stepClass; + /** * Execute all the tasks from a specific initial step, until the end (complete or error). * @@ -58,6 +63,7 @@ public function run(): int $requireRestart = false; while ($this->canContinue() && !$requireRestart) { $controller = TaskRepository::get($this->step, $this->container); + $this->stepClass = get_class($controller); $controller->init(); $this->logger->debug('Step ' . $this->step); try { diff --git a/classes/Task/Runner/SingleTask.php b/classes/Task/Runner/SingleTask.php index 1197ff33b..6e2d58edf 100644 --- a/classes/Task/Runner/SingleTask.php +++ b/classes/Task/Runner/SingleTask.php @@ -29,8 +29,6 @@ class SingleTask extends ChainedTasks { - protected $step; - /** @var bool */ private $stepHasRun = false; diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index 5c9a470fe..aad620426 100644 --- a/classes/UpgradeContainer.php +++ b/classes/UpgradeContainer.php @@ -28,6 +28,7 @@ namespace PrestaShop\Module\AutoUpgrade; use Exception; +use InvalidArgumentException; use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder; use PrestaShop\Module\AutoUpgrade\Backup\BackupManager; use PrestaShop\Module\AutoUpgrade\Log\Logger; @@ -45,10 +46,12 @@ use PrestaShop\Module\AutoUpgrade\Services\LogsService; use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService; use PrestaShop\Module\AutoUpgrade\Services\PrestashopVersionService; +use PrestaShop\Module\AutoUpgrade\State\AbstractState; use PrestaShop\Module\AutoUpgrade\State\BackupState; use PrestaShop\Module\AutoUpgrade\State\LogsState; use PrestaShop\Module\AutoUpgrade\State\RestoreState; use PrestaShop\Module\AutoUpgrade\State\UpdateState; +use PrestaShop\Module\AutoUpgrade\Task\TaskType; use PrestaShop\Module\AutoUpgrade\Twig\AssetsEnvironment; use PrestaShop\Module\AutoUpgrade\Twig\TransFilterExtension; use PrestaShop\Module\AutoUpgrade\Twig\TransFilterExtension3; @@ -671,6 +674,23 @@ public function getUpdateState(): UpdateState return $this->updateState; } + /** + * @param TaskType::TASK_TYPE_* $taskType + */ + public function getStateFromTaskType($taskType): AbstractState + { + switch ($taskType) { + case TaskType::TASK_TYPE_BACKUP: + return $this->getBackupState(); + case TaskType::TASK_TYPE_RESTORE: + return $this->getRestoreState(); + case TaskType::TASK_TYPE_UPDATE: + return $this->getUpdateState(); + default: + throw new InvalidArgumentException('Unknown task type "' . $taskType . '"'); + } + } + /** * @throws Exception */ diff --git a/controllers/admin/AdminSelfUpgradeController.php b/controllers/admin/AdminSelfUpgradeController.php index a13b64c8d..d64f21f1f 100644 --- a/controllers/admin/AdminSelfUpgradeController.php +++ b/controllers/admin/AdminSelfUpgradeController.php @@ -312,6 +312,7 @@ public function init() ); } + // TODO: Can be removed when the old UI is not needed anymore if (!$this->upgradeContainer->getBackupState()->isInitialized()) { $this->upgradeContainer->getBackupState()->initDefault( $this->upgradeContainer->getProperty(UpgradeContainer::PS_VERSION) diff --git a/tests/unit/UpgradeContainerTest.php b/tests/unit/UpgradeContainerTest.php index ae387e98d..caa2a5a9b 100644 --- a/tests/unit/UpgradeContainerTest.php +++ b/tests/unit/UpgradeContainerTest.php @@ -25,6 +25,26 @@ */ use PHPUnit\Framework\TestCase; +use PrestaShop\Module\AutoUpgrade\State\BackupState; +use PrestaShop\Module\AutoUpgrade\State\RestoreState; +use PrestaShop\Module\AutoUpgrade\State\UpdateState; +use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupComplete; +use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupDatabase; +use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupFiles; +use PrestaShop\Module\AutoUpgrade\Task\Backup\BackupInitialization; +use PrestaShop\Module\AutoUpgrade\Task\Restore\Restore; +use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreComplete; +use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreDatabase; +use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreEmpty; +use PrestaShop\Module\AutoUpgrade\Task\Restore\RestoreFiles; +use PrestaShop\Module\AutoUpgrade\Task\Update\CleanDatabase; +use PrestaShop\Module\AutoUpgrade\Task\Update\Download; +use PrestaShop\Module\AutoUpgrade\Task\Update\Unzip; +use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateComplete; +use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateDatabase; +use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateFiles; +use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateInitialization; +use PrestaShop\Module\AutoUpgrade\Task\Update\UpdateModules; use PrestaShop\Module\AutoUpgrade\UpgradeContainer; class UpgradeContainerTest extends TestCase @@ -76,4 +96,40 @@ public function objectsToInstanciateProvider() ['getZipAction', PrestaShop\Module\AutoUpgrade\ZipAction::class], ]; } + + /** + * @dataProvider stateRelatedToTaskProvider + */ + public function testRetrievalOfStateWhenGeneratingResponse(string $task, string $expectedStateClass) + { + $container = new UpgradeContainer(__DIR__, __DIR__ . '/..'); + + $state = $container->getStateFromTaskType($task::TASK_TYPE); + $this->assertSame($expectedStateClass, get_class($state)); + } + + public function stateRelatedToTaskProvider(): array + { + return [ + [BackupComplete::class, BackupState::class], + [BackupDatabase::class, BackupState::class], + [BackupFiles::class, BackupState::class], + [BackupInitialization::class, BackupState::class], + + [Restore::class, RestoreState::class], + [RestoreComplete::class, RestoreState::class], + [RestoreDatabase::class, RestoreState::class], + [RestoreEmpty::class, RestoreState::class], + [RestoreFiles::class, RestoreState::class], + + [CleanDatabase::class, UpdateState::class], + [Download::class, UpdateState::class], + [Unzip::class, UpdateState::class], + [UpdateComplete::class, UpdateState::class], + [UpdateDatabase::class, UpdateState::class], + [UpdateFiles::class, UpdateState::class], + [UpdateInitialization::class, UpdateState::class], + [UpdateModules::class, UpdateState::class], + ]; + } }