[BUGFIX] Auto-generate slug when invalid characters are given 55/58155/2
authorBenni Mack <benni@typo3.org>
Mon, 3 Sep 2018 19:14:45 +0000 (21:14 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 3 Sep 2018 19:59:08 +0000 (21:59 +0200)
If a page title is like "!!!" this currently results in an invalid slug.

The generator now auto-generates something with "default-hashXYZ"
in order to always be unique on creation time.

Resolves: #86122
Releases: master
Change-Id: I453e2155c29f1bde4d05d481e0ae43913bc5ad8b
Reviewed-on: https://review.typo3.org/58155
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/DataHandling/SlugHelper.php
typo3/sysext/core/Tests/Unit/DataHandling/SlugHelperTest.php

index dfe6cc3..90f736e 100644 (file)
@@ -142,8 +142,8 @@ class SlugHelper
             return '/';
         }
         $prefix = '';
-        $languageId = (int)$recordData[$GLOBALS['TCA'][$this->tableName]['ctrl']['languageField']];
         if ($this->configuration['generatorOptions']['prefixParentPageSlug'] ?? false) {
+            $languageId = (int)$recordData[$GLOBALS['TCA'][$this->tableName]['ctrl']['languageField']];
             $rootLine = BackendUtility::BEgetRootLine($pid, '', true, ['nav_title']);
             $parentPageRecord = reset($rootLine);
             if ($languageId > 0) {
@@ -174,8 +174,13 @@ class SlugHelper
             }
         }
         $slug = implode($fieldSeparator, $slugParts);
+        $slug = $this->sanitize($slug);
+        // No valid data found
+        if ($slug === '/') {
+            $slug .= 'default-' . GeneralUtility::shortMD5(json_encode($recordData));
+        }
         if (!empty($prefix)) {
-            $slug = $prefix . '/' . $slug;
+            $slug = $prefix . $slug;
         }
 
         return $this->sanitize($slug);
index 81ec25f..cba006a 100644 (file)
@@ -149,4 +149,46 @@ class SlugHelperTest extends UnitTestCase
             $subject->sanitize($input)
         );
     }
+
+    public function generateNeverDeliversEmptySlugDataProvider()
+    {
+        return [
+            'simple title' => [
+                'Products',
+                '/products'
+            ],
+            'title with spaces' => [
+                'Product Cow',
+                '/product-cow'
+            ],
+            'title with invalid characters' => [
+                'Products - Cows',
+                '/products-cows'
+            ],
+            'title with only invalid characters' => [
+                '!!!',
+                '/default-51cf35392c'
+            ],
+        ];
+    }
+
+    /**
+     * @dataProvider generateNeverDeliversEmptySlugDataProvider
+     * @param string $input
+     * @param string $expected
+     * @test
+     */
+    public function generateNeverDeliversEmptySlug(string $input, string $expected)
+    {
+        $GLOBALS['dummyTable']['ctrl'] = [];
+        $subject = new SlugHelper(
+            'dummyTable',
+            'dummyField',
+            ['generatorOptions' => ['fields' => ['title']]]
+        );
+        static::assertEquals(
+            $expected,
+            $subject->generate(['title' => $input, 'uid' => 13], 13)
+        );
+    }
 }