[FEATURE] Add a new TCA type "slug"
[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 'lowercase characters' => [
40 [],
41 '1AZÄ',
42 '/1azae',
43 ],
44 'strig tags' => [
45 [],
46 '<foo>bar</foo>',
47 '/bar'
48 ],
49 'replace special chars to -' => [
50 [],
51 '1 2-3+4_5',
52 '/1-2-3-4-5',
53 ],
54 'empty fallback character' => [
55 [
56 'fallbackCharacter' => '',
57 ],
58 '1_2',
59 '/12',
60 ],
61 'different fallback character' => [
62 [
63 'fallbackCharacter' => '_',
64 ],
65 '1-2',
66 '/1_2',
67 ],
68 'convert umlauts' => [
69 [],
70 'ä ß Ö',
71 '/ae-ss-oe'
72 ],
73 'keep slashes' => [
74 [],
75 '1/2',
76 '/1/2',
77 ],
78 'keep pending slash' => [
79 [],
80 '/1/2',
81 '/1/2',
82 ],
83 'remove trailing slash' => [
84 [],
85 '1/2/',
86 '/1/2',
87 ],
88 'keep pending slash and remove fallback' => [
89 [],
90 '/-1/2',
91 '/1/2',
92 ],
93 'remove trailing slash and fallback' => [
94 [],
95 '1/2-/',
96 '/1/2',
97 ],
98 'reduce multiple fallback chars to one' => [
99 [],
100 '1---2',
101 '/1-2',
102 ],
103 'various special chars' => [
104 [],
105 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
106 '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
107 ],
108 'various special chars, allow unicode' => [
109 [
110 'allowUnicodeCharacters' => true,
111 ],
112 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–',
113 '/special-chars-eur-r-o-oe-p-aa-f-c-a-o-yen-c-u'
114 ]
115 ];
116 }
117
118 /**
119 * @test
120 * @dataProvider sanitizeDataProvider
121 * @param array $configuration
122 * @param string $input
123 * @param string $expected
124 */
125 public function sanitizeConvertsString(array $configuration, string $input, string $expected)
126 {
127 $subject = new SlugHelper(
128 'dummyTable',
129 'dummyField',
130 $configuration
131 );
132 static::assertEquals(
133 $expected,
134 $subject->sanitize($input)
135 );
136 }
137 }