ab6e078997fc6eb1975570d5372741febca31166
[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 * @author Oliver Klee <typo3-coding@oliverklee.de>
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35 class t3lib_div_testcase extends tx_phpunit_testcase {
36 /**
37 * backup of the global variables _GET, _POST, _SERVER
38 *
39 * @var array
40 */
41 private $backupGlobalVariables;
42
43 public function setUp() {
44 $this->backupGlobalVariables = array(
45 '_GET' => $_GET,
46 '_POST' => $_POST,
47 '_SERVER' => $_SERVER,
48 'TYPO3_CONF_VARS' => $GLOBALS['TYPO3_CONF_VARS'],
49 );
50 }
51
52 public function tearDown() {
53 foreach ($this->backupGlobalVariables as $key => $data) {
54 $GLOBALS[$key] = $data;
55 }
56 }
57
58
59 /**
60 * @test
61 */
62 public function checkIntExplodeConvertsStringsToInteger() {
63 $testString = '1,foo,2';
64 $expectedArray = array(1, 0, 2);
65 $actualArray = t3lib_div::intExplode(',', $testString);
66
67 $this->assertEquals($expectedArray, $actualArray);
68 }
69
70 /**
71 * @test
72 */
73 public function checkRevExplodeCorrectlyExplodesString() {
74 $testString = 'my:words:here';
75 $expectedArray = array('my:words', 'here');
76 $actualArray = t3lib_div::revExplode(':', $testString, 2);
77
78 $this->assertEquals($expectedArray, $actualArray);
79 }
80
81 /**
82 * @test
83 */
84 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
85 $testString = ' a , b , c ,d ,, e,f,';
86 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
87 $actualArray = t3lib_div::trimExplode(',', $testString);
88
89 $this->assertEquals($expectedArray, $actualArray);
90 }
91
92 /**
93 * @test
94 */
95 public function checkTrimExplodeRemovesNewLines() {
96 $testString = ' a , b , ' . LF . ' ,d ,, e,f,';
97 $expectedArray = array('a', 'b', 'd', 'e', 'f');
98 $actualArray = t3lib_div::trimExplode(',', $testString, true);
99
100 $this->assertEquals($expectedArray, $actualArray);
101 }
102
103 /**
104 * @test
105 */
106 public function checkTrimExplodeRemovesEmptyElements() {
107 $testString = 'a , b , c , ,d ,, ,e,f,';
108 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
109 $actualArray = t3lib_div::trimExplode(',', $testString, true);
110
111 $this->assertEquals($expectedArray, $actualArray);
112 }
113
114 /**
115 * @test
116 */
117 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
118 $testString = ' a , b , c , , d,, ,e ';
119 $expectedArray = array('a', 'b', 'c,,d,,,e'); // limiting returns the rest of the string as the last element
120 $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
121
122 $this->assertEquals($expectedArray, $actualArray);
123 }
124
125 /**
126 * @test
127 */
128 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
129 $testString = ' a , b , c , , d,, ,e ';
130 $expectedArray = array('a', 'b', 'c,d,e'); // limiting returns the rest of the string as the last element
131 $actualArray = t3lib_div::trimExplode(',', $testString, true, 3);
132
133 $this->assertEquals($expectedArray, $actualArray);
134 }
135
136 /**
137 * @test
138 */
139 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
140 $testString = ' a , b , c , d, ,e, f , , ';
141 $expectedArray = array('a', 'b', 'c', 'd', '', 'e'); // limiting returns the rest of the string as the last element
142 $actualArray = t3lib_div::trimExplode(',', $testString, false, -3);
143
144 $this->assertEquals($expectedArray, $actualArray);
145 }
146
147 /**
148 * @test
149 */
150 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
151 $testString = ' a , b , c , d, ,e, f , , ';
152 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
153 $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
154
155 $this->assertEquals($expectedArray, $actualArray);
156 }
157
158 /**
159 * @test
160 */
161 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
162 $testString = ' a , b , , c , , , ';
163 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
164 $actualArray = t3lib_div::trimExplode(',', $testString, true, 4);
165
166 $this->assertEquals($expectedArray, $actualArray);
167 }
168
169 /**
170 * @test
171 */
172 public function checkTrimExplodeKeepsZeroAsString() {
173 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
174 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
175 $actualArray = t3lib_div::trimExplode(',', $testString, true);
176
177 $this->assertEquals($expectedArray, $actualArray);
178 }
179
180 /**
181 * Checks whether measurement strings like "100k" return the accordant
182 * byte representation like 102400 in this case.
183 *
184 * @test
185 */
186 public function checkGetBytesFromSizeMeasurement() {
187 $this->assertEquals(
188 '102400',
189 t3lib_div::getBytesFromSizeMeasurement('100k')
190 );
191
192 $this->assertEquals(
193 '104857600',
194 t3lib_div::getBytesFromSizeMeasurement('100m')
195 );
196
197 $this->assertEquals(
198 '107374182400',
199 t3lib_div::getBytesFromSizeMeasurement('100g')
200 );
201 }
202
203 /**
204 * @test
205 */
206 public function checkIndpEnvTypo3SitePathNotEmpty() {
207 $actualEnv = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
208 $this->assertTrue(strlen($actualEnv) >= 1);
209 $this->assertEquals('/', $actualEnv{0});
210 $this->assertEquals('/', $actualEnv{strlen($actualEnv) - 1});
211 }
212
213 /**
214 * @test
215 * @see t3lib_div::underscoredToUpperCamelCase
216 */
217 public function canConvertFromUnderscoredToUpperCamelCase() {
218 $this->assertEquals('BlogExample', t3lib_div::underscoredToUpperCamelCase('blog_example'));
219 $this->assertEquals('Blogexample', t3lib_div::underscoredToUpperCamelCase('blogexample'));
220 }
221
222 /**
223 * @test
224 * @see t3lib_div::underscoredToLowerCamelCase
225 */
226 public function canConvertFromUnderscoredToLowerCamelCase() {
227 $this->assertEquals('minimalValue', t3lib_div::underscoredToLowerCamelCase('minimal_value'));
228 $this->assertEquals('minimalvalue', t3lib_div::underscoredToLowerCamelCase('minimalvalue'));
229 }
230
231 /**
232 * @test
233 * @see t3lib_div::camelCaseToLowerCaseUnderscored
234 */
235 public function canConvertFromCamelCaseToLowerCaseUnderscored() {
236 $this->assertEquals('blog_example', t3lib_div::camelCaseToLowerCaseUnderscored('BlogExample'));
237 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('Blogexample'));
238 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('blogexample'));
239
240 $this->assertEquals('minimal_value', t3lib_div::camelCaseToLowerCaseUnderscored('minimalValue'));
241 }
242
243 /**
244 * @test
245 * @see t3lib_div::lcfirst
246 */
247 public function canConvertFirstCharacterToBeLowerCase() {
248 $this->assertEquals('blogexample', t3lib_div::lcfirst('Blogexample'));
249 $this->assertEquals('blogExample', t3lib_div::lcfirst('BlogExample'));
250 $this->assertEquals('blogexample', t3lib_div::lcfirst('blogexample'));
251 }
252
253 /**
254 * Tests whether whitespaces are encoded correctly in a quoted-printable mail header.
255 * @test
256 */
257 public function areWhitespacesEncodedInQuotedPrintableMailHeader() {
258 $this->assertEquals(
259 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
260 t3lib_div::encodeHeader(
261 "We test whether the copyright character \xc2\xa9 is encoded correctly",
262 'quoted-printable',
263 'utf-8'
264 )
265 );
266 }
267
268 /**
269 * Tests whether question marks are encoded correctly in a quoted-printable mail header.
270 * @test
271 */
272 public function areQuestionMarksEncodedInQuotedPrintableMailHeader() {
273 $this->assertEquals(
274 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
275 t3lib_div::encodeHeader(
276 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
277 'quoted-printable',
278 'utf-8'
279 )
280 );
281 }
282
283 /**
284 * Data provider for valid URLs, like PHP's source code test cases
285 */
286 public function validUrlDataProvider() {
287 return array(
288 array('http://example.com/index.html'),
289 array('http://www.example.com/index.php'),
290 array('http://www.example/img/test.png'),
291 array('http://www.example/img/dir/'),
292 array('http://www.example/img/dir'),
293 array('file:///tmp/test.c'),
294 array('ftp://ftp.example.com/tmp/'),
295 array('mailto:foo@bar.com'),
296 array('news:news.php.net'),
297 array('file://foo/bar'),
298 array('http://qwe'),
299 );
300 }
301
302 /**
303 * Data provider for invalid URLs, like PHP's source code test cases
304 */
305 public function invalidUrlDataProvider() {
306 return array(
307 array('http//www.example/wrong/url/'),
308 array('http:/www.example'),
309 array('/tmp/test.c'),
310 array('/'),
311 array('http://'),
312 array('http:/'),
313 array('http:'),
314 array('http'),
315 array(''),
316 array('-1'),
317 array('array()'),
318 array('qwe'),
319 );
320 }
321
322 /**
323 * @test
324 * @dataProvider validUrlDataProvider
325 * @see t3lib_div::isValidUrl()
326 */
327 public function checkisValidURL($url) {
328 $this->assertTrue(t3lib_div::isValidUrl($url));
329 }
330
331 /**
332 * @test
333 * @dataProvider invalidUrlDataProvider
334 * @see t3lib_div::isValidUrl()
335 */
336 public function checkisInValidURL($url) {
337 $this->assertFalse(t3lib_div::isValidUrl($url));
338 }
339
340 /**
341 * @test
342 * @see t3lib_div::isValidUrl()
343 */
344 public function checkisValidURLSucceedsWithWebRessource() {
345 $testUrl = 'http://www.example.org/';
346 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
347 }
348
349 /**
350 * @test
351 * @see t3lib_div::isValidUrl()
352 */
353 public function checkisValidURLSucceedsWithExtentedWebRessource() {
354 $testUrl = 'https://user:pw@www.example.org:80/path?arg=value#fragment';
355 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
356 }
357
358 /**
359 * @test
360 * @see t3lib_div::isValidUrl()
361 */
362 public function checkisValidURLSucceedsWithTelnetRessource() {
363 $testUrl = 'telnet://192.0.2.16:80/';
364 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
365 }
366
367 /**
368 * @test
369 */
370 public function checkisValidURLSucceedsWithLdapRessource() {
371 $testUrl = 'ldap://[2001:db8::7]/c=GB?objectClass?one';
372 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
373 }
374
375 /**
376 * @test
377 * @see t3lib_div::isValidUrl()
378 */
379 public function checkisValidURLSucceedsWithFileRessource() {
380 $testUrl = 'file:///etc/passwd';
381 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
382 }
383
384 /**
385 * @test
386 * @see t3lib_div::isValidUrl()
387 */
388 public function checkisValidURLFailsWithHostnameOnly() {
389 $testUrl = 'www.example.org/';
390 $this->assertFalse(t3lib_div::isValidUrl($testUrl));
391 }
392
393 /**
394 * @test
395 * @see t3lib_div::isOnCurrentHost()
396 */
397 public function checkisOnCurrentHostFailsWithLocalhostIPOnly() {
398 $testUrl = '127.0.0.1';
399 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
400 }
401
402 /**
403 * @test
404 * @see t3lib_div::isOnCurrentHost()
405 */
406 public function checkisOnCurrentHostFailsWithPathsOnly() {
407 $testUrl = './relpath/file.txt';
408 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
409 $testUrl = '/abspath/file.txt?arg=value';
410 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
411 }
412
413 /**
414 * @test
415 * @see t3lib_div::isOnCurrentHost()
416 */
417 public function checkisOnCurrentHostFailsWithArbitraryString() {
418 $testUrl = 'arbitrary string';
419 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
420 }
421
422 /**
423 * @test
424 * @see t3lib_div::isOnCurrentHost()
425 */
426 public function checkisOnCurrentHostFailsWithEmptyUrl() {
427 $testUrl = '';
428 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
429 }
430
431 /**
432 * @test
433 * @see t3lib_div::isOnCurrentHost()
434 */
435 public function checkisOnCurrentHostFailsWithDifferentHost() {
436 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org';
437 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
438 }
439
440 /**
441 * @test
442 * @see t3lib_div::isOnCurrentHost()
443 */
444 public function checkisOnCurrentHostSucceedsWithCurrentHost() {
445 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
446 $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
447 }
448
449
450 ////////////////////////////////////////
451 // Tests concerning sanitizeLocalUrl
452 ////////////////////////////////////////
453
454 /**
455 * Data provider for valid URLs.
456 * @see sanitizeLocalUrlAcceptsValidUrls
457 */
458 public function validLocalUrlDataProvider() {
459 return array(
460 array('alt_intro.php'),
461 array('alt_intro.php?foo=1&bar=2'),
462 array('/typo3/alt_intro.php'),
463 array('/index.php'),
464 array('../index.php'),
465 array('../typo3/alt_intro.php'),
466 array('../~userDirectory/index.php'),
467 array('../typo3/mod.php?var1=test-case&var2=~user'),
468 array(PATH_site . 'typo3/alt_intro.php'),
469 array(t3lib_div::getIndpEnv('TYPO3_SITE_URL') . 'typo3/alt_intro.php'),
470 array(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '/index.php'),
471 );
472 }
473
474 /**
475 * Data provider for invalid URLs.
476 * @see sanitizeLocalUrlDeniesInvalidUrls
477 */
478 public function invalidLocalUrlDataProvider() {
479 return array(
480 array(''),
481 array('http://www.google.de/'),
482 array('https://www.google.de/'),
483 array('../typo3/whatever.php?argument=javascript:alert(0)'),
484 );
485 }
486
487 /**
488 * Tests whether valid local URLs are handled correctly.
489 * @dataProvider validLocalUrlDataProvider
490 * @test
491 */
492 public function sanitizeLocalUrlAcceptsPlainValidUrls($url) {
493 $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
494 }
495
496 /**
497 * Tests whether valid local URLs are handled correctly.
498 * @dataProvider validLocalUrlDataProvider
499 * @test
500 */
501 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
502 $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
503 }
504
505 /**
506 * Tests whether valid local URLs are handled correctly.
507 * @dataProvider invalidLocalUrlDataProvider
508 * @test
509 */
510 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
511 $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
512 }
513
514 /**
515 * Tests whether valid local URLs are handled correctly.
516 * @dataProvider invalidLocalUrlDataProvider
517 * @test
518 */
519 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
520 $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
521 }
522
523 //////////////////////////////////////
524 // Tests concerning arrayDiffAssocRecursive
525 //////////////////////////////////////
526
527 /**
528 * Test if a one dimensional array is correctly diffed.
529 *
530 * @test
531 * @see t3lib_div::arrayDiffAssocRecursive
532 */
533 public function doesArrayDiffAssocRecursiveCorrectlyHandleOneDimensionalArrays() {
534 $array1 = array(
535 'key1' => 'value1',
536 'key2' => 'value2',
537 'key3' => 'value3',
538 );
539 $array2 = array(
540 'key1' => 'value1',
541 'key3' => 'value3',
542 );
543 $expectedResult = array(
544 'key2' => 'value2',
545 );
546 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
547 $this->assertEquals($expectedResult, $actualResult);
548 }
549
550 /**
551 * Test if a three dimensional array is correctly diffed.
552 *
553 * @test
554 * @see t3lib_div::arrayDiffAssocRecursive
555 */
556 public function doesArrayDiffAssocRecursiveCorrectlyHandleMultiDimensionalArrays() {
557 $array1 = array(
558 'key1' => 'value1',
559 'key2' => array(
560 'key21' => 'value21',
561 'key22' => 'value22',
562 'key23' => array(
563 'key231' => 'value231',
564 'key232' => 'value232',
565 ),
566 ),
567 );
568 $array2 = array(
569 'key1' => 'value1',
570 'key2' => array(
571 'key21' => 'value21',
572 'key23' => array(
573 'key231' => 'value231',
574 ),
575 ),
576 );
577 $expectedResult = array(
578 'key2' => array(
579 'key22' => 'value22',
580 'key23' => array(
581 'key232' => 'value232',
582 ),
583 ),
584 );
585 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
586 $this->assertEquals($expectedResult, $actualResult);
587 }
588
589 /**
590 * Test if arrays are correctly diffed if types are different.
591 *
592 * @test
593 * @see t3lib_div::arrayDiffAssocRecursive
594 */
595 public function doesArrayDiffAssocRecursiveCorrectlyHandleMixedArrays() {
596 $array1 = array(
597 'key1' => array(
598 'key11' => 'value11',
599 'key12' => 'value12',
600 ),
601 'key2' => 'value2',
602 'key3' => 'value3',
603 );
604 $array2 = array(
605 'key1' => 'value1',
606 'key2' => array(
607 'key21' => 'value21',
608 ),
609 );
610 $expectedResult = array(
611 'key3' => 'value3',
612 );
613 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
614 $this->assertEquals($expectedResult, $actualResult);
615 }
616
617 //////////////////////////////////////
618 // Tests concerning removeDotsFromTS
619 //////////////////////////////////////
620
621 /**
622 * Tests whether removeDotsFromTS() behaves correctly.
623 * @test
624 * @see t3lib_div::removeDotsFromTS()
625 */
626 public function doesRemoveDotsFromTypoScriptSucceed() {
627 $typoScript = array(
628 'propertyA.' => array(
629 'keyA.' => array(
630 'valueA' => 1,
631 ),
632 'keyB' => 2,
633 ),
634 'propertyB' => 3,
635 );
636
637 $expectedResult = array(
638 'propertyA' => array(
639 'keyA' => array(
640 'valueA' => 1,
641 ),
642 'keyB' => 2,
643 ),
644 'propertyB' => 3,
645 );
646
647 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
648 }
649
650 /**
651 * Tests whether removeDotsFromTS() behaves correctly.
652 * @test
653 * @see t3lib_div::removeDotsFromTS()
654 */
655 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
656 $typoScript = array(
657 'propertyA.' => array(
658 'keyA' => 'getsOverridden',
659 'keyA.' => array(
660 'valueA' => 1,
661 ),
662 'keyB' => 2,
663 ),
664 'propertyB' => 3,
665 );
666
667 $expectedResult = array(
668 'propertyA' => array(
669 'keyA' => array(
670 'valueA' => 1,
671 ),
672 'keyB' => 2,
673 ),
674 'propertyB' => 3,
675 );
676
677 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
678 }
679
680 /**
681 * Tests whether removeDotsFromTS() behaves correctly.
682 * @test
683 * @see t3lib_div::removeDotsFromTS()
684 */
685 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
686 $typoScript = array(
687 'propertyA.' => array(
688 'keyA.' => array(
689 'valueA' => 1,
690 ),
691 'keyA' => 'willOverride',
692 'keyB' => 2,
693 ),
694 'propertyB' => 3,
695 );
696
697 $expectedResult = array(
698 'propertyA' => array(
699 'keyA' => 'willOverride',
700 'keyB' => 2,
701 ),
702 'propertyB' => 3,
703 );
704
705 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
706 }
707
708 /**
709 * Tests whether getDirs() returns an array of diretories from a given path
710 * @test
711 * @see t3lib_div::getDirs($path)
712 */
713 public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
714 $path = PATH_t3lib;
715 $directories = t3lib_div::get_dirs($path);
716
717 $this->assertType('array', $directories);
718 }
719
720 /**
721 * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
722 * @test
723 * @see t3lib_div::getDirs($path)
724 */
725 public function checkGetDirsReturnsStringErrorOnPathFailure() {
726 $path = 'foo';
727 $result = t3lib_div::get_dirs($path);
728 $expectedResult = 'error';
729
730 $this->assertEquals($expectedResult, $result);
731 }
732
733
734 //////////////////////////////////
735 // Tests concerning quoteJSvalue
736 //////////////////////////////////
737
738 /**
739 * @test
740 */
741 public function quoteJSvalueHtmlspecialcharsDataByDefault() {
742 $this->assertContains(
743 '&gt;',
744 t3lib_div::quoteJSvalue('>')
745 );
746 }
747
748 /**
749 * @test
750 */
751 public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
752 $this->assertContains(
753 '&gt;',
754 t3lib_div::quoteJSvalue('>', false)
755 );
756 }
757
758 /**
759 * @test
760 */
761 public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
762 $this->assertContains(
763 '>',
764 t3lib_div::quoteJSvalue('>', true)
765 );
766 }
767
768 /**
769 * @test
770 */
771 public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
772 $this->assertEquals(
773 "''",
774 t3lib_div::quoteJSvalue("", true)
775 );
776 }
777
778 /**
779 * @test
780 */
781 public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
782 $this->assertEquals(
783 "'Hello world!'",
784 t3lib_div::quoteJSvalue("Hello world!", true)
785 );
786 }
787
788 /**
789 * @test
790 */
791 public function quoteJSvalueEscapesSingleQuote() {
792 $this->assertEquals(
793 "'\\''",
794 t3lib_div::quoteJSvalue("'", true)
795 );
796 }
797
798 /**
799 * @test
800 */
801 public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
802 $this->assertEquals(
803 "'\\\"'",
804 t3lib_div::quoteJSvalue('"', true)
805 );
806 }
807
808 /**
809 * @test
810 */
811 public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
812 $this->assertEquals(
813 "'\\&quot;'",
814 t3lib_div::quoteJSvalue('"', false)
815 );
816 }
817
818 /**
819 * @test
820 */
821 public function quoteJSvalueEscapesTab() {
822 $this->assertEquals(
823 "'" . '\t' . "'",
824 t3lib_div::quoteJSvalue(TAB)
825 );
826 }
827
828 /**
829 * @test
830 */
831 public function quoteJSvalueEscapesLinefeed() {
832 $this->assertEquals(
833 "'" . '\n' . "'",
834 t3lib_div::quoteJSvalue(LF)
835 );
836 }
837
838 /**
839 * @test
840 */
841 public function quoteJSvalueEscapesCarriageReturn() {
842 $this->assertEquals(
843 "'" . '\r' . "'",
844 t3lib_div::quoteJSvalue(CR)
845 );
846 }
847
848 /**
849 * @test
850 */
851 public function quoteJSvalueEscapesBackslah() {
852 $this->assertEquals(
853 "'\\\\'",
854 t3lib_div::quoteJSvalue('\\')
855 );
856 }
857
858 /**
859 * Tests the locallangXMLOverride feature of readLLfile()
860 * @test
861 */
862 public function readLLfileLocallangXMLOverride() {
863 $unique = uniqid('locallangXMLOverrideTest');
864
865 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
866 <T3locallang>
867 <data type="array">
868 <languageKey index="default" type="array">
869 <label index="buttons.logout">EXIT</label>
870 </languageKey>
871 </data>
872 </T3locallang>';
873
874 $file = PATH_site . 'typo3temp/' . $unique . '.xml';
875 t3lib_div::writeFileToTypo3tempDir($file, $xml);
876
877 // get default value
878 $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
879
880 // set override file
881 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
882
883 // get override value
884 $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
885
886 $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
887 $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
888 $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
889
890 unlink($file);
891 }
892
893
894 ///////////////////////////////
895 // Tests concerning _GETset()
896 ///////////////////////////////
897
898 /**
899 * @test
900 */
901 public function getSetCanSetWholeArray() {
902 $_GET = array();
903 $GLOBALS['HTTP_GET_VARS'] = array();
904 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
905
906 $this->assertEquals(
907 array('oneKey' => 'oneValue'),
908 $_GET
909 );
910 $this->assertEquals(
911 array('oneKey' => 'oneValue'),
912 $GLOBALS['HTTP_GET_VARS']
913 );
914 }
915
916 /**
917 * @test
918 */
919 public function getSetForArrayDropsExistingValues() {
920 $_GET = array();
921 $GLOBALS['HTTP_GET_VARS'] = array();
922
923 t3lib_div::_GETset(array('foo' => 'bar'));
924 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
925
926 $this->assertEquals(
927 array('oneKey' => 'oneValue'),
928 $_GET
929 );
930 $this->assertEquals(
931 array('oneKey' => 'oneValue'),
932 $GLOBALS['HTTP_GET_VARS']
933 );
934 }
935
936 /**
937 * @test
938 */
939 public function getSetCanAssignOneValueToOneKey() {
940 $_GET = array();
941 $GLOBALS['HTTP_GET_VARS'] = array();
942
943 t3lib_div::_GETset('oneValue', 'oneKey');
944
945 $this->assertEquals(
946 'oneValue',
947 $_GET['oneKey']
948 );
949 $this->assertEquals(
950 'oneValue',
951 $GLOBALS['HTTP_GET_VARS']['oneKey']
952 );
953 }
954
955 /**
956 * @test
957 */
958 public function getSetForOneValueNotDropsExistingValues() {
959 $_GET = array();
960 $GLOBALS['HTTP_GET_VARS'] = array();
961
962 t3lib_div::_GETset(array('foo' => 'bar'));
963 t3lib_div::_GETset('oneValue', 'oneKey');
964
965 $this->assertEquals(
966 array('foo' => 'bar', 'oneKey' => 'oneValue'),
967 $_GET
968 );
969 $this->assertEquals(
970 array('foo' => 'bar', 'oneKey' => 'oneValue'),
971 $GLOBALS['HTTP_GET_VARS']
972 );
973 }
974
975 /**
976 * @test
977 */
978 public function getSetCanAssignAnArrayToSpecificArrayElement() {
979 $_GET = array();
980 $GLOBALS['HTTP_GET_VARS'] = array();
981
982 t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
983
984 $this->assertEquals(
985 array('parentKey' => array('childKey' => 'oneValue')),
986 $_GET
987 );
988 $this->assertEquals(
989 array('parentKey' => array('childKey' => 'oneValue')),
990 $GLOBALS['HTTP_GET_VARS']
991 );
992 }
993
994 /**
995 * @test
996 */
997 public function getSetCanAssignAValueToSpecificArrayChildElement() {
998 $_GET = array();
999 $GLOBALS['HTTP_GET_VARS'] = array();
1000
1001 t3lib_div::_GETset('oneValue', 'parentKey|childKey');
1002
1003 $this->assertEquals(
1004 array('parentKey' => array('childKey' => 'oneValue')),
1005 $_GET
1006 );
1007 $this->assertEquals(
1008 array('parentKey' => array('childKey' => 'oneValue')),
1009 $GLOBALS['HTTP_GET_VARS']
1010 );
1011 }
1012
1013 /**
1014 * @test
1015 */
1016 public function getSetCanAssignAnArrayToSpecificArrayChildElement() {
1017 $_GET = array();
1018 $GLOBALS['HTTP_GET_VARS'] = array();
1019
1020 t3lib_div::_GETset(
1021 array('key1' => 'value1', 'key2' => 'value2'),
1022 'parentKey|childKey'
1023 );
1024
1025 $this->assertEquals(
1026 array(
1027 'parentKey' => array(
1028 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1029 )
1030 ),
1031 $_GET
1032 );
1033 $this->assertEquals(
1034 array(
1035 'parentKey' => array(
1036 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1037 )
1038 ),
1039 $GLOBALS['HTTP_GET_VARS']
1040 );
1041 }
1042
1043 /**
1044 * Checks if t3lib_div::fixPermissions() correctly sets permissions to single file
1045 * This test assumes directory 'PATH_site'/typo3temp exists
1046 * This test is not available on windows OS
1047 *
1048 * @test
1049 * @see t3lib_div::fixPermissions()
1050 */
1051 public function checkFixPermissionsCorrectlySetsPermissionsToFile() {
1052 if (TYPO3_OS == 'WIN') {
1053 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1054 }
1055
1056 // Create and prepare test file
1057 $filename = PATH_site . 'typo3temp/' . uniqid('test_');
1058 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1059 chmod($filename, 0742);
1060
1061 // Set target permissions and run method
1062 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1063 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1064 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1065
1066 // Get actual permissions and clean up
1067 clearstatcache();
1068 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1069 $resultFileGroup = filegroup($filename);
1070 unlink($filename);
1071
1072 // Test if everything was ok
1073 $this->assertTrue($fixPermissionsResult);
1074 $this->assertEquals($resultFilePermissions, '0660');
1075 $this->assertEquals($resultFileGroup, posix_getegid());
1076 }
1077
1078 /**
1079 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden file
1080 * This test assumes directory 'PATH_site'/typo3temp exists
1081 * This test is not available on windows OS
1082 *
1083 * @test
1084 * @see t3lib_div::fixPermissions()
1085 */
1086 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenFile() {
1087 if (TYPO3_OS == 'WIN') {
1088 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1089 }
1090
1091 // Create and prepare test file
1092 $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
1093 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1094 chmod($filename, 0742);
1095
1096 // Set target permissions and run method
1097 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1098 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1099 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1100
1101 // Get actual permissions and clean up
1102 clearstatcache();
1103 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1104 $resultFileGroup = filegroup($filename);
1105 unlink($filename);
1106
1107 // Test if everything was ok
1108 $this->assertTrue($fixPermissionsResult);
1109 $this->assertEquals($resultFilePermissions, '0660');
1110 $this->assertEquals($resultFileGroup, posix_getegid());
1111 }
1112
1113 /**
1114 * Checks if t3lib_div::fixPermissions() correctly sets permissions to directory with trailing slash
1115 * This test assumes directory 'PATH_site'/typo3temp exists
1116 * This test is not available on windows OS
1117 *
1118 * @test
1119 * @see t3lib_div::fixPermissions()
1120 */
1121 public function checkFixPermissionsCorrectlySetsPermissionsToDirectory() {
1122 if (TYPO3_OS == 'WIN') {
1123 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1124 }
1125
1126 // Create and prepare test directory
1127 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1128 t3lib_div::mkdir($directory);
1129 chmod($directory, 1551);
1130
1131 // Set target permissions and run method
1132 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '2770';
1133 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1134 $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
1135
1136 // Get actual permissions and clean up
1137 clearstatcache();
1138 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1139 $resultDirectoryGroup = filegroup($directory);
1140 t3lib_div::rmdir($directory);
1141
1142 // Test if everything was ok
1143 $this->assertTrue($fixPermissionsResult);
1144 $this->assertEquals($resultDirectoryPermissions, '2770');
1145 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1146 }
1147
1148 /**
1149 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden directory
1150 * This test assumes directory 'PATH_site'/typo3temp exists
1151 * This test is not available on windows OS
1152 *
1153 * @test
1154 * @see t3lib_div::fixPermissions()
1155 */
1156 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
1157 if (TYPO3_OS == 'WIN') {
1158 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1159 }
1160
1161 // Create and prepare test directory
1162 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1163 t3lib_div::mkdir($directory);
1164 chmod($directory, 1551);
1165
1166 // Set target permissions and run method
1167 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '2770';
1168 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1169 $fixPermissionsResult = t3lib_div::fixPermissions($directory);
1170
1171 // Get actual permissions and clean up
1172 clearstatcache();
1173 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1174 $resultDirectoryGroup = filegroup($directory);
1175 t3lib_div::rmdir($directory);
1176
1177 // Test if everything was ok
1178 $this->assertTrue($fixPermissionsResult);
1179 $this->assertEquals($resultDirectoryPermissions, '2770');
1180 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1181 }
1182
1183 /**
1184 * Checks if t3lib_div::fixPermissions() correctly sets permissions recursivly
1185 * This test assumes directory 'PATH_site'/typo3temp exists
1186 * This test is not available on windows OS
1187 *
1188 * @test
1189 * @see t3lib_div::fixPermissions()
1190 */
1191 public function checkFixPermissionsCorrectlySetsPermissionsRecursive() {
1192 if (TYPO3_OS == 'WIN') {
1193 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1194 }
1195
1196 // Create and prepare test directory and file structure
1197 $baseDirectory = PATH_site . 'typo3temp/' . uniqid('test_');
1198 t3lib_div::mkdir($baseDirectory);
1199 chmod($baseDirectory, 1751);
1200 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
1201 chmod($baseDirectory . '/file', 0742);
1202 t3lib_div::mkdir($baseDirectory . '/foo');
1203 chmod($baseDirectory . '/foo', 1751);
1204 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
1205 chmod($baseDirectory . '/foo/file', 0742);
1206 t3lib_div::mkdir($baseDirectory . '/.bar');
1207 chmod($baseDirectory . '/.bar', 1751);
1208 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
1209 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
1210 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
1211 touch($baseDirectory . '/.bar/.file', '42');
1212 chmod($baseDirectory . '/.bar/.file', 0742);
1213 touch($baseDirectory . '/.bar/..file2', '42');
1214 chmod($baseDirectory . '/.bar/..file2', 0742);
1215
1216 // Set target permissions and run method
1217 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1218 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '2770';
1219 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1220 $fixPermissionsResult = t3lib_div::fixPermissions($baseDirectory, TRUE);
1221
1222 // Get actual permissions
1223 clearstatcache();
1224 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
1225 $resultBaseDirectoryGroup = filegroup($baseDirectory);
1226 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
1227 $resultBaseFileGroup = filegroup($baseDirectory . '/file');
1228 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
1229 $resultFooDirectoryGroup = filegroup($baseDirectory . '/foo');
1230 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
1231 $resultFooFileGroup = filegroup($baseDirectory . '/foo/file');
1232 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
1233 $resultBarDirectoryGroup = filegroup($baseDirectory . '/.bar');
1234 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
1235 $resultBarFileGroup = filegroup($baseDirectory . '/.bar/.file');
1236 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
1237 $resultBarFile2Group = filegroup($baseDirectory . '/.bar/..file2');
1238
1239 // Clean up
1240 unlink($baseDirectory . '/file');
1241 unlink($baseDirectory . '/foo/file');
1242 unlink($baseDirectory . '/.bar/.file');
1243 unlink($baseDirectory . '/.bar/..file2');
1244 t3lib_div::rmdir($baseDirectory . '/foo');
1245 t3lib_div::rmdir($baseDirectory . '/.bar');
1246 t3lib_div::rmdir($baseDirectory);
1247
1248 // Test if everything was ok
1249 $this->assertTrue($fixPermissionsResult);
1250 $this->assertEquals($resultBaseDirectoryPermissions, '2770');
1251 $this->assertEquals($resultBaseDirectoryGroup, posix_getegid());
1252 $this->assertEquals($resultBaseFilePermissions, '0660');
1253 $this->assertEquals($resultBaseFileGroup, posix_getegid());
1254 $this->assertEquals($resultFooDirectoryPermissions, '2770');
1255 $this->assertEquals($resultFooDirectoryGroup, posix_getegid());
1256 $this->assertEquals($resultFooFilePermissions, '0660');
1257 $this->assertEquals($resultFooFileGroup, posix_getegid());
1258 $this->assertEquals($resultBarDirectoryPermissions, '2770');
1259 $this->assertEquals($resultBarDirectoryGroup, posix_getegid());
1260 $this->assertEquals($resultBarFilePermissions, '0660');
1261 $this->assertEquals($resultBarFileGroup, posix_getegid());
1262 $this->assertEquals($resultBarFile2Permissions, '0660');
1263 $this->assertEquals($resultBarFile2Group, posix_getegid());
1264 }
1265
1266 /**
1267 * Checks if t3lib_div::fixPermissions() does not fix permissions on not allowed path
1268 * This test assumes directory 'PATH_site'/typo3temp exists
1269 * This test is not available on windows OS
1270 *
1271 * @test
1272 * @see t3lib_div::fixPermissions()
1273 */
1274 public function checkFixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
1275 if (TYPO3_OS == 'WIN') {
1276 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1277 }
1278
1279 // Create and prepare test file
1280 $filename = PATH_site . 'typo3temp/../typo3temp/' . uniqid('test_');
1281 touch($filename);
1282 chmod($filename, 0742);
1283
1284 // Set target permissions and run method
1285 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1286 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1287
1288 // Get actual permissions and clean up
1289 clearstatcache();
1290 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1291 unlink($filename);
1292
1293 // Test if everything was ok
1294 $this->assertFalse($fixPermissionsResult);
1295 $this->assertEquals($resultFilePermissions, '0742');
1296 }
1297
1298 /**
1299 * Checks if t3lib_div::mkdir() correctly creates a directory
1300 * This test assumes directory 'PATH_site'/typo3temp exists
1301 *
1302 * @test
1303 * @see t3lib_div::mkdir()
1304 */
1305 public function checkMkdirCorrectlyCreatesDirectory() {
1306 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1307 $mkdirResult = t3lib_div::mkdir($directory);
1308 $directoryCreated = is_dir($directory);
1309 t3lib_div::rmdir($directory);
1310 $this->assertTrue($mkdirResult);
1311 $this->assertTrue($directoryCreated);
1312 }
1313
1314 /**
1315 * Checks if t3lib_div::mkdir() correctly creates a hidden directory
1316 * This test assumes directory 'PATH_site'/typo3temp exists
1317 *
1318 * @test
1319 * @see t3lib_div::mkdir()
1320 */
1321 public function checkMkdirCorrectlyCreatesHiddenDirectory() {
1322 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1323 $mkdirResult = t3lib_div::mkdir($directory);
1324 $directoryCreated = is_dir($directory);
1325 t3lib_div::rmdir($directory);
1326 $this->assertTrue($mkdirResult);
1327 $this->assertTrue($directoryCreated);
1328 }
1329
1330 /**
1331 * Checks if t3lib_div::mkdir() correctly creates a directory with trailing slash
1332 * This test assumes directory 'PATH_site'/typo3temp exists
1333 *
1334 * @test
1335 * @see t3lib_div::mkdir()
1336 */
1337 public function checkMkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
1338 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1339 $mkdirResult = t3lib_div::mkdir($directory);
1340 $directoryCreated = is_dir($directory);
1341 t3lib_div::rmdir($directory);
1342 $this->assertTrue($mkdirResult);
1343 $this->assertTrue($directoryCreated);
1344 }
1345 }
1346 ?>