diff --git a/ajax-upgradetabconfig.php b/ajax-upgradetabconfig.php index f49cb2724..a1660f162 100644 --- a/ajax-upgradetabconfig.php +++ b/ajax-upgradetabconfig.php @@ -24,13 +24,8 @@ * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ -use PrestaShop\Module\AutoUpgrade\Tools14; -if (function_exists('date_default_timezone_set')) { - // date_default_timezone_get calls date_default_timezone_set, which can provide warning - $timezone = @date_default_timezone_get(); - date_default_timezone_set($timezone); -} +use PrestaShop\Module\AutoUpgrade\Tools14; /** * Set constants & general values used by the autoupgrade. @@ -41,13 +36,6 @@ */ function autoupgrade_init_container($callerFilePath) { - if (PHP_SAPI === 'cli') { - $options = getopt('', ['dir:']); - if (isset($options['dir'])) { - $_POST['dir'] = $options['dir']; - } - } - // the following test confirm the directory exists if (empty($_POST['dir'])) { echo 'No admin directory provided (dir). Update assistant cannot proceed.'; diff --git a/classes/Commands/AbstractCommand.php b/classes/Commands/AbstractCommand.php index a46aed1f0..b52fa72d8 100644 --- a/classes/Commands/AbstractCommand.php +++ b/classes/Commands/AbstractCommand.php @@ -81,6 +81,8 @@ protected function setupEnvironment(InputInterface $input, OutputInterface $outp define('_PS_ADMIN_DIR_', $adminDir); $this->upgradeContainer = new UpgradeContainer($prodRootDir, $adminDir); + $this->upgradeContainer->getLogsState()->setTimeZone(date_default_timezone_get()); + $this->logger->debug('Update container initialized.'); $this->logger->debug('Logger initialized: ' . get_class($this->logger)); diff --git a/classes/State/LogsState.php b/classes/State/LogsState.php index 0f3f5fa95..d5e8af97f 100644 --- a/classes/State/LogsState.php +++ b/classes/State/LogsState.php @@ -40,6 +40,9 @@ class LogsState extends AbstractState /** @var string|null */ protected $activeUpdateLogFile; + /** @var string|null */ + protected $timeZone; + protected function getFileNameForPersistentStorage(): string { return UpgradeFileNames::STATE_LOGS_FILENAME; @@ -83,4 +86,17 @@ public function setActiveUpdateLogFromDateTime(string $datetime): self return $this; } + + public function getTimeZone(): ?string + { + return $this->timeZone; + } + + public function setTimeZone(string $timeZone): self + { + $this->timeZone = $timeZone; + $this->save(); + + return $this; + } } diff --git a/classes/Task/Runner/ChainedTasks.php b/classes/Task/Runner/ChainedTasks.php index 474c943f7..e8637b01b 100644 --- a/classes/Task/Runner/ChainedTasks.php +++ b/classes/Task/Runner/ChainedTasks.php @@ -29,6 +29,8 @@ use Exception; use PrestaShop\Module\AutoUpgrade\AjaxResponse; +use PrestaShop\Module\AutoUpgrade\DbWrapper; +use PrestaShop\Module\AutoUpgrade\Exceptions\UpdateDatabaseException; use PrestaShop\Module\AutoUpgrade\Task\AbstractTask; use PrestaShop\Module\AutoUpgrade\Task\TaskName; use PrestaShop\Module\AutoUpgrade\UpgradeTools\TaskRepository; @@ -112,21 +114,41 @@ protected function checkIfRestartRequested(AjaxResponse $response): bool return false; } + /** + * @throws Exception + */ private function setupLogging(): void { + $logsState = $this->container->getLogsState(); + $timeZone = $logsState->getTimeZone(); + if ($timeZone) { + date_default_timezone_set($timeZone); + } + $initializationSteps = [TaskName::TASK_BACKUP_INITIALIZATION, TaskName::TASK_UPDATE_INITIALIZATION, TaskName::TASK_RESTORE_INITIALIZATION]; if (in_array($this->step, $initializationSteps)) { + if (php_sapi_name() !== 'cli') { + $this->container->initPrestaShopCore(); + try { + $timeZone = DbWrapper::getValue('SELECT `value` FROM `' . _DB_PREFIX_ . 'configuration` WHERE `name` = \'PS_TIMEZONE\''); + } catch (UpdateDatabaseException $e) { + $timeZone = date_default_timezone_get(); + } + $logsState->setTimeZone($timeZone); + date_default_timezone_set($timeZone); + } + $timestamp = date('Y-m-d-His'); switch ($this->step) { case TaskName::TASK_BACKUP_INITIALIZATION: - $this->container->getLogsState()->setActiveBackupLogFromDateTime($timestamp); + $logsState->setActiveBackupLogFromDateTime($timestamp); break; case TaskName::TASK_RESTORE_INITIALIZATION: - $this->container->getLogsState()->setActiveRestoreLogFromDateTime($timestamp); + $logsState->setActiveRestoreLogFromDateTime($timestamp); break; case TaskName::TASK_UPDATE_INITIALIZATION: - $this->container->getLogsState()->setActiveUpdateLogFromDateTime($timestamp); + $logsState->setActiveUpdateLogFromDateTime($timestamp); break; } } diff --git a/classes/UpgradeContainer.php b/classes/UpgradeContainer.php index 463f3b510..8b0aa2fc1 100644 --- a/classes/UpgradeContainer.php +++ b/classes/UpgradeContainer.php @@ -969,6 +969,11 @@ public function initPrestaShopCore(): void $id_employee = !empty($_COOKIE['id_employee']) ? $_COOKIE['id_employee'] : 1; \Context::getContext()->employee = new \Employee((int) $id_employee); + + // During a CLI process, we reset the original time zone, which was modified with the call to CORE + if (php_sapi_name() === 'cli') { + date_default_timezone_set($this->getLogsState()->getTimeZone()); + } } /** diff --git a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php index 61738fc48..4804598e4 100644 --- a/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php +++ b/classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php @@ -234,9 +234,6 @@ protected function initConstants(): void if (!defined('_PS_INSTALLER_PHP_UPGRADE_DIR_')) { define('_PS_INSTALLER_PHP_UPGRADE_DIR_', $this->pathToUpgradeScripts . 'php/'); } - if (function_exists('date_default_timezone_set')) { - date_default_timezone_set('Europe/Paris'); - } // if _PS_ROOT_DIR_ is defined, use it instead of "guessing" the module dir. if (defined('_PS_ROOT_DIR_') && !defined('_PS_MODULE_DIR_')) { diff --git a/tests/unit/State/LogsStateTest.php b/tests/unit/State/LogsStateTest.php index e75da49e1..218c0797f 100644 --- a/tests/unit/State/LogsStateTest.php +++ b/tests/unit/State/LogsStateTest.php @@ -22,11 +22,13 @@ public function testExportOfData(): void $this->state->setActiveBackupLogFromDateTime('20121212121212'); $this->state->setActiveRestoreLogFromDateTime('20251225133713'); $this->state->setActiveUpdateLogFromDateTime('20250101213000'); + $this->state->setTimeZone('Europe/Paris'); $expected = [ 'activeBackupLogFile' => '20121212121212-backup.txt', 'activeRestoreLogFile' => '20251225133713-restore.txt', 'activeUpdateLogFile' => '20250101213000-update.txt', + 'timeZone' => 'Europe/Paris', ]; $this->assertEquals($expected, $this->state->export()); @@ -44,6 +46,7 @@ public function testSetAndGetActiveBackupLogFile(): void 'activeBackupLogFile' => $expectedFileName, 'activeRestoreLogFile' => null, 'activeUpdateLogFile' => null, + 'timeZone' => null, ]); $this->state->setActiveBackupLogFromDateTime($timestamp); @@ -62,6 +65,7 @@ public function testSetAndGetActiveRestoreLogFile(): void 'activeBackupLogFile' => null, 'activeRestoreLogFile' => $expectedFileName, 'activeUpdateLogFile' => null, + 'timeZone' => null, ]); $this->state->setActiveRestoreLogFromDateTime($timestamp); @@ -80,18 +84,38 @@ public function testSetAndGetActiveUpdateLogFile(): void 'activeBackupLogFile' => null, 'activeRestoreLogFile' => null, 'activeUpdateLogFile' => $expectedFileName, + 'timeZone' => null, ]); $this->state->setActiveUpdateLogFromDateTime($timestamp); $this->assertEquals($expectedFileName, $this->state->getActiveUpdateLogFile()); } + public function testSetAndGetTimezone(): void + { + $timezone = 'Europe/Paris'; + + $this->fileConfigurationStorageMock + ->expects($this->once()) + ->method('save') + ->with([ + 'activeBackupLogFile' => null, + 'activeRestoreLogFile' => null, + 'activeUpdateLogFile' => null, + 'timeZone' => $timezone, + ]); + + $this->state->setTimeZone($timezone); + $this->assertEquals($timezone, $this->state->getTimeZone()); + } + public function testLoadState(): void { $savedState = [ 'activeBackupLogFile' => '20241218-backup.txt', 'activeRestoreLogFile' => '20241218-restore.txt', 'activeUpdateLogFile' => '20241218-update.txt', + 'timeZone' => 'Europe/Paris', ]; $this->fileConfigurationStorageMock @@ -105,6 +129,7 @@ public function testLoadState(): void $this->assertEquals('20241218-backup.txt', $this->state->getActiveBackupLogFile()); $this->assertEquals('20241218-restore.txt', $this->state->getActiveRestoreLogFile()); $this->assertEquals('20241218-update.txt', $this->state->getActiveUpdateLogFile()); + $this->assertEquals('Europe/Paris', $this->state->getTimeZone()); } public function testSaveState(): void @@ -113,6 +138,7 @@ public function testSaveState(): void 'activeBackupLogFile' => '20241218210000-backup.txt', 'activeRestoreLogFile' => '20241218210000-restore.txt', 'activeUpdateLogFile' => '20241218210000-update.txt', + 'timeZone' => 'Europe/Paris', ]; $this->fileConfigurationStorageMock @@ -127,12 +153,14 @@ public function testSaveState(): void 'activeBackupLogFile' => '20241218210000-backup.txt', 'activeRestoreLogFile' => '20241218210000-restore.txt', 'activeUpdateLogFile' => '20241218210000-update.txt', + 'timeZone' => 'Europe/Paris', ]; $expectedState1 = [ 'activeBackupLogFile' => '20241218210000-backup.txt', 'activeRestoreLogFile' => '20250101122600-restore.txt', 'activeUpdateLogFile' => '20241218210000-update.txt', + 'timeZone' => 'Europe/Paris', ]; $this->fileConfigurationStorageMock