[BUGFIX] Avoid suffixing base slug again
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / DataHandling / SlugHelperTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\DataHandling;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\DataHandling\SlugHelper;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 class SlugHelperTest extends UnitTestCase
22 {
23 /**
24 * @var bool
25 */
26 protected $resetSingletonInstances = true;
27
28 /**
29 * @return array
30 */
31 public function sanitizeDataProvider(): array
32 {
33 return [
34 'empty string' => [
35 [],
36 '',
37 '/',
38 ],
39 'existing base' => [
40 [],
41 '/',
42 '/',
43 ],
44 'invalid base' => [
45 [],
46 '//',
47 '/',
48 ],
49 'invalid slug' => [
50 [],
51 '/slug//',
52 '/slug/',
53 ],
54 'lowercase characters' => [
55 [],
56 '1AZÄ',
57 '/1azae',
58 ],
59 'strig tags' => [
60 [],
61 '<foo>bar</foo>',
62 '/bar'
63 ],
64 'replace special chars to -' => [
65 [],
66 '1 2-3+4_5',
67 '/1-2-3-4-5',
68 ],
69 'empty fallback character' => [
70 [
71 'fallbackCharacter' => '',
72 ],
73 '1_2',
74 '/12',
75 ],
76 'different fallback character' => [
77 [
78 'fallbackCharacter' => '_',
79 ],
80 '1-2',
81 '/1_2',
82 ],
83 'convert umlauts' => [
84 [],
85 'ä ß Ö',
86 '/ae-ss-oe'
87 ],
88 'keep slashes' => [
89 [],
90 '1/2',
91 '/1/2',
92 ],
93 'keep pending slash' => [
94 [],
95 '/1/2',
96 '/1/2',
97 ],
98 'do not remove trailing slash' => [
99 [],
100 '1/2/',
101 '/1/2/',
102 ],
103 'keep pending slash and remove fallback' => [
104 [],
105 '/-1/2',
106 '/1/2',
107 ],
108 'do not remove trailing slash, but remove fallback' => [
109 [],
110 '1/2-/',
111 '/1/2/',
112 ],
113 'reduce multiple fallback chars to one' => [
114 [],
115 '1---2',
116 '/1-2',
117 ],
118 'various special chars' => [
119 [],
120 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
121 '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
122 ],
123 'various special chars, allow unicode' => [
124 [
125 'allowUnicodeCharacters' => true,
126 ],
127 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
128 '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
129 ]
130 ];
131 }
132
133 /**
134 * @test
135 * @dataProvider sanitizeDataProvider
136 * @param array $configuration
137 * @param string $input
138 * @param string $expected
139 */
140 public function sanitizeConvertsString(array $configuration, string $input, string $expected)
141 {
142 $subject = new SlugHelper(
143 'dummyTable',
144 'dummyField',
145 $configuration
146 );
147 static::assertEquals(
148 $expected,
149 $subject->sanitize($input)
150 );
151 }
152 }