Skip to content

Commit

Permalink
Fix fileDifferences cache
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rgan01 committed Dec 17, 2024
1 parent 2015ff9 commit c09d6ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
58 changes: 40 additions & 18 deletions classes/Xml/ChecksumCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

class ChecksumCompare
{
public const CATEGORY_MAIL = 'mail';
public const CATEGORY_TRANSLATION = 'translation';
public const CATEGORY_CORE = 'core';
public const CATEGORY_THEME = 'themes';

public const FILE_MISSING = 'missing';
public const FILE_ALTERED = 'altered';
/**
* @var FileLoader
*/
Expand All @@ -48,6 +55,7 @@ class ChecksumCompare
* @var string
*/
private $adminPath;

/**
* @var array{
* 'mail':array{'missing':string[],'altered':string[]},
Expand All @@ -57,21 +65,21 @@ class ChecksumCompare
* }|false
*/
private $fileDifferences = [
'mail' => [
'missing' => [],
'altered' => [],
self::CATEGORY_MAIL => [
self::FILE_MISSING => [],
self::FILE_ALTERED => [],
],
'translation' => [
'missing' => [],
'altered' => [],
self::CATEGORY_TRANSLATION => [
self::FILE_MISSING => [],
self::FILE_ALTERED => [],
],
'core' => [
'missing' => [],
'altered' => [],
self::CATEGORY_CORE => [
self::FILE_MISSING => [],
self::FILE_ALTERED => [],
],
'themes' => [
'missing' => [],
'altered' => [],
self::CATEGORY_THEME => [
self::FILE_MISSING => [],
self::FILE_ALTERED => [],
],
];

Expand Down Expand Up @@ -116,7 +124,21 @@ public function getFilesDiffBetweenVersions(string $version1, ?string $version2)
*/
public function getTamperedFilesOnShop(string $version)
{
if (is_array($this->fileDifferences) && count($this->fileDifferences['core']['altered']) == 0) {
if (is_array($this->fileDifferences)) {
$useCache = false;

foreach ($this->fileDifferences as $section) {
foreach ([self::FILE_MISSING, self::FILE_ALTERED] as $key) {
if (!empty($section[$key])) {
$useCache = true;
}
}
}

if ($useCache) {
return $this->fileDifferences;
}

$checksum = $this->fileLoader->getXmlMd5File($version);
if (!$checksum) {
$this->fileDifferences = false;
Expand Down Expand Up @@ -250,15 +272,15 @@ protected function md5FileAsArray(SimpleXMLElement $node, string $dir = '/')
*/
protected function addFileDifferences(string $path, bool $isDeletedFile = false): void
{
$key = $isDeletedFile ? 'missing' : 'altered';
$key = $isDeletedFile ? self::FILE_MISSING : self::FILE_ALTERED;

$categories = [
'mail' => ['mails/'],
'translation' => [
self::CATEGORY_MAIL => ['mails/'],
self::CATEGORY_TRANSLATION => [
'/en.php', '/fr.php', '/es.php', '/it.php', '/de.php',
'translations/',
],
'themes' => ['themes/'],
self::CATEGORY_THEME => ['themes/'],
];

foreach ($categories as $category => $patterns) {
Expand All @@ -271,7 +293,7 @@ protected function addFileDifferences(string $path, bool $isDeletedFile = false)
}
}

$this->fileDifferences['core'][$key][] = $path;
$this->fileDifferences[self::CATEGORY_CORE][$key][] = $path;
}

protected function compareChecksum(string $filepath, string $md5sum): bool
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Xml/ChecksumCompareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public function testGetTamperedFilesOnShop()
$tamperedFiles = $checksumCompare->getTamperedFilesOnShop('8.1.0');

$expected = [
'mail' => ['missing' => [], 'altered' => []],
'translation' => ['missing' => [], 'altered' => ['translations/default/AdminActions.xlf']],
'core' => ['missing' => ['admin/init.php'], 'altered' => ['admin/.htaccess']],
'themes' => ['missing' => [], 'altered' => ['themes/classic/config/theme.yml']],
ChecksumCompare::CATEGORY_MAIL => [ChecksumCompare::FILE_MISSING => [], ChecksumCompare::FILE_ALTERED => []],
ChecksumCompare::CATEGORY_TRANSLATION => [ChecksumCompare::FILE_MISSING => [], ChecksumCompare::FILE_ALTERED => ['translations/default/AdminActions.xlf']],
ChecksumCompare::CATEGORY_CORE => [ChecksumCompare::FILE_MISSING => ['admin/init.php'], ChecksumCompare::FILE_ALTERED => ['admin/.htaccess']],
ChecksumCompare::CATEGORY_THEME => [ChecksumCompare::FILE_MISSING => [], ChecksumCompare::FILE_ALTERED => ['themes/classic/config/theme.yml']],
];

$this->assertEquals($expected, $tamperedFiles);
Expand Down

0 comments on commit c09d6ba

Please sign in to comment.