2 /***************************************************************
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Testcase for class t3lib_div
28 * @author Ingo Renner <ingo@typo3.org>
29 * @author Oliver Klee <typo3-coding@oliverklee.de>
34 class t3lib_divTest
extends tx_phpunit_testcase
{
37 * Enable backup of global and system variables
41 protected $backupGlobals = TRUE;
44 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
45 * because resource types cannot be handled during serializing
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
51 public function tearDown() {
52 t3lib_div
::purgeInstances();
56 ///////////////////////////////
57 // Tests concerning gif_compress
58 ///////////////////////////////
63 public function gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() {
64 if (TYPO3_OS
== 'WIN') {
65 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
68 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] ||
!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
69 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available without imagemagick setup.');
72 $testFinder = t3lib_div
::makeInstance('Tx_Phpunit_Service_TestFinder');
73 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
75 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress'] = TRUE;
77 // Copy file to unique filename in typo3temp, set target permissions and run method
78 $testFilename = PATH_site
. 'typo3temp/' . uniqid('test_') . '.gif';
79 @copy
($fixtureGifFile, $testFilename);
80 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
81 t3lib_div
::gif_compress($testFilename, 'IM');
83 // Get actual permissions and clean up
85 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
86 t3lib_div
::unlink_tempfile($testFilename);
88 $this->assertEquals($resultFilePermissions, '0777');
94 public function gifCompressFixesPermissionOfConvertedFileIfUsingGd() {
95 if (TYPO3_OS
== 'WIN') {
96 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
99 $testFinder = t3lib_div
::makeInstance('Tx_Phpunit_Service_TestFinder');
100 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
102 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'] = TRUE;
103 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] = FALSE;
105 // Copy file to unique filename in typo3temp, set target permissions and run method
106 $testFilename = PATH_site
. 'typo3temp/' . uniqid('test_') . '.gif';
107 @copy
($fixtureGifFile, $testFilename);
108 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
109 t3lib_div
::gif_compress($testFilename, 'GD');
111 // Get actual permissions and clean up
113 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
114 t3lib_div
::unlink_tempfile($testFilename);
116 $this->assertEquals($resultFilePermissions, '0777');
119 ///////////////////////////////
120 // Tests concerning png_to_gif_by_imagemagick
121 ///////////////////////////////
126 public function pngToGifByImagemagickFixesPermissionsOfConvertedFile() {
127 if (TYPO3_OS
== 'WIN') {
128 $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available on Windows.');
131 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] ||
!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
132 $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
135 $testFinder = t3lib_div
::makeInstance('Tx_Phpunit_Service_TestFinder');
136 $fixturePngFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.png';
138 $GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif'] = TRUE;
140 // Copy file to unique filename in typo3temp, set target permissions and run method
141 $testFilename = PATH_site
. 'typo3temp/' . uniqid('test_') . '.png';
142 @copy
($fixturePngFile, $testFilename);
143 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
144 $newGifFile = t3lib_div
::png_to_gif_by_imagemagick($testFilename);
146 // Get actual permissions and clean up
148 $resultFilePermissions = substr(decoct(fileperms($newGifFile)), 2);
149 t3lib_div
::unlink_tempfile($newGifFile);
151 $this->assertEquals($resultFilePermissions, '0777');
154 ///////////////////////////////
155 // Tests concerning read_png_gif
156 ///////////////////////////////
161 public function readPngGifFixesPermissionsOfConvertedFile() {
162 if (TYPO3_OS
== 'WIN') {
163 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available on Windows.');
166 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
167 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
170 $testFinder = t3lib_div
::makeInstance('Tx_Phpunit_Service_TestFinder');
171 $testGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
173 // Set target permissions and run method
174 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
175 $newPngFile = t3lib_div
::read_png_gif($testGifFile, TRUE);
177 // Get actual permissions and clean up
179 $resultFilePermissions = substr(decoct(fileperms($newPngFile)), 2);
180 t3lib_div
::unlink_tempfile($newPngFile);
182 $this->assertEquals($resultFilePermissions, '0777');
185 ///////////////////////////////
186 // Tests concerning validIP
187 ///////////////////////////////
190 * Data provider for checkValidIpReturnsTrueForValidIp
192 * @return array Data sets
194 public static function validIpDataProvider() {
196 '0.0.0.0' => array('0.0.0.0'),
197 'private IPv4 class C' => array('192.168.0.1'),
198 'private IPv4 class A' => array('10.0.13.1'),
199 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb'),
205 * @dataProvider validIpDataProvider
207 public function validIpReturnsTrueForValidIp($ip) {
208 $this->assertTrue(t3lib_div
::validIP($ip));
212 * Data provider for checkValidIpReturnsFalseForInvalidIp
214 * @return array Data sets
216 public static function invalidIpDataProvider() {
218 'null' => array(null),
220 'string' => array('test'),
221 'string empty' => array(''),
222 'string null' => array('null'),
223 'out of bounds IPv4' => array('300.300.300.300'),
224 'dotted decimal notation with only two dots' => array('127.0.1'),
230 * @dataProvider invalidIpDataProvider
232 public function validIpReturnsFalseForInvalidIp($ip) {
233 $this->assertFalse(t3lib_div
::validIP($ip));
237 ///////////////////////////////
238 // Tests concerning testInt
239 ///////////////////////////////
242 * Data provider for testIntReturnsTrue
244 * @return array Data sets
246 public function functionTestIntValidDataProvider() {
248 'int' => array(32425),
249 'negative int' => array(-32425),
250 'largest int' => array(PHP_INT_MAX
),
251 'int as string' => array('32425'),
252 'negative int as string' => array('-32425'),
254 'zero as string' => array('0'),
260 * @dataProvider functionTestIntValidDataProvider
262 public function testIntReturnsTrue($int) {
263 $this->assertTrue(t3lib_div
::testInt($int));
267 * Data provider for testIntReturnsFalse
269 * @return array Data sets
271 public function functionTestIntInvalidDataProvider() {
273 'int as string with leading zero' => array('01234'),
274 'positive int as string with plus modifier' => array('+1234'),
275 'negative int as string with leading zero' => array('-01234'),
276 'largest int plus one' => array(PHP_INT_MAX +
1),
277 'string' => array('testInt'),
278 'empty string' => array(''),
279 'int in string' => array('5 times of testInt'),
280 'int as string with space after' => array('5 '),
281 'int as string with space before' => array(' 5'),
282 'int as string with many spaces before' => array(' 5'),
283 'float' => array(3.14159),
284 'float as string' => array('3.14159'),
285 'float as string only a dot' => array('10.'),
286 'float as string trailing zero would evaluate to int 10' => array('10.0'),
287 'float as string trailing zeros would evaluate to int 10' => array('10.00'),
288 'null' => array(NULL),
289 'empty array' => array(array()),
290 'int in array' => array(array(32425)),
291 'int as string in array' => array(array('32425')),
297 * @dataProvider functionTestIntInvalidDataProvider
299 public function testIntReturnsFalse($int) {
300 $this->assertFalse(t3lib_div
::testInt($int));
304 ///////////////////////////////
305 // Tests concerning isFirstPartOfStr
306 ///////////////////////////////
309 * Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
313 public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider() {
315 'match first part of string' => array('hello world', 'hello'),
316 'match whole string' => array('hello', 'hello'),
317 'integer is part of string with same number' => array('24', 24),
318 'string is part of integer with same number' => array(24, '24'),
319 'integer is part of string starting with same number' => array('24 beer please', 24),
325 * @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
327 public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part) {
328 $this->assertTrue(t3lib_div
::isFirstPartOfStr($string, $part));
332 * Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
336 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider() {
338 'no string match' => array('hello', 'bye'),
339 'no case sensitive string match' => array('hello world', 'Hello'),
340 'array is not part of string' => array('string', array()),
341 'string is not part of array' => array(array(), 'string'),
342 'null is not part of string' => array('string', NULL),
343 'string is not part of array' => array(NULL, 'string'),
344 'null is not part of array' => array(array(), NULL),
345 'array is not part of string' => array(NULL, array()),
346 'empty string is not part of empty string' => array('', ''),
347 'null is not part of empty string' => array('', NULL),
348 'false is not part of empty string' => array('', FALSE),
349 'empty string is not part of null' => array(NULL, ''),
350 'empty string is not part of false' => array(FALSE, ''),
351 'empty string is not part of zero integer' => array(0, ''),
352 'zero integer is not part of null' => array(NULL, 0),
353 'zero integer is not part of empty string' => array('', 0),
359 * @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
361 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part) {
362 $this->assertFalse(t3lib_div
::isFirstPartOfStr($string, $part));
366 ///////////////////////////////
367 // Tests concerning splitCalc
368 ///////////////////////////////
371 * Data provider for splitCalc
373 * @return array expected values, arithmetic expression
375 public function splitCalcDataProvider() {
377 'empty string returns empty array' => array(
381 'number without operator returns array with plus and number' => array(
382 array(array('+', 42)),
385 'two numbers with asterisk return first number with plus and second number with asterisk' => array(
386 array(array('+', 42), array('*', 31)),
394 * @dataProvider splitCalcDataProvider
396 public function splitCalcCorrectlySplitsExpression($expected, $expression) {
397 $this->assertEquals($expected, t3lib_div
::splitCalc($expression, '+-*/'));
401 //////////////////////////////////
402 // Tests concerning calcPriority
403 //////////////////////////////////
406 * Data provider for calcPriority
408 * @return array expected values, arithmetic expression
410 public function calcPriorityDataProvider() {
412 'add' => array(9, '6 + 3'),
413 'substract with positive result' => array(3, '6 - 3'),
414 'substract with negative result' => array(-3, '3 - 6'),
415 'multiply' => array(6, '2 * 3'),
416 'divide' => array(2.5, '5 / 2'),
417 'modulus' => array(1, '5 % 2'),
418 'power' => array(8, '2 ^ 3'),
419 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
420 'three operands with power' => array(14, '5 + 3 ^ 2'),
421 'three operads with modulus' => array(4, '5 % 2 + 3'),
422 'four operands' => array(3, '2 + 6 / 2 - 2'),
428 * @dataProvider calcPriorityDataProvider
430 public function calcPriorityCorrectlyCalculatesExpression($expected, $expression) {
431 $this->assertEquals($expected, t3lib_div
::calcPriority($expression));
435 //////////////////////////////////
436 // Tests concerning calcPriority
437 //////////////////////////////////
440 * Data provider for valid validEmail's
442 * @return array Valid email addresses
444 public function validEmailValidDataProvider() {
446 'short mail address' => array('a@b.c'),
447 'simple mail address' => array('test@example.com'),
448 'uppercase characters' => array('QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET'),
449 // 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
450 // 'equal sign in local part' => array('test=mail@example.com'),
451 'dash in local part' => array('test-mail@example.com'),
452 'plus in local part' => array('test+mail@example.com'),
453 // 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
454 // 'question mark in local part' => array('test?mail@example.com'),
455 'slash in local part' => array('foo/bar@example.com'),
456 'hash in local part' => array('foo#bar@example.com'),
457 // 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
458 // 'dot in local part' => array('firstname.lastname@employee.2something.com'),
459 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
460 // 'dash as local part' => array('-@foo.com'),
466 * @dataProvider validEmailValidDataProvider
468 public function validEmailReturnsTrueForValidMailAddress($address) {
469 $this->assertTrue(t3lib_div
::validEmail($address));
473 * Data provider for invalid validEmail's
475 * @return array Invalid email addresses
477 public function validEmailInvalidDataProvider() {
479 '@ sign only' => array('@'),
480 'duplicate @' => array('test@@example.com'),
481 'duplicate @ combined with further special characters in local part' => array('test!.!@#$%^&*@example.com'),
482 'opening parenthesis in local part' => array('foo(bar@example.com'),
483 'closing parenthesis in local part' => array('foo)bar@example.com'),
484 'opening square bracket in local part' => array('foo[bar@example.com'),
485 'closing square bracket as local part' => array(']@example.com'),
486 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
487 // 'top level domain only' => array('test@com'),
488 'dash as second level domain' => array('foo@-.com'),
489 'domain part starting with dash' => array('foo@-foo.com'),
490 'domain part ending with dash' => array('foo@foo-.com'),
491 'number as top level domain' => array('foo@bar.123'),
492 // Fix / change if TYPO3 php requirement changed: Address not ok with 5.2.6, but ok with 5.3.2 (?)
493 // 'dash as top level domain' => array('foo@bar.-'),
494 'dot at beginning of domain part' => array('test@.com'),
495 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
496 // 'local part ends with dot' => array('e.x.a.m.p.l.e.@example.com'),
497 'trailing whitespace' => array('test@example.com '),
498 'trailing carriage return' => array('test@example.com' . CR
),
499 'trailing linefeed' => array('test@example.com' . LF
),
500 'trailing carriage return linefeed' => array('test@example.com' . CRLF
),
501 'trailing tab' => array('test@example.com' . TAB
),
507 * @dataProvider validEmailInvalidDataProvider
509 public function validEmailReturnsFalseForInvalidMailAddress($address) {
510 $this->assertFalse(t3lib_div
::validEmail($address));
514 //////////////////////////////////
515 // Tests concerning intExplode
516 //////////////////////////////////
521 public function intExplodeConvertsStringsToInteger() {
522 $testString = '1,foo,2';
523 $expectedArray = array(1, 0, 2);
524 $actualArray = t3lib_div
::intExplode(',', $testString);
526 $this->assertEquals($expectedArray, $actualArray);
530 //////////////////////////////////
531 // Tests concerning revExplode
532 //////////////////////////////////
537 public function revExplodeExplodesString() {
538 $testString = 'my:words:here';
539 $expectedArray = array('my:words', 'here');
540 $actualArray = t3lib_div
::revExplode(':', $testString, 2);
542 $this->assertEquals($expectedArray, $actualArray);
546 //////////////////////////////////
547 // Tests concerning trimExplode
548 //////////////////////////////////
553 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
554 $testString = ' a , b , c ,d ,, e,f,';
555 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
556 $actualArray = t3lib_div
::trimExplode(',', $testString);
558 $this->assertEquals($expectedArray, $actualArray);
564 public function checkTrimExplodeRemovesNewLines() {
565 $testString = ' a , b , ' . LF
. ' ,d ,, e,f,';
566 $expectedArray = array('a', 'b', 'd', 'e', 'f');
567 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE);
569 $this->assertEquals($expectedArray, $actualArray);
575 public function checkTrimExplodeRemovesEmptyElements() {
576 $testString = 'a , b , c , ,d ,, ,e,f,';
577 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
578 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE);
580 $this->assertEquals($expectedArray, $actualArray);
586 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
587 $testString = ' a , b , c , , d,, ,e ';
588 $expectedArray = array('a', 'b', 'c,,d,,,e');
589 // Limiting returns the rest of the string as the last element
590 $actualArray = t3lib_div
::trimExplode(',', $testString, FALSE, 3);
592 $this->assertEquals($expectedArray, $actualArray);
598 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
599 $testString = ' a , b , c , , d,, ,e ';
600 $expectedArray = array('a', 'b', 'c,d,e');
601 // Limiting returns the rest of the string as the last element
602 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE, 3);
604 $this->assertEquals($expectedArray, $actualArray);
610 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
611 $testString = ' a , b , c , d, ,e, f , , ';
612 $expectedArray = array('a', 'b', 'c', 'd', '', 'e');
613 // limiting returns the rest of the string as the last element
614 $actualArray = t3lib_div
::trimExplode(',', $testString, FALSE, -3);
616 $this->assertEquals($expectedArray, $actualArray);
622 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
623 $testString = ' a , b , c , d, ,e, f , , ';
624 $expectedArray = array('a', 'b', 'c');
625 // Limiting returns the rest of the string as the last element
626 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE, -3);
628 $this->assertEquals($expectedArray, $actualArray);
634 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
635 $testString = ' a , b , , c , , , ';
636 $expectedArray = array('a', 'b', 'c');
637 // Limiting returns the rest of the string as the last element
638 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE, 4);
640 $this->assertEquals($expectedArray, $actualArray);
646 public function checkTrimExplodeKeepsZeroAsString() {
647 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
648 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
649 $actualArray = t3lib_div
::trimExplode(',', $testString, TRUE);
651 $this->assertEquals($expectedArray, $actualArray);
655 //////////////////////////////////
656 // Tests concerning removeArrayEntryByValue
657 //////////////////////////////////
662 public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
669 $compareValue = 'test2';
670 $expectedResult = array(
674 $actualResult = t3lib_div
::removeArrayEntryByValue($inputArray, $compareValue);
675 $this->assertEquals($expectedResult, $actualResult);
681 public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
689 $compareValue = 'bar';
690 $expectedResult = array(
694 $actualResult = t3lib_div
::removeArrayEntryByValue($inputArray, $compareValue);
695 $this->assertEquals($expectedResult, $actualResult);
701 public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
708 $expectedResult = array(
712 $actualResult = t3lib_div
::removeArrayEntryByValue($inputArray, $compareValue);
713 $this->assertEquals($expectedResult, $actualResult);
716 //////////////////////////////////
717 // Tests concerning getBytesFromSizeMeasurement
718 //////////////////////////////////
721 * Data provider for getBytesFromSizeMeasurement
723 * @return array expected value, input string
725 public function getBytesFromSizeMeasurementDataProvider() {
727 '100 kilo Bytes' => array('102400', '100k'),
728 '100 mega Bytes' => array('104857600', '100m'),
729 '100 giga Bytes' => array('107374182400', '100g'),
735 * @dataProvider getBytesFromSizeMeasurementDataProvider
737 public function getBytesFromSizeMeasurementCalculatesCorrectByteValue($expected, $byteString) {
738 $this->assertEquals($expected, t3lib_div
::getBytesFromSizeMeasurement($byteString));
742 //////////////////////////////////
743 // Tests concerning getIndpEnv
744 //////////////////////////////////
749 public function getIndpEnvTypo3SitePathReturnNonEmptyString() {
750 $this->assertTrue(strlen(t3lib_div
::getIndpEnv('TYPO3_SITE_PATH')) >= 1);
756 public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() {
757 $result = t3lib_div
::getIndpEnv('TYPO3_SITE_PATH');
758 $this->assertEquals('/', $result[0]);
764 public function getIndpEnvTypo3SitePathReturnsStringEndingWithSlash() {
765 $result = t3lib_div
::getIndpEnv('TYPO3_SITE_PATH');
766 $this->assertEquals('/', $result[strlen($result) - 1]);
772 public static function hostnameAndPortDataProvider() {
774 'localhost ipv4 without port' => array('127.0.0.1', '127.0.0.1', ''),
775 'localhost ipv4 with port' => array('127.0.0.1:81', '127.0.0.1', '81'),
776 'localhost ipv6 without port' => array('[::1]', '[::1]', ''),
777 'localhost ipv6 with port' => array('[::1]:81', '[::1]', '81'),
778 'ipv6 without port' => array('[2001:DB8::1]', '[2001:DB8::1]', ''),
779 'ipv6 with port' => array('[2001:DB8::1]:81', '[2001:DB8::1]', '81'),
780 'hostname without port' => array('lolli.did.this', 'lolli.did.this', ''),
781 'hostname with port' => array('lolli.did.this:42', 'lolli.did.this', '42'),
787 * @dataProvider hostnameAndPortDataProvider
789 public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
790 $_SERVER['HTTP_HOST'] = $httpHost;
791 $this->assertEquals($expectedIp, t3lib_div
::getIndpEnv('TYPO3_HOST_ONLY'));
796 * @dataProvider hostnameAndPortDataProvider
798 public function getIndpEnvTypo3PortParsesHostnamesAndIpAdresses($httpHost, $dummy, $expectedPort) {
799 $_SERVER['HTTP_HOST'] = $httpHost;
800 $this->assertEquals($expectedPort, t3lib_div
::getIndpEnv('TYPO3_PORT'));
804 //////////////////////////////////
805 // Tests concerning underscoredToUpperCamelCase
806 //////////////////////////////////
809 * Data provider for underscoredToUpperCamelCase
811 * @return array expected, input string
813 public function underscoredToUpperCamelCaseDataProvider() {
815 'single word' => array('Blogexample', 'blogexample'),
816 'multiple words' => array('BlogExample', 'blog_example'),
822 * @dataProvider underscoredToUpperCamelCaseDataProvider
824 public function underscoredToUpperCamelCase($expected, $inputString) {
825 $this->assertEquals($expected, t3lib_div
::underscoredToUpperCamelCase($inputString));
829 //////////////////////////////////
830 // Tests concerning underscoredToLowerCamelCase
831 //////////////////////////////////
834 * Data provider for underscoredToLowerCamelCase
836 * @return array expected, input string
838 public function underscoredToLowerCamelCaseDataProvider() {
840 'single word' => array('minimalvalue', 'minimalvalue'),
841 'multiple words' => array('minimalValue', 'minimal_value'),
847 * @dataProvider underscoredToLowerCamelCaseDataProvider
849 public function underscoredToLowerCamelCase($expected, $inputString) {
850 $this->assertEquals($expected, t3lib_div
::underscoredToLowerCamelCase($inputString));
853 //////////////////////////////////
854 // Tests concerning camelCaseToLowerCaseUnderscored
855 //////////////////////////////////
858 * Data provider for camelCaseToLowerCaseUnderscored
860 * @return array expected, input string
862 public function camelCaseToLowerCaseUnderscoredDataProvider() {
864 'single word' => array('blogexample', 'blogexample'),
865 'single word starting upper case' => array('blogexample', 'Blogexample'),
866 'two words starting lower case' => array('minimal_value', 'minimalValue'),
867 'two words starting upper case' => array('blog_example', 'BlogExample'),
873 * @dataProvider camelCaseToLowerCaseUnderscoredDataProvider
875 public function camelCaseToLowerCaseUnderscored($expected, $inputString) {
876 $this->assertEquals($expected, t3lib_div
::camelCaseToLowerCaseUnderscored($inputString));
880 //////////////////////////////////
881 // Tests concerning lcFirst
882 //////////////////////////////////
885 * Data provider for lcFirst
887 * @return array expected, input string
889 public function lcfirstDataProvider() {
891 'single word' => array('blogexample', 'blogexample'),
892 'single Word starting upper case' => array('blogexample', 'Blogexample'),
893 'two words' => array('blogExample', 'BlogExample'),
899 * @dataProvider lcfirstDataProvider
901 public function lcFirst($expected, $inputString) {
902 $this->assertEquals($expected, t3lib_div
::lcfirst($inputString));
906 //////////////////////////////////
907 // Tests concerning encodeHeader
908 //////////////////////////////////
913 public function encodeHeaderEncodesWhitespacesInQuotedPrintableMailHeader() {
915 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
916 t3lib_div
::encodeHeader(
917 "We test whether the copyright character \xc2\xa9 is encoded correctly",
927 public function encodeHeaderEncodesQuestionmarksInQuotedPrintableMailHeader() {
929 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
930 t3lib_div
::encodeHeader(
931 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
939 //////////////////////////////////
940 // Tests concerning isValidUrl
941 //////////////////////////////////
944 * Data provider for valid isValidUrl's
946 * @return array Valid ressource
948 public function validUrlValidRessourceDataProvider() {
950 'http' => array('http://www.example.org/'),
951 'http without trailing slash' => array('http://qwe'),
952 'http directory with trailing slash' => array('http://www.example/img/dir/'),
953 'http directory without trailing slash' => array('http://www.example/img/dir'),
954 'http index.html' => array('http://example.com/index.html'),
955 'http index.php' => array('http://www.example.com/index.php'),
956 'http test.png' => array('http://www.example/img/test.png'),
957 'http username password querystring and ancher' => array('https://user:pw@www.example.org:80/path?arg=value#fragment'),
958 'file' => array('file:///tmp/test.c'),
959 'file directory' => array('file://foo/bar'),
960 'ftp directory' => array('ftp://ftp.example.com/tmp/'),
961 'mailto' => array('mailto:foo@bar.com'),
962 'news' => array('news:news.php.net'),
963 'telnet'=> array('telnet://192.0.2.16:80/'),
964 'ldap' => array('ldap://[2001:db8::7]/c=GB?objectClass?one'),
970 * @dataProvider validUrlValidRessourceDataProvider
972 public function validURLReturnsTrueForValidRessource($url) {
973 $this->assertTrue(t3lib_div
::isValidUrl($url));
977 * Data provider for invalid isValidUrl's
979 * @return array Invalid ressource
981 public function isValidUrlInvalidRessourceDataProvider() {
983 'http missing colon' => array('http//www.example/wrong/url/'),
984 'http missing slash' => array('http:/www.example'),
985 'hostname only' => array('www.example.org/'),
986 'file missing protocol specification' => array('/tmp/test.c'),
987 'slash only' => array('/'),
988 'string http://' => array('http://'),
989 'string http:/' => array('http:/'),
990 'string http:' => array('http:'),
991 'string http' => array('http'),
992 'empty string' => array(''),
993 'string -1' => array('-1'),
994 'string array()' => array('array()'),
995 'random string' => array('qwe'),
1001 * @dataProvider isValidUrlInvalidRessourceDataProvider
1003 public function validURLReturnsFalseForInvalidRessoure($url) {
1004 $this->assertFalse(t3lib_div
::isValidUrl($url));
1008 //////////////////////////////////
1009 // Tests concerning isOnCurrentHost
1010 //////////////////////////////////
1015 public function isOnCurrentHostReturnsTrueWithCurrentHost() {
1016 $testUrl = t3lib_div
::getIndpEnv('TYPO3_REQUEST_URL');
1017 $this->assertTrue(t3lib_div
::isOnCurrentHost($testUrl));
1021 * Data provider for invalid isOnCurrentHost's
1023 * @return array Invalid Hosts
1025 public function checkisOnCurrentHostInvalidHosts() {
1027 'empty string' => array(''),
1028 'arbitrary string' => array('arbitrary string'),
1029 'localhost IP' => array('127.0.0.1'),
1030 'relative path' => array('./relpath/file.txt'),
1031 'absolute path' => array('/abspath/file.txt?arg=value'),
1032 'differnt host' => array(t3lib_div
::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org'),
1037 ////////////////////////////////////////
1038 // Tests concerning sanitizeLocalUrl
1039 ////////////////////////////////////////
1042 * Data provider for valid sanitizeLocalUrl's
1044 * @return array Valid url
1046 public function sanitizeLocalUrlValidUrlDataProvider() {
1047 $subDirectory = t3lib_div
::getIndpEnv('TYPO3_SITE_PATH');
1048 $typo3SiteUrl = t3lib_div
::getIndpEnv('TYPO3_SITE_URL');
1049 $typo3RequestHost = t3lib_div
::getIndpEnv('TYPO3_REQUEST_HOST');
1052 'alt_intro.php' => array('alt_intro.php'),
1053 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
1054 $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
1055 $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
1056 '../index.php' => array('../index.php'),
1057 '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
1058 '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
1059 '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
1060 PATH_site
. 'typo3/alt_intro.php' => array(PATH_site
. 'typo3/alt_intro.php'),
1061 $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
1062 $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php'),
1068 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1070 public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
1071 $this->assertEquals($url, t3lib_div
::sanitizeLocalUrl($url));
1076 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1078 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
1079 $this->assertEquals(rawurlencode($url), t3lib_div
::sanitizeLocalUrl(rawurlencode($url)));
1083 * Data provider for invalid sanitizeLocalUrl's
1085 * @return array Valid url
1087 public function sanitizeLocalUrlInvalidDataProvider() {
1089 'empty string' => array(''),
1090 'http domain' => array('http://www.google.de/'),
1091 'https domain' => array('https://www.google.de/'),
1092 'relative path with XSS' => array('../typo3/whatever.php?argument=javascript:alert(0)'),
1098 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1100 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
1101 $this->assertEquals('', t3lib_div
::sanitizeLocalUrl($url));
1106 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1108 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
1109 $this->assertEquals('', t3lib_div
::sanitizeLocalUrl(rawurlencode($url)));
1113 //////////////////////////////////////
1114 // Tests concerning arrayDiffAssocRecursive
1115 //////////////////////////////////////
1120 public function arrayDiffAssocRecursiveHandlesOneDimensionalArrays() {
1130 $expectedResult = array(
1133 $actualResult = t3lib_div
::arrayDiffAssocRecursive($array1, $array2);
1134 $this->assertEquals($expectedResult, $actualResult);
1140 public function arrayDiffAssocRecursiveHandlesMultiDimensionalArrays() {
1144 'key21' => 'value21',
1145 'key22' => 'value22',
1147 'key231' => 'value231',
1148 'key232' => 'value232',
1155 'key21' => 'value21',
1157 'key231' => 'value231',
1161 $expectedResult = array(
1163 'key22' => 'value22',
1165 'key232' => 'value232',
1169 $actualResult = t3lib_div
::arrayDiffAssocRecursive($array1, $array2);
1170 $this->assertEquals($expectedResult, $actualResult);
1176 public function arrayDiffAssocRecursiveHandlesMixedArrays() {
1179 'key11' => 'value11',
1180 'key12' => 'value12',
1188 'key21' => 'value21',
1191 $expectedResult = array(
1194 $actualResult = t3lib_div
::arrayDiffAssocRecursive($array1, $array2);
1195 $this->assertEquals($expectedResult, $actualResult);
1199 //////////////////////////////////////
1200 // Tests concerning removeDotsFromTS
1201 //////////////////////////////////////
1206 public function removeDotsFromTypoScriptSucceedsWithDottedArray() {
1207 $typoScript = array(
1208 'propertyA.' => array(
1217 $expectedResult = array(
1218 'propertyA' => array(
1227 $this->assertEquals($expectedResult, t3lib_div
::removeDotsFromTS($typoScript));
1233 public function removeDotsFromTypoScriptOverridesSubArray() {
1234 $typoScript = array(
1235 'propertyA.' => array(
1236 'keyA' => 'getsOverridden',
1245 $expectedResult = array(
1246 'propertyA' => array(
1255 $this->assertEquals($expectedResult, t3lib_div
::removeDotsFromTS($typoScript));
1261 public function removeDotsFromTypoScriptOverridesWithScalar() {
1262 $typoScript = array(
1263 'propertyA.' => array(
1267 'keyA' => 'willOverride',
1273 $expectedResult = array(
1274 'propertyA' => array(
1275 'keyA' => 'willOverride',
1281 $this->assertEquals($expectedResult, t3lib_div
::removeDotsFromTS($typoScript));
1285 //////////////////////////////////////
1286 // Tests concerning get_dirs
1287 //////////////////////////////////////
1292 public function getDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
1294 $directories = t3lib_div
::get_dirs($path);
1296 $this->assertInternalType(PHPUnit_Framework_Constraint_IsType
::TYPE_ARRAY
, $directories);
1302 public function getDirsReturnsStringErrorOnPathFailure() {
1304 $result = t3lib_div
::get_dirs($path);
1305 $expectedResult = 'error';
1307 $this->assertEquals($expectedResult, $result);
1311 //////////////////////////////////
1312 // Tests concerning hmac
1313 //////////////////////////////////
1318 public function hmacReturnsHashOfProperLength() {
1319 $hmac = t3lib_div
::hmac('message');
1320 $this->assertTrue(!empty($hmac) && is_string($hmac));
1321 $this->assertTrue(strlen($hmac) == 40);
1327 public function hmacReturnsEqualHashesForEqualInput() {
1330 $this->assertEquals(t3lib_div
::hmac($msg0), t3lib_div
::hmac($msg1));
1336 public function hmacReturnsNoEqualHashesForNonEqualInput() {
1339 $this->assertNotEquals(t3lib_div
::hmac($msg0), t3lib_div
::hmac($msg1));
1343 //////////////////////////////////
1344 // Tests concerning quoteJSvalue
1345 //////////////////////////////////
1350 public function quoteJSvalueHtmlspecialcharsDataByDefault() {
1351 $this->assertContains(
1353 t3lib_div
::quoteJSvalue('>')
1360 public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
1361 $this->assertContains(
1363 t3lib_div
::quoteJSvalue('>', false)
1370 public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
1371 $this->assertContains(
1373 t3lib_div
::quoteJSvalue('>', true)
1380 public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
1381 $this->assertEquals(
1383 t3lib_div
::quoteJSvalue("", true)
1390 public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
1391 $this->assertEquals(
1393 t3lib_div
::quoteJSvalue("Hello world!", true)
1400 public function quoteJSvalueEscapesSingleQuote() {
1401 $this->assertEquals(
1403 t3lib_div
::quoteJSvalue("'", true)
1410 public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
1411 $this->assertEquals(
1413 t3lib_div
::quoteJSvalue('"', true)
1420 public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
1421 $this->assertEquals(
1423 t3lib_div
::quoteJSvalue('"', false)
1430 public function quoteJSvalueEscapesTab() {
1431 $this->assertEquals(
1433 t3lib_div
::quoteJSvalue(TAB
)
1440 public function quoteJSvalueEscapesLinefeed() {
1441 $this->assertEquals(
1443 t3lib_div
::quoteJSvalue(LF
)
1450 public function quoteJSvalueEscapesCarriageReturn() {
1451 $this->assertEquals(
1453 t3lib_div
::quoteJSvalue(CR
)
1460 public function quoteJSvalueEscapesBackslah() {
1461 $this->assertEquals(
1463 t3lib_div
::quoteJSvalue('\\')
1467 //////////////////////////////////
1468 // Tests concerning readLLfile
1469 //////////////////////////////////
1474 public function readLLfileHandlesLocallangXMLOverride() {
1475 $unique = uniqid('locallangXMLOverrideTest');
1477 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
1480 <languageKey index="default" type="array">
1481 <label index="buttons.logout">EXIT</label>
1486 $file = PATH_site
. 'typo3temp/' . $unique . '.xml';
1487 t3lib_div
::writeFileToTypo3tempDir($file, $xml);
1489 // Get default value
1490 $defaultLL = t3lib_div
::readLLfile('EXT:lang/locallang_core.xml', 'default');
1492 // Set override file
1493 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
1495 // Get override value
1496 $overrideLL = t3lib_div
::readLLfile('EXT:lang/locallang_core.xml', 'default');
1501 $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
1502 $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
1503 $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
1507 ///////////////////////////////
1508 // Tests concerning _GETset()
1509 ///////////////////////////////
1514 public function getSetWritesArrayToGetSystemVariable() {
1516 $GLOBALS['HTTP_GET_VARS'] = array();
1518 $getParameters = array('foo' => 'bar');
1519 t3lib_div
::_GETset($getParameters);
1520 $this->assertSame($getParameters, $_GET);
1526 public function getSetWritesArrayToGlobalsHttpGetVars() {
1528 $GLOBALS['HTTP_GET_VARS'] = array();
1530 $getParameters = array('foo' => 'bar');
1531 t3lib_div
::_GETset($getParameters);
1532 $this->assertSame($getParameters, $GLOBALS['HTTP_GET_VARS']);
1538 public function getSetForArrayDropsExistingValues() {
1540 $GLOBALS['HTTP_GET_VARS'] = array();
1542 t3lib_div
::_GETset(array('foo' => 'bar'));
1544 t3lib_div
::_GETset(array('oneKey' => 'oneValue'));
1546 $this->assertEquals(
1547 array('oneKey' => 'oneValue'),
1548 $GLOBALS['HTTP_GET_VARS']
1555 public function getSetAssignsOneValueToOneKey() {
1557 $GLOBALS['HTTP_GET_VARS'] = array();
1559 t3lib_div
::_GETset('oneValue', 'oneKey');
1561 $this->assertEquals(
1563 $GLOBALS['HTTP_GET_VARS']['oneKey']
1570 public function getSetForOneValueDoesNotDropUnrelatedValues() {
1572 $GLOBALS['HTTP_GET_VARS'] = array();
1574 t3lib_div
::_GETset(array('foo' => 'bar'));
1575 t3lib_div
::_GETset('oneValue', 'oneKey');
1577 $this->assertEquals(
1578 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1579 $GLOBALS['HTTP_GET_VARS']
1586 public function getSetCanAssignsAnArrayToASpecificArrayElement() {
1588 $GLOBALS['HTTP_GET_VARS'] = array();
1590 t3lib_div
::_GETset(array('childKey' => 'oneValue'), 'parentKey');
1592 $this->assertEquals(
1593 array('parentKey' => array('childKey' => 'oneValue')),
1594 $GLOBALS['HTTP_GET_VARS']
1601 public function getSetCanAssignAStringValueToASpecificArrayChildElement() {
1603 $GLOBALS['HTTP_GET_VARS'] = array();
1605 t3lib_div
::_GETset('oneValue', 'parentKey|childKey');
1607 $this->assertEquals(
1608 array('parentKey' => array('childKey' => 'oneValue')),
1609 $GLOBALS['HTTP_GET_VARS']
1616 public function getSetCanAssignAnArrayToASpecificArrayChildElement() {
1618 $GLOBALS['HTTP_GET_VARS'] = array();
1621 array('key1' => 'value1', 'key2' => 'value2'),
1622 'parentKey|childKey'
1625 $this->assertEquals(
1627 'parentKey' => array(
1628 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1631 $GLOBALS['HTTP_GET_VARS']
1636 ///////////////////////////////
1637 // Tests concerning fixPermissions
1638 ///////////////////////////////
1643 public function fixPermissionsCorrectlySetsGroup() {
1644 if (TYPO3_OS
== 'WIN') {
1645 $this->markTestSkipped('fixPermissionsCorrectlySetsGroupOwnerOfFile() tests not available on Windows');
1647 if (!function_exists('posix_getegid')) {
1648 $this->markTestSkipped('Function posix_getegid() not available, fixPermissionsCorrectlySetsGroupOwnerOfFile() tests skipped');
1651 // Create and prepare test file
1652 $filename = PATH_site
. 'typo3temp/' . uniqid('test_');
1653 t3lib_div
::writeFileToTypo3tempDir($filename, '42');
1655 // Set target group and run method
1656 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1657 $fixPermissionsResult = t3lib_div
::fixPermissions($filename);
1660 $resultFileGroup = filegroup($filename);
1663 $this->assertEquals($resultFileGroup, posix_getegid());
1669 public function fixPermissionsCorrectlySetsPermissionsToFile() {
1670 if (TYPO3_OS
== 'WIN') {
1671 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1674 // Create and prepare test file
1675 $filename = PATH_site
. 'typo3temp/' . uniqid('test_');
1676 t3lib_div
::writeFileToTypo3tempDir($filename, '42');
1677 chmod($filename, 0742);
1679 // Set target permissions and run method
1680 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1681 $fixPermissionsResult = t3lib_div
::fixPermissions($filename);
1683 // Get actual permissions and clean up
1685 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1688 // Test if everything was ok
1689 $this->assertTrue($fixPermissionsResult);
1690 $this->assertEquals($resultFilePermissions, '0660');
1696 public function fixPermissionsCorrectlySetsPermissionsToHiddenFile() {
1697 if (TYPO3_OS
== 'WIN') {
1698 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1701 // Create and prepare test file
1702 $filename = PATH_site
. 'typo3temp/' . uniqid('.test_');
1703 t3lib_div
::writeFileToTypo3tempDir($filename, '42');
1704 chmod($filename, 0742);
1706 // Set target permissions and run method
1707 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1708 $fixPermissionsResult = t3lib_div
::fixPermissions($filename);
1710 // Get actual permissions and clean up
1712 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1715 // Test if everything was ok
1716 $this->assertTrue($fixPermissionsResult);
1717 $this->assertEquals($resultFilePermissions, '0660');
1723 public function fixPermissionsCorrectlySetsPermissionsToDirectory() {
1724 if (TYPO3_OS
== 'WIN') {
1725 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1728 // Create and prepare test directory
1729 $directory = PATH_site
. 'typo3temp/' . uniqid('test_');
1730 t3lib_div
::mkdir($directory);
1731 chmod($directory, 1551);
1733 // Set target permissions and run method
1734 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1735 $fixPermissionsResult = t3lib_div
::fixPermissions($directory . '/');
1737 // Get actual permissions and clean up
1739 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1740 t3lib_div
::rmdir($directory);
1742 // Test if everything was ok
1743 $this->assertTrue($fixPermissionsResult);
1744 $this->assertEquals($resultDirectoryPermissions, '0770');
1750 public function fixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
1751 if (TYPO3_OS
== 'WIN') {
1752 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1755 // Create and prepare test directory
1756 $directory = PATH_site
. 'typo3temp/' . uniqid('.test_');
1757 t3lib_div
::mkdir($directory);
1758 chmod($directory, 1551);
1760 // Set target permissions and run method
1761 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1762 $fixPermissionsResult = t3lib_div
::fixPermissions($directory);
1764 // Get actual permissions and clean up
1766 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1767 t3lib_div
::rmdir($directory);
1769 // Test if everything was ok
1770 $this->assertTrue($fixPermissionsResult);
1771 $this->assertEquals($resultDirectoryPermissions, '0770');
1777 public function fixPermissionsCorrectlySetsPermissionsRecursive() {
1778 if (TYPO3_OS
== 'WIN') {
1779 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1782 // Create and prepare test directory and file structure
1783 $baseDirectory = PATH_site
. 'typo3temp/' . uniqid('test_');
1784 t3lib_div
::mkdir($baseDirectory);
1785 chmod($baseDirectory, 1751);
1786 t3lib_div
::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
1787 chmod($baseDirectory . '/file', 0742);
1788 t3lib_div
::mkdir($baseDirectory . '/foo');
1789 chmod($baseDirectory . '/foo', 1751);
1790 t3lib_div
::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
1791 chmod($baseDirectory . '/foo/file', 0742);
1792 t3lib_div
::mkdir($baseDirectory . '/.bar');
1793 chmod($baseDirectory . '/.bar', 1751);
1794 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
1795 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
1796 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
1797 touch($baseDirectory . '/.bar/.file', '42');
1798 chmod($baseDirectory . '/.bar/.file', 0742);
1799 touch($baseDirectory . '/.bar/..file2', '42');
1800 chmod($baseDirectory . '/.bar/..file2', 0742);
1802 // Set target permissions and run method
1803 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1804 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1805 $fixPermissionsResult = t3lib_div
::fixPermissions($baseDirectory, TRUE);
1807 // Get actual permissions
1809 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
1810 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
1811 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
1812 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
1813 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
1814 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
1815 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
1818 unlink($baseDirectory . '/file');
1819 unlink($baseDirectory . '/foo/file');
1820 unlink($baseDirectory . '/.bar/.file');
1821 unlink($baseDirectory . '/.bar/..file2');
1822 t3lib_div
::rmdir($baseDirectory . '/foo');
1823 t3lib_div
::rmdir($baseDirectory . '/.bar');
1824 t3lib_div
::rmdir($baseDirectory);
1826 // Test if everything was ok
1827 $this->assertTrue($fixPermissionsResult);
1828 $this->assertEquals($resultBaseDirectoryPermissions, '0770');
1829 $this->assertEquals($resultBaseFilePermissions, '0660');
1830 $this->assertEquals($resultFooDirectoryPermissions, '0770');
1831 $this->assertEquals($resultFooFilePermissions, '0660');
1832 $this->assertEquals($resultBarDirectoryPermissions, '0770');
1833 $this->assertEquals($resultBarFilePermissions, '0660');
1834 $this->assertEquals($resultBarFile2Permissions, '0660');
1840 public function fixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
1841 if (TYPO3_OS
== 'WIN') {
1842 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1845 // Create and prepare test file
1846 $filename = PATH_site
. 'typo3temp/../typo3temp/' . uniqid('test_');
1848 chmod($filename, 0742);
1850 // Set target permissions and run method
1851 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1852 $fixPermissionsResult = t3lib_div
::fixPermissions($filename);
1854 // Get actual permissions and clean up
1856 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1859 // Test if everything was ok
1860 $this->assertFalse($fixPermissionsResult);
1866 public function fixPermissionsSetsPermissionsWithRelativeFileReference() {
1867 if (TYPO3_OS
== 'WIN') {
1868 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1871 $filename = 'typo3temp/' . uniqid('test_');
1872 t3lib_div
::writeFileToTypo3tempDir(PATH_site
. $filename, '42');
1873 chmod($filename, 0742);
1875 // Set target permissions and run method
1876 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1877 $fixPermissionsResult = t3lib_div
::fixPermissions($filename);
1879 // Get actual permissions and clean up
1881 $resultFilePermissions = substr(decoct(fileperms(PATH_site
. $filename)), 2);
1882 unlink(PATH_site
. $filename);
1884 // Test if everything was ok
1885 $this->assertTrue($fixPermissionsResult);
1886 $this->assertEquals($resultFilePermissions, '0660');
1890 ///////////////////////////////
1891 // Tests concerning mkdir
1892 ///////////////////////////////
1897 public function mkdirCorrectlyCreatesDirectory() {
1898 $directory = PATH_site
. 'typo3temp/' . uniqid('test_');
1899 $mkdirResult = t3lib_div
::mkdir($directory);
1900 $directoryCreated = is_dir($directory);
1901 t3lib_div
::rmdir($directory);
1902 $this->assertTrue($mkdirResult);
1903 $this->assertTrue($directoryCreated);
1909 public function mkdirCorrectlyCreatesHiddenDirectory() {
1910 $directory = PATH_site
. 'typo3temp/' . uniqid('.test_');
1911 $mkdirResult = t3lib_div
::mkdir($directory);
1912 $directoryCreated = is_dir($directory);
1913 t3lib_div
::rmdir($directory);
1914 $this->assertTrue($mkdirResult);
1915 $this->assertTrue($directoryCreated);
1921 public function mkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
1922 $directory = PATH_site
. 'typo3temp/' . uniqid('test_');
1923 $mkdirResult = t3lib_div
::mkdir($directory);
1924 $directoryCreated = is_dir($directory);
1925 t3lib_div
::rmdir($directory);
1926 $this->assertTrue($mkdirResult);
1927 $this->assertTrue($directoryCreated);
1931 * Data provider for ImageMagick shell commands
1932 * @see explodeAndUnquoteImageMagickCommands
1934 public function imageMagickCommandsDataProvider() {
1936 // Some theoretical tests first
1939 array('aa', 'bb', '"cc"', '"dd"'),
1940 array('aa', 'bb', 'cc', 'dd'),
1944 array('aa', 'bb', '"cc dd"'),
1945 array('aa', 'bb', 'cc dd'),
1948 '\'aa bb\' "cc dd"',
1949 array('\'aa bb\'', '"cc dd"'),
1950 array('aa bb', 'cc dd'),
1953 '\'aa bb\' cc "dd"',
1954 array('\'aa bb\'', 'cc', '"dd"'),
1955 array('aa bb', 'cc', 'dd'),
1957 // Now test against some real world examples
1959 '/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
1961 '/opt/local/bin/gm.exe',
1968 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1969 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1972 '/opt/local/bin/gm.exe',
1979 'C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
1980 'C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
1984 'C:/opt/local/bin/gm.exe convert +profile \'*\' -geometry 170x136! -negate "C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
1986 'C:/opt/local/bin/gm.exe',
1993 '"C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1994 '"C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1997 'C:/opt/local/bin/gm.exe',
2004 'C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
2005 'C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
2009 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
2018 '"/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
2019 '"/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
2029 '/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
2030 '/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
2034 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate "/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]" "/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"',
2043 '"/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
2044 '"/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
2054 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
2055 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
2059 '/usr/bin/gm convert +profile \'*\' -geometry 170x136! -negate \'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\' \'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\'',
2068 '\'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\'',
2069 '\'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\''
2079 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
2080 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
2087 * Tests if the commands are exploded and unquoted correctly
2089 * @dataProvider imageMagickCommandsDataProvider
2092 public function explodeAndUnquoteImageMagickCommands($source, $expectedQuoted, $expectedUnquoted) {
2093 $actualQuoted = t3lib_div
::unQuoteFilenames($source);
2094 $acutalUnquoted = t3lib_div
::unQuoteFilenames($source, TRUE);
2096 $this->assertEquals($expectedQuoted, $actualQuoted, 'The exploded command does not match the expected');
2097 $this->assertEquals($expectedUnquoted, $acutalUnquoted, 'The exploded and unquoted command does not match the expected');
2101 ///////////////////////////////
2102 // Tests concerning split_fileref
2103 ///////////////////////////////
2108 public function splitFileRefReturnsFileTypeNotForFolders(){
2109 $directoryName = uniqid('test_') . '.com';
2110 $directoryPath = PATH_site
. 'typo3temp/';
2111 $directory = $directoryPath . $directoryName;
2112 mkdir($directory, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
2114 $fileInfo = t3lib_div
::split_fileref($directory);
2116 $directoryCreated = is_dir($directory);
2119 $this->assertTrue($directoryCreated);
2120 $this->assertInternalType(PHPUnit_Framework_Constraint_IsType
::TYPE_ARRAY
, $fileInfo);
2121 $this->assertEquals($directoryPath, $fileInfo['path']);
2122 $this->assertEquals($directoryName, $fileInfo['file']);
2123 $this->assertEquals($directoryName, $fileInfo['filebody']);
2124 $this->assertEquals('', $fileInfo['fileext']);
2125 $this->assertArrayNotHasKey('realFileext', $fileInfo);
2131 public function splitFileRefReturnsFileTypeForFilesWithoutPathSite() {
2132 $testFile = 'fileadmin/media/someFile.png';
2134 $fileInfo = t3lib_div
::split_fileref($testFile);
2135 $this->assertInternalType(PHPUnit_Framework_Constraint_IsType
::TYPE_ARRAY
, $fileInfo);
2136 $this->assertEquals('fileadmin/media/', $fileInfo['path']);
2137 $this->assertEquals('someFile.png', $fileInfo['file']);
2138 $this->assertEquals('someFile', $fileInfo['filebody']);
2139 $this->assertEquals('png', $fileInfo['fileext']);
2143 /////////////////////////////
2144 // Tests concerning dirname
2145 /////////////////////////////
2148 * @see dirnameWithDataProvider
2150 * @return array<array>
2152 public function dirnameDataProvider() {
2154 'absolute path with multiple part and file' => array('/dir1/dir2/script.php', '/dir1/dir2'),
2155 'absolute path with one part' => array('/dir1/', '/dir1'),
2156 'absolute path to file without extension' => array('/dir1/something', '/dir1'),
2157 'relative path with one part and file' => array('dir1/script.php', 'dir1'),
2158 'relative one-character path with one part and file' => array('d/script.php', 'd'),
2159 'absolute zero-part path with file' => array('/script.php', ''),
2160 'empty string' => array('', ''),
2167 * @dataProvider dirnameDataProvider
2169 * @param string $input the input for dirname
2170 * @param string $expectedValue the expected return value expected from dirname
2172 public function dirnameWithDataProvider($input, $expectedValue) {
2173 $this->assertEquals(
2175 t3lib_div
::dirname($input)
2180 /////////////////////////////////////
2181 // Tests concerning resolveBackPath
2182 /////////////////////////////////////
2185 * @see resolveBackPathWithDataProvider
2187 * @return array<array>
2189 public function resolveBackPathDataProvider() {
2191 'empty path' => array('', ''),
2192 'this directory' => array('./', './'),
2193 'relative directory without ..' => array('dir1/dir2/dir3/', 'dir1/dir2/dir3/'),
2194 'relative path without ..' => array('dir1/dir2/script.php', 'dir1/dir2/script.php'),
2195 'absolute directory without ..' => array('/dir1/dir2/dir3/', '/dir1/dir2/dir3/'),
2196 'absolute path without ..' => array('/dir1/dir2/script.php', '/dir1/dir2/script.php'),
2197 'only one directory upwards without trailing slash' => array('..', '..'),
2198 'only one directory upwards with trailing slash' => array('../', '../'),
2199 'one level with trailing ..' => array('dir1/..', ''),
2200 'one level with trailing ../' => array('dir1/../', ''),
2201 'two levels with trailing ..' => array('dir1/dir2/..', 'dir1'),
2202 'two levels with trailing ../' => array('dir1/dir2/../', 'dir1/'),
2203 'leading ../ without trailing /' => array('../dir1', '../dir1'),
2204 'leading ../ with trailing /' => array('../dir1/', '../dir1/'),
2205 'leading ../ and inside path' => array('../dir1/dir2/../dir3/', '../dir1/dir3/'),
2206 'one times ../ in relative directory' => array('dir1/../dir2/', 'dir2/'),
2207 'one times ../ in absolute directory' => array('/dir1/../dir2/', '/dir2/'),
2208 'one times ../ in relative path' => array('dir1/../dir2/script.php', 'dir2/script.php'),
2209 'one times ../ in absolute path' => array('/dir1/../dir2/script.php', '/dir2/script.php'),
2210 'consecutive ../' => array('dir1/dir2/dir3/../../../dir4', 'dir4'),
2211 'distrubuted ../ with trailing /' => array('dir1/../dir2/dir3/../', 'dir2/'),
2212 'distributed ../ without trailing /' => array('dir1/../dir2/dir3/..', 'dir2'),
2213 'multiple distributed and consecutive ../ together' => array('dir1/dir2/dir3/dir4/../../dir5/dir6/dir7/../dir8/', 'dir1/dir2/dir5/dir6/dir8/'),
2214 'multiple distributed and consecutive ../ together' => array('dir1/dir2/dir3/dir4/../../dir5/dir6/dir7/../dir8/', 'dir1/dir2/dir5/dir6/dir8/'),
2215 'dirname with leading ..' => array('dir1/..dir2/dir3/', 'dir1/..dir2/dir3/'),
2216 'dirname with trailing ..' => array('dir1/dir2../dir3/', 'dir1/dir2../dir3/'),
2217 'more times upwards than downwards in directory' => array('dir1/../../', '../'),
2218 'more times upwards than downwards in path' => array('dir1/../../script.php', '../script.php'),
2225 * @dataProvider resolveBackPathDataProvider
2227 * @param string $input the input for resolveBackPath
2228 * @param $expectedValue the expected return value from resolveBackPath
2230 public function resolveBackPathWithDataProvider($input, $expectedValue) {
2231 $this->assertEquals(
2233 t3lib_div
::resolveBackPath($input)
2238 /////////////////////////////////////////////////////////////////////////////////////
2239 // Tests concerning makeInstance, setSingletonInstance, addInstance, purgeInstances
2240 /////////////////////////////////////////////////////////////////////////////////////
2245 * @expectedException InvalidArgumentException
2247 public function makeInstanceWithEmptyClassNameThrowsException() {
2248 t3lib_div
::makeInstance('');
2254 public function makeInstanceReturnsClassInstance() {
2255 $className = get_class($this->getMock('foo'));
2258 t3lib_div
::makeInstance($className) instanceof $className
2265 public function makeInstancePassesParametersToConstructor() {
2266 $className = 'testingClass' . uniqid();
2267 if (!class_exists($className, FALSE)) {
2269 'class ' . $className . ' {' .
2270 ' public $constructorParameter1;' .
2271 ' public $constructorParameter2;' .
2272 ' public function __construct($parameter1, $parameter2) {' .
2273 ' $this->constructorParameter1 = $parameter1;' .
2274 ' $this->constructorParameter2 = $parameter2;' .
2280 $instance = t3lib_div
::makeInstance($className, 'one parameter', 'another parameter');
2282 $this->assertEquals(
2284 $instance->constructorParameter1
,
2285 'The first constructor parameter has not been set.'
2287 $this->assertEquals(
2288 'another parameter',
2289 $instance->constructorParameter2
,
2290 'The second constructor parameter has not been set.'
2297 public function makeInstanceCalledTwoTimesForNonSingletonClassReturnsDifferentInstances() {
2298 $className = get_class($this->getMock('foo'));
2300 $this->assertNotSame(
2301 t3lib_div
::makeInstance($className),
2302 t3lib_div
::makeInstance($className)
2309 public function makeInstanceCalledTwoTimesForSingletonClassReturnsSameInstance() {
2310 $className = get_class($this->getMock('t3lib_Singleton'));
2313 t3lib_div
::makeInstance($className),
2314 t3lib_div
::makeInstance($className)
2321 public function makeInstanceCalledTwoTimesForSingletonClassWithPurgeInstancesInbetweenReturnsDifferentInstances() {
2322 $className = get_class($this->getMock('t3lib_Singleton'));
2324 $instance = t3lib_div
::makeInstance($className);
2325 t3lib_div
::purgeInstances();
2327 $this->assertNotSame(
2329 t3lib_div
::makeInstance($className)
2335 * @expectedException InvalidArgumentException
2337 public function setSingletonInstanceForEmptyClassNameThrowsException() {
2338 $instance = $this->getMock('t3lib_Singleton');
2340 t3lib_div
::setSingletonInstance('', $instance);
2345 * @expectedException InvalidArgumentException
2347 public function setSingletonInstanceForClassThatIsNoSubclassOfProvidedClassThrowsException() {
2348 $instance = $this->getMock('t3lib_Singleton', array('foo'));
2349 $singletonClassName = get_class($this->getMock('t3lib_Singleton'));
2351 t3lib_div
::setSingletonInstance($singletonClassName, $instance);
2357 public function setSingletonInstanceMakesMakeInstanceReturnThatInstance() {
2358 $instance = $this->getMock('t3lib_Singleton');
2359 $singletonClassName = get_class($instance);
2361 t3lib_div
::setSingletonInstance($singletonClassName, $instance);
2365 t3lib_div
::makeInstance($singletonClassName)
2372 public function setSingletonInstanceCalledTwoTimesMakesMakeInstanceReturnLastSetInstance() {
2373 $instance1 = $this->getMock('t3lib_Singleton');
2374 $singletonClassName = get_class($instance1);
2375 $instance2 = new $singletonClassName();
2377 t3lib_div
::setSingletonInstance($singletonClassName, $instance1);
2378 t3lib_div
::setSingletonInstance($singletonClassName, $instance2);
2382 t3lib_div
::makeInstance($singletonClassName)
2388 * @expectedException InvalidArgumentException
2390 public function addInstanceForEmptyClassNameThrowsException() {
2391 $instance = $this->getMock('foo');
2393 t3lib_div
::addInstance('', $instance);
2398 * @expectedException InvalidArgumentException
2400 public function addInstanceForClassThatIsNoSubclassOfProvidedClassThrowsException() {
2401 $instance = $this->getMock('foo', array('bar'));
2402 $singletonClassName = get_class($this->getMock('foo'));
2404 t3lib_div
::addInstance($singletonClassName, $instance);
2409 * @expectedException InvalidArgumentException
2411 public function addInstanceWithSingletonInstanceThrowsException() {
2412 $instance = $this->getMock('t3lib_Singleton');
2414 t3lib_div
::addInstance(get_class($instance), $instance);
2420 public function addInstanceMakesMakeInstanceReturnThatInstance() {
2421 $instance = $this->getMock('foo');
2422 $className = get_class($instance);
2424 t3lib_div
::addInstance($className, $instance);
2428 t3lib_div
::makeInstance($className)
2435 public function makeInstanceCalledTwoTimesAfterAddInstanceReturnTwoDifferentInstances() {
2436 $instance = $this->getMock('foo');
2437 $className = get_class($instance);
2439 t3lib_div
::addInstance($className, $instance);
2441 $this->assertNotSame(
2442 t3lib_div
::makeInstance($className),
2443 t3lib_div
::makeInstance($className)
2450 public function addInstanceCalledTwoTimesMakesMakeInstanceReturnBothInstancesInAddingOrder() {
2451 $instance1 = $this->getMock('foo');
2452 $className = get_class($instance1);
2453 t3lib_div
::addInstance($className, $instance1);
2455 $instance2 = new $className();
2456 t3lib_div
::addInstance($className, $instance2);
2460 t3lib_div
::makeInstance($className),
2461 'The first returned instance does not match the first added instance.'
2465 t3lib_div
::makeInstance($className),
2466 'The second returned instance does not match the second added instance.'
2473 public function purgeInstancesDropsAddedInstance() {
2474 $instance = $this->getMock('foo');
2475 $className = get_class($instance);
2477 t3lib_div
::addInstance($className, $instance);
2478 t3lib_div
::purgeInstances();
2480 $this->assertNotSame(
2482 t3lib_div
::makeInstance($className)
2487 * Data provider for validPathStrDetectsInvalidCharacters.
2491 public function validPathStrInvalidCharactersDataProvider() {
2493 'double slash in path' => array('path//path'),
2494 'backslash in path' => array('path\\path'),
2495 'directory up in path' => array('path/../path'),
2496 'directory up at the beginning' => array('../path'),
2497 'NUL character in path' => array("path\x00path"),
2498 'BS character in path' => array("path\x08path"),
2503 * Tests whether invalid characters are detected.
2505 * @param string $path
2506 * @dataProvider validPathStrInvalidCharactersDataProvider
2509 public function validPathStrDetectsInvalidCharacters($path) {
2510 $this->assertNull(t3lib_div
::validPathStr($path));
2514 * Tests whether verifyFilenameAgainstDenyPattern detects the null character.
2518 public function verifyFilenameAgainstDenyPatternDetectsNullCharacter() {
2519 $this->assertFalse(t3lib_div
::verifyFilenameAgainstDenyPattern("image\x00.gif"));
2523 /////////////////////////////////////////////////////////////////////////////////////
2524 // Tests concerning sysLog
2525 /////////////////////////////////////////////////////////////////////////////////////
2530 public function syslogFixesPermissionsOnFileIfUsingFileLogging() {
2531 if (TYPO3_OS
== 'WIN') {
2532 $this->markTestSkipped('syslogFixesPermissionsOnFileIfUsingFileLogging() test not available on Windows.');
2535 // Fake all required settings
2536 $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLogLevel'] = 0;
2537 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit'] = TRUE;
2538 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog']);
2539 $testLogFilename = PATH_site
. 'typo3temp/' . uniqid('test_') . '.txt';
2540 $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLog'] = 'file,' . $testLogFilename . ',0';
2541 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
2543 // Call method, get actual permissions and clean up
2544 t3lib_div
::syslog('testLog', 'test', 1);
2546 $resultFilePermissions = substr(decoct(fileperms($testLogFilename)), 2);
2547 t3lib_div
::unlink_tempfile($testLogFilename);
2549 $this->assertEquals($resultFilePermissions, '0777');
2555 public function deprecationLogFixesPermissionsOnLogFile() {
2556 if (TYPO3_OS
== 'WIN') {
2557 $this->markTestSkipped('deprecationLogFixesPermissionsOnLogFile() test not available on Windows.');
2560 // Fake all required settings and get an unique logfilename
2561 $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = uniqid('test_');
2562 $deprecationLogFilename = t3lib_div
::getDeprecationLogFileName();
2563 $GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog'] = TRUE;
2564 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
2566 // Call method, get actual permissions and clean up
2567 t3lib_div
::deprecationLog('foo');
2569 $resultFilePermissions = substr(decoct(fileperms($deprecationLogFilename)), 2);
2570 @unlink
($deprecationLogFilename);
2572 $this->assertEquals($resultFilePermissions, '0777');