Follow-up to bugfix #9779: Modified the limit argument in t3lib_div::trimExplode...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_div_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_div
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_div_testcase extends tx_phpunit_testcase {
34
35 /**
36 * @test
37 */
38 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
39 $testString = ' a , b , c ,d ,, e,f,';
40 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
41 $actualArray = t3lib_div::trimExplode(',', $testString);
42
43 $this->assertEquals($expectedArray, $actualArray);
44 }
45
46 /**
47 * @test
48 */
49 public function checkTrimExplodeRemovesNewLines() {
50 $testString = ' a , b , ' . chr(10) . ' ,d ,, e,f,';
51 $expectedArray = array('a', 'b', 'd', 'e', 'f');
52 $actualArray = t3lib_div::trimExplode(',', $testString, true);
53
54 $this->assertEquals($expectedArray, $actualArray);
55 }
56
57 /**
58 * @test
59 */
60 public function checkTrimExplodeRemovesEmptyElements() {
61 $testString = 'a , b , c , ,d ,, ,e,f,';
62 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
63 $actualArray = t3lib_div::trimExplode(',', $testString, true);
64
65 $this->assertEquals($expectedArray, $actualArray);
66 }
67
68 /**
69 * @test
70 */
71 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
72 $testString = ' a , b , c , , d,, ,e ';
73 $expectedArray = array('a', 'b', 'c,,d,,,e'); // limiting returns the rest of the string as the last element
74 $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
75
76 $this->assertEquals($expectedArray, $actualArray);
77 }
78
79 /**
80 * @test
81 */
82 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
83 $testString = ' a , b , c , , d,, ,e ';
84 $expectedArray = array('a', 'b', 'c,d,e'); // limiting returns the rest of the string as the last element
85 $actualArray = t3lib_div::trimExplode(',', $testString, true, 3);
86
87 $this->assertEquals($expectedArray, $actualArray);
88 }
89
90 /**
91 * @test
92 */
93 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
94 $testString = ' a , b , c , d, ,e, f , , ';
95 $expectedArray = array('a', 'b', 'c', 'd', '', 'e'); // limiting returns the rest of the string as the last element
96 $actualArray = t3lib_div::trimExplode(',', $testString, false, -3);
97
98 $this->assertEquals($expectedArray, $actualArray);
99 }
100
101 /**
102 * @test
103 */
104 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
105 $testString = ' a , b , c , d, ,e, f , , ';
106 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
107 $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
108
109 $this->assertEquals($expectedArray, $actualArray);
110 }
111
112 /**
113 * @test
114 */
115 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
116 $testString = ' a , b , , c , , , ';
117 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
118 $actualArray = t3lib_div::trimExplode(',', $testString, true, 4);
119
120 $this->assertEquals($expectedArray, $actualArray);
121 }
122
123 /**
124 * @test
125 */
126 public function checkTrimExplodeKeepsZeroAsString() {
127 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
128 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
129 $actualArray = t3lib_div::trimExplode(',', $testString, true);
130
131 $this->assertEquals($expectedArray, $actualArray);
132 }
133
134 /**
135 * Checks whether measurement strings like "100k" return the accordant
136 * byte representation like 102400 in this case.
137 *
138 * @test
139 */
140 public function checkGetBytesFromSizeMeasurement() {
141 $this->assertEquals(
142 '102400',
143 t3lib_div::getBytesFromSizeMeasurement('100k')
144 );
145
146 $this->assertEquals(
147 '104857600',
148 t3lib_div::getBytesFromSizeMeasurement('100m')
149 );
150
151 $this->assertEquals(
152 '107374182400',
153 t3lib_div::getBytesFromSizeMeasurement('100g')
154 );
155 }
156
157 /**
158 * @test
159 */
160 public function checkIndpEnvTypo3SitePathNotEmpty() {
161 $actualEnv = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
162 $this->assertTrue(strlen($actualEnv) >= 1);
163 $this->assertEquals('/', $actualEnv{0});
164 $this->assertEquals('/', $actualEnv{strlen($actualEnv) - 1});
165 }
166
167 /**
168 * @test
169 * @see t3lib_div::underscoredToUpperCamelCase
170 */
171 public function canConvertFromUnderscoredToUpperCamelCase() {
172 $this->assertEquals('BlogExample', t3lib_div::underscoredToUpperCamelCase('blog_example'));
173 $this->assertEquals('Blogexample', t3lib_div::underscoredToUpperCamelCase('blogexample'));
174 }
175
176 /**
177 * @test
178 * @see t3lib_div::underscoredToLowerCamelCase
179 */
180 public function canConvertFromUnderscoredToLowerCamelCase() {
181 $this->assertEquals('minimalValue', t3lib_div::underscoredToLowerCamelCase('minimal_value'));
182 $this->assertEquals('minimalvalue', t3lib_div::underscoredToLowerCamelCase('minimalvalue'));
183 }
184
185 /**
186 * @test
187 * @see t3lib_div::camelCaseToLowerCaseUnderscored
188 */
189 public function canConvertFromCamelCaseToLowerCaseUnderscored() {
190 $this->assertEquals('blog_example', t3lib_div::camelCaseToLowerCaseUnderscored('BlogExample'));
191 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('Blogexample'));
192 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('blogexample'));
193
194 $this->assertEquals('minimal_value', t3lib_div::camelCaseToLowerCaseUnderscored('minimalValue'));
195 }
196
197 /**
198 * @test
199 * @see t3lib_div::lcfirst
200 */
201 public function canConvertFirstCharacterToBeLowerCase() {
202 $this->assertEquals('blogexample', t3lib_div::lcfirst('Blogexample'));
203 $this->assertEquals('blogExample', t3lib_div::lcfirst('BlogExample'));
204 $this->assertEquals('blogexample', t3lib_div::lcfirst('blogexample'));
205 }
206
207 /**
208 * Tests whether whitespaces are encoded correctly in a quoted-printable mail header.
209 * @test
210 */
211 public function areWhitespacesEncodedInQuotedPrintableMailHeader() {
212 $this->assertEquals(
213 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
214 t3lib_div::encodeHeader(
215 "We test whether the copyright character \xc2\xa9 is encoded correctly",
216 'quoted-printable',
217 'utf-8'
218 )
219 );
220 }
221
222 /**
223 * Tests whether question marks are encoded correctly in a quoted-printable mail header.
224 * @test
225 */
226 public function areQuestionMarksEncodedInQuotedPrintableMailHeader() {
227 $this->assertEquals(
228 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
229 t3lib_div::encodeHeader(
230 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
231 'quoted-printable',
232 'utf-8'
233 )
234 );
235 }
236
237 /**
238 * @test
239 * @see t3lib_div::isValidUrl()
240 */
241 public function checkisValidURLSucceedsWithWebRessource() {
242 $testUrl = 'http://www.example.org/';
243 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
244 }
245
246 /**
247 * @test
248 * @see t3lib_div::isValidUrl()
249 */
250 public function checkisValidURLSucceedsWithExtentedWebRessource() {
251 $testUrl = 'https://user:pw@www.example.org:80/path?arg=value#fragment';
252 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
253 }
254
255 /**
256 * @test
257 * @see t3lib_div::isValidUrl()
258 */
259 public function checkisValidURLSucceedsWithTelnetRessource() {
260 $testUrl = 'telnet://192.0.2.16:80/';
261 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
262 }
263
264 /**
265 * @test
266 */
267 public function checkisValidURLSucceedsWithLdapRessource() {
268 $testUrl = 'ldap://[2001:db8::7]/c=GB?objectClass?one';
269 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
270 }
271
272 /**
273 * @test
274 * @see t3lib_div::isValidUrl()
275 */
276 public function checkisValidURLSucceedsWithFileRessource() {
277 $testUrl = 'file:///etc/passwd';
278 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
279 }
280
281 /**
282 * @test
283 * @see t3lib_div::isValidUrl()
284 */
285 public function checkisValidURLFailsWithHostnameOnly() {
286 $testUrl = 'www.example.org/';
287 $this->assertFalse(t3lib_div::isValidUrl($testUrl));
288 }
289
290 /**
291 * @test
292 * @see t3lib_div::isOnCurrentHost()
293 */
294 public function checkisOnCurrentHostFailsWithLocalhostIPOnly() {
295 $testUrl = '127.0.0.1';
296 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
297 }
298
299 /**
300 * @test
301 * @see t3lib_div::isOnCurrentHost()
302 */
303 public function checkisOnCurrentHostFailsWithPathsOnly() {
304 $testUrl = './relpath/file.txt';
305 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
306 $testUrl = '/abspath/file.txt?arg=value';
307 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
308 }
309
310 /**
311 * @test
312 * @see t3lib_div::isOnCurrentHost()
313 */
314 public function checkisOnCurrentHostFailsWithArbitraryString() {
315 $testUrl = 'arbitrary string';
316 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
317 }
318
319 /**
320 * @test
321 * @see t3lib_div::isOnCurrentHost()
322 */
323 public function checkisOnCurrentHostFailsWithEmptyUrl() {
324 $testUrl = '';
325 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
326 }
327
328 /**
329 * @test
330 * @see t3lib_div::isOnCurrentHost()
331 */
332 public function checkisOnCurrentHostFailsWithDifferentHost() {
333 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org';
334 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
335 }
336
337 /**
338 * @test
339 * @see t3lib_div::isOnCurrentHost()
340 */
341 public function checkisOnCurrentHostSucceedsWithCurrentHost() {
342 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
343 $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
344 }
345
346 /**
347 * Tests whether removeDotsFromTS() behaves correctly.
348 * @test
349 * @see t3lib_div::removeDotsFromTS()
350 */
351 public function doesRemoveDotsFromTypoScriptSucceed() {
352 $typoScript = array(
353 'propertyA.' => array(
354 'keyA.' => array(
355 'valueA' => 1,
356 ),
357 'keyB' => 2,
358 ),
359 'propertyB' => 3,
360 );
361
362 $expectedResult = array(
363 'propertyA' => array(
364 'keyA' => array(
365 'valueA' => 1,
366 ),
367 'keyB' => 2,
368 ),
369 'propertyB' => 3,
370 );
371
372 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
373 }
374
375 /**
376 * Tests whether removeDotsFromTS() behaves correctly.
377 * @test
378 * @see t3lib_div::removeDotsFromTS()
379 */
380 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
381 $typoScript = array(
382 'propertyA.' => array(
383 'keyA' => 'getsOverridden',
384 'keyA.' => array(
385 'valueA' => 1,
386 ),
387 'keyB' => 2,
388 ),
389 'propertyB' => 3,
390 );
391
392 $expectedResult = array(
393 'propertyA' => array(
394 'keyA' => array(
395 'valueA' => 1,
396 ),
397 'keyB' => 2,
398 ),
399 'propertyB' => 3,
400 );
401
402 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
403 }
404
405 /**
406 * Tests whether removeDotsFromTS() behaves correctly.
407 * @test
408 * @see t3lib_div::removeDotsFromTS()
409 */
410 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
411 $typoScript = array(
412 'propertyA.' => array(
413 'keyA.' => array(
414 'valueA' => 1,
415 ),
416 'keyA' => 'willOverride',
417 'keyB' => 2,
418 ),
419 'propertyB' => 3,
420 );
421
422 $expectedResult = array(
423 'propertyA' => array(
424 'keyA' => 'willOverride',
425 'keyB' => 2,
426 ),
427 'propertyB' => 3,
428 );
429
430 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
431 }
432
433 /**
434 * Tests whether getDirs() returns an array of diretories from a given path
435 * @test
436 * @see t3lib_div::getDirs($path)
437 */
438 public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
439 $path = PATH_t3lib;
440 $directories = t3lib_div::get_dirs($path);
441
442 $this->assertType('array', $directories);
443 }
444
445 /**
446 * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
447 * @test
448 * @see t3lib_div::getDirs($path)
449 */
450 public function checkGetDirsReturnsStringErrorOnPathFailure() {
451 $path = 'foo';
452 $result = t3lib_div::get_dirs($path);
453 $expectedResult = 'error';
454
455 $this->assertEquals($expectedResult, $result);
456 }
457 }
458
459 ?>