[TASK] Throw exception when StringUtility is called with array
[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 /**
26 * Data provider for endsWithReturnsTrueForMatchingFirstPart
27 *
28 * @return array
29 */
30 public function endsWithReturnsTrueForMatchingLastPartDataProvider()
31 {
32 return [
33 'match last part of string' => ['hello world', 'world'],
34 'match last char of string' => ['hellod world', 'd'],
35 'match whole string' => ['hello', 'hello'],
36 'integer is part of string with same number' => ['24', 24],
37 'string is part of integer with same number' => [24, '24'],
38 'integer is part of string ending with same number' => ['please gimme beer, 24', 24]
39 ];
40 }
41
42 /**
43 * @test
44 * @dataProvider endsWithReturnsTrueForMatchingLastPartDataProvider
45 */
46 public function endsWithReturnsTrueForMatchingLastPart($string, $part)
47 {
48 $this->assertTrue(StringUtility::endsWith($string, $part));
49 }
50
51 /**
52 * Data provider for check endsWithReturnsFalseForNotMatchingLastPart
53 *
54 * @return array
55 */
56 public function endsWithReturnsFalseForNotMatchingLastPartDataProvider()
57 {
58 return [
59 'no string match' => ['hello', 'bye'],
60 'no case sensitive string match' => ['hello world', 'World'],
61 'string is part but not last part' => ['hello world', 'worl'],
62 'integer is not part of empty string' => ['', 0],
63 'longer string is not part of shorter string' => ['a', 'aa'],
64 ];
65 }
66
67 /**
68 * @test
69 * @dataProvider endsWithReturnsFalseForNotMatchingLastPartDataProvider
70 */
71 public function endsWithReturnsFalseForNotMatchingLastPart($string, $part)
72 {
73 $this->assertFalse(StringUtility::endsWith($string, $part));
74 }
75
76 /**
77 * Data provider for endsWithReturnsThrowsExceptionWithInvalidArguments
78 *
79 * @return array
80 */
81 public function endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider()
82 {
83 return [
84 'array is not part of string' => ['string', [], 1347135545],
85 'NULL is not part of string' => ['string', null, 1347135545],
86 'empty string is not part of string' => ['string', '', 1347135545],
87 'string is not part of array' => [[], 'string', 1347135544],
88 'NULL is not part of array' => [[], null, 1347135544],
89 'string is not part of NULL' => [null, 'string', 1347135544],
90 'array is not part of NULL' => [null, [], 1347135544],
91 'integer is not part of NULL' => [null, 0, 1347135544],
92 'empty string is not part of NULL' => [null, '', 1347135544],
93 'NULL is not part of empty string' => ['', null, 1347135545],
94 'FALSE is not part of empty string' => ['', false, 1347135545],
95 'empty string is not part of FALSE' => [false, '', 1347135545],
96 'empty string is not part of integer' => [0, '', 1347135545],
97 'string is not part of object' => [new \stdClass(), 'foo', 1347135544],
98 'object is not part of string' => ['foo', new \stdClass(), 1347135545],
99 ];
100 }
101
102 /**
103 * @test
104 * @dataProvider endsWithReturnsThrowsExceptionWithInvalidArgumentsDataProvider
105 */
106 public function endsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
107 {
108 $this->expectException(\InvalidArgumentException::class);
109 $this->expectExceptionCode($expectedException);
110
111 StringUtility::endsWith($string, $part);
112 }
113
114 /**
115 * Data provider for beginsWithReturnsTrueForMatchingFirstPart
116 *
117 * @return array
118 */
119 public function beginsWithReturnsTrueForMatchingFirstPartDataProvider()
120 {
121 return [
122 'match first part of string' => ['hello world', 'hello'],
123 'match first char of string' => ['hello world', 'h'],
124 'match whole string' => ['hello', 'hello'],
125 'integer is part of string with same number' => ['24', 24],
126 'string is part of integer with same number' => [24, '24'],
127 'integer is part of string starting with same number' => ['24, please gimme beer', 24],
128 ];
129 }
130
131 /**
132 * @test
133 * @dataProvider beginsWithReturnsTrueForMatchingFirstPartDataProvider
134 */
135 public function beginsWithReturnsTrueForMatchingFirstPart($string, $part)
136 {
137 $this->assertTrue(StringUtility::beginsWith($string, $part));
138 }
139
140 /**
141 * Data provider for check beginsWithReturnsFalseForNotMatchingFirstPart
142 *
143 * @return array
144 */
145 public function beginsWithReturnsFalseForNotMatchingFirstPartDataProvider()
146 {
147 return [
148 'no string match' => ['hello', 'bye'],
149 'no case sensitive string match' => ['hello world', 'Hello'],
150 'string in empty string' => ['', 'foo']
151 ];
152 }
153
154 /**
155 * @test
156 * @dataProvider beginsWithReturnsFalseForNotMatchingFirstPartDataProvider
157 */
158 public function beginsWithReturnsFalseForNotMatchingFirstPart($string, $part)
159 {
160 $this->assertFalse(StringUtility::beginsWith($string, $part));
161 }
162
163 /**
164 * Data provider for beginsWithReturnsThrowsExceptionWithInvalidArguments
165 *
166 * @return array
167 */
168 public function beginsWithReturnsInvalidArgumentDataProvider()
169 {
170 return [
171 'array is not part of string' => ['string', [], 1347135547],
172 'NULL is not part of string' => ['string', null, 1347135547],
173 'empty string is not part of string' => ['string', '', 1347135547],
174 'string is not part of array' => [[], 'string', 1347135546],
175 'NULL is not part of array' => [[], null, 1347135546],
176 'string is not part of NULL' => [null, 'string', 1347135546],
177 'array is not part of NULL' => [null, [], 1347135546],
178 'integer is not part of NULL' => [null, 0, 1347135546],
179 'empty string is not part of NULL' => [null, '', 1347135546],
180 'NULL is not part of empty string' => ['', null, 1347135547],
181 'FALSE is not part of empty string' => ['', false, 1347135547],
182 'empty string is not part of FALSE' => [false, '', 1347135547],
183 'empty string is not part of integer' => [0, '', 1347135547],
184 'string is not part of object' => [new \stdClass(), 'foo', 1347135546],
185 'object is not part of string' => ['foo', new \stdClass(), 1347135547],
186 ];
187 }
188
189 /**
190 * @test
191 * @dataProvider beginsWithReturnsInvalidArgumentDataProvider
192 *
193 * @param mixed $string
194 * @param mixed $part
195 * @param int $expectedException
196 */
197 public function beginsWithReturnsThrowsExceptionWithInvalidArguments($string, $part, $expectedException)
198 {
199 $this->expectException(\InvalidArgumentException::class);
200 $this->expectExceptionCode($expectedException);
201
202 StringUtility::beginsWith($string, $part);
203 }
204
205 /**
206 * @test
207 */
208 public function getUniqueIdReturnsIdWithPrefix()
209 {
210 $id = StringUtility::getUniqueId('NEW');
211 $this->assertEquals('NEW', substr($id, 0, 3));
212 }
213
214 /**
215 * @test
216 */
217 public function getUniqueIdReturnsIdWithoutDot()
218 {
219 $this->assertNotContains('.', StringUtility::getUniqueId());
220 }
221
222 /**
223 * @test
224 * @param string $selector
225 * @param string $expectedValue
226 * @dataProvider escapeCssSelectorDataProvider
227 */
228 public function escapeCssSelector(string $selector, string $expectedValue)
229 {
230 $this->assertEquals($expectedValue, StringUtility::escapeCssSelector($selector));
231 }
232
233 /**
234 * @return array
235 */
236 public function escapeCssSelectorDataProvider() : array
237 {
238 return [
239 ['data.field', 'data\\.field'],
240 ['#theId', '\\#theId'],
241 ['.theId:hover', '\\.theId\\:hover'],
242 ['.theId:hover', '\\.theId\\:hover'],
243 ['input[name=foo]', 'input\\[name\\=foo\\]'],
244 ];
245 }
246 }