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