Skip to content

Commit

Permalink
Merge pull request #1142 from M0rgan01/update-logs-translation
Browse files Browse the repository at this point in the history
[LOG] adding missing translations
  • Loading branch information
M0rgan01 authored Feb 11, 2025
2 parents 2c8e6b3 + 34bb7c7 commit fa58e36
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 37 deletions.
12 changes: 7 additions & 5 deletions classes/Backup/BackupFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use DateTime;
use PrestaShop\Module\AutoUpgrade\Exceptions\BackupException;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class BackupFinder
{
Expand All @@ -39,12 +40,13 @@ class BackupFinder
private $backupPath;

/**
* BackupFinder constructor.
*
* @param string $backupPath
* @var Translator
*/
public function __construct(string $backupPath)
private $translator;

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

Expand Down Expand Up @@ -156,7 +158,7 @@ public function parseBackupMetadata(string $backupName): array
];
}

throw new BackupException('An error occurred while formatting the backup name.');
throw new BackupException($this->translator->trans('An error occurred while formatting the backup name.'));
}

/**
Expand Down
12 changes: 7 additions & 5 deletions classes/Backup/BackupManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
namespace PrestaShop\Module\AutoUpgrade\Backup;

use InvalidArgumentException;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
use Symfony\Component\Filesystem\Filesystem;

class BackupManager
{
/**
* @var BackupFinder
*/
/** @var Translator */
private $translator;
/** @var BackupFinder */
private $backupFinder;

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

Expand All @@ -42,7 +44,7 @@ public function __construct(BackupFinder $backupFinder)
public function deleteBackup(string $backupName): void
{
if (!in_array($backupName, $this->backupFinder->getAvailableBackups())) {
throw new InvalidArgumentException('Backup requested for deletion does not exist.');
throw new InvalidArgumentException($this->translator->trans('Backup requested for deletion does not exist.'));
}

$filesystem = new Filesystem();
Expand Down
4 changes: 2 additions & 2 deletions classes/Commands/AbstractBackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ abstract class AbstractBackupCommand extends AbstractCommand
protected function setupEnvironment(InputInterface $input, OutputInterface $output): void
{
parent::setupEnvironment($input, $output);
$this->backupFinder = new BackupFinder($this->upgradeContainer->getProperty(UpgradeContainer::BACKUP_PATH));
$this->backupManager = new BackupManager($this->backupFinder);
$this->backupFinder = new BackupFinder($this->upgradeContainer->getTranslator(), $this->upgradeContainer->getProperty(UpgradeContainer::BACKUP_PATH));
$this->backupManager = new BackupManager($this->upgradeContainer->getTranslator(), $this->backupFinder);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions classes/Services/DistributionApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@

use PrestaShop\Module\AutoUpgrade\Exceptions\DistributionApiException;
use PrestaShop\Module\AutoUpgrade\Models\PrestashopRelease;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;

class DistributionApiService
{
public const API_URL = 'https://api.prestashop-project.org';
/** @var Translator */
private $translator;

/**
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}

/**
* @throws DistributionApiException
Expand All @@ -38,7 +49,7 @@ public function getPhpVersionRequirements(string $targetVersion): array
$response = @file_get_contents(self::API_URL . '/prestashop');

if (!$response) {
throw new DistributionApiException('Error when retrieving Prestashop versions from Distribution api', DistributionApiException::API_NOT_CALLABLE_CODE);
throw new DistributionApiException($this->translator->trans('Error when retrieving Prestashop versions from Distribution api'), DistributionApiException::API_NOT_CALLABLE_CODE);
} else {
$data = json_decode($response, true);
foreach ($data as $versionInfo) {
Expand All @@ -50,7 +61,7 @@ public function getPhpVersionRequirements(string $targetVersion): array
}
}
}
throw new DistributionApiException('No version match in Distribution api for ' . $targetVersion, DistributionApiException::VERSION_NOT_FOUND_CODE);
throw new DistributionApiException($this->translator->trans('No version match in Distribution api for %s', [$targetVersion]), DistributionApiException::VERSION_NOT_FOUND_CODE);
}

/**
Expand All @@ -63,7 +74,7 @@ public function getReleases(): array
$response = @file_get_contents(self::API_URL . '/prestashop');

if (!$response) {
throw new DistributionApiException('Error when retrieving Prestashop versions from Distribution api', DistributionApiException::API_NOT_CALLABLE_CODE);
throw new DistributionApiException($this->translator->trans('Error when retrieving Prestashop versions from Distribution api'), DistributionApiException::API_NOT_CALLABLE_CODE);
} else {
$releases = [];
$data = json_decode($response, true);
Expand Down
10 changes: 7 additions & 3 deletions classes/UpgradeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function getAnalytics(): Analytics
public function getBackupFinder(): BackupFinder
{
if (null === $this->backupFinder) {
$this->backupFinder = new BackupFinder($this->getProperty(self::BACKUP_PATH));
$this->backupFinder = new BackupFinder($this->getTranslator(), $this->getProperty(self::BACKUP_PATH));
}

return $this->backupFinder;
Expand All @@ -329,7 +329,7 @@ public function getBackupFinder(): BackupFinder
public function getBackupManager(): BackupManager
{
if (null === $this->backupManager) {
$this->backupManager = new BackupManager($this->getBackupFinder());
$this->backupManager = new BackupManager($this->getTranslator(), $this->getBackupFinder());
}

return $this->backupManager;
Expand Down Expand Up @@ -435,6 +435,7 @@ public function getUpgrader(): Upgrader
}

$upgrader = new Upgrader(
$this->getTranslator(),
$this->getPhpVersionResolverService(),
$this->getUpdateConfiguration(),
$this->getFileSystem(),
Expand Down Expand Up @@ -737,10 +738,13 @@ public function getRestoreConfiguration(): RestoreConfiguration
return $this->restoreConfiguration;
}

/**
* @throws Exception
*/
public function getDistributionApiService(): DistributionApiService
{
if (null === $this->distributionApiService) {
$this->distributionApiService = new DistributionApiService();
$this->distributionApiService = new DistributionApiService($this->getTranslator());
}

return $this->distributionApiService;
Expand Down
15 changes: 6 additions & 9 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ protected function runPhpQuery(string $upgrade_file, string $query): void
(empty($phpRes['msg']) ? '' : ' - ' . $phpRes['msg'] . "\n");
$this->logPhpError($upgrade_file, $query, $message);
} else {
$this->logger->debug('Migration file: ' . $upgrade_file . ', Query: ' . $query);
$this->logger->debug($this->container->getTranslator()->trans('Migration file: %s, Query: %s', [$upgrade_file, $query]));
}
}

Expand Down Expand Up @@ -486,7 +486,7 @@ protected function runSqlQuery(string $upgrade_file, string $query): void
}

if ($this->db->execute($query, false)) {
$this->logger->debug('Migration file: ' . $upgrade_file . ', Query: ' . $query);
$this->logger->debug($this->container->getTranslator()->trans('Migration file: %s, Query: %s', [$upgrade_file, $query]));

return;
}
Expand All @@ -499,7 +499,7 @@ protected function runSqlQuery(string $upgrade_file, string $query): void

$duplicates = ['1050', '1054', '1060', '1061', '1062', '1091'];
if (!in_array($error_number, $duplicates)) {
$this->logger->error('SQL ' . $upgrade_file . ' ' . $error_number . ' in ' . $query . ': ' . $error);
$this->logger->error($this->container->getTranslator()->trans('SQL %s %s in %s: %s'), [$upgrade_file, $error_number, $query, $error]);
$this->container->getUpdateState()->setWarningExists(true);
}
}
Expand Down Expand Up @@ -808,10 +808,7 @@ protected function updateRTLFiles(): void
try {
$commandBus->handle($adaptThemeToTRLLanguages);
} catch (CoreException $e) {
$this->logger->error('
PHP Impossible to generate RTL files for theme' . $theme['name'] . "\n" .
$e->getMessage()
);
$this->logger->warning($this->container->getTranslator()->trans('PHP Impossible to generate RTL files for theme %s: %s', [$theme['name'], $e->getMessage()]));

$this->container->getUpdateState()->setWarningExists(true);
}
Expand Down Expand Up @@ -863,8 +860,8 @@ public function warmupCoreCache(): void
exec($command, $output, $resultCode);

if ($resultCode !== 0) {
throw new UpgradeException("An error was raised when warming up the core cache: \n" . implode("\n", $output));
throw new UpgradeException($this->container->getTranslator()->trans("An error was raised when warming up the core cache: \n %s", [implode("\n", $output)]));
}
$this->logger->debug('Core cache has been generated to avoid dependency conflicts.');
$this->logger->debug($this->container->getTranslator()->trans('Core cache has been generated to avoid dependency conflicts.'));
}
}
2 changes: 1 addition & 1 deletion classes/UpgradeTools/Module/ModuleDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function downloadModule(ModuleDownloaderContext $moduleDownloaderContext)
}

if (!$downloadSuccessful) {
throw (new UpgradeException('All download attempts have failed. Check your environment and try again.'))->setSeverity(UpgradeException::SEVERITY_ERROR);
throw (new UpgradeException($this->translator->trans('All download attempts have failed. Check your environment and try again.')))->setSeverity(UpgradeException::SEVERITY_ERROR);
}
}

Expand Down
7 changes: 6 additions & 1 deletion classes/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
use PrestaShop\Module\AutoUpgrade\Models\PrestashopRelease;
use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration;
use PrestaShop\Module\AutoUpgrade\Services\PhpVersionResolverService;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
use PrestaShop\Module\AutoUpgrade\Xml\FileLoader;
use Symfony\Component\Filesystem\Filesystem;

class Upgrader
{
const DEFAULT_CHECK_VERSION_DELAY_HOURS = 12;

/** @var Translator */
protected $translator;
/** @var PrestashopRelease */
private $onlineDestinationRelease;
/** @var string */
Expand All @@ -47,12 +50,14 @@ class Upgrader
protected $fileLoader;

public function __construct(
Translator $translator,
PhpVersionResolverService $phpRequirementService,
UpgradeConfiguration $updateConfiguration,
Filesystem $filesystem,
FileLoader $fileLoader,
string $currentPsVersion
) {
$this->translator = $translator;
$this->currentPsVersion = $currentPsVersion;
$this->phpVersionResolverService = $phpRequirementService;
$this->updateConfiguration = $updateConfiguration;
Expand Down Expand Up @@ -123,7 +128,7 @@ public function getLatestModuleVersion(): string
$channelFile = $this->fileLoader->getXmlChannel();

if (empty($channelFile)) {
throw new UpgradeException('Unable to retrieve channel.xml.');
throw new UpgradeException($this->translator->trans('Unable to retrieve channel.xml.'));
}

return $channelFile->autoupgrade->last_version;
Expand Down
25 changes: 19 additions & 6 deletions tests/unit/Backup/BackupFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
use PrestaShop\Module\AutoUpgrade\Exceptions\BackupException;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
use Symfony\Component\Filesystem\Filesystem;

class BackupFinderTest extends TestCase
{
/** string */
private static $pathToBackup;
/** @var Translator */
private $translator;

public function setUp()
{
$this->translator = $this->createMock(Translator::class);
}

public static function setUpBeforeClass()
{
Expand All @@ -37,7 +45,7 @@ public static function setUpBeforeClass()

public function testListingOfBackups()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);

$expected = [
'V1.7.5.0_20240927-115034-19c6d35c',
Expand Down Expand Up @@ -65,7 +73,7 @@ private static function createTreeStructureFromJsonFile($fixturePath, $destinati

public function testParseBackupMetadata()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);

$backups = [
'V1.7.5.0_20240927-115034-19c6d35c',
Expand Down Expand Up @@ -106,17 +114,22 @@ public function testParseBackupMetadata()
*/
public function testParseBackupMetadataError()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$errorMessage = 'An error occurred while formatting the backup name.';

$this->translator->method('trans')
->willReturn($errorMessage);

$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);

$this->expectException(BackupException::class);
$this->expectExceptionMessage('An error occurred while formatting the backup name.');
$this->expectExceptionMessage($errorMessage);

$backupFinder->parseBackupMetadata('V1.7.5.0_toto_20240927-115034-19c6d35c');
}

public function testSortBackups()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);

$actual = [
[
Expand Down Expand Up @@ -167,7 +180,7 @@ public function testSortBackups()

public function testGetSortedAndFormattedAvailableBackups()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);

$expected = [
[
Expand Down
12 changes: 10 additions & 2 deletions tests/unit/Backup/BackupManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@
use PHPUnit\Framework\TestCase;
use PrestaShop\Module\AutoUpgrade\Backup\BackupFinder;
use PrestaShop\Module\AutoUpgrade\Backup\BackupManager;
use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator;
use Symfony\Component\Filesystem\Filesystem;

class BackupManagerTest extends TestCase
{
/** string */
private static $pathToBackup;
/** @var Translator */
private $translator;

public function setUp()
{
$this->translator = $this->createMock(Translator::class);
}

public static function setUpBeforeClass()
{
Expand All @@ -36,8 +44,8 @@ public static function setUpBeforeClass()

public function testBackupIsDeleted()
{
$backupFinder = new BackupFinder(self::$pathToBackup);
$backupManager = new BackupManager($backupFinder);
$backupFinder = new BackupFinder($this->translator, self::$pathToBackup);
$backupManager = new BackupManager($this->translator, $backupFinder);

$expectedBeforeDeletion = [
'V1.7.5.0_20240927-115034-19c6d35c',
Expand Down
Loading

0 comments on commit fa58e36

Please sign in to comment.