[BUGFIX] Prevent type converters from being registered multiple times 36/56236/2
authorRomain Canon <romain.hydrocanon@gmail.com>
Mon, 2 Oct 2017 22:09:15 +0000 (00:09 +0200)
committerAndreas Wolf <andreas.wolf@typo3.org>
Fri, 16 Mar 2018 15:46:35 +0000 (16:46 +0100)
Registering an exact same type converter class name with the Exbase method
`\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter()` will now
add it only once to the global array.

This will prevent issues that could occur during a strong cache clearing, in
which case the `ext_localconf.php` files are included twice, leading to the
Extbase default type converters being registered twice.

If type converters were to be used early during the runtime (just after the
caches were cleared), a `DuplicateTypeConverterException` would be thrown when
it should not be.

---

Resolves: #82651
Releases: master, 8.7
Change-Id: I01e1f6c2399473ff65c22d9a33f919963f49608f
Reviewed-on: https://review.typo3.org/56236
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/extbase/Classes/Utility/ExtensionUtility.php
typo3/sysext/extbase/Tests/Unit/Utility/ExtensionUtilityTest.php

index f55dfa9..74b9f2d 100644 (file)
@@ -218,7 +218,9 @@ tt_content.' . $pluginSignature . ' {
         if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
             $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'] = [];
         }
-        $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
+        if (!in_array($typeConverterClassName, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'])) {
+            $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters'][] = $typeConverterClassName;
+        }
     }
 
     /**
index 95d7614..9b753a7 100644 (file)
@@ -326,4 +326,25 @@ class ExtensionUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
             $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'][0][2]
         );
     }
+
+    /**
+     * A type converter added several times with the exact same class name must
+     * not be added more than once to the global array.
+     *
+     * @test
+     */
+    public function sameTypeConvertersRegisteredAreAddedOnlyOnce()
+    {
+        $typeConverterClassName = \TYPO3\CMS\Extbase\Property\TypeConverter\ArrayConverter::class;
+
+        $this->assertEmpty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']);
+
+        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter($typeConverterClassName);
+
+        $this->assertContains($typeConverterClassName, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']);
+        $this->assertEquals(1, count($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']));
+
+        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerTypeConverter($typeConverterClassName);
+        $this->assertEquals(1, count($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['typeConverters']));
+    }
 }