From be47f2d869429eacada868acebf703d92bac4e96 Mon Sep 17 00:00:00 2001
From: Frans Saris <franssaris@gmail.com>
Date: Tue, 31 Oct 2017 16:32:51 +0100
Subject: [PATCH] [BUGFIX] Add correct conversion of RTE pageTS config

Add all pageTS.RTE options to config without dots appended to the key
to be able to override the RTE config with pageTS like before.

Resolves: #82865
Releases: master, 8,7
Change-Id: Idb7d7d3b39d44ec37be8f93cc9de8b42b9c8a55c
Reviewed-on: https://review.typo3.org/54511
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
---
 .../core/Classes/Configuration/Richtext.php   | 20 +++--
 .../Tests/Unit/Configuration/RichtextTest.php | 86 ++++++++++++++-----
 2 files changed, 78 insertions(+), 28 deletions(-)

diff --git a/typo3/sysext/core/Classes/Configuration/Richtext.php b/typo3/sysext/core/Classes/Configuration/Richtext.php
index 93ac17b66a5e..3d39af917d3e 100644
--- a/typo3/sysext/core/Classes/Configuration/Richtext.php
+++ b/typo3/sysext/core/Classes/Configuration/Richtext.php
@@ -66,7 +66,7 @@ class Richtext
         // overlay preset configuration with pageTs
         ArrayUtility::mergeRecursiveWithOverrule(
             $configuration,
-            $this->cleanDotsFromEditorConfigKeys($pageTs)
+            $this->addFlattenedPageTsConfig($pageTs)
         );
 
         // Handle "mode" / "transformation" config when overridden
@@ -144,18 +144,22 @@ class Richtext
     }
 
     /**
-     * Strip dots from the 'editor.' part of a given TypoScript array
+     * Add all PageTS.RTE options keys to configuration without dots
      *
-     * @param array $typoScriptArray TypoScriptArray (with dots) that may contain an 'editor.' subarray
-     * @return array array with dots stripped from the editor subarray
+     * We need to keep the dotted keys for backwards compatibility like ext:rtehtmlarea
+     *
+     * @param array $typoScriptArray TypoScriptArray
+     * @return array array with config without dots added
      */
-    protected function cleanDotsFromEditorConfigKeys(array $typoScriptArray): array
+    protected function addFlattenedPageTsConfig(array $typoScriptArray): array
     {
-        if (isset($typoScriptArray['editor.'])) {
+        foreach ($typoScriptArray as $key => $data) {
+            if (substr($key, -1) !== '.') {
+                continue;
+            }
             /** @var TypoScriptService $typoScriptService */
             $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
-            $typoScriptArray['editor'] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray['editor.']);
-            unset($typoScriptArray['editor.']);
+            $typoScriptArray[substr($key, 0, -1)] = $typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptArray[$key]);
         }
 
         return $typoScriptArray;
diff --git a/typo3/sysext/core/Tests/Unit/Configuration/RichtextTest.php b/typo3/sysext/core/Tests/Unit/Configuration/RichtextTest.php
index d4ba9c0d18bb..b9ee5b0a789e 100644
--- a/typo3/sysext/core/Tests/Unit/Configuration/RichtextTest.php
+++ b/typo3/sysext/core/Tests/Unit/Configuration/RichtextTest.php
@@ -34,7 +34,7 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'removeComments' => '1',
@@ -56,12 +56,18 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         ];
         $expected = [
             'classes.' => [
-                'aClass.' => 'aConfig',
+                'aClass' => 'aConfig',
             ],
             'removeComments' => '1',
             'proc.' => [
                 'overruleMode' => 'myTransformation',
             ],
+            'classes' => [
+                'aClass' => 'aConfig',
+            ],
+            'proc' => [
+                'overruleMode' => 'myTransformation',
+            ],
         ];
         // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
         // which can't be mocked in a sane way
@@ -83,7 +89,7 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'removeComments' => '1',
@@ -101,12 +107,18 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         ];
         $expected = [
             'classes.' => [
-                'aClass.' => 'aConfig',
+                'aClass' => 'aConfig',
             ],
             'removeComments' => '1',
             'proc.' => [
                 'overruleMode' => 'myTransformation',
             ],
+            'classes' => [
+                'aClass' => 'aConfig',
+            ],
+            'proc' => [
+                'overruleMode' => 'myTransformation',
+            ],
         ];
         // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
         // which can't be mocked in a sane way
@@ -128,7 +140,7 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'removeComments' => '1',
@@ -137,9 +149,12 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         ];
         $expected = [
             'classes.' => [
-                'aClass.' => 'aConfig',
+                'aClass' => 'aConfig',
             ],
             'removeComments' => '1',
+            'classes' => [
+                'aClass' => 'aConfig',
+            ],
             'proc.' => [
                 'overruleMode' => 'default',
             ],
@@ -164,11 +179,11 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'classes.' => [
-                        'aClass.' => 'anotherConfig',
+                        'aClass' => 'anotherConfig',
                     ],
                     'editor.' => [
                         'config.' => [
@@ -180,7 +195,15 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         ];
         $expected = [
             'classes.' => [
-                'aClass.' => 'anotherConfig',
+                'aClass' => 'anotherConfig',
+            ],
+            'editor.' => [
+                'config.' => [
+                    'contentsCss' => 'my.css'
+                ]
+            ],
+            'classes' => [
+                'aClass' => 'anotherConfig',
             ],
             'editor' => [
                 'config' => [
@@ -211,18 +234,18 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'classes.' => [
-                        'aClass.' => 'anotherConfig',
+                        'aClass' => 'anotherConfig',
                     ],
                 ],
                 'config.' => [
                     'aTable.' => [
                         'aField.' => [
                             'classes.' => [
-                                'aClass.' => 'aThirdConfig',
+                                'aClass' => 'aThirdConfig',
                             ],
                             'editor.' => [
                                 'config.' => [
@@ -235,10 +258,19 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
             ],
         ];
         $expected = [
+            // Config with pagets dots
             'classes.' => [
-                'aClass.' => 'aThirdConfig',
+                'aClass' => 'aThirdConfig',
+            ],
+            'editor.' => [
+                'config.' => [
+                    'contentsCss' => 'my.css'
+                ]
+            ],
+            // Config without pagets dots
+            'classes' => [
+                'aClass' => 'aThirdConfig',
             ],
-            // editor config without pagets dots
             'editor' => [
                 'config' => [
                     'contentsCss' => 'my.css'
@@ -268,18 +300,18 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
         $pageTsConfig = [
             'properties' => [
                 'classes.' => [
-                    'aClass.' => 'aConfig',
+                    'aClass' => 'aConfig',
                 ],
                 'default.' => [
                     'classes.' => [
-                        'aClass.' => 'anotherConfig',
+                        'aClass' => 'anotherConfig',
                     ],
                 ],
                 'config.' => [
                     'aTable.' => [
                         'aField.' => [
                             'classes.' => [
-                                'aClass.' => 'aThirdConfig',
+                                'aClass' => 'aThirdConfig',
                             ],
                             'editor.' => [
                                 'config.' => [
@@ -289,7 +321,7 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
                             'types.' => [
                                 'textmedia.' => [
                                     'classes.' => [
-                                        'aClass.' => 'aTypeSpecifcConfig',
+                                        'aClass' => 'aTypeSpecifcConfig',
                                     ],
                                     'editor.' => [
                                         'config.' => [
@@ -304,10 +336,19 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
             ],
         ];
         $expected = [
+            // Config with pagets dots
             'classes.' => [
-                'aClass.' => 'aTypeSpecifcConfig',
+                'aClass' => 'aTypeSpecifcConfig',
+            ],
+            'editor.' => [
+                'config.' => [
+                    'contentsCss' => 'your.css'
+                ]
+            ],
+            // Config without pagets dots
+            'classes' => [
+                'aClass' => 'aTypeSpecifcConfig',
             ],
-            // editor config without pagets dots
             'editor' => [
                 'config' => [
                     'contentsCss' => 'your.css'
@@ -371,6 +412,11 @@ class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
                 ],
             ],
             'preset' => 'default',
+            'editor.' => [
+                'config.' => [
+                    'width' => 200
+                ],
+            ],
             'proc.' => [
                 'overruleMode' => 'default',
             ],
-- 
GitLab