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