Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIDDLEWARE] page restore #1152

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions classes/Parameters/RestoreConfigurationValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\AutoUpgrade\Parameters;

use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class RestoreConfigurationValidator
{
/**
* @var Translator
*/
private $translator;

/**
* @var BackupFinder
*/
private $backupFinder;

public function __construct(Translator $translator, BackupFinder $backupFinder)
{
$this->translator = $translator;
$this->backupFinder = $backupFinder;
}

/**
* @param array<string, mixed> $array
*
* @return array<array{'message': string, 'target': string}>
*/
public function validate(array $array = []): array
{
$errors = [];

$backupNameErrors = $this->validateBackupName($array);
if ($backupNameErrors) {
$errors[] = [
'message' => $backupNameErrors,
'target' => RestoreConfiguration::BACKUP_NAME,
];

return $errors;
}

$backupNameExistErrors = $this->validateBackupExist($array[RestoreConfiguration::BACKUP_NAME]);
if ($backupNameExistErrors) {
$errors[] = [
'message' => $backupNameExistErrors,
'target' => RestoreConfiguration::BACKUP_NAME,
];
}

return $errors;
}

/**
* @param array<string, mixed> $backupConfiguration
*
* @return string|null
*/
private function validateBackupName(array $backupConfiguration): ?string
{
if (empty($backupConfiguration[RestoreConfiguration::BACKUP_NAME])) {
return $this->translator->trans('Backup name is missing');
}

return null;
}

private function validateBackupExist(string $backupName): ?string
{
if (!in_array($backupName, $this->backupFinder->getAvailableBackups())) {
return $this->translator->trans('Backup %s does not exist', [$backupName]);
}

return null;
}
}
39 changes: 39 additions & 0 deletions classes/Router/Middlewares/RestoreConfigurationIsValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\AutoUpgrade\Router\Middlewares;

use PrestaShop\Module\AutoUpgrade\Router\Routes;

class RestoreConfigurationIsValid extends AbstractMiddleware
{
public function process(): ?string
{
$restoreConfiguration = $this->upgradeContainer->getRestoreConfiguration();

$errors = $this->upgradeContainer->getRestoreConfigurationValidator()->validate($restoreConfiguration->toArray());

if (!empty($errors)) {
return Routes::RESTORE_PAGE_BACKUP_SELECTION;
}

return null;
}
}
32 changes: 32 additions & 0 deletions classes/Router/Middlewares/RestoreIsConfigured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

namespace PrestaShop\Module\AutoUpgrade\Router\Middlewares;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Router\Routes;

class RestoreIsConfigured extends AbstractMiddleware
{
public function process(): ?string
{
return $this->upgradeContainer->getFileStorage()->exists(UpgradeFileNames::RESTORE_CONFIG_FILENAME) ? null : Routes::RESTORE_PAGE_BACKUP_SELECTION;
}
}
7 changes: 7 additions & 0 deletions classes/Router/RoutesConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\BackupChoiceHasBeenMade;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\HasBackupAvailable;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\LocalChannelXmlAndZipAreValid;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\RestoreConfigurationIsValid;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\RestoreIsConfigured;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\RestoreLogExists;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\UpdateIsConfigured;
use PrestaShop\Module\AutoUpgrade\Router\Middlewares\UpdateLogExists;
Expand Down Expand Up @@ -204,6 +206,11 @@ class RoutesConfig
Routes::RESTORE_PAGE_RESTORE => [
'controller' => RestorePageRestoreController::class,
'method' => 'index',
'middleware' => [
HasBackupAvailable::class,
RestoreIsConfigured::class,
RestoreConfigurationIsValid::class,
],
],
Routes::RESTORE_STEP_RESTORE => [
'controller' => RestorePageRestoreController::class,
Expand Down
18 changes: 18 additions & 0 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PrestaShop\Module\AutoUpgrade\Parameters\FileStorage;
use PrestaShop\Module\AutoUpgrade\Parameters\LocalChannelConfigurationValidator;
use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfigurationValidator;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Progress\CompletionCalculator;
use PrestaShop\Module\AutoUpgrade\Repository\LocalArchiveRepository;
Expand Down Expand Up @@ -194,6 +195,11 @@ class UpgradeContainer
/** @var LocalChannelConfigurationValidator */
private $localChannelConfigurationValidator;

/**
* @var RestoreConfigurationValidator
*/
private $restoreConfigurationValidator;

/** @var PrestashopVersionService */
private $prestashopVersionService;

Expand Down Expand Up @@ -890,6 +896,18 @@ public function getLocalChannelConfigurationValidator(): LocalChannelConfigurati
return $this->localChannelConfigurationValidator;
}

public function getRestoreConfigurationValidator(): RestoreConfigurationValidator
{
if (null === $this->restoreConfigurationValidator) {
$this->restoreConfigurationValidator = new RestoreConfigurationValidator(
$this->getTranslator(),
$this->getBackupFinder()
);
}

return $this->restoreConfigurationValidator;
}

/**
* @return PrestashopVersionService
*/
Expand Down
82 changes: 82 additions & 0 deletions tests/unit/Parameters/RestoreConfigurationValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfiguration;
use PrestaShop\Module\AutoUpgrade\Parameters\RestoreConfigurationValidator;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

class RestoreConfigurationValidatorTest extends TestCase
{
/**
* @var RestoreConfigurationValidator
*/
private $validator;

private $backupFinderMock;

protected function setUp(): void
{
$this->container = new UpgradeContainer('/html', '/html/admin');

$this->backupFinderMock = $this->createMock(BackupFinder::class);

$this->validator = new RestoreConfigurationValidator(
$this->container->getTranslator(),
$this->backupFinderMock
);
}

public function testValidateReturnsErrorWhenBackupNameIsMissing(): void
{
$errors = $this->validator->validate([]);

$this->assertCount(1, $errors);
$this->assertSame(['message' => 'Backup name is missing', 'target' => RestoreConfiguration::BACKUP_NAME], $errors[0]);
}

public function testValidateReturnsErrorWhenBackupDoesNotExist(): void
{
$backupName = 'non_existing_backup.zip';

$this->backupFinderMock
->method('getAvailableBackups')
->willReturn(['existing_backup.zip']);

$errors = $this->validator->validate([RestoreConfiguration::BACKUP_NAME => $backupName]);

$this->assertCount(1, $errors);
$this->assertSame(['message' => 'Backup non_existing_backup.zip does not exist', 'target' => RestoreConfiguration::BACKUP_NAME], $errors[0]);
}

public function testValidateReturnsNoErrorsWhenBackupIsValid(): void
{
$backupName = 'existing_backup.zip';

$this->backupFinderMock
->method('getAvailableBackups')
->willReturn([$backupName]);

$errors = $this->validator->validate([RestoreConfiguration::BACKUP_NAME => $backupName]);

$this->assertCount(0, $errors);
}
}
Loading