From f75945cc074633389bb88a1cb6a98dbba2af9a97 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Sun, 14 Nov 2021 20:50:20 +0100
Subject: [PATCH] [BUGFIX] Ignore prefixParentPageSlug for non-page record in
 SlugHelper

TCA slug field generator option 'prefixParentPageSlug' has been added
to be used for page records only. Slug generation for non-page records
with pid=0 has been fixed with #94655 which introduced a endless loop
if 'prefixParentPageSlug' is used as slug field generator option for
record tables.

This patch only process 'prefixParentPageSlug' for page records, thus
ignoring it for non-page records and avoiding the endlees loop and as
such prevent segmentation faults or memory_limit fatal errors.

Resolves: #95485
Related: #94655
Releases: master, 11.5, 10.4
Change-Id: I20e1ac68bad6ad9447b03debbedb646495045ac3
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/72168
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: Oliver Bartsch <bo@cedev.de>
---
 .../core/Classes/DataHandling/SlugHelper.php  |  2 +-
 .../Unit/DataHandling/SlugHelperTest.php      | 46 +++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/core/Classes/DataHandling/SlugHelper.php b/typo3/sysext/core/Classes/DataHandling/SlugHelper.php
index ecb81965fa43..ff7961afb791 100644
--- a/typo3/sysext/core/Classes/DataHandling/SlugHelper.php
+++ b/typo3/sysext/core/Classes/DataHandling/SlugHelper.php
@@ -170,7 +170,7 @@ class SlugHelper
             return '/';
         }
         $prefix = '';
-        if ($this->configuration['generatorOptions']['prefixParentPageSlug'] ?? false) {
+        if ($this->tableName === 'pages' && ($this->configuration['generatorOptions']['prefixParentPageSlug'] ?? false)) {
             $languageFieldName = $GLOBALS['TCA'][$this->tableName]['ctrl']['languageField'] ?? null;
             $languageId = (int)($recordData[$languageFieldName] ?? 0);
             $parentPageRecord = $this->resolveParentPageRecord($pid, $languageId);
diff --git a/typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php b/typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php
index 87ff88febd09..f25729273d12 100644
--- a/typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php
+++ b/typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php
@@ -729,4 +729,50 @@ class SlugHelperTest extends UnitTestCase
             $subject->generate(['title' => $input['title'], 'uid' => 13], 0)
         );
     }
+
+    public function generatePrependsSlugsForNonPagesDataProvider(): array
+    {
+        return [
+            'simple title' => [
+                'Product Name',
+                'product-name',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ],
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider generatePrependsSlugsForNonPagesDataProvider
+     */
+    public function generatePrependsSlugsForNonPages(string $input, string $expected, array $options): void
+    {
+        $GLOBALS['dummyTable']['ctrl'] = [];
+        $parentPage = [
+            'uid' => '0',
+            'pid' => null,
+        ];
+        $subject = $this->getAccessibleMock(
+            SlugHelper::class,
+            ['resolveParentPageRecord'],
+            [
+                'another_table',
+                'slug',
+                $options,
+            ]
+        );
+        $subject->expects(self::any())
+            ->method('resolveParentPageRecord')
+            ->withAnyParameters()
+            ->willReturn($parentPage);
+        self::assertEquals(
+            $expected,
+            $subject->generate(['title' => $input, 'uid' => 13], 13)
+        );
+    }
 }
-- 
GitLab