[FEATURE] Make prepending slash in TCA slug configurable
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / DataHandling / SlugHelperTest.php
index cba006a..b2c6635 100644 (file)
@@ -34,6 +34,166 @@ class SlugHelperTest extends UnitTestCase
             'empty string' => [
                 [],
                 '',
+                '',
+            ],
+            'existing base' => [
+                [],
+                '/',
+                '',
+            ],
+            'invalid base' => [
+                [],
+                '//',
+                '',
+            ],
+            'invalid slug' => [
+                [],
+                '/slug//',
+                'slug/',
+            ],
+            'lowercase characters' => [
+                [],
+                '1AZÄ',
+                '1azae',
+            ],
+            'strig tags' => [
+                [],
+                '<foo>bar</foo>',
+                'bar'
+            ],
+            'replace special chars to -' => [
+                [],
+                '1 2-3+4_5',
+                '1-2-3-4-5',
+            ],
+            'empty fallback character' => [
+                [
+                    'fallbackCharacter' => '',
+                ],
+                '1_2',
+                '12',
+            ],
+            'different fallback character' => [
+                [
+                    'fallbackCharacter' => '_',
+                ],
+                '1-2',
+                '1_2',
+            ],
+            'convert umlauts' => [
+                [],
+                'ä ß Ö',
+                'ae-ss-oe'
+            ],
+            'keep slashes' => [
+                [],
+                '1/2',
+                '1/2',
+            ],
+            'keep pending slash' => [
+                [],
+                '/1/2',
+                '1/2',
+            ],
+            'do not remove trailing slash' => [
+                [],
+                '1/2/',
+                '1/2/',
+            ],
+            'keep pending slash and remove fallback' => [
+                [],
+                '/-1/2',
+                '1/2',
+            ],
+            'do not remove trailing slash, but remove fallback' => [
+                [],
+                '1/2-/',
+                '1/2/',
+            ],
+            'reduce multiple fallback chars to one' => [
+                [],
+                '1---2',
+                '1-2',
+            ],
+            'various special chars' => [
+                [],
+                'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
+                'special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
+            ],
+        ];
+    }
+
+    /**
+     * @test
+     * @dataProvider sanitizeDataProvider
+     * @param array $configuration
+     * @param string $input
+     * @param string $expected
+     */
+    public function sanitizeConvertsString(array $configuration, string $input, string $expected)
+    {
+        $subject = new SlugHelper(
+            'dummyTable',
+            'dummyField',
+            $configuration
+        );
+        static::assertEquals(
+            $expected,
+            $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)
+        );
+    }
+
+    /**
+     * @return array
+     */
+    public function sanitizeForPagesDataProvider(): array
+    {
+        return [
+            'empty string' => [
+                [],
+                '',
                 '/',
             ],
             'existing base' => [
@@ -120,28 +280,21 @@ class SlugHelperTest extends UnitTestCase
                 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
                 '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
             ],
-            'various special chars, allow unicode' => [
-                [
-                    'allowUnicodeCharacters' => true,
-                ],
-                'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
-                '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
-            ]
         ];
     }
 
     /**
      * @test
-     * @dataProvider sanitizeDataProvider
+     * @dataProvider sanitizeForPagesDataProvider
      * @param array $configuration
      * @param string $input
      * @param string $expected
      */
-    public function sanitizeConvertsString(array $configuration, string $input, string $expected)
+    public function sanitizeConvertsStringForPages(array $configuration, string $input, string $expected)
     {
         $subject = new SlugHelper(
-            'dummyTable',
-            'dummyField',
+            'pages',
+            'slug',
             $configuration
         );
         static::assertEquals(
@@ -150,7 +303,7 @@ class SlugHelperTest extends UnitTestCase
         );
     }
 
-    public function generateNeverDeliversEmptySlugDataProvider()
+    public function generateNeverDeliversEmptySlugForPagesDataProvider()
     {
         return [
             'simple title' => [
@@ -173,17 +326,17 @@ class SlugHelperTest extends UnitTestCase
     }
 
     /**
-     * @dataProvider generateNeverDeliversEmptySlugDataProvider
+     * @dataProvider generateNeverDeliversEmptySlugForPagesDataProvider
      * @param string $input
      * @param string $expected
      * @test
      */
-    public function generateNeverDeliversEmptySlug(string $input, string $expected)
+    public function generateNeverDeliversEmptySlugForPages(string $input, string $expected)
     {
         $GLOBALS['dummyTable']['ctrl'] = [];
         $subject = new SlugHelper(
-            'dummyTable',
-            'dummyField',
+            'pages',
+            'slug',
             ['generatorOptions' => ['fields' => ['title']]]
         );
         static::assertEquals(