Fixed bug #14971: Unit test for sanitizeLocalUrl fails on installations inside a...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_divTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 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_divTest 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 // Tests concerning validIP
61 ///////////////////////////////
62
63 /**
64 * Data provider for checkValidIpReturnsTrueForValidIp
65 *
66 * @return array Data sets
67 */
68 public static function validIpDataProvider() {
69 return array(
70 '0.0.0.0' => array('0.0.0.0'),
71 'private IPv4 class C' => array('192.168.0.1'),
72 'private IPv4 class A' => array('10.0.13.1'),
73 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb'),
74 );
75 }
76
77 /**
78 * Checks if t3lib_div::validIP() returns true for valid IPs
79 *
80 * @test
81 * @see t3lib_div::validIP()
82 * @dataProvider validIpDataProvider
83 */
84 public function checkValidIpReturnsTrueForValidIp($ip) {
85 $this->assertTrue(t3lib_div::validIP($ip));
86 }
87
88 /**
89 * Data provider for checkValidIpReturnsFalseForInvalidIp
90 *
91 * @return array Data sets
92 */
93 public static function invalidIpDataProvider() {
94 return array(
95 'null' => array(null),
96 'zero' => array(0),
97 'string' => array('test'),
98 'string empty' => array(''),
99 'string null' => array('null'),
100 'out of bounds IPv4' => array('300.300.300.300'),
101 'wrong dotted decimal notation with only two dots' => array('127.0.1'),
102 );
103 }
104
105 /**
106 * Checks if t3lib_div::validIP() returns false for invalid IPs
107 *
108 * @test
109 * @see t3lib_div::validIP()
110 * @dataProvider invalidIpDataProvider
111 */
112 public function checkValidIpReturnsFalseForInvalidIp($ip) {
113 $this->assertFalse(t3lib_div::validIP($ip));
114 }
115
116
117 ///////////////////////////////
118 // Tests concerning splitCalc
119 ///////////////////////////////
120
121 /**
122 * @test
123 */
124 public function splitCalcForEmptyStringReturnsEmptyArray() {
125 $this->assertEquals(
126 array(),
127 t3lib_div::splitCalc('', '+-*/')
128 );
129 }
130
131 /**
132 * @test
133 */
134 public function splitCalcForNumberWithoutOperatorReturnsArrayWithPlusAndNumber() {
135 $this->assertEquals(
136 array(array('+', 42)),
137 t3lib_div::splitCalc('42', '+-*/')
138 );
139 }
140
141 /**
142 * @test
143 */
144 public function splitCalcForTwoNumbersWithAsterikReturnsFirstNumberWithPlusAndSecondNumberWithOperator() {
145 $this->assertEquals(
146 array(
147 array('+', 42),
148 array('*', 31),
149 ),
150 t3lib_div::splitCalc('42 * 31', '+-*/')
151 );
152 }
153
154
155 //////////////////////////////////
156 // Tests concerning calcPriority
157 //////////////////////////////////
158
159 /**
160 * @see calcPriorityCalculatesBasicArithmeticOperation
161 */
162 public function calcPriorityTwoOperandsDataProvider() {
163 return array(
164 'add' => array(9, '6 + 3'),
165 'substractWithPositiveResult' => array(3, '6 - 3'),
166 'substractWithNegativeResult' => array(-3, '3 - 6'),
167 'multiply' => array(6, '2 * 3'),
168 'divide' => array(2.5, '5 / 2'),
169 'modulus' => array(1, '5 % 2'),
170 'power' => array(8, '2 ^ 3'),
171 );
172 }
173
174 /**
175 * @test
176 *
177 * @dataProvider calcPriorityTwoOperandsDataProvider
178 *
179 * @param string $expected the expected value from calcPriority
180 * @param string $arithmeticExpression the string to feed to calcPriority
181 */
182 public function calcPriorityCalculatesBasicArithmeticOperation($expected, $arithmeticExpression) {
183 $this->assertEquals(
184 $expected,
185 t3lib_div::calcPriority($arithmeticExpression)
186 );
187 }
188
189 /**
190 * @test
191 */
192 public function calcPriorityCalculatesArithmeticOperationWithMultipleOperands() {
193 $this->assertEquals(6.5, t3lib_div::calcPriority('5 + 3 / 2'));
194 $this->assertEquals(14, t3lib_div::calcPriority('5 + 3 ^ 2'));
195 $this->assertEquals(4, t3lib_div::calcPriority('5 % 2 + 3'));
196 $this->assertEquals(3, t3lib_div::calcPriority('2 + 6 / 2 - 2'));
197 }
198
199 /**
200 * @test
201 */
202 public function checkIntExplodeConvertsStringsToInteger() {
203 $testString = '1,foo,2';
204 $expectedArray = array(1, 0, 2);
205 $actualArray = t3lib_div::intExplode(',', $testString);
206
207 $this->assertEquals($expectedArray, $actualArray);
208 }
209
210 /**
211 * @test
212 */
213 public function checkRevExplodeCorrectlyExplodesString() {
214 $testString = 'my:words:here';
215 $expectedArray = array('my:words', 'here');
216 $actualArray = t3lib_div::revExplode(':', $testString, 2);
217
218 $this->assertEquals($expectedArray, $actualArray);
219 }
220
221 /**
222 * @test
223 */
224 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
225 $testString = ' a , b , c ,d ,, e,f,';
226 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
227 $actualArray = t3lib_div::trimExplode(',', $testString);
228
229 $this->assertEquals($expectedArray, $actualArray);
230 }
231
232 /**
233 * @test
234 */
235 public function checkTrimExplodeRemovesNewLines() {
236 $testString = ' a , b , ' . LF . ' ,d ,, e,f,';
237 $expectedArray = array('a', 'b', 'd', 'e', 'f');
238 $actualArray = t3lib_div::trimExplode(',', $testString, true);
239
240 $this->assertEquals($expectedArray, $actualArray);
241 }
242
243 /**
244 * @test
245 */
246 public function checkTrimExplodeRemovesEmptyElements() {
247 $testString = 'a , b , c , ,d ,, ,e,f,';
248 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
249 $actualArray = t3lib_div::trimExplode(',', $testString, true);
250
251 $this->assertEquals($expectedArray, $actualArray);
252 }
253
254 /**
255 * @test
256 */
257 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
258 $testString = ' a , b , c , , d,, ,e ';
259 $expectedArray = array('a', 'b', 'c,,d,,,e'); // limiting returns the rest of the string as the last element
260 $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
261
262 $this->assertEquals($expectedArray, $actualArray);
263 }
264
265 /**
266 * @test
267 */
268 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
269 $testString = ' a , b , c , , d,, ,e ';
270 $expectedArray = array('a', 'b', 'c,d,e'); // limiting returns the rest of the string as the last element
271 $actualArray = t3lib_div::trimExplode(',', $testString, true, 3);
272
273 $this->assertEquals($expectedArray, $actualArray);
274 }
275
276 /**
277 * @test
278 */
279 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
280 $testString = ' a , b , c , d, ,e, f , , ';
281 $expectedArray = array('a', 'b', 'c', 'd', '', 'e'); // limiting returns the rest of the string as the last element
282 $actualArray = t3lib_div::trimExplode(',', $testString, false, -3);
283
284 $this->assertEquals($expectedArray, $actualArray);
285 }
286
287 /**
288 * @test
289 */
290 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
291 $testString = ' a , b , c , d, ,e, f , , ';
292 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
293 $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
294
295 $this->assertEquals($expectedArray, $actualArray);
296 }
297
298 /**
299 * @test
300 */
301 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
302 $testString = ' a , b , , c , , , ';
303 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
304 $actualArray = t3lib_div::trimExplode(',', $testString, true, 4);
305
306 $this->assertEquals($expectedArray, $actualArray);
307 }
308
309 /**
310 * @test
311 */
312 public function checkTrimExplodeKeepsZeroAsString() {
313 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
314 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
315 $actualArray = t3lib_div::trimExplode(',', $testString, true);
316
317 $this->assertEquals($expectedArray, $actualArray);
318 }
319
320 /**
321 * @test
322 */
323 public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
324 $inputArray = array(
325 '0' => 'test1',
326 '1' => 'test2',
327 '2' => 'test3',
328 '3' => 'test2',
329 );
330 $compareValue = 'test2';
331 $expectedResult = array(
332 '0' => 'test1',
333 '2' => 'test3',
334 );
335 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
336 $this->assertEquals($expectedResult, $actualResult);
337 }
338
339 /**
340 * @test
341 */
342 public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
343 $inputArray = array(
344 '0' => 'foo',
345 '1' => array(
346 '10' => 'bar',
347 ),
348 '2' => 'bar',
349 );
350 $compareValue = 'bar';
351 $expectedResult = array(
352 '0' => 'foo',
353 '1' => array(),
354 );
355 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
356 $this->assertEquals($expectedResult, $actualResult);
357 }
358
359 /**
360 * @test
361 */
362 public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
363 $inputArray = array(
364 '0' => 'foo',
365 '1' => '',
366 '2' => 'bar',
367 );
368 $compareValue = '';
369 $expectedResult = array(
370 '0' => 'foo',
371 '2' => 'bar',
372 );
373 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
374 $this->assertEquals($expectedResult, $actualResult);
375 }
376
377 /**
378 * Checks whether measurement strings like "100k" return the accordant
379 * byte representation like 102400 in this case.
380 *
381 * @test
382 */
383 public function checkGetBytesFromSizeMeasurement() {
384 $this->assertEquals(
385 '102400',
386 t3lib_div::getBytesFromSizeMeasurement('100k')
387 );
388
389 $this->assertEquals(
390 '104857600',
391 t3lib_div::getBytesFromSizeMeasurement('100m')
392 );
393
394 $this->assertEquals(
395 '107374182400',
396 t3lib_div::getBytesFromSizeMeasurement('100g')
397 );
398 }
399
400 /**
401 * @test
402 */
403 public function checkIndpEnvTypo3SitePathNotEmpty() {
404 $actualEnv = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
405 $this->assertTrue(strlen($actualEnv) >= 1);
406 $this->assertEquals('/', $actualEnv{0});
407 $this->assertEquals('/', $actualEnv{strlen($actualEnv) - 1});
408 }
409
410 /**
411 * @test
412 * @see t3lib_div::underscoredToUpperCamelCase
413 */
414 public function canConvertFromUnderscoredToUpperCamelCase() {
415 $this->assertEquals('BlogExample', t3lib_div::underscoredToUpperCamelCase('blog_example'));
416 $this->assertEquals('Blogexample', t3lib_div::underscoredToUpperCamelCase('blogexample'));
417 }
418
419 /**
420 * @test
421 * @see t3lib_div::underscoredToLowerCamelCase
422 */
423 public function canConvertFromUnderscoredToLowerCamelCase() {
424 $this->assertEquals('minimalValue', t3lib_div::underscoredToLowerCamelCase('minimal_value'));
425 $this->assertEquals('minimalvalue', t3lib_div::underscoredToLowerCamelCase('minimalvalue'));
426 }
427
428 /**
429 * @test
430 * @see t3lib_div::camelCaseToLowerCaseUnderscored
431 */
432 public function canConvertFromCamelCaseToLowerCaseUnderscored() {
433 $this->assertEquals('blog_example', t3lib_div::camelCaseToLowerCaseUnderscored('BlogExample'));
434 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('Blogexample'));
435 $this->assertEquals('blogexample', t3lib_div::camelCaseToLowerCaseUnderscored('blogexample'));
436
437 $this->assertEquals('minimal_value', t3lib_div::camelCaseToLowerCaseUnderscored('minimalValue'));
438 }
439
440 /**
441 * @test
442 * @see t3lib_div::lcfirst
443 */
444 public function canConvertFirstCharacterToBeLowerCase() {
445 $this->assertEquals('blogexample', t3lib_div::lcfirst('Blogexample'));
446 $this->assertEquals('blogExample', t3lib_div::lcfirst('BlogExample'));
447 $this->assertEquals('blogexample', t3lib_div::lcfirst('blogexample'));
448 }
449
450 /**
451 * Tests whether whitespaces are encoded correctly in a quoted-printable mail header.
452 * @test
453 */
454 public function areWhitespacesEncodedInQuotedPrintableMailHeader() {
455 $this->assertEquals(
456 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
457 t3lib_div::encodeHeader(
458 "We test whether the copyright character \xc2\xa9 is encoded correctly",
459 'quoted-printable',
460 'utf-8'
461 )
462 );
463 }
464
465 /**
466 * Tests whether question marks are encoded correctly in a quoted-printable mail header.
467 * @test
468 */
469 public function areQuestionMarksEncodedInQuotedPrintableMailHeader() {
470 $this->assertEquals(
471 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
472 t3lib_div::encodeHeader(
473 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
474 'quoted-printable',
475 'utf-8'
476 )
477 );
478 }
479
480 /**
481 * Data provider for valid URLs, like PHP's source code test cases
482 */
483 public function validUrlDataProvider() {
484 return array(
485 array('http://example.com/index.html'),
486 array('http://www.example.com/index.php'),
487 array('http://www.example/img/test.png'),
488 array('http://www.example/img/dir/'),
489 array('http://www.example/img/dir'),
490 array('file:///tmp/test.c'),
491 array('ftp://ftp.example.com/tmp/'),
492 array('mailto:foo@bar.com'),
493 array('news:news.php.net'),
494 array('file://foo/bar'),
495 array('http://qwe'),
496 );
497 }
498
499 /**
500 * Data provider for invalid URLs, like PHP's source code test cases
501 */
502 public function invalidUrlDataProvider() {
503 return array(
504 array('http//www.example/wrong/url/'),
505 array('http:/www.example'),
506 array('/tmp/test.c'),
507 array('/'),
508 array('http://'),
509 array('http:/'),
510 array('http:'),
511 array('http'),
512 array(''),
513 array('-1'),
514 array('array()'),
515 array('qwe'),
516 );
517 }
518
519 /**
520 * @test
521 * @dataProvider validUrlDataProvider
522 * @see t3lib_div::isValidUrl()
523 */
524 public function checkisValidURL($url) {
525 $this->assertTrue(t3lib_div::isValidUrl($url));
526 }
527
528 /**
529 * @test
530 * @dataProvider invalidUrlDataProvider
531 * @see t3lib_div::isValidUrl()
532 */
533 public function checkisInValidURL($url) {
534 $this->assertFalse(t3lib_div::isValidUrl($url));
535 }
536
537 /**
538 * @test
539 * @see t3lib_div::isValidUrl()
540 */
541 public function checkisValidURLSucceedsWithWebRessource() {
542 $testUrl = 'http://www.example.org/';
543 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
544 }
545
546 /**
547 * @test
548 * @see t3lib_div::isValidUrl()
549 */
550 public function checkisValidURLSucceedsWithExtentedWebRessource() {
551 $testUrl = 'https://user:pw@www.example.org:80/path?arg=value#fragment';
552 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
553 }
554
555 /**
556 * @test
557 * @see t3lib_div::isValidUrl()
558 */
559 public function checkisValidURLSucceedsWithTelnetRessource() {
560 $testUrl = 'telnet://192.0.2.16:80/';
561 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
562 }
563
564 /**
565 * @test
566 */
567 public function checkisValidURLSucceedsWithLdapRessource() {
568 $testUrl = 'ldap://[2001:db8::7]/c=GB?objectClass?one';
569 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
570 }
571
572 /**
573 * @test
574 * @see t3lib_div::isValidUrl()
575 */
576 public function checkisValidURLSucceedsWithFileRessource() {
577 $testUrl = 'file:///etc/passwd';
578 $this->assertTrue(t3lib_div::isValidUrl($testUrl));
579 }
580
581 /**
582 * @test
583 * @see t3lib_div::isValidUrl()
584 */
585 public function checkisValidURLFailsWithHostnameOnly() {
586 $testUrl = 'www.example.org/';
587 $this->assertFalse(t3lib_div::isValidUrl($testUrl));
588 }
589
590 /**
591 * @test
592 * @see t3lib_div::isOnCurrentHost()
593 */
594 public function checkisOnCurrentHostFailsWithLocalhostIPOnly() {
595 $testUrl = '127.0.0.1';
596 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
597 }
598
599 /**
600 * @test
601 * @see t3lib_div::isOnCurrentHost()
602 */
603 public function checkisOnCurrentHostFailsWithPathsOnly() {
604 $testUrl = './relpath/file.txt';
605 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
606 $testUrl = '/abspath/file.txt?arg=value';
607 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
608 }
609
610 /**
611 * @test
612 * @see t3lib_div::isOnCurrentHost()
613 */
614 public function checkisOnCurrentHostFailsWithArbitraryString() {
615 $testUrl = 'arbitrary string';
616 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
617 }
618
619 /**
620 * @test
621 * @see t3lib_div::isOnCurrentHost()
622 */
623 public function checkisOnCurrentHostFailsWithEmptyUrl() {
624 $testUrl = '';
625 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
626 }
627
628 /**
629 * @test
630 * @see t3lib_div::isOnCurrentHost()
631 */
632 public function checkisOnCurrentHostFailsWithDifferentHost() {
633 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org';
634 $this->assertFalse(t3lib_div::isOnCurrentHost($testUrl));
635 }
636
637 /**
638 * @test
639 * @see t3lib_div::isOnCurrentHost()
640 */
641 public function checkisOnCurrentHostSucceedsWithCurrentHost() {
642 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
643 $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
644 }
645
646
647 ////////////////////////////////////////
648 // Tests concerning sanitizeLocalUrl
649 ////////////////////////////////////////
650
651 /**
652 * Data provider for valid URLs.
653 * @see sanitizeLocalUrlAcceptsValidUrls
654 */
655 public function validLocalUrlDataProvider() {
656 $subDirectory = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
657 $typo3SiteUrl = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
658 $typo3RequestHost = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST');
659
660 return array(
661 'alt_intro.php' => array('alt_intro.php'),
662 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
663 $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
664 $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
665 '../index.php' => array('../index.php'),
666 '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
667 '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
668 '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
669 PATH_site . 'typo3/alt_intro.php' => array(PATH_site . 'typo3/alt_intro.php'),
670 $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
671 $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php'),
672 );
673 }
674
675 /**
676 * Data provider for invalid URLs.
677 * @see sanitizeLocalUrlDeniesInvalidUrls
678 */
679 public function invalidLocalUrlDataProvider() {
680 return array(
681 array(''),
682 array('http://www.google.de/'),
683 array('https://www.google.de/'),
684 array('../typo3/whatever.php?argument=javascript:alert(0)'),
685 );
686 }
687
688 /**
689 * Tests whether valid local URLs are handled correctly.
690 * @dataProvider validLocalUrlDataProvider
691 * @test
692 */
693 public function sanitizeLocalUrlAcceptsPlainValidUrls($url) {
694 $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
695 }
696
697 /**
698 * Tests whether valid local URLs are handled correctly.
699 * @dataProvider validLocalUrlDataProvider
700 * @test
701 */
702 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
703 $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
704 }
705
706 /**
707 * Tests whether valid local URLs are handled correctly.
708 * @dataProvider invalidLocalUrlDataProvider
709 * @test
710 */
711 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
712 $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
713 }
714
715 /**
716 * Tests whether valid local URLs are handled correctly.
717 * @dataProvider invalidLocalUrlDataProvider
718 * @test
719 */
720 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
721 $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
722 }
723
724 //////////////////////////////////////
725 // Tests concerning arrayDiffAssocRecursive
726 //////////////////////////////////////
727
728 /**
729 * Test if a one dimensional array is correctly diffed.
730 *
731 * @test
732 * @see t3lib_div::arrayDiffAssocRecursive
733 */
734 public function doesArrayDiffAssocRecursiveCorrectlyHandleOneDimensionalArrays() {
735 $array1 = array(
736 'key1' => 'value1',
737 'key2' => 'value2',
738 'key3' => 'value3',
739 );
740 $array2 = array(
741 'key1' => 'value1',
742 'key3' => 'value3',
743 );
744 $expectedResult = array(
745 'key2' => 'value2',
746 );
747 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
748 $this->assertEquals($expectedResult, $actualResult);
749 }
750
751 /**
752 * Test if a three dimensional array is correctly diffed.
753 *
754 * @test
755 * @see t3lib_div::arrayDiffAssocRecursive
756 */
757 public function doesArrayDiffAssocRecursiveCorrectlyHandleMultiDimensionalArrays() {
758 $array1 = array(
759 'key1' => 'value1',
760 'key2' => array(
761 'key21' => 'value21',
762 'key22' => 'value22',
763 'key23' => array(
764 'key231' => 'value231',
765 'key232' => 'value232',
766 ),
767 ),
768 );
769 $array2 = array(
770 'key1' => 'value1',
771 'key2' => array(
772 'key21' => 'value21',
773 'key23' => array(
774 'key231' => 'value231',
775 ),
776 ),
777 );
778 $expectedResult = array(
779 'key2' => array(
780 'key22' => 'value22',
781 'key23' => array(
782 'key232' => 'value232',
783 ),
784 ),
785 );
786 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
787 $this->assertEquals($expectedResult, $actualResult);
788 }
789
790 /**
791 * Test if arrays are correctly diffed if types are different.
792 *
793 * @test
794 * @see t3lib_div::arrayDiffAssocRecursive
795 */
796 public function doesArrayDiffAssocRecursiveCorrectlyHandleMixedArrays() {
797 $array1 = array(
798 'key1' => array(
799 'key11' => 'value11',
800 'key12' => 'value12',
801 ),
802 'key2' => 'value2',
803 'key3' => 'value3',
804 );
805 $array2 = array(
806 'key1' => 'value1',
807 'key2' => array(
808 'key21' => 'value21',
809 ),
810 );
811 $expectedResult = array(
812 'key3' => 'value3',
813 );
814 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
815 $this->assertEquals($expectedResult, $actualResult);
816 }
817
818 //////////////////////////////////////
819 // Tests concerning removeDotsFromTS
820 //////////////////////////////////////
821
822 /**
823 * Tests whether removeDotsFromTS() behaves correctly.
824 * @test
825 * @see t3lib_div::removeDotsFromTS()
826 */
827 public function doesRemoveDotsFromTypoScriptSucceed() {
828 $typoScript = array(
829 'propertyA.' => array(
830 'keyA.' => array(
831 'valueA' => 1,
832 ),
833 'keyB' => 2,
834 ),
835 'propertyB' => 3,
836 );
837
838 $expectedResult = array(
839 'propertyA' => array(
840 'keyA' => array(
841 'valueA' => 1,
842 ),
843 'keyB' => 2,
844 ),
845 'propertyB' => 3,
846 );
847
848 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
849 }
850
851 /**
852 * Tests whether removeDotsFromTS() behaves correctly.
853 * @test
854 * @see t3lib_div::removeDotsFromTS()
855 */
856 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
857 $typoScript = array(
858 'propertyA.' => array(
859 'keyA' => 'getsOverridden',
860 'keyA.' => array(
861 'valueA' => 1,
862 ),
863 'keyB' => 2,
864 ),
865 'propertyB' => 3,
866 );
867
868 $expectedResult = array(
869 'propertyA' => array(
870 'keyA' => array(
871 'valueA' => 1,
872 ),
873 'keyB' => 2,
874 ),
875 'propertyB' => 3,
876 );
877
878 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
879 }
880
881 /**
882 * Tests whether removeDotsFromTS() behaves correctly.
883 * @test
884 * @see t3lib_div::removeDotsFromTS()
885 */
886 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
887 $typoScript = array(
888 'propertyA.' => array(
889 'keyA.' => array(
890 'valueA' => 1,
891 ),
892 'keyA' => 'willOverride',
893 'keyB' => 2,
894 ),
895 'propertyB' => 3,
896 );
897
898 $expectedResult = array(
899 'propertyA' => array(
900 'keyA' => 'willOverride',
901 'keyB' => 2,
902 ),
903 'propertyB' => 3,
904 );
905
906 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
907 }
908
909 /**
910 * Tests whether getDirs() returns an array of diretories from a given path
911 * @test
912 * @see t3lib_div::getDirs($path)
913 */
914 public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
915 $path = PATH_t3lib;
916 $directories = t3lib_div::get_dirs($path);
917
918 $this->assertType('array', $directories);
919 }
920
921 /**
922 * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
923 * @test
924 * @see t3lib_div::getDirs($path)
925 */
926 public function checkGetDirsReturnsStringErrorOnPathFailure() {
927 $path = 'foo';
928 $result = t3lib_div::get_dirs($path);
929 $expectedResult = 'error';
930
931 $this->assertEquals($expectedResult, $result);
932 }
933
934
935 //////////////////////////////////
936 // Tests concerning hmac
937 //////////////////////////////////
938
939 /**
940 * @test
941 */
942 public function hmacReturnsHashOfProperLength() {
943 $hmac = t3lib_div::hmac('message');
944 $this->assertTrue(!empty($hmac) && is_string($hmac));
945 $this->assertTrue(strlen($hmac) == 40);
946 }
947
948 /**
949 * @test
950 */
951 public function hmacReturnsEqualHashesForEqualInput() {
952 $msg0 = 'message';
953 $msg1 = 'message';
954 $this->assertEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
955 }
956
957 /**
958 * @test
959 */
960 public function hmacReturnsNotEqualHashesForNotEqualInput() {
961 $msg0 = 'message0';
962 $msg1 = 'message1';
963 $this->assertNotEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
964 }
965
966
967 //////////////////////////////////
968 // Tests concerning quoteJSvalue
969 //////////////////////////////////
970
971 /**
972 * @test
973 */
974 public function quoteJSvalueHtmlspecialcharsDataByDefault() {
975 $this->assertContains(
976 '&gt;',
977 t3lib_div::quoteJSvalue('>')
978 );
979 }
980
981 /**
982 * @test
983 */
984 public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
985 $this->assertContains(
986 '&gt;',
987 t3lib_div::quoteJSvalue('>', false)
988 );
989 }
990
991 /**
992 * @test
993 */
994 public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
995 $this->assertContains(
996 '>',
997 t3lib_div::quoteJSvalue('>', true)
998 );
999 }
1000
1001 /**
1002 * @test
1003 */
1004 public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
1005 $this->assertEquals(
1006 "''",
1007 t3lib_div::quoteJSvalue("", true)
1008 );
1009 }
1010
1011 /**
1012 * @test
1013 */
1014 public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
1015 $this->assertEquals(
1016 "'Hello world!'",
1017 t3lib_div::quoteJSvalue("Hello world!", true)
1018 );
1019 }
1020
1021 /**
1022 * @test
1023 */
1024 public function quoteJSvalueEscapesSingleQuote() {
1025 $this->assertEquals(
1026 "'\\''",
1027 t3lib_div::quoteJSvalue("'", true)
1028 );
1029 }
1030
1031 /**
1032 * @test
1033 */
1034 public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
1035 $this->assertEquals(
1036 "'\\\"'",
1037 t3lib_div::quoteJSvalue('"', true)
1038 );
1039 }
1040
1041 /**
1042 * @test
1043 */
1044 public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
1045 $this->assertEquals(
1046 "'\\&quot;'",
1047 t3lib_div::quoteJSvalue('"', false)
1048 );
1049 }
1050
1051 /**
1052 * @test
1053 */
1054 public function quoteJSvalueEscapesTab() {
1055 $this->assertEquals(
1056 "'" . '\t' . "'",
1057 t3lib_div::quoteJSvalue(TAB)
1058 );
1059 }
1060
1061 /**
1062 * @test
1063 */
1064 public function quoteJSvalueEscapesLinefeed() {
1065 $this->assertEquals(
1066 "'" . '\n' . "'",
1067 t3lib_div::quoteJSvalue(LF)
1068 );
1069 }
1070
1071 /**
1072 * @test
1073 */
1074 public function quoteJSvalueEscapesCarriageReturn() {
1075 $this->assertEquals(
1076 "'" . '\r' . "'",
1077 t3lib_div::quoteJSvalue(CR)
1078 );
1079 }
1080
1081 /**
1082 * @test
1083 */
1084 public function quoteJSvalueEscapesBackslah() {
1085 $this->assertEquals(
1086 "'\\\\'",
1087 t3lib_div::quoteJSvalue('\\')
1088 );
1089 }
1090
1091 /**
1092 * Tests the locallangXMLOverride feature of readLLfile()
1093 * @test
1094 */
1095 public function readLLfileLocallangXMLOverride() {
1096 $unique = uniqid('locallangXMLOverrideTest');
1097
1098 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
1099 <T3locallang>
1100 <data type="array">
1101 <languageKey index="default" type="array">
1102 <label index="buttons.logout">EXIT</label>
1103 </languageKey>
1104 </data>
1105 </T3locallang>';
1106
1107 $file = PATH_site . 'typo3temp/' . $unique . '.xml';
1108 t3lib_div::writeFileToTypo3tempDir($file, $xml);
1109
1110 // get default value
1111 $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1112
1113 // set override file
1114 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
1115
1116 // get override value
1117 $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1118
1119 $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
1120 $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
1121 $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
1122
1123 unlink($file);
1124 }
1125
1126
1127 ///////////////////////////////
1128 // Tests concerning _GETset()
1129 ///////////////////////////////
1130
1131 /**
1132 * @test
1133 */
1134 public function getSetCanSetWholeArray() {
1135 $_GET = array();
1136 $GLOBALS['HTTP_GET_VARS'] = array();
1137 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
1138
1139 $this->assertEquals(
1140 array('oneKey' => 'oneValue'),
1141 $_GET
1142 );
1143 $this->assertEquals(
1144 array('oneKey' => 'oneValue'),
1145 $GLOBALS['HTTP_GET_VARS']
1146 );
1147 }
1148
1149 /**
1150 * @test
1151 */
1152 public function getSetForArrayDropsExistingValues() {
1153 $_GET = array();
1154 $GLOBALS['HTTP_GET_VARS'] = array();
1155
1156 t3lib_div::_GETset(array('foo' => 'bar'));
1157 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
1158
1159 $this->assertEquals(
1160 array('oneKey' => 'oneValue'),
1161 $_GET
1162 );
1163 $this->assertEquals(
1164 array('oneKey' => 'oneValue'),
1165 $GLOBALS['HTTP_GET_VARS']
1166 );
1167 }
1168
1169 /**
1170 * @test
1171 */
1172 public function getSetCanAssignOneValueToOneKey() {
1173 $_GET = array();
1174 $GLOBALS['HTTP_GET_VARS'] = array();
1175
1176 t3lib_div::_GETset('oneValue', 'oneKey');
1177
1178 $this->assertEquals(
1179 'oneValue',
1180 $_GET['oneKey']
1181 );
1182 $this->assertEquals(
1183 'oneValue',
1184 $GLOBALS['HTTP_GET_VARS']['oneKey']
1185 );
1186 }
1187
1188 /**
1189 * @test
1190 */
1191 public function getSetForOneValueNotDropsExistingValues() {
1192 $_GET = array();
1193 $GLOBALS['HTTP_GET_VARS'] = array();
1194
1195 t3lib_div::_GETset(array('foo' => 'bar'));
1196 t3lib_div::_GETset('oneValue', 'oneKey');
1197
1198 $this->assertEquals(
1199 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1200 $_GET
1201 );
1202 $this->assertEquals(
1203 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1204 $GLOBALS['HTTP_GET_VARS']
1205 );
1206 }
1207
1208 /**
1209 * @test
1210 */
1211 public function getSetCanAssignAnArrayToSpecificArrayElement() {
1212 $_GET = array();
1213 $GLOBALS['HTTP_GET_VARS'] = array();
1214
1215 t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
1216
1217 $this->assertEquals(
1218 array('parentKey' => array('childKey' => 'oneValue')),
1219 $_GET
1220 );
1221 $this->assertEquals(
1222 array('parentKey' => array('childKey' => 'oneValue')),
1223 $GLOBALS['HTTP_GET_VARS']
1224 );
1225 }
1226
1227 /**
1228 * @test
1229 */
1230 public function getSetCanAssignAValueToSpecificArrayChildElement() {
1231 $_GET = array();
1232 $GLOBALS['HTTP_GET_VARS'] = array();
1233
1234 t3lib_div::_GETset('oneValue', 'parentKey|childKey');
1235
1236 $this->assertEquals(
1237 array('parentKey' => array('childKey' => 'oneValue')),
1238 $_GET
1239 );
1240 $this->assertEquals(
1241 array('parentKey' => array('childKey' => 'oneValue')),
1242 $GLOBALS['HTTP_GET_VARS']
1243 );
1244 }
1245
1246 /**
1247 * @test
1248 */
1249 public function getSetCanAssignAnArrayToSpecificArrayChildElement() {
1250 $_GET = array();
1251 $GLOBALS['HTTP_GET_VARS'] = array();
1252
1253 t3lib_div::_GETset(
1254 array('key1' => 'value1', 'key2' => 'value2'),
1255 'parentKey|childKey'
1256 );
1257
1258 $this->assertEquals(
1259 array(
1260 'parentKey' => array(
1261 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1262 )
1263 ),
1264 $_GET
1265 );
1266 $this->assertEquals(
1267 array(
1268 'parentKey' => array(
1269 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1270 )
1271 ),
1272 $GLOBALS['HTTP_GET_VARS']
1273 );
1274 }
1275
1276 /**
1277 * Checks if t3lib_div::fixPermissions() correctly sets permissions to single file
1278 * This test assumes directory 'PATH_site'/typo3temp exists
1279 * This test is not available on windows OS
1280 *
1281 * @test
1282 * @see t3lib_div::fixPermissions()
1283 */
1284 public function checkFixPermissionsCorrectlySetsPermissionsToFile() {
1285 if (TYPO3_OS == 'WIN') {
1286 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1287 }
1288
1289 // Create and prepare test file
1290 $filename = PATH_site . 'typo3temp/' . uniqid('test_');
1291 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1292 chmod($filename, 0742);
1293
1294 // Set target permissions and run method
1295 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1296 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1297 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1298
1299 // Get actual permissions and clean up
1300 clearstatcache();
1301 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1302 $resultFileGroup = filegroup($filename);
1303 unlink($filename);
1304
1305 // Test if everything was ok
1306 $this->assertTrue($fixPermissionsResult);
1307 $this->assertEquals($resultFilePermissions, '0660');
1308 $this->assertEquals($resultFileGroup, posix_getegid());
1309 }
1310
1311 /**
1312 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden file
1313 * This test assumes directory 'PATH_site'/typo3temp exists
1314 * This test is not available on windows OS
1315 *
1316 * @test
1317 * @see t3lib_div::fixPermissions()
1318 */
1319 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenFile() {
1320 if (TYPO3_OS == 'WIN') {
1321 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1322 }
1323
1324 // Create and prepare test file
1325 $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
1326 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1327 chmod($filename, 0742);
1328
1329 // Set target permissions and run method
1330 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1331 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1332 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1333
1334 // Get actual permissions and clean up
1335 clearstatcache();
1336 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1337 $resultFileGroup = filegroup($filename);
1338 unlink($filename);
1339
1340 // Test if everything was ok
1341 $this->assertTrue($fixPermissionsResult);
1342 $this->assertEquals($resultFilePermissions, '0660');
1343 $this->assertEquals($resultFileGroup, posix_getegid());
1344 }
1345
1346 /**
1347 * Checks if t3lib_div::fixPermissions() correctly sets permissions to directory with trailing slash
1348 * This test assumes directory 'PATH_site'/typo3temp exists
1349 * This test is not available on windows OS
1350 *
1351 * @test
1352 * @see t3lib_div::fixPermissions()
1353 */
1354 public function checkFixPermissionsCorrectlySetsPermissionsToDirectory() {
1355 if (TYPO3_OS == 'WIN') {
1356 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1357 }
1358
1359 // Create and prepare test directory
1360 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1361 t3lib_div::mkdir($directory);
1362 chmod($directory, 1551);
1363
1364 // Set target permissions and run method
1365 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1366 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1367 $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
1368
1369 // Get actual permissions and clean up
1370 clearstatcache();
1371 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1372 $resultDirectoryGroup = filegroup($directory);
1373 t3lib_div::rmdir($directory);
1374
1375 // Test if everything was ok
1376 $this->assertTrue($fixPermissionsResult);
1377 $this->assertEquals($resultDirectoryPermissions, '0770');
1378 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1379 }
1380
1381 /**
1382 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden directory
1383 * This test assumes directory 'PATH_site'/typo3temp exists
1384 * This test is not available on windows OS
1385 *
1386 * @test
1387 * @see t3lib_div::fixPermissions()
1388 */
1389 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
1390 if (TYPO3_OS == 'WIN') {
1391 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1392 }
1393
1394 // Create and prepare test directory
1395 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1396 t3lib_div::mkdir($directory);
1397 chmod($directory, 1551);
1398
1399 // Set target permissions and run method
1400 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1401 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1402 $fixPermissionsResult = t3lib_div::fixPermissions($directory);
1403
1404 // Get actual permissions and clean up
1405 clearstatcache();
1406 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1407 $resultDirectoryGroup = filegroup($directory);
1408 t3lib_div::rmdir($directory);
1409
1410 // Test if everything was ok
1411 $this->assertTrue($fixPermissionsResult);
1412 $this->assertEquals($resultDirectoryPermissions, '0770');
1413 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1414 }
1415
1416 /**
1417 * Checks if t3lib_div::fixPermissions() correctly sets permissions recursivly
1418 * This test assumes directory 'PATH_site'/typo3temp exists
1419 * This test is not available on windows OS
1420 *
1421 * @test
1422 * @see t3lib_div::fixPermissions()
1423 */
1424 public function checkFixPermissionsCorrectlySetsPermissionsRecursive() {
1425 if (TYPO3_OS == 'WIN') {
1426 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1427 }
1428
1429 // Create and prepare test directory and file structure
1430 $baseDirectory = PATH_site . 'typo3temp/' . uniqid('test_');
1431 t3lib_div::mkdir($baseDirectory);
1432 chmod($baseDirectory, 1751);
1433 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
1434 chmod($baseDirectory . '/file', 0742);
1435 t3lib_div::mkdir($baseDirectory . '/foo');
1436 chmod($baseDirectory . '/foo', 1751);
1437 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
1438 chmod($baseDirectory . '/foo/file', 0742);
1439 t3lib_div::mkdir($baseDirectory . '/.bar');
1440 chmod($baseDirectory . '/.bar', 1751);
1441 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
1442 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
1443 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
1444 touch($baseDirectory . '/.bar/.file', '42');
1445 chmod($baseDirectory . '/.bar/.file', 0742);
1446 touch($baseDirectory . '/.bar/..file2', '42');
1447 chmod($baseDirectory . '/.bar/..file2', 0742);
1448
1449 // Set target permissions and run method
1450 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1451 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1452 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1453 $fixPermissionsResult = t3lib_div::fixPermissions($baseDirectory, TRUE);
1454
1455 // Get actual permissions
1456 clearstatcache();
1457 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
1458 $resultBaseDirectoryGroup = filegroup($baseDirectory);
1459 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
1460 $resultBaseFileGroup = filegroup($baseDirectory . '/file');
1461 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
1462 $resultFooDirectoryGroup = filegroup($baseDirectory . '/foo');
1463 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
1464 $resultFooFileGroup = filegroup($baseDirectory . '/foo/file');
1465 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
1466 $resultBarDirectoryGroup = filegroup($baseDirectory . '/.bar');
1467 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
1468 $resultBarFileGroup = filegroup($baseDirectory . '/.bar/.file');
1469 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
1470 $resultBarFile2Group = filegroup($baseDirectory . '/.bar/..file2');
1471
1472 // Clean up
1473 unlink($baseDirectory . '/file');
1474 unlink($baseDirectory . '/foo/file');
1475 unlink($baseDirectory . '/.bar/.file');
1476 unlink($baseDirectory . '/.bar/..file2');
1477 t3lib_div::rmdir($baseDirectory . '/foo');
1478 t3lib_div::rmdir($baseDirectory . '/.bar');
1479 t3lib_div::rmdir($baseDirectory);
1480
1481 // Test if everything was ok
1482 $this->assertTrue($fixPermissionsResult);
1483 $this->assertEquals($resultBaseDirectoryPermissions, '0770');
1484 $this->assertEquals($resultBaseDirectoryGroup, posix_getegid());
1485 $this->assertEquals($resultBaseFilePermissions, '0660');
1486 $this->assertEquals($resultBaseFileGroup, posix_getegid());
1487 $this->assertEquals($resultFooDirectoryPermissions, '0770');
1488 $this->assertEquals($resultFooDirectoryGroup, posix_getegid());
1489 $this->assertEquals($resultFooFilePermissions, '0660');
1490 $this->assertEquals($resultFooFileGroup, posix_getegid());
1491 $this->assertEquals($resultBarDirectoryPermissions, '0770');
1492 $this->assertEquals($resultBarDirectoryGroup, posix_getegid());
1493 $this->assertEquals($resultBarFilePermissions, '0660');
1494 $this->assertEquals($resultBarFileGroup, posix_getegid());
1495 $this->assertEquals($resultBarFile2Permissions, '0660');
1496 $this->assertEquals($resultBarFile2Group, posix_getegid());
1497 }
1498
1499 /**
1500 * Checks if t3lib_div::fixPermissions() does not fix permissions on not allowed path
1501 * This test assumes directory 'PATH_site'/typo3temp exists
1502 * This test is not available on windows OS
1503 *
1504 * @test
1505 * @see t3lib_div::fixPermissions()
1506 */
1507 public function checkFixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
1508 if (TYPO3_OS == 'WIN') {
1509 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1510 }
1511
1512 // Create and prepare test file
1513 $filename = PATH_site . 'typo3temp/../typo3temp/' . uniqid('test_');
1514 touch($filename);
1515 chmod($filename, 0742);
1516
1517 // Set target permissions and run method
1518 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1519 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1520
1521 // Get actual permissions and clean up
1522 clearstatcache();
1523 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1524 unlink($filename);
1525
1526 // Test if everything was ok
1527 $this->assertFalse($fixPermissionsResult);
1528 $this->assertEquals($resultFilePermissions, '0742');
1529 }
1530
1531 /**
1532 * Checks if t3lib_div::mkdir() correctly creates a directory
1533 * This test assumes directory 'PATH_site'/typo3temp exists
1534 *
1535 * @test
1536 * @see t3lib_div::mkdir()
1537 */
1538 public function checkMkdirCorrectlyCreatesDirectory() {
1539 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1540 $mkdirResult = t3lib_div::mkdir($directory);
1541 $directoryCreated = is_dir($directory);
1542 t3lib_div::rmdir($directory);
1543 $this->assertTrue($mkdirResult);
1544 $this->assertTrue($directoryCreated);
1545 }
1546
1547 /**
1548 * Checks if t3lib_div::mkdir() correctly creates a hidden directory
1549 * This test assumes directory 'PATH_site'/typo3temp exists
1550 *
1551 * @test
1552 * @see t3lib_div::mkdir()
1553 */
1554 public function checkMkdirCorrectlyCreatesHiddenDirectory() {
1555 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1556 $mkdirResult = t3lib_div::mkdir($directory);
1557 $directoryCreated = is_dir($directory);
1558 t3lib_div::rmdir($directory);
1559 $this->assertTrue($mkdirResult);
1560 $this->assertTrue($directoryCreated);
1561 }
1562
1563 /**
1564 * Checks if t3lib_div::mkdir() correctly creates a directory with trailing slash
1565 * This test assumes directory 'PATH_site'/typo3temp exists
1566 *
1567 * @test
1568 * @see t3lib_div::mkdir()
1569 */
1570 public function checkMkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
1571 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1572 $mkdirResult = t3lib_div::mkdir($directory);
1573 $directoryCreated = is_dir($directory);
1574 t3lib_div::rmdir($directory);
1575 $this->assertTrue($mkdirResult);
1576 $this->assertTrue($directoryCreated);
1577 }
1578
1579 /**
1580 * Checks if t3lib_div::split_fileref() return NO file extension if incomming $fileref is a folder
1581 * This test avoid bug #0014845: Filelist module reports "type" of files also for directories
1582 * This test assumes directory 'PATH_site'/typo3temp exists
1583 *
1584 * @test
1585 * @see t3lib_div::split_fileref()
1586 */
1587 public function checkIfSplitFileRefReturnsFileTypeNotForFolders(){
1588 $directoryName = uniqid('test_') . '.com';
1589 $directoryPath = PATH_site . 'typo3temp/';
1590 $directory = $directoryPath . $directoryName;
1591 mkdir($directory, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
1592
1593 $fileInfo = t3lib_div::split_fileref($directory);
1594
1595 $directoryCreated = is_dir($directory);
1596 $this->assertTrue($directoryCreated);
1597
1598 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
1599 $this->assertEquals($directoryPath, $fileInfo['path']);
1600 $this->assertEquals($directoryName, $fileInfo['file']);
1601 $this->assertEquals($directoryName, $fileInfo['filebody']);
1602 $this->assertEquals('', $fileInfo['fileext']);
1603 $this->assertArrayNotHasKey('realFileext', $fileInfo);
1604
1605 rmdir($directory);
1606 }
1607
1608 /**
1609 * @test
1610 * @see t3lib_div::split_fileref()
1611 */
1612 public function checkIfSplitFileRefReturnsFileTypeForFilesWithoutPathSite() {
1613 $testFile = 'fileadmin/media/someFile.png';
1614
1615 $fileInfo = t3lib_div::split_fileref($testFile);
1616 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
1617 $this->assertEquals('fileadmin/media/', $fileInfo['path']);
1618 $this->assertEquals('someFile.png', $fileInfo['file']);
1619 $this->assertEquals('someFile', $fileInfo['filebody']);
1620 $this->assertEquals('png', $fileInfo['fileext']);
1621 }
1622 }
1623 ?>