[FEATURE] Allow replacing strings in slug
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / DataHandling / SlugHelperTest.php
index 22ad184..90efdaa 100644 (file)
@@ -34,7 +34,182 @@ 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' => [
+                [],
+                '/',
+                '/',
+            ],
+            'invalid base' => [
+                [],
+                '//',
+                '/',
+            ],
+            'invalid slug' => [
+                [],
+                '/slug//',
+                '/slug/',
             ],
             'lowercase characters' => [
                 [],
@@ -80,20 +255,20 @@ class SlugHelperTest extends UnitTestCase
                 '/1/2',
                 '/1/2',
             ],
-            'remove trailing slash' => [
+            'do not remove trailing slash' => [
                 [],
                 '1/2/',
-                '/1/2',
+                '/1/2/',
             ],
             'keep pending slash and remove fallback' => [
                 [],
                 '/-1/2',
                 '/1/2',
             ],
-            'remove trailing slash and fallback' => [
+            'do not remove trailing slash, but remove fallback' => [
                 [],
                 '1/2-/',
-                '/1/2',
+                '/1/2/',
             ],
             'reduce multiple fallback chars to one' => [
                 [],
@@ -105,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(
@@ -134,4 +302,165 @@ class SlugHelperTest extends UnitTestCase
             $subject->sanitize($input)
         );
     }
+
+    public function generateNeverDeliversEmptySlugForPagesDataProvider()
+    {
+        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 generateNeverDeliversEmptySlugForPagesDataProvider
+     * @param string $input
+     * @param string $expected
+     * @test
+     */
+    public function generateNeverDeliversEmptySlugForPages(string $input, string $expected)
+    {
+        $GLOBALS['dummyTable']['ctrl'] = [];
+        $subject = new SlugHelper(
+            'pages',
+            'slug',
+            ['generatorOptions' => ['fields' => ['title']]]
+        );
+        static::assertEquals(
+            $expected,
+            $subject->generate(['title' => $input, 'uid' => 13], 13)
+        );
+    }
+
+    /**
+     * @return array
+     */
+    public function generatePrependsSlugsForPagesDataProvider(): array
+    {
+        return [
+            'simple title' => [
+                'Products',
+                '/parent-page/products',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ]
+            ],
+            'title with spaces' => [
+                'Product Cow',
+                '/parent-page/product-cow',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ]
+            ],
+            'title with slash' => [
+                'Product/Cow',
+                '/parent-page/product/cow',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ]
+            ],
+            'title with slash and replace' => [
+                'Product/Cow',
+                '/parent-page/productcow',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                        'replacements' => [
+                            '/' => ''
+                        ]
+                    ],
+                ]
+            ],
+            'title with slash and replace #2' => [
+                'Some Job in city1/city2 (m/w)',
+                '/parent-page/some-job-in-city1-city2',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                        'replacements' => [
+                            '(m/w)' => '',
+                            '/' => '-'
+                        ]
+                    ],
+                ]
+            ],
+            'title with invalid characters' => [
+                'Products - Cows',
+                '/parent-page/products-cows',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ]
+            ],
+            'title with only invalid characters' => [
+                '!!!',
+                '/parent-page/default-51cf35392c',
+                [
+                    'generatorOptions' => [
+                        'fields' => ['title'],
+                        'prefixParentPageSlug' => true,
+                    ],
+                ]
+            ],
+        ];
+    }
+
+    /**
+     * @dataProvider generatePrependsSlugsForPagesDataProvider
+     * @param string $input
+     * @param string $expected
+     * @test
+     */
+    public function generatePrependsSlugsForPages(string $input, string $expected, array $options)
+    {
+        $GLOBALS['dummyTable']['ctrl'] = [];
+        $parentPage = [
+            'uid' => '13',
+            'pid' => '10',
+            'title' => 'Parent Page',
+        ];
+        $subject = $this->getAccessibleMock(
+            SlugHelper::class,
+            ['resolveParentPageRecord'],
+            [
+                'pages',
+                'slug',
+                $options
+            ]
+        );
+        $subject->expects(static::at(0))
+            ->method('resolveParentPageRecord')->with(13)->willReturn($parentPage);
+        $subject->expects(static::at(1))
+            ->method('resolveParentPageRecord')->with(10)->willReturn(null);
+        static::assertEquals(
+            $expected,
+            $subject->generate(['title' => $input, 'uid' => 13], 13)
+        );
+    }
 }