2ecc2a66050c1d686c9676a47e539f298822ceed
[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 return array(
657 array('alt_intro.php'),
658 array('alt_intro.php?foo=1&bar=2'),
659 array('/typo3/alt_intro.php'),
660 array('/index.php'),
661 array('../index.php'),
662 array('../typo3/alt_intro.php'),
663 array('../~userDirectory/index.php'),
664 array('../typo3/mod.php?var1=test-case&var2=~user'),
665 array(PATH_site . 'typo3/alt_intro.php'),
666 array(t3lib_div::getIndpEnv('TYPO3_SITE_URL') . 'typo3/alt_intro.php'),
667 array(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '/index.php'),
668 );
669 }
670
671 /**
672 * Data provider for invalid URLs.
673 * @see sanitizeLocalUrlDeniesInvalidUrls
674 */
675 public function invalidLocalUrlDataProvider() {
676 return array(
677 array(''),
678 array('http://www.google.de/'),
679 array('https://www.google.de/'),
680 array('../typo3/whatever.php?argument=javascript:alert(0)'),
681 );
682 }
683
684 /**
685 * Tests whether valid local URLs are handled correctly.
686 * @dataProvider validLocalUrlDataProvider
687 * @test
688 */
689 public function sanitizeLocalUrlAcceptsPlainValidUrls($url) {
690 $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
691 }
692
693 /**
694 * Tests whether valid local URLs are handled correctly.
695 * @dataProvider validLocalUrlDataProvider
696 * @test
697 */
698 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
699 $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
700 }
701
702 /**
703 * Tests whether valid local URLs are handled correctly.
704 * @dataProvider invalidLocalUrlDataProvider
705 * @test
706 */
707 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
708 $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
709 }
710
711 /**
712 * Tests whether valid local URLs are handled correctly.
713 * @dataProvider invalidLocalUrlDataProvider
714 * @test
715 */
716 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
717 $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
718 }
719
720 //////////////////////////////////////
721 // Tests concerning arrayDiffAssocRecursive
722 //////////////////////////////////////
723
724 /**
725 * Test if a one dimensional array is correctly diffed.
726 *
727 * @test
728 * @see t3lib_div::arrayDiffAssocRecursive
729 */
730 public function doesArrayDiffAssocRecursiveCorrectlyHandleOneDimensionalArrays() {
731 $array1 = array(
732 'key1' => 'value1',
733 'key2' => 'value2',
734 'key3' => 'value3',
735 );
736 $array2 = array(
737 'key1' => 'value1',
738 'key3' => 'value3',
739 );
740 $expectedResult = array(
741 'key2' => 'value2',
742 );
743 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
744 $this->assertEquals($expectedResult, $actualResult);
745 }
746
747 /**
748 * Test if a three dimensional array is correctly diffed.
749 *
750 * @test
751 * @see t3lib_div::arrayDiffAssocRecursive
752 */
753 public function doesArrayDiffAssocRecursiveCorrectlyHandleMultiDimensionalArrays() {
754 $array1 = array(
755 'key1' => 'value1',
756 'key2' => array(
757 'key21' => 'value21',
758 'key22' => 'value22',
759 'key23' => array(
760 'key231' => 'value231',
761 'key232' => 'value232',
762 ),
763 ),
764 );
765 $array2 = array(
766 'key1' => 'value1',
767 'key2' => array(
768 'key21' => 'value21',
769 'key23' => array(
770 'key231' => 'value231',
771 ),
772 ),
773 );
774 $expectedResult = array(
775 'key2' => array(
776 'key22' => 'value22',
777 'key23' => array(
778 'key232' => 'value232',
779 ),
780 ),
781 );
782 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
783 $this->assertEquals($expectedResult, $actualResult);
784 }
785
786 /**
787 * Test if arrays are correctly diffed if types are different.
788 *
789 * @test
790 * @see t3lib_div::arrayDiffAssocRecursive
791 */
792 public function doesArrayDiffAssocRecursiveCorrectlyHandleMixedArrays() {
793 $array1 = array(
794 'key1' => array(
795 'key11' => 'value11',
796 'key12' => 'value12',
797 ),
798 'key2' => 'value2',
799 'key3' => 'value3',
800 );
801 $array2 = array(
802 'key1' => 'value1',
803 'key2' => array(
804 'key21' => 'value21',
805 ),
806 );
807 $expectedResult = array(
808 'key3' => 'value3',
809 );
810 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
811 $this->assertEquals($expectedResult, $actualResult);
812 }
813
814 //////////////////////////////////////
815 // Tests concerning removeDotsFromTS
816 //////////////////////////////////////
817
818 /**
819 * Tests whether removeDotsFromTS() behaves correctly.
820 * @test
821 * @see t3lib_div::removeDotsFromTS()
822 */
823 public function doesRemoveDotsFromTypoScriptSucceed() {
824 $typoScript = array(
825 'propertyA.' => array(
826 'keyA.' => array(
827 'valueA' => 1,
828 ),
829 'keyB' => 2,
830 ),
831 'propertyB' => 3,
832 );
833
834 $expectedResult = array(
835 'propertyA' => array(
836 'keyA' => array(
837 'valueA' => 1,
838 ),
839 'keyB' => 2,
840 ),
841 'propertyB' => 3,
842 );
843
844 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
845 }
846
847 /**
848 * Tests whether removeDotsFromTS() behaves correctly.
849 * @test
850 * @see t3lib_div::removeDotsFromTS()
851 */
852 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithArray() {
853 $typoScript = array(
854 'propertyA.' => array(
855 'keyA' => 'getsOverridden',
856 'keyA.' => array(
857 'valueA' => 1,
858 ),
859 'keyB' => 2,
860 ),
861 'propertyB' => 3,
862 );
863
864 $expectedResult = array(
865 'propertyA' => array(
866 'keyA' => array(
867 'valueA' => 1,
868 ),
869 'keyB' => 2,
870 ),
871 'propertyB' => 3,
872 );
873
874 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
875 }
876
877 /**
878 * Tests whether removeDotsFromTS() behaves correctly.
879 * @test
880 * @see t3lib_div::removeDotsFromTS()
881 */
882 public function doesRemoveDotsFromTypoScriptCorrectlyOverrideWithScalar() {
883 $typoScript = array(
884 'propertyA.' => array(
885 'keyA.' => array(
886 'valueA' => 1,
887 ),
888 'keyA' => 'willOverride',
889 'keyB' => 2,
890 ),
891 'propertyB' => 3,
892 );
893
894 $expectedResult = array(
895 'propertyA' => array(
896 'keyA' => 'willOverride',
897 'keyB' => 2,
898 ),
899 'propertyB' => 3,
900 );
901
902 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
903 }
904
905 /**
906 * Tests whether getDirs() returns an array of diretories from a given path
907 * @test
908 * @see t3lib_div::getDirs($path)
909 */
910 public function checkGetDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
911 $path = PATH_t3lib;
912 $directories = t3lib_div::get_dirs($path);
913
914 $this->assertType('array', $directories);
915 }
916
917 /**
918 * Tests whether getDirs() returns the string 'error' in case of problems reading from the given path
919 * @test
920 * @see t3lib_div::getDirs($path)
921 */
922 public function checkGetDirsReturnsStringErrorOnPathFailure() {
923 $path = 'foo';
924 $result = t3lib_div::get_dirs($path);
925 $expectedResult = 'error';
926
927 $this->assertEquals($expectedResult, $result);
928 }
929
930
931 //////////////////////////////////
932 // Tests concerning hmac
933 //////////////////////////////////
934
935 /**
936 * @test
937 */
938 public function hmacReturnsHashOfProperLength() {
939 $hmac = t3lib_div::hmac('message');
940 $this->assertTrue(!empty($hmac) && is_string($hmac));
941 $this->assertTrue(strlen($hmac) == 40);
942 }
943
944 /**
945 * @test
946 */
947 public function hmacReturnsEqualHashesForEqualInput() {
948 $msg0 = 'message';
949 $msg1 = 'message';
950 $this->assertEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
951 }
952
953 /**
954 * @test
955 */
956 public function hmacReturnsNotEqualHashesForNotEqualInput() {
957 $msg0 = 'message0';
958 $msg1 = 'message1';
959 $this->assertNotEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
960 }
961
962
963 //////////////////////////////////
964 // Tests concerning quoteJSvalue
965 //////////////////////////////////
966
967 /**
968 * @test
969 */
970 public function quoteJSvalueHtmlspecialcharsDataByDefault() {
971 $this->assertContains(
972 '&gt;',
973 t3lib_div::quoteJSvalue('>')
974 );
975 }
976
977 /**
978 * @test
979 */
980 public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
981 $this->assertContains(
982 '&gt;',
983 t3lib_div::quoteJSvalue('>', false)
984 );
985 }
986
987 /**
988 * @test
989 */
990 public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
991 $this->assertContains(
992 '>',
993 t3lib_div::quoteJSvalue('>', true)
994 );
995 }
996
997 /**
998 * @test
999 */
1000 public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
1001 $this->assertEquals(
1002 "''",
1003 t3lib_div::quoteJSvalue("", true)
1004 );
1005 }
1006
1007 /**
1008 * @test
1009 */
1010 public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
1011 $this->assertEquals(
1012 "'Hello world!'",
1013 t3lib_div::quoteJSvalue("Hello world!", true)
1014 );
1015 }
1016
1017 /**
1018 * @test
1019 */
1020 public function quoteJSvalueEscapesSingleQuote() {
1021 $this->assertEquals(
1022 "'\\''",
1023 t3lib_div::quoteJSvalue("'", true)
1024 );
1025 }
1026
1027 /**
1028 * @test
1029 */
1030 public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
1031 $this->assertEquals(
1032 "'\\\"'",
1033 t3lib_div::quoteJSvalue('"', true)
1034 );
1035 }
1036
1037 /**
1038 * @test
1039 */
1040 public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
1041 $this->assertEquals(
1042 "'\\&quot;'",
1043 t3lib_div::quoteJSvalue('"', false)
1044 );
1045 }
1046
1047 /**
1048 * @test
1049 */
1050 public function quoteJSvalueEscapesTab() {
1051 $this->assertEquals(
1052 "'" . '\t' . "'",
1053 t3lib_div::quoteJSvalue(TAB)
1054 );
1055 }
1056
1057 /**
1058 * @test
1059 */
1060 public function quoteJSvalueEscapesLinefeed() {
1061 $this->assertEquals(
1062 "'" . '\n' . "'",
1063 t3lib_div::quoteJSvalue(LF)
1064 );
1065 }
1066
1067 /**
1068 * @test
1069 */
1070 public function quoteJSvalueEscapesCarriageReturn() {
1071 $this->assertEquals(
1072 "'" . '\r' . "'",
1073 t3lib_div::quoteJSvalue(CR)
1074 );
1075 }
1076
1077 /**
1078 * @test
1079 */
1080 public function quoteJSvalueEscapesBackslah() {
1081 $this->assertEquals(
1082 "'\\\\'",
1083 t3lib_div::quoteJSvalue('\\')
1084 );
1085 }
1086
1087 /**
1088 * Tests the locallangXMLOverride feature of readLLfile()
1089 * @test
1090 */
1091 public function readLLfileLocallangXMLOverride() {
1092 $unique = uniqid('locallangXMLOverrideTest');
1093
1094 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
1095 <T3locallang>
1096 <data type="array">
1097 <languageKey index="default" type="array">
1098 <label index="buttons.logout">EXIT</label>
1099 </languageKey>
1100 </data>
1101 </T3locallang>';
1102
1103 $file = PATH_site . 'typo3temp/' . $unique . '.xml';
1104 t3lib_div::writeFileToTypo3tempDir($file, $xml);
1105
1106 // get default value
1107 $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1108
1109 // set override file
1110 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
1111
1112 // get override value
1113 $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1114
1115 $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
1116 $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
1117 $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
1118
1119 unlink($file);
1120 }
1121
1122
1123 ///////////////////////////////
1124 // Tests concerning _GETset()
1125 ///////////////////////////////
1126
1127 /**
1128 * @test
1129 */
1130 public function getSetCanSetWholeArray() {
1131 $_GET = array();
1132 $GLOBALS['HTTP_GET_VARS'] = array();
1133 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
1134
1135 $this->assertEquals(
1136 array('oneKey' => 'oneValue'),
1137 $_GET
1138 );
1139 $this->assertEquals(
1140 array('oneKey' => 'oneValue'),
1141 $GLOBALS['HTTP_GET_VARS']
1142 );
1143 }
1144
1145 /**
1146 * @test
1147 */
1148 public function getSetForArrayDropsExistingValues() {
1149 $_GET = array();
1150 $GLOBALS['HTTP_GET_VARS'] = array();
1151
1152 t3lib_div::_GETset(array('foo' => 'bar'));
1153 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
1154
1155 $this->assertEquals(
1156 array('oneKey' => 'oneValue'),
1157 $_GET
1158 );
1159 $this->assertEquals(
1160 array('oneKey' => 'oneValue'),
1161 $GLOBALS['HTTP_GET_VARS']
1162 );
1163 }
1164
1165 /**
1166 * @test
1167 */
1168 public function getSetCanAssignOneValueToOneKey() {
1169 $_GET = array();
1170 $GLOBALS['HTTP_GET_VARS'] = array();
1171
1172 t3lib_div::_GETset('oneValue', 'oneKey');
1173
1174 $this->assertEquals(
1175 'oneValue',
1176 $_GET['oneKey']
1177 );
1178 $this->assertEquals(
1179 'oneValue',
1180 $GLOBALS['HTTP_GET_VARS']['oneKey']
1181 );
1182 }
1183
1184 /**
1185 * @test
1186 */
1187 public function getSetForOneValueNotDropsExistingValues() {
1188 $_GET = array();
1189 $GLOBALS['HTTP_GET_VARS'] = array();
1190
1191 t3lib_div::_GETset(array('foo' => 'bar'));
1192 t3lib_div::_GETset('oneValue', 'oneKey');
1193
1194 $this->assertEquals(
1195 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1196 $_GET
1197 );
1198 $this->assertEquals(
1199 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1200 $GLOBALS['HTTP_GET_VARS']
1201 );
1202 }
1203
1204 /**
1205 * @test
1206 */
1207 public function getSetCanAssignAnArrayToSpecificArrayElement() {
1208 $_GET = array();
1209 $GLOBALS['HTTP_GET_VARS'] = array();
1210
1211 t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
1212
1213 $this->assertEquals(
1214 array('parentKey' => array('childKey' => 'oneValue')),
1215 $_GET
1216 );
1217 $this->assertEquals(
1218 array('parentKey' => array('childKey' => 'oneValue')),
1219 $GLOBALS['HTTP_GET_VARS']
1220 );
1221 }
1222
1223 /**
1224 * @test
1225 */
1226 public function getSetCanAssignAValueToSpecificArrayChildElement() {
1227 $_GET = array();
1228 $GLOBALS['HTTP_GET_VARS'] = array();
1229
1230 t3lib_div::_GETset('oneValue', 'parentKey|childKey');
1231
1232 $this->assertEquals(
1233 array('parentKey' => array('childKey' => 'oneValue')),
1234 $_GET
1235 );
1236 $this->assertEquals(
1237 array('parentKey' => array('childKey' => 'oneValue')),
1238 $GLOBALS['HTTP_GET_VARS']
1239 );
1240 }
1241
1242 /**
1243 * @test
1244 */
1245 public function getSetCanAssignAnArrayToSpecificArrayChildElement() {
1246 $_GET = array();
1247 $GLOBALS['HTTP_GET_VARS'] = array();
1248
1249 t3lib_div::_GETset(
1250 array('key1' => 'value1', 'key2' => 'value2'),
1251 'parentKey|childKey'
1252 );
1253
1254 $this->assertEquals(
1255 array(
1256 'parentKey' => array(
1257 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1258 )
1259 ),
1260 $_GET
1261 );
1262 $this->assertEquals(
1263 array(
1264 'parentKey' => array(
1265 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1266 )
1267 ),
1268 $GLOBALS['HTTP_GET_VARS']
1269 );
1270 }
1271
1272 /**
1273 * Checks if t3lib_div::fixPermissions() correctly sets permissions to single file
1274 * This test assumes directory 'PATH_site'/typo3temp exists
1275 * This test is not available on windows OS
1276 *
1277 * @test
1278 * @see t3lib_div::fixPermissions()
1279 */
1280 public function checkFixPermissionsCorrectlySetsPermissionsToFile() {
1281 if (TYPO3_OS == 'WIN') {
1282 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1283 }
1284
1285 // Create and prepare test file
1286 $filename = PATH_site . 'typo3temp/' . uniqid('test_');
1287 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1288 chmod($filename, 0742);
1289
1290 // Set target permissions and run method
1291 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1292 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1293 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1294
1295 // Get actual permissions and clean up
1296 clearstatcache();
1297 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1298 $resultFileGroup = filegroup($filename);
1299 unlink($filename);
1300
1301 // Test if everything was ok
1302 $this->assertTrue($fixPermissionsResult);
1303 $this->assertEquals($resultFilePermissions, '0660');
1304 $this->assertEquals($resultFileGroup, posix_getegid());
1305 }
1306
1307 /**
1308 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden file
1309 * This test assumes directory 'PATH_site'/typo3temp exists
1310 * This test is not available on windows OS
1311 *
1312 * @test
1313 * @see t3lib_div::fixPermissions()
1314 */
1315 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenFile() {
1316 if (TYPO3_OS == 'WIN') {
1317 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1318 }
1319
1320 // Create and prepare test file
1321 $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
1322 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1323 chmod($filename, 0742);
1324
1325 // Set target permissions and run method
1326 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1327 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1328 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1329
1330 // Get actual permissions and clean up
1331 clearstatcache();
1332 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1333 $resultFileGroup = filegroup($filename);
1334 unlink($filename);
1335
1336 // Test if everything was ok
1337 $this->assertTrue($fixPermissionsResult);
1338 $this->assertEquals($resultFilePermissions, '0660');
1339 $this->assertEquals($resultFileGroup, posix_getegid());
1340 }
1341
1342 /**
1343 * Checks if t3lib_div::fixPermissions() correctly sets permissions to directory with trailing slash
1344 * This test assumes directory 'PATH_site'/typo3temp exists
1345 * This test is not available on windows OS
1346 *
1347 * @test
1348 * @see t3lib_div::fixPermissions()
1349 */
1350 public function checkFixPermissionsCorrectlySetsPermissionsToDirectory() {
1351 if (TYPO3_OS == 'WIN') {
1352 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1353 }
1354
1355 // Create and prepare test directory
1356 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1357 t3lib_div::mkdir($directory);
1358 chmod($directory, 1551);
1359
1360 // Set target permissions and run method
1361 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1362 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1363 $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
1364
1365 // Get actual permissions and clean up
1366 clearstatcache();
1367 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1368 $resultDirectoryGroup = filegroup($directory);
1369 t3lib_div::rmdir($directory);
1370
1371 // Test if everything was ok
1372 $this->assertTrue($fixPermissionsResult);
1373 $this->assertEquals($resultDirectoryPermissions, '0770');
1374 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1375 }
1376
1377 /**
1378 * Checks if t3lib_div::fixPermissions() correctly sets permissions to hidden directory
1379 * This test assumes directory 'PATH_site'/typo3temp exists
1380 * This test is not available on windows OS
1381 *
1382 * @test
1383 * @see t3lib_div::fixPermissions()
1384 */
1385 public function checkFixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
1386 if (TYPO3_OS == 'WIN') {
1387 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1388 }
1389
1390 // Create and prepare test directory
1391 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1392 t3lib_div::mkdir($directory);
1393 chmod($directory, 1551);
1394
1395 // Set target permissions and run method
1396 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1397 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1398 $fixPermissionsResult = t3lib_div::fixPermissions($directory);
1399
1400 // Get actual permissions and clean up
1401 clearstatcache();
1402 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1403 $resultDirectoryGroup = filegroup($directory);
1404 t3lib_div::rmdir($directory);
1405
1406 // Test if everything was ok
1407 $this->assertTrue($fixPermissionsResult);
1408 $this->assertEquals($resultDirectoryPermissions, '0770');
1409 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1410 }
1411
1412 /**
1413 * Checks if t3lib_div::fixPermissions() correctly sets permissions recursivly
1414 * This test assumes directory 'PATH_site'/typo3temp exists
1415 * This test is not available on windows OS
1416 *
1417 * @test
1418 * @see t3lib_div::fixPermissions()
1419 */
1420 public function checkFixPermissionsCorrectlySetsPermissionsRecursive() {
1421 if (TYPO3_OS == 'WIN') {
1422 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1423 }
1424
1425 // Create and prepare test directory and file structure
1426 $baseDirectory = PATH_site . 'typo3temp/' . uniqid('test_');
1427 t3lib_div::mkdir($baseDirectory);
1428 chmod($baseDirectory, 1751);
1429 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
1430 chmod($baseDirectory . '/file', 0742);
1431 t3lib_div::mkdir($baseDirectory . '/foo');
1432 chmod($baseDirectory . '/foo', 1751);
1433 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
1434 chmod($baseDirectory . '/foo/file', 0742);
1435 t3lib_div::mkdir($baseDirectory . '/.bar');
1436 chmod($baseDirectory . '/.bar', 1751);
1437 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
1438 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
1439 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
1440 touch($baseDirectory . '/.bar/.file', '42');
1441 chmod($baseDirectory . '/.bar/.file', 0742);
1442 touch($baseDirectory . '/.bar/..file2', '42');
1443 chmod($baseDirectory . '/.bar/..file2', 0742);
1444
1445 // Set target permissions and run method
1446 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1447 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1448 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1449 $fixPermissionsResult = t3lib_div::fixPermissions($baseDirectory, TRUE);
1450
1451 // Get actual permissions
1452 clearstatcache();
1453 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
1454 $resultBaseDirectoryGroup = filegroup($baseDirectory);
1455 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
1456 $resultBaseFileGroup = filegroup($baseDirectory . '/file');
1457 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
1458 $resultFooDirectoryGroup = filegroup($baseDirectory . '/foo');
1459 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
1460 $resultFooFileGroup = filegroup($baseDirectory . '/foo/file');
1461 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
1462 $resultBarDirectoryGroup = filegroup($baseDirectory . '/.bar');
1463 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
1464 $resultBarFileGroup = filegroup($baseDirectory . '/.bar/.file');
1465 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
1466 $resultBarFile2Group = filegroup($baseDirectory . '/.bar/..file2');
1467
1468 // Clean up
1469 unlink($baseDirectory . '/file');
1470 unlink($baseDirectory . '/foo/file');
1471 unlink($baseDirectory . '/.bar/.file');
1472 unlink($baseDirectory . '/.bar/..file2');
1473 t3lib_div::rmdir($baseDirectory . '/foo');
1474 t3lib_div::rmdir($baseDirectory . '/.bar');
1475 t3lib_div::rmdir($baseDirectory);
1476
1477 // Test if everything was ok
1478 $this->assertTrue($fixPermissionsResult);
1479 $this->assertEquals($resultBaseDirectoryPermissions, '0770');
1480 $this->assertEquals($resultBaseDirectoryGroup, posix_getegid());
1481 $this->assertEquals($resultBaseFilePermissions, '0660');
1482 $this->assertEquals($resultBaseFileGroup, posix_getegid());
1483 $this->assertEquals($resultFooDirectoryPermissions, '0770');
1484 $this->assertEquals($resultFooDirectoryGroup, posix_getegid());
1485 $this->assertEquals($resultFooFilePermissions, '0660');
1486 $this->assertEquals($resultFooFileGroup, posix_getegid());
1487 $this->assertEquals($resultBarDirectoryPermissions, '0770');
1488 $this->assertEquals($resultBarDirectoryGroup, posix_getegid());
1489 $this->assertEquals($resultBarFilePermissions, '0660');
1490 $this->assertEquals($resultBarFileGroup, posix_getegid());
1491 $this->assertEquals($resultBarFile2Permissions, '0660');
1492 $this->assertEquals($resultBarFile2Group, posix_getegid());
1493 }
1494
1495 /**
1496 * Checks if t3lib_div::fixPermissions() does not fix permissions on not allowed path
1497 * This test assumes directory 'PATH_site'/typo3temp exists
1498 * This test is not available on windows OS
1499 *
1500 * @test
1501 * @see t3lib_div::fixPermissions()
1502 */
1503 public function checkFixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
1504 if (TYPO3_OS == 'WIN') {
1505 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1506 }
1507
1508 // Create and prepare test file
1509 $filename = PATH_site . 'typo3temp/../typo3temp/' . uniqid('test_');
1510 touch($filename);
1511 chmod($filename, 0742);
1512
1513 // Set target permissions and run method
1514 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1515 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1516
1517 // Get actual permissions and clean up
1518 clearstatcache();
1519 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1520 unlink($filename);
1521
1522 // Test if everything was ok
1523 $this->assertFalse($fixPermissionsResult);
1524 $this->assertEquals($resultFilePermissions, '0742');
1525 }
1526
1527 /**
1528 * Checks if t3lib_div::mkdir() correctly creates a directory
1529 * This test assumes directory 'PATH_site'/typo3temp exists
1530 *
1531 * @test
1532 * @see t3lib_div::mkdir()
1533 */
1534 public function checkMkdirCorrectlyCreatesDirectory() {
1535 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1536 $mkdirResult = t3lib_div::mkdir($directory);
1537 $directoryCreated = is_dir($directory);
1538 t3lib_div::rmdir($directory);
1539 $this->assertTrue($mkdirResult);
1540 $this->assertTrue($directoryCreated);
1541 }
1542
1543 /**
1544 * Checks if t3lib_div::mkdir() correctly creates a hidden directory
1545 * This test assumes directory 'PATH_site'/typo3temp exists
1546 *
1547 * @test
1548 * @see t3lib_div::mkdir()
1549 */
1550 public function checkMkdirCorrectlyCreatesHiddenDirectory() {
1551 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1552 $mkdirResult = t3lib_div::mkdir($directory);
1553 $directoryCreated = is_dir($directory);
1554 t3lib_div::rmdir($directory);
1555 $this->assertTrue($mkdirResult);
1556 $this->assertTrue($directoryCreated);
1557 }
1558
1559 /**
1560 * Checks if t3lib_div::mkdir() correctly creates a directory with trailing slash
1561 * This test assumes directory 'PATH_site'/typo3temp exists
1562 *
1563 * @test
1564 * @see t3lib_div::mkdir()
1565 */
1566 public function checkMkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
1567 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1568 $mkdirResult = t3lib_div::mkdir($directory);
1569 $directoryCreated = is_dir($directory);
1570 t3lib_div::rmdir($directory);
1571 $this->assertTrue($mkdirResult);
1572 $this->assertTrue($directoryCreated);
1573 }
1574
1575 /**
1576 * Checks if t3lib_div::split_fileref() return NO file extension if incomming $fileref is a folder
1577 * This test avoid bug #0014845: Filelist module reports "type" of files also for directories
1578 * This test assumes directory 'PATH_site'/typo3temp exists
1579 *
1580 * @test
1581 * @see t3lib_div::split_fileref()
1582 */
1583 public function checkIfSplitFileRefReturnsFileTypeNotForFolders(){
1584 $directoryName = uniqid('test_') . '.com';
1585 $directoryPath = PATH_site . 'typo3temp/';
1586 $directory = $directoryPath . $directoryName;
1587 mkdir($directory, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
1588
1589 $fileInfo = t3lib_div::split_fileref($directory);
1590
1591 $directoryCreated = is_dir($directory);
1592 $this->assertTrue($directoryCreated);
1593
1594 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
1595 $this->assertEquals($directoryPath, $fileInfo['path']);
1596 $this->assertEquals($directoryName, $fileInfo['file']);
1597 $this->assertEquals($directoryName, $fileInfo['filebody']);
1598 $this->assertEquals('', $fileInfo['fileext']);
1599 $this->assertArrayNotHasKey('realFileext', $fileInfo);
1600
1601 rmdir($directory);
1602 }
1603
1604 /**
1605 * @test
1606 * @see t3lib_div::split_fileref()
1607 */
1608 public function checkIfSplitFileRefReturnsFileTypeForFilesWithoutPathSite() {
1609 $testFile = 'fileadmin/media/someFile.png';
1610
1611 $fileInfo = t3lib_div::split_fileref($testFile);
1612 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
1613 $this->assertEquals('fileadmin/media/', $fileInfo['path']);
1614 $this->assertEquals('someFile.png', $fileInfo['file']);
1615 $this->assertEquals('someFile', $fileInfo['filebody']);
1616 $this->assertEquals('png', $fileInfo['fileext']);
1617 }
1618 }
1619 ?>