Skip to content

Commit

Permalink
Adding a CLI/WEB context for the timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Feb 4, 2025
1 parent 854c220 commit 4adb0ba
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
14 changes: 1 addition & 13 deletions ajax-upgradetabconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.';
Expand Down
2 changes: 2 additions & 0 deletions classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
16 changes: 16 additions & 0 deletions classes/State/LogsState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
28 changes: 25 additions & 3 deletions classes/Task/Runner/ChainedTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 5 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand Down
3 changes: 0 additions & 3 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_')) {
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/State/LogsStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -44,6 +46,7 @@ public function testSetAndGetActiveBackupLogFile(): void
'activeBackupLogFile' => $expectedFileName,
'activeRestoreLogFile' => null,
'activeUpdateLogFile' => null,
'timeZone' => null,
]);

$this->state->setActiveBackupLogFromDateTime($timestamp);
Expand All @@ -62,6 +65,7 @@ public function testSetAndGetActiveRestoreLogFile(): void
'activeBackupLogFile' => null,
'activeRestoreLogFile' => $expectedFileName,
'activeUpdateLogFile' => null,
'timeZone' => null,
]);

$this->state->setActiveRestoreLogFromDateTime($timestamp);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4adb0ba

Please sign in to comment.