Skip to content

Commit

Permalink
Fix retrieval of state
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Dec 20, 2024
1 parent 8715ec9 commit 918ec6f
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 19 deletions.
2 changes: 1 addition & 1 deletion classes/Backup/BackupFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions classes/Services/LogsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions classes/State/BackupState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
31 changes: 17 additions & 14 deletions classes/Task/AbstractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions classes/Task/Backup/BackupInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
);
Expand Down
6 changes: 6 additions & 0 deletions classes/Task/Runner/ChainedTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions classes/Task/Runner/SingleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

class SingleTask extends ChainedTasks
{
protected $step;

/** @var bool */
private $stepHasRun = false;

Expand Down
20 changes: 20 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions tests/unit/UpgradeContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
];
}
}

0 comments on commit 918ec6f

Please sign in to comment.