[BUGFIX] Prevent type converters from being registered multiple times 63/54263/3
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:33:49 +0000 (16:33 +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/54263
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
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 f809605..bd448eb 100644 (file)
@@ -214,7 +214,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 d24c3b1..7336cee 100644 (file)
@@ -330,4 +330,25 @@ class ExtensionUtilityTest extends UnitTestCase
             $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']));
+    }
 }