From 005f04a3b4c4e0450cc17e4b23aaeb92c5aac5ad Mon Sep 17 00:00:00 2001 From: Daniel Siepmann <daniel.siepmann@typo3.org> Date: Tue, 23 Apr 2024 11:42:20 +0200 Subject: [PATCH] [BUGFIX] Properly handle diff while updating meta data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing solution using `array_diff` did not work, as it only respects values, not a combination with keys. Given an existing record with some empty values would prevent unsetting another value with an empty. The same would apply for setting another value to an existing value. That's why we change to `array_diff_assoc`, which handles proper diff of value in combination with key. Resolves: #103708 Releases: main, 12.4 Change-Id: I36adbc8279e5160c33e8feffcd8741c30c667adc Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/84000 Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: Stefan Bürk <stefan@buerk.tech> Tested-by: core-ci <typo3@b13.com> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Stefan Bürk <stefan@buerk.tech> --- .../Resource/Index/MetaDataRepository.php | 2 +- .../Index/Fixtures/FileWithMetaData.csv | 6 +++ .../Resource/Index/MetaDataRepositoryTest.php | 49 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 typo3/sysext/core/Tests/Functional/Resource/Index/Fixtures/FileWithMetaData.csv create mode 100644 typo3/sysext/core/Tests/Functional/Resource/Index/MetaDataRepositoryTest.php diff --git a/typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php b/typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php index 895d23c3b906..364772ed13e0 100644 --- a/typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php +++ b/typo3/sysext/core/Classes/Resource/Index/MetaDataRepository.php @@ -176,7 +176,7 @@ class MetaDataRepository implements SingletonInterface if (array_key_exists('uid', $updateRow)) { unset($updateRow['uid']); } - $updateRow = array_diff($updateRow, $metaDataFromDatabase); + $updateRow = array_diff_assoc($updateRow, $metaDataFromDatabase); if ($updateRow === []) { // Nothing to update - return current database row return $metaDataFromDatabase; diff --git a/typo3/sysext/core/Tests/Functional/Resource/Index/Fixtures/FileWithMetaData.csv b/typo3/sysext/core/Tests/Functional/Resource/Index/Fixtures/FileWithMetaData.csv new file mode 100644 index 000000000000..42a9c945fd15 --- /dev/null +++ b/typo3/sysext/core/Tests/Functional/Resource/Index/Fixtures/FileWithMetaData.csv @@ -0,0 +1,6 @@ +"sys_file",,,,,,,,,, +,"uid","pid","storage","identifier","folder_hash","name","identifier_hash","extension","mime_type","sha1" +,1,0,1,"/bar/bla/foo.txt","d839c094d3294deb5b61cf7e0fe4f1cddaa96579","foo","84e70e8b11db88cea944e4d7be107092ab148b72","txt","text/plain","6e237d7c3e98387f924b81aada9b8d1f43f5eb5b" +"sys_file_metadata",,,,,,,,,, +,"uid","pid","crdate","file","sys_language_uid","title","description","l10n_parent","copyright", +,1,0,1556435417,1,0,"Title for foo","FoDescription",0,"John Doe", diff --git a/typo3/sysext/core/Tests/Functional/Resource/Index/MetaDataRepositoryTest.php b/typo3/sysext/core/Tests/Functional/Resource/Index/MetaDataRepositoryTest.php new file mode 100644 index 000000000000..0f97071f49e6 --- /dev/null +++ b/typo3/sysext/core/Tests/Functional/Resource/Index/MetaDataRepositoryTest.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Core\Tests\Functional\Resource\Index; + +use PHPUnit\Framework\Attributes\Test; +use TYPO3\CMS\Core\Resource\Index\MetaDataRepository; +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; + +final class MetaDataRepositoryTest extends FunctionalTestCase +{ + protected array $coreExtensionsToLoad = ['filemetadata']; + + #[Test] + public function canUpdateAnExistingFieldWithEmptyValue(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/FileWithMetaData.csv'); + $subject = $this->get(MetaDataRepository::class); + $result = $subject->update(1, [ + 'copyright' => '', + ]); + self::assertSame('', $result['copyright']); + } + + #[Test] + public function canUpdateAnExistingFieldWithNewValue(): void + { + $this->importCSVDataSet(__DIR__ . '/Fixtures/FileWithMetaData.csv'); + $subject = $this->get(MetaDataRepository::class); + $result = $subject->update(1, [ + 'copyright' => 'Something New', + ]); + self::assertSame('Something New', $result['copyright']); + } +} -- GitLab