[FEATURE] Allow replacing strings in slug
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / DataHandling / SlugHelperTest.php
index b2c6635..90efdaa 100644 (file)
@@ -344,4 +344,123 @@ class SlugHelperTest extends UnitTestCase
             $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)
+        );
+    }
 }