From 0dc79b48410b65cda6dc92a60f284bddedeb3d06 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann <daniel.siepmann@typo3.org> Date: Wed, 25 Aug 2021 15:11:52 +0200 Subject: [PATCH] [TASK] Public GeneralUtility::xml2arrayProcess() It can be beneficial to suppress caching using xml2arrayProcess() directly instead of the cache enabled wrapper xml2array() in some situations. The patch makes the worker method public, adapts tests to mock less and moves a data changing detail from xml2array() to xml2arrayProcess(). Resolves: #94993 Releases: master, 10.4 Change-Id: I2b0347ebb1a293d2826b2d13b00f21b56568195c Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/70757 Tested-by: core-ci <typo3@b13.com> Tested-by: Jochen <rothjochen@gmail.com> Tested-by: Daniel Siepmann <coding@daniel-siepmann.de> Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Reviewed-by: Jochen <rothjochen@gmail.com> Reviewed-by: Daniel Siepmann <coding@daniel-siepmann.de> Reviewed-by: crell <larry@garfieldtech.com> Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> --- .../core/Classes/Utility/GeneralUtility.php | 5 +- .../Tests/Unit/Utility/GeneralUtilityTest.php | 88 +++++++------------ 2 files changed, 37 insertions(+), 56 deletions(-) diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index 18c55a086404..46350c9942de 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -1483,7 +1483,7 @@ class GeneralUtility $identifier = md5($string . $NSprefix . ($reportDocTag ? '1' : '0')); // Look up in first level cache if (empty($firstLevelCache[$identifier])) { - $firstLevelCache[$identifier] = self::xml2arrayProcess(trim($string), $NSprefix, $reportDocTag); + $firstLevelCache[$identifier] = self::xml2arrayProcess($string, $NSprefix, $reportDocTag); $runtimeCache->set('generalUtilityXml2Array', $firstLevelCache); } return $firstLevelCache[$identifier]; @@ -1499,8 +1499,9 @@ class GeneralUtility * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content. * @see array2xml() */ - protected static function xml2arrayProcess($string, $NSprefix = '', $reportDocTag = false) + public static function xml2arrayProcess($string, $NSprefix = '', $reportDocTag = false) { + $string = trim($string); // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept $previousValueOfEntityLoader = null; if (PHP_MAJOR_VERSION < 8) { diff --git a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php index 74e7fdfee25f..2ac79985db78 100644 --- a/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php +++ b/typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php @@ -4085,10 +4085,24 @@ class GeneralUtilityTest extends UnitTestCase </phparray>', $output); } + /** + * @test + */ + public function xml2arrayUsesCache(): void + { + $cacheManagerProphecy = $this->prophesize(CacheManager::class); + $cacheProphecy = $this->prophesize(FrontendInterface::class); + $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); + $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); + $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); + GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); + GeneralUtility::xml2array('<?xml version="1.0" encoding="utf-8" standalone="yes"?>', 'T3:'); + } + /** * @return string[][] */ - public function xml2arrayHandlesWhitespacesDataProvider(): array + public function xml2arrayProcessHandlesWhitespacesDataProvider(): array { $headerVariants = [ 'utf-8' => '<?xml version="1.0" encoding="utf-8" standalone="yes"?>', @@ -4143,17 +4157,10 @@ class GeneralUtilityTest extends UnitTestCase /** * @test - * @dataProvider xml2arrayHandlesWhitespacesDataProvider - * @param string $input + * @dataProvider xml2arrayProcessHandlesWhitespacesDataProvider */ - public function xml2arrayHandlesWhitespaces(string $input) + public function xml2arrayProcessHandlesWhitespaces(string $input): void { - $cacheManagerProphecy = $this->prophesize(CacheManager::class); - $cacheProphecy = $this->prophesize(FrontendInterface::class); - $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); - $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); - $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); $expected = [ 'data' => [ 'settings.persistenceIdentifier' => [ @@ -4161,13 +4168,13 @@ class GeneralUtilityTest extends UnitTestCase ] ], ]; - self::assertSame($expected, GeneralUtility::xml2array($input)); + self::assertSame($expected, GeneralUtility::xml2arrayProcess($input)); } /** * @return string[][] */ - public function xml2arrayHandlesTagNamespacesDataProvider(): array + public function xml2arrayProcessHandlesTagNamespacesDataProvider(): array { return [ 'inputWithNameSpaceOnRootLevel' => [ @@ -4205,17 +4212,10 @@ class GeneralUtilityTest extends UnitTestCase /** * @test - * @dataProvider xml2arrayHandlesTagNamespacesDataProvider - * @param string $input + * @dataProvider xml2arrayProcessHandlesTagNamespacesDataProvider */ - public function xml2arrayHandlesTagNamespaces(string $input) + public function xml2arrayProcessHandlesTagNamespaces(string $input): void { - $cacheManagerProphecy = $this->prophesize(CacheManager::class); - $cacheProphecy = $this->prophesize(FrontendInterface::class); - $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); - $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); - $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); $expected = [ 'data' => [ 'settings.persistenceIdentifier' => [ @@ -4223,13 +4223,13 @@ class GeneralUtilityTest extends UnitTestCase ] ], ]; - self::assertSame($expected, GeneralUtility::xml2array($input, 'T3:')); + self::assertSame($expected, GeneralUtility::xml2arrayProcess($input, 'T3:')); } /** * @return array[] */ - public function xml2arrayHandlesDocumentTagDataProvider(): array + public function xml2arrayProcessHandlesDocumentTagDataProvider(): array { return [ 'input' => [ @@ -4270,18 +4270,10 @@ class GeneralUtilityTest extends UnitTestCase /** * @test - * @dataProvider xml2arrayHandlesDocumentTagDataProvider - * @param string $input - * @param string $docTag + * @dataProvider xml2arrayProcessHandlesDocumentTagDataProvider */ - public function xml2arrayHandlesDocumentTag(string $input, string $docTag) + public function xml2arrayProcessHandlesDocumentTag(string $input, string $docTag): void { - $cacheManagerProphecy = $this->prophesize(CacheManager::class); - $cacheProphecy = $this->prophesize(FrontendInterface::class); - $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); - $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); - $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); $expected = [ 'data' => [ 'settings.persistenceIdentifier' => [ @@ -4290,13 +4282,13 @@ class GeneralUtilityTest extends UnitTestCase ], '_DOCUMENT_TAG' => $docTag ]; - self::assertSame($expected, GeneralUtility::xml2array($input, '', true)); + self::assertSame($expected, GeneralUtility::xml2arrayProcess($input, '', true)); } /** * @return array[] */ - public function xml2ArrayHandlesBigXmlContentDataProvider(): array + public function xml2ArrayProcessHandlesBigXmlContentDataProvider(): array { return [ '1mb' => [ @@ -4326,18 +4318,12 @@ class GeneralUtilityTest extends UnitTestCase /** * @test - * @dataProvider xml2ArrayHandlesBigXmlContentDataProvider + * @dataProvider xml2ArrayProcessHandlesBigXmlContentDataProvider * @param string $input * @param string $testValue */ - public function xml2ArrayHandlesBigXmlContent(string $input, string $testValue) + public function xml2ArrayProcessHandlesBigXmlContent(string $input, string $testValue): void { - $cacheManagerProphecy = $this->prophesize(CacheManager::class); - $cacheProphecy = $this->prophesize(FrontendInterface::class); - $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); - $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); - $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); $expected = [ 'data' => [ 'settings.persistenceIdentifier' => [ @@ -4345,7 +4331,7 @@ class GeneralUtilityTest extends UnitTestCase ] ], ]; - self::assertSame($expected, GeneralUtility::xml2array($input)); + self::assertSame($expected, GeneralUtility::xml2arrayProcess($input)); } /** @@ -4381,7 +4367,7 @@ class GeneralUtilityTest extends UnitTestCase /** * @return array[] */ - public function xml2ArrayHandlesAttributeTypesDataProvider() + public function xml2ArrayProcessHandlesAttributeTypesDataProvider(): array { $prefix = '<?xml version="1.0" encoding="utf-8" standalone="yes"?><T3FlexForms><field index="index">'; $suffix = '</field></T3FlexForms>'; @@ -4455,19 +4441,13 @@ class GeneralUtilityTest extends UnitTestCase /** * @test - * @dataProvider xml2ArrayHandlesAttributeTypesDataProvider + * @dataProvider xml2ArrayProcessHandlesAttributeTypesDataProvider * @param string $input * @param $expected */ - public function xml2ArrayHandlesAttributeTypes(string $input, $expected) + public function xml2ArrayProcessHandlesAttributeTypes(string $input, $expected): void { - $cacheManagerProphecy = $this->prophesize(CacheManager::class); - $cacheProphecy = $this->prophesize(FrontendInterface::class); - $cacheManagerProphecy->getCache('runtime')->willReturn($cacheProphecy->reveal()); - $cacheProphecy->get('generalUtilityXml2Array')->shouldBeCalled()->willReturn(false); - $cacheProphecy->set('generalUtilityXml2Array', Argument::cetera())->shouldBeCalled(); - GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal()); - $result = GeneralUtility::xml2array($input); + $result = GeneralUtility::xml2arrayProcess($input); self::assertSame($expected, $result['index']['vDEF']); } -- GitLab