From c09d6baad9405d9370195e69a748bb6f64b86198 Mon Sep 17 00:00:00 2001 From: morgan Date: Tue, 17 Dec 2024 10:33:52 +0100 Subject: [PATCH] Fix fileDifferences cache --- classes/Xml/ChecksumCompare.php | 58 ++++++++++++++++++-------- tests/unit/Xml/ChecksumCompareTest.php | 8 ++-- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/classes/Xml/ChecksumCompare.php b/classes/Xml/ChecksumCompare.php index 11775fadf..884af4bdd 100644 --- a/classes/Xml/ChecksumCompare.php +++ b/classes/Xml/ChecksumCompare.php @@ -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 */ @@ -48,6 +55,7 @@ class ChecksumCompare * @var string */ private $adminPath; + /** * @var array{ * 'mail':array{'missing':string[],'altered':string[]}, @@ -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 => [], ], ]; @@ -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; @@ -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) { @@ -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 diff --git a/tests/unit/Xml/ChecksumCompareTest.php b/tests/unit/Xml/ChecksumCompareTest.php index 0dc67aeaf..d7ae1f8c8 100644 --- a/tests/unit/Xml/ChecksumCompareTest.php +++ b/tests/unit/Xml/ChecksumCompareTest.php @@ -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);