From eb639f4d8add9eca3cb1095f6367eeaf0ef0e93b Mon Sep 17 00:00:00 2001
From: Andreas Fernandez <a.fernandez@scripting-base.de>
Date: Mon, 20 Dec 2021 15:08:18 +0100
Subject: [PATCH] [BUGFIX] Allow CSV in startingPoints config as advertised

The category starting points introduced with #95037 were designed to
allow comma-separated values in its configuration. Due to improper
tests, this specific notation was not discovered as being broken right
now.

If the incoming configuration is a string, GeneralUtility::intExplode()
is applied to remove any non-integer value. Afterwards, a new CSV
string is generated.

Resolves: #96397
Related: #95037
Releases: main, 11.5
Change-Id: I0759d8093bb4665b7709f4e20539307467f246df
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72738
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
---
 .../FormDataProvider/AbstractItemProvider.php   | 11 ++++++++---
 .../Form/FormDataProvider/TcaCategoryTest.php   | 17 ++++++++++++++++-
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php b/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
index f75b65f273ef..4db2e9ad9366 100644
--- a/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
+++ b/typo3/sysext/backend/Classes/Form/FormDataProvider/AbstractItemProvider.php
@@ -889,14 +889,19 @@ abstract class AbstractItemProvider
         $parsedSiteConfiguration = $this->parseSiteConfiguration($result['site'], $fieldConfig['config']['treeConfig']['startingPoints']);
         if ($parsedSiteConfiguration !== []) {
             // $this->quoteParsedSiteConfiguration() is omitted on purpose, all values are cast to integers
-            $parsedSiteConfiguration = array_unique(array_map(static function ($value) {
+            $parsedSiteConfiguration = array_unique(array_map(static function ($value): string {
                 if (is_array($value)) {
                     return implode(',', array_map('intval', $value));
                 }
 
-                return (int)$value;
+                return implode(',', GeneralUtility::intExplode(',', $value, true));
             }, $parsedSiteConfiguration));
-            $fieldConfig['config']['treeConfig']['startingPoints'] = $this->replaceParsedSiteConfiguration($fieldConfig['config']['treeConfig']['startingPoints'], $parsedSiteConfiguration);
+            $resolvedStartingPoins = $this->replaceParsedSiteConfiguration($fieldConfig['config']['treeConfig']['startingPoints'], $parsedSiteConfiguration);
+            // Add the resolved starting points while removing empty values
+            $fieldConfig['config']['treeConfig']['startingPoints'] = implode(
+                ',',
+                GeneralUtility::trimExplode(',', $resolvedStartingPoins, true)
+            );
         }
 
         return $fieldConfig;
diff --git a/typo3/sysext/backend/Tests/Functional/Form/FormDataProvider/TcaCategoryTest.php b/typo3/sysext/backend/Tests/Functional/Form/FormDataProvider/TcaCategoryTest.php
index b5632a351f91..364629c6ea53 100644
--- a/typo3/sysext/backend/Tests/Functional/Form/FormDataProvider/TcaCategoryTest.php
+++ b/typo3/sysext/backend/Tests/Functional/Form/FormDataProvider/TcaCategoryTest.php
@@ -191,16 +191,31 @@ class TcaCategoryTest extends FunctionalTestCase
                 'expectedStartingPoints' => '42,4711,12',
                 'site' => new Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategory' => 4711]]),
             ],
-            'one setting, multiple categories' => [
+            'one setting, multiple categories as array' => [
                 'inputStartingPoints' => '###SITE:categories.contentCategories###',
                 'expectedStartingPoints' => '4711,4712,42',
                 'site' => new Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategories' => [4711, 4712, 42]]]),
             ],
+            'one setting, multiple categories as csv' => [
+                'inputStartingPoints' => '###SITE:categories.contentCategories###',
+                'expectedStartingPoints' => '4711,4712,42',
+                'site' => new Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategories' => '4711,4712,42']]),
+            ],
             'two settings' => [
                 'inputStartingPoints' => '42,###SITE:categories.contentCategory###,12,###SITE:foobar###',
                 'expectedStartingPoints' => '42,4711,12,1',
                 'site' => new Site('some-site', 1, ['rootPageId' => 1, 'foobar' => 1, 'categories' => ['contentCategory' => 4711]]),
             ],
+            'one invalid settings' => [
+                'inputStartingPoints' => '42,12,###SITE:invalid###',
+                'expectedStartingPoints' => '42,12',
+                'site' => new Site('some-site', 1, ['rootPageId' => 1]),
+            ],
+            'one valid and one invalid setting' => [
+                'inputStartingPoints' => '42,###SITE:invalid###,12,###SITE:categories.contentCategory###',
+                'expectedStartingPoints' => '42,12,4711,4712',
+                'site' => new Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategory' => '4711,4712']]),
+            ],
         ];
     }
 
-- 
GitLab