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