[BUGFIX] Properly escape "dropzone-target" selector in DragUploader.js
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / StringUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\StringUtility;
18
19 /**
20 * Testcase for class \TYPO3\CMS\Core\Utility\StringUtility
21 */
22 class StringUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * Data provider for endsWithReturnsTrueForMatchingFirstPart
26 *
27 * @return array
28 */
29 public function endsWithReturnsTrueForMatchingLastPartDataProvider()
30 {
31 return [
32 'match last part of string' => ['hello world', 'world'],
33 'match last char of string' => ['hellod world', 'd'],
34 'match whole string' => ['hello', 'hello'],
35 'integer is part of string with same number' => ['24', 24],
36 'string is part of integer with same number' => [24, '24'],
37 'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
38 ];
39 }
40
41 /**
42 * @test
43 * @dataProvider endsWithReturnsTrueForMatchingLastPartDataProvider
44 */
45 public function endsWithReturnsTrueForMatchingLastPart($string, $part)
46 {
47 $this->assertTrue(StringUtility::endsWith($string, $part));
48 }
49
50 /**
51 * Data provider for check endsWithReturnsFalseForNotMatchingLastPart
52 *
53 * @return array
54 */
55 public function endsWithReturnsFalseForNotMatchingLastPartDataProvider()
56 {
57 return [
58 'no string match' => ['hello', 'bye'],
59 'no case sensitive string match' => ['hello world', 'World'],
60 'string is part but not last part' => ['hello world', 'worl'],
61 'integer is not part of empty string' => ['', 0],
62 'longer string is not part of shorter string' => ['a', 'aa'],
63 ];
64 }
65
66 /**
67 * @test
68 * @dataProvider endsWithReturnsFalseForNotMatchingLastPartDataProvider
69 */
70 public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
71 {
72 $this->assertFalse(StringUtility::endsWith($string, $part));
73 }
74
75 /**
76 * Data provider for endsWithReturnsThrowsExceptionWithInvalidArguments
77 *
78 * @return array
79 */
80 public function endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
81 {
82 return [
83 'array is not part of string' => ['string', [], 1347135545],
84 'NULL is not part of string' => ['string', null, 1347135545],
85 'empty string is not part of string' => ['string', '', 1347135545],
86 'string is not part of array' => [[], 'string', 1347135544],
87 'NULL is not part of array' => [[], null, 1347135544],
88 'string is not part of NULL' => [null, 'string', 1347135544],
89 'array is not part of NULL' => [null, [], 1347135544],
90 'integer is not part of NULL' => [null, 0, 1347135544],
91 'empty string is not part of NULL' => [null, '', 1347135544],
92 'NULL is not part of empty string' => ['', null, 1347135545],
93 'FALSE is not part of empty string' => ['', false, 1347135545],
94 'empty string is not part of FALSE' => [false, '', 1347135545],
95 'empty string is not part of integer' => [0, '', 1347135545],
96 'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
97 'object is not part of string' => ['foo', new \stdClass(), 1347135545],
98 ];
99 }
100
101 /**
102 * @test
103 * @dataProvider endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
104 */
105 public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
106 {
107 $this->expectException(\InvalidArgumentException::class);
108 $this->expectExceptionCode($expectedException);
109
110 StringUtility::endsWith($string, $part);
111 }
112
113 /**
114 * Data provider for beginsWithReturnsTrueForMatchingFirstPart
115 *
116 * @return array
117 */
118 public function beginsWithReturnsTrueForMatchingFirstPartDataProvider()
119 {
120 return [
121 'match first part of string' => ['hello world', 'hello'],
122 'match first char of string' => ['hello world', 'h'],
123 'match whole string' => ['hello', 'hello'],
124 'integer is part of string with same number' => ['24', 24],
125 'string is part of integer with same number' => [24, '24'],
126 'integer is part of string starting with same number' => ['24, please gimme beer', 24],
127 ];
128 }
129
130 /**
131 * @test
132 * @dataProvider beginsWithReturnsTrueForMatchingFirstPartDataProvider
133 */
134 public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
135 {
136 $this->assertTrue(StringUtility::beginsWith($string, $part));
137 }
138
139 /**
140 * Data provider for check beginsWithReturnsFalseForNotMatchingFirstPart
141 *
142 * @return array
143 */
144 public function beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
145 {
146 return [
147 'no string match' => ['hello', 'bye'],
148 'no case sensitive string match' => ['hello world', 'Hello'],
149 'string in empty string' => ['', 'foo']
150 ];
151 }
152
153 /**
154 * @test
155 * @dataProvider beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
156 */
157 public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
158 {
159 $this->assertFalse(StringUtility::beginsWith($string, $part));
160 }
161
162 /**
163 * Data provider for beginsWithReturnsThrowsExceptionWithInvalidArguments
164 *
165 * @return array
166 */
167 public function beginsWithReturnsInvalidArgumentDataProvider()
168 {
169 return [
170 'array is not part of string' => ['string', [], 1347135547],
171 'NULL is not part of string' => ['string', null, 1347135547],
172 'empty string is not part of string' => ['string', '', 1347135547],
173 'string is not part of array' => [[], 'string', 1347135546],
174 'NULL is not part of array' => [[], null, 1347135546],
175 'string is not part of NULL' => [null, 'string', 1347135546],
176 'array is not part of NULL' => [null, [], 1347135546],
177 'integer is not part of NULL' => [null, 0, 1347135546],
178 'empty string is not part of NULL' => [null, '', 1347135546],
179 'NULL is not part of empty string' => ['', null, 1347135547],
180 'FALSE is not part of empty string' => ['', false, 1347135547],
181 'empty string is not part of FALSE' => [false, '', 1347135547],
182 'empty string is not part of integer' => [0, '', 1347135547],
183 'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
184 'object is not part of string' => ['foo', new \stdClass(), 1347135547],
185 ];
186 }
187
188 /**
189 * @test
190 * @dataProvider beginsWithReturnsInvalidArgumentDataProvider
191 *
192 * @param mixed $string
193 * @param mixed $part
194 * @param int $expectedException
195 */
196 public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
197 {
198 $this->expectException(\InvalidArgumentException::class);
199 $this->expectExceptionCode($expectedException);
200
201 StringUtility::beginsWith($string, $part);
202 }
203
204 /**
205 * @test
206 */
207 public function getUniqueIdReturnsIdWithPrefix()
208 {
209 $id = StringUtility::getUniqueId('NEW');
210 $this->assertEquals('NEW', substr($id, 0, 3));
211 }
212
213 /**
214 * @test
215 */
216 public function getUniqueIdReturnsIdWithoutDot()
217 {
218 $this->assertNotContains('.', StringUtility::getUniqueId());
219 }
220
221 /**
222 * @param string $selector
223 * @param string $expectedValue
224 * @dataProvider escapeCssSelectorDataProvider
225 */
226 public function escapeCssSelector(string $selector, string $expectedValue)
227 {
228 $this->assertEquals($expectedValue, StringUtility::escapeCssSelector($selector));
229 }
230
231 /**
232 * @return array
233 */
234 public function escapeCssSelectorDataProvider() : array
235 {
236 return [
237 ['data.field', 'data\\.field'],
238 ['#theId', '\\#theId'],
239 ['.theId:hover', '\\.theId\\:hover'],
240 ['.theId:hover', '\\.theId\\:hover'],
241 ['input[name=foo]', 'input\\[name\\=foo\\]'],
242 ];
243 }
244 }