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