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