Fixed bug #16847: Use Tx_Phpunit_Service_TestFinder for finding the path of the Core...
[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 * Testcase for class t3lib_div
27 *
28 * @author Ingo Renner <ingo@typo3.org>
29 * @author Oliver Klee <typo3-coding@oliverklee.de>
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34 class t3lib_divTest extends tx_phpunit_testcase {
35
36 /**
37 * Enable backup of global and system variables
38 *
39 * @var boolean
40 */
41 protected $backupGlobals = TRUE;
42
43 /**
44 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
45 * because resource types cannot be handled during serializing
46 *
47 * @var array
48 */
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
50
51 public function tearDown() {
52 t3lib_div::purgeInstances();
53 }
54
55
56 ///////////////////////////////
57 // Tests concerning gif_compress
58 ///////////////////////////////
59
60 /**
61 * @test
62 */
63 public function gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() {
64 if (TYPO3_OS == 'WIN') {
65 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
66 }
67
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.');
70 }
71
72 $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
73 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
74
75 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress'] = TRUE;
76
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');
82
83 // Get actual permissions and clean up
84 clearstatcache();
85 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
86 t3lib_div::unlink_tempfile($testFilename);
87
88 $this->assertEquals($resultFilePermissions, '0777');
89 }
90
91 /**
92 * @test
93 */
94 public function gifCompressFixesPermissionOfConvertedFileIfUsingGd() {
95 if (TYPO3_OS == 'WIN') {
96 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
97 }
98
99 $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
100 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
101
102 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'] = TRUE;
103 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] = FALSE;
104
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');
110
111 // Get actual permissions and clean up
112 clearstatcache();
113 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
114 t3lib_div::unlink_tempfile($testFilename);
115
116 $this->assertEquals($resultFilePermissions, '0777');
117 }
118
119 ///////////////////////////////
120 // Tests concerning png_to_gif_by_imagemagick
121 ///////////////////////////////
122
123 /**
124 * @test
125 */
126 public function pngToGifByImagemagickFixesPermissionsOfConvertedFile() {
127 if (TYPO3_OS == 'WIN') {
128 $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available on Windows.');
129 }
130
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.');
133 }
134
135 $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
136 $fixturePngFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.png';
137
138 $GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif'] = TRUE;
139
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);
145
146 // Get actual permissions and clean up
147 clearstatcache();
148 $resultFilePermissions = substr(decoct(fileperms($newGifFile)), 2);
149 t3lib_div::unlink_tempfile($newGifFile);
150
151 $this->assertEquals($resultFilePermissions, '0777');
152 }
153
154 ///////////////////////////////
155 // Tests concerning read_png_gif
156 ///////////////////////////////
157
158 /**
159 * @test
160 */
161 public function readPngGifFixesPermissionsOfConvertedFile() {
162 if (TYPO3_OS == 'WIN') {
163 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available on Windows.');
164 }
165
166 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
167 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
168 }
169
170 $testFinder = t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder');
171 $testGifFile = $testFinder->getAbsoluteCoreTestsPath() . 't3lib/fixtures/clear.gif';
172
173 // Set target permissions and run method
174 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
175 $newPngFile = t3lib_div::read_png_gif($testGifFile, TRUE);
176
177 // Get actual permissions and clean up
178 clearstatcache();
179 $resultFilePermissions = substr(decoct(fileperms($newPngFile)), 2);
180 t3lib_div::unlink_tempfile($newPngFile);
181
182 $this->assertEquals($resultFilePermissions, '0777');
183 }
184
185 ///////////////////////////////
186 // Tests concerning validIP
187 ///////////////////////////////
188
189 /**
190 * Data provider for checkValidIpReturnsTrueForValidIp
191 *
192 * @return array Data sets
193 */
194 public static function validIpDataProvider() {
195 return array(
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'),
200 );
201 }
202
203 /**
204 * @test
205 * @dataProvider validIpDataProvider
206 */
207 public function validIpReturnsTrueForValidIp($ip) {
208 $this->assertTrue(t3lib_div::validIP($ip));
209 }
210
211 /**
212 * Data provider for checkValidIpReturnsFalseForInvalidIp
213 *
214 * @return array Data sets
215 */
216 public static function invalidIpDataProvider() {
217 return array(
218 'null' => array(null),
219 'zero' => array(0),
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'),
225 );
226 }
227
228 /**
229 * @test
230 * @dataProvider invalidIpDataProvider
231 */
232 public function validIpReturnsFalseForInvalidIp($ip) {
233 $this->assertFalse(t3lib_div::validIP($ip));
234 }
235
236
237 ///////////////////////////////
238 // Tests concerning testInt
239 ///////////////////////////////
240
241 /**
242 * Data provider for testIntReturnsTrue
243 *
244 * @return array Data sets
245 */
246 public function functionTestIntValidDataProvider() {
247 return array(
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'),
253 'zero' => array(0),
254 'zero as string' => array('0'),
255 );
256 }
257
258 /**
259 * @test
260 * @dataProvider functionTestIntValidDataProvider
261 */
262 public function testIntReturnsTrue($int) {
263 $this->assertTrue(t3lib_div::testInt($int));
264 }
265
266 /**
267 * Data provider for testIntReturnsFalse
268 *
269 * @return array Data sets
270 */
271 public function functionTestIntInvalidDataProvider() {
272 return array(
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')),
292 );
293 }
294
295 /**
296 * @test
297 * @dataProvider functionTestIntInvalidDataProvider
298 */
299 public function testIntReturnsFalse($int) {
300 $this->assertFalse(t3lib_div::testInt($int));
301 }
302
303
304 ///////////////////////////////
305 // Tests concerning isFirstPartOfStr
306 ///////////////////////////////
307
308 /**
309 * Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
310 *
311 * @return array
312 */
313 public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider() {
314 return array(
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),
320 );
321 }
322
323 /**
324 * @test
325 * @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
326 */
327 public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part) {
328 $this->assertTrue(t3lib_div::isFirstPartOfStr($string, $part));
329 }
330
331 /**
332 * Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
333 *
334 * @return array
335 */
336 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider() {
337 return array(
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),
354 );
355 }
356
357 /**
358 * @test
359 * @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
360 */
361 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part) {
362 $this->assertFalse(t3lib_div::isFirstPartOfStr($string, $part));
363 }
364
365
366 ///////////////////////////////
367 // Tests concerning splitCalc
368 ///////////////////////////////
369
370 /**
371 * Data provider for splitCalc
372 *
373 * @return array expected values, arithmetic expression
374 */
375 public function splitCalcDataProvider() {
376 return array(
377 'empty string returns empty array' => array(
378 array(),
379 '',
380 ),
381 'number without operator returns array with plus and number' => array(
382 array(array('+', 42)),
383 '42',
384 ),
385 'two numbers with asterisk return first number with plus and second number with asterisk' => array(
386 array(array('+', 42), array('*', 31)),
387 '42 * 31',
388 ),
389 );
390 }
391
392 /**
393 * @test
394 * @dataProvider splitCalcDataProvider
395 */
396 public function splitCalcCorrectlySplitsExpression($expected, $expression) {
397 $this->assertEquals($expected, t3lib_div::splitCalc($expression, '+-*/'));
398 }
399
400
401 //////////////////////////////////
402 // Tests concerning calcPriority
403 //////////////////////////////////
404
405 /**
406 * Data provider for calcPriority
407 *
408 * @return array expected values, arithmetic expression
409 */
410 public function calcPriorityDataProvider() {
411 return array(
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'),
423 );
424 }
425
426 /**
427 * @test
428 * @dataProvider calcPriorityDataProvider
429 */
430 public function calcPriorityCorrectlyCalculatesExpression($expected, $expression) {
431 $this->assertEquals($expected, t3lib_div::calcPriority($expression));
432 }
433
434
435 //////////////////////////////////
436 // Tests concerning calcPriority
437 //////////////////////////////////
438
439 /**
440 * Data provider for valid validEmail's
441 *
442 * @return array Valid email addresses
443 */
444 public function validEmailValidDataProvider() {
445 return array(
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'),
461 );
462 }
463
464 /**
465 * @test
466 * @dataProvider validEmailValidDataProvider
467 */
468 public function validEmailReturnsTrueForValidMailAddress($address) {
469 $this->assertTrue(t3lib_div::validEmail($address));
470 }
471
472 /**
473 * Data provider for invalid validEmail's
474 *
475 * @return array Invalid email addresses
476 */
477 public function validEmailInvalidDataProvider() {
478 return array(
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),
502 );
503 }
504
505 /**
506 * @test
507 * @dataProvider validEmailInvalidDataProvider
508 */
509 public function validEmailReturnsFalseForInvalidMailAddress($address) {
510 $this->assertFalse(t3lib_div::validEmail($address));
511 }
512
513
514 //////////////////////////////////
515 // Tests concerning intExplode
516 //////////////////////////////////
517
518 /**
519 * @test
520 */
521 public function intExplodeConvertsStringsToInteger() {
522 $testString = '1,foo,2';
523 $expectedArray = array(1, 0, 2);
524 $actualArray = t3lib_div::intExplode(',', $testString);
525
526 $this->assertEquals($expectedArray, $actualArray);
527 }
528
529
530 //////////////////////////////////
531 // Tests concerning revExplode
532 //////////////////////////////////
533
534 /**
535 * @test
536 */
537 public function revExplodeExplodesString() {
538 $testString = 'my:words:here';
539 $expectedArray = array('my:words', 'here');
540 $actualArray = t3lib_div::revExplode(':', $testString, 2);
541
542 $this->assertEquals($expectedArray, $actualArray);
543 }
544
545
546 //////////////////////////////////
547 // Tests concerning trimExplode
548 //////////////////////////////////
549
550 /**
551 * @test
552 */
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);
557
558 $this->assertEquals($expectedArray, $actualArray);
559 }
560
561 /**
562 * @test
563 */
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);
568
569 $this->assertEquals($expectedArray, $actualArray);
570 }
571
572 /**
573 * @test
574 */
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);
579
580 $this->assertEquals($expectedArray, $actualArray);
581 }
582
583 /**
584 * @test
585 */
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);
591
592 $this->assertEquals($expectedArray, $actualArray);
593 }
594
595 /**
596 * @test
597 */
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);
603
604 $this->assertEquals($expectedArray, $actualArray);
605 }
606
607 /**
608 * @test
609 */
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);
615
616 $this->assertEquals($expectedArray, $actualArray);
617 }
618
619 /**
620 * @test
621 */
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);
627
628 $this->assertEquals($expectedArray, $actualArray);
629 }
630
631 /**
632 * @test
633 */
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);
639
640 $this->assertEquals($expectedArray, $actualArray);
641 }
642
643 /**
644 * @test
645 */
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);
650
651 $this->assertEquals($expectedArray, $actualArray);
652 }
653
654
655 //////////////////////////////////
656 // Tests concerning removeArrayEntryByValue
657 //////////////////////////////////
658
659 /**
660 * @test
661 */
662 public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
663 $inputArray = array(
664 '0' => 'test1',
665 '1' => 'test2',
666 '2' => 'test3',
667 '3' => 'test2',
668 );
669 $compareValue = 'test2';
670 $expectedResult = array(
671 '0' => 'test1',
672 '2' => 'test3',
673 );
674 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
675 $this->assertEquals($expectedResult, $actualResult);
676 }
677
678 /**
679 * @test
680 */
681 public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
682 $inputArray = array(
683 '0' => 'foo',
684 '1' => array(
685 '10' => 'bar',
686 ),
687 '2' => 'bar',
688 );
689 $compareValue = 'bar';
690 $expectedResult = array(
691 '0' => 'foo',
692 '1' => array(),
693 );
694 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
695 $this->assertEquals($expectedResult, $actualResult);
696 }
697
698 /**
699 * @test
700 */
701 public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
702 $inputArray = array(
703 '0' => 'foo',
704 '1' => '',
705 '2' => 'bar',
706 );
707 $compareValue = '';
708 $expectedResult = array(
709 '0' => 'foo',
710 '2' => 'bar',
711 );
712 $actualResult = t3lib_div::removeArrayEntryByValue($inputArray, $compareValue);
713 $this->assertEquals($expectedResult, $actualResult);
714 }
715
716 //////////////////////////////////
717 // Tests concerning getBytesFromSizeMeasurement
718 //////////////////////////////////
719
720 /**
721 * Data provider for getBytesFromSizeMeasurement
722 *
723 * @return array expected value, input string
724 */
725 public function getBytesFromSizeMeasurementDataProvider() {
726 return array(
727 '100 kilo Bytes' => array('102400', '100k'),
728 '100 mega Bytes' => array('104857600', '100m'),
729 '100 giga Bytes' => array('107374182400', '100g'),
730 );
731 }
732
733 /**
734 * @test
735 * @dataProvider getBytesFromSizeMeasurementDataProvider
736 */
737 public function getBytesFromSizeMeasurementCalculatesCorrectByteValue($expected, $byteString) {
738 $this->assertEquals($expected, t3lib_div::getBytesFromSizeMeasurement($byteString));
739 }
740
741
742 //////////////////////////////////
743 // Tests concerning getIndpEnv
744 //////////////////////////////////
745
746 /**
747 * @test
748 */
749 public function getIndpEnvTypo3SitePathReturnNonEmptyString() {
750 $this->assertTrue(strlen(t3lib_div::getIndpEnv('TYPO3_SITE_PATH')) >= 1);
751 }
752
753 /**
754 * @test
755 */
756 public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() {
757 $result = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
758 $this->assertEquals('/', $result[0]);
759 }
760
761 /**
762 * @test
763 */
764 public function getIndpEnvTypo3SitePathReturnsStringEndingWithSlash() {
765 $result = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
766 $this->assertEquals('/', $result[strlen($result) - 1]);
767 }
768
769
770 //////////////////////////////////
771 // Tests concerning underscoredToUpperCamelCase
772 //////////////////////////////////
773
774 /**
775 * Data provider for underscoredToUpperCamelCase
776 *
777 * @return array expected, input string
778 */
779 public function underscoredToUpperCamelCaseDataProvider() {
780 return array(
781 'single word' => array('Blogexample', 'blogexample'),
782 'multiple words' => array('BlogExample', 'blog_example'),
783 );
784 }
785
786 /**
787 * @test
788 * @dataProvider underscoredToUpperCamelCaseDataProvider
789 */
790 public function underscoredToUpperCamelCase($expected, $inputString) {
791 $this->assertEquals($expected, t3lib_div::underscoredToUpperCamelCase($inputString));
792 }
793
794
795 //////////////////////////////////
796 // Tests concerning underscoredToLowerCamelCase
797 //////////////////////////////////
798
799 /**
800 * Data provider for underscoredToLowerCamelCase
801 *
802 * @return array expected, input string
803 */
804 public function underscoredToLowerCamelCaseDataProvider() {
805 return array(
806 'single word' => array('minimalvalue', 'minimalvalue'),
807 'multiple words' => array('minimalValue', 'minimal_value'),
808 );
809 }
810
811 /**
812 * @test
813 * @dataProvider underscoredToLowerCamelCaseDataProvider
814 */
815 public function underscoredToLowerCamelCase($expected, $inputString) {
816 $this->assertEquals($expected, t3lib_div::underscoredToLowerCamelCase($inputString));
817 }
818
819 //////////////////////////////////
820 // Tests concerning camelCaseToLowerCaseUnderscored
821 //////////////////////////////////
822
823 /**
824 * Data provider for camelCaseToLowerCaseUnderscored
825 *
826 * @return array expected, input string
827 */
828 public function camelCaseToLowerCaseUnderscoredDataProvider() {
829 return array(
830 'single word' => array('blogexample', 'blogexample'),
831 'single word starting upper case' => array('blogexample', 'Blogexample'),
832 'two words starting lower case' => array('minimal_value', 'minimalValue'),
833 'two words starting upper case' => array('blog_example', 'BlogExample'),
834 );
835 }
836
837 /**
838 * @test
839 * @dataProvider camelCaseToLowerCaseUnderscoredDataProvider
840 */
841 public function camelCaseToLowerCaseUnderscored($expected, $inputString) {
842 $this->assertEquals($expected, t3lib_div::camelCaseToLowerCaseUnderscored($inputString));
843 }
844
845
846 //////////////////////////////////
847 // Tests concerning lcFirst
848 //////////////////////////////////
849
850 /**
851 * Data provider for lcFirst
852 *
853 * @return array expected, input string
854 */
855 public function lcfirstDataProvider() {
856 return array(
857 'single word' => array('blogexample', 'blogexample'),
858 'single Word starting upper case' => array('blogexample', 'Blogexample'),
859 'two words' => array('blogExample', 'BlogExample'),
860 );
861 }
862
863 /**
864 * @test
865 * @dataProvider lcfirstDataProvider
866 */
867 public function lcFirst($expected, $inputString) {
868 $this->assertEquals($expected, t3lib_div::lcfirst($inputString));
869 }
870
871
872 //////////////////////////////////
873 // Tests concerning encodeHeader
874 //////////////////////////////////
875
876 /**
877 * @test
878 */
879 public function encodeHeaderEncodesWhitespacesInQuotedPrintableMailHeader() {
880 $this->assertEquals(
881 '=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=',
882 t3lib_div::encodeHeader(
883 "We test whether the copyright character \xc2\xa9 is encoded correctly",
884 'quoted-printable',
885 'utf-8'
886 )
887 );
888 }
889
890 /**
891 * @test
892 */
893 public function encodeHeaderEncodesQuestionmarksInQuotedPrintableMailHeader() {
894 $this->assertEquals(
895 '=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=',
896 t3lib_div::encodeHeader(
897 "Is the copyright character \xc2\xa9 really encoded correctly? Really?",
898 'quoted-printable',
899 'utf-8'
900 )
901 );
902 }
903
904
905 //////////////////////////////////
906 // Tests concerning isValidUrl
907 //////////////////////////////////
908
909 /**
910 * Data provider for valid isValidUrl's
911 *
912 * @return array Valid ressource
913 */
914 public function validUrlValidRessourceDataProvider() {
915 return array(
916 'http' => array('http://www.example.org/'),
917 'http without trailing slash' => array('http://qwe'),
918 'http directory with trailing slash' => array('http://www.example/img/dir/'),
919 'http directory without trailing slash' => array('http://www.example/img/dir'),
920 'http index.html' => array('http://example.com/index.html'),
921 'http index.php' => array('http://www.example.com/index.php'),
922 'http test.png' => array('http://www.example/img/test.png'),
923 'http username password querystring and ancher' => array('https://user:pw@www.example.org:80/path?arg=value#fragment'),
924 'file' => array('file:///tmp/test.c'),
925 'file directory' => array('file://foo/bar'),
926 'ftp directory' => array('ftp://ftp.example.com/tmp/'),
927 'mailto' => array('mailto:foo@bar.com'),
928 'news' => array('news:news.php.net'),
929 'telnet'=> array('telnet://192.0.2.16:80/'),
930 'ldap' => array('ldap://[2001:db8::7]/c=GB?objectClass?one'),
931 );
932 }
933
934 /**
935 * @test
936 * @dataProvider validUrlValidRessourceDataProvider
937 */
938 public function validURLReturnsTrueForValidRessource($url) {
939 $this->assertTrue(t3lib_div::isValidUrl($url));
940 }
941
942 /**
943 * Data provider for invalid isValidUrl's
944 *
945 * @return array Invalid ressource
946 */
947 public function isValidUrlInvalidRessourceDataProvider() {
948 return array(
949 'http missing colon' => array('http//www.example/wrong/url/'),
950 'http missing slash' => array('http:/www.example'),
951 'hostname only' => array('www.example.org/'),
952 'file missing protocol specification' => array('/tmp/test.c'),
953 'slash only' => array('/'),
954 'string http://' => array('http://'),
955 'string http:/' => array('http:/'),
956 'string http:' => array('http:'),
957 'string http' => array('http'),
958 'empty string' => array(''),
959 'string -1' => array('-1'),
960 'string array()' => array('array()'),
961 'random string' => array('qwe'),
962 );
963 }
964
965 /**
966 * @test
967 * @dataProvider isValidUrlInvalidRessourceDataProvider
968 */
969 public function validURLReturnsFalseForInvalidRessoure($url) {
970 $this->assertFalse(t3lib_div::isValidUrl($url));
971 }
972
973
974 //////////////////////////////////
975 // Tests concerning isOnCurrentHost
976 //////////////////////////////////
977
978 /**
979 * @test
980 */
981 public function isOnCurrentHostReturnsTrueWithCurrentHost() {
982 $testUrl = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
983 $this->assertTrue(t3lib_div::isOnCurrentHost($testUrl));
984 }
985
986 /**
987 * Data provider for invalid isOnCurrentHost's
988 *
989 * @return array Invalid Hosts
990 */
991 public function checkisOnCurrentHostInvalidHosts() {
992 return array(
993 'empty string' => array(''),
994 'arbitrary string' => array('arbitrary string'),
995 'localhost IP' => array('127.0.0.1'),
996 'relative path' => array('./relpath/file.txt'),
997 'absolute path' => array('/abspath/file.txt?arg=value'),
998 'differnt host' => array(t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org'),
999 );
1000 }
1001
1002
1003 ////////////////////////////////////////
1004 // Tests concerning sanitizeLocalUrl
1005 ////////////////////////////////////////
1006
1007 /**
1008 * Data provider for valid sanitizeLocalUrl's
1009 *
1010 * @return array Valid url
1011 */
1012 public function sanitizeLocalUrlValidUrlDataProvider() {
1013 $subDirectory = t3lib_div::getIndpEnv('TYPO3_SITE_PATH');
1014 $typo3SiteUrl = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
1015 $typo3RequestHost = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST');
1016
1017 return array(
1018 'alt_intro.php' => array('alt_intro.php'),
1019 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
1020 $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
1021 $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
1022 '../index.php' => array('../index.php'),
1023 '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
1024 '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
1025 '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
1026 PATH_site . 'typo3/alt_intro.php' => array(PATH_site . 'typo3/alt_intro.php'),
1027 $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
1028 $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php'),
1029 );
1030 }
1031
1032 /**
1033 * @test
1034 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1035 */
1036 public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
1037 $this->assertEquals($url, t3lib_div::sanitizeLocalUrl($url));
1038 }
1039
1040 /**
1041 * @test
1042 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1043 */
1044 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
1045 $this->assertEquals(rawurlencode($url), t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
1046 }
1047
1048 /**
1049 * Data provider for invalid sanitizeLocalUrl's
1050 *
1051 * @return array Valid url
1052 */
1053 public function sanitizeLocalUrlInvalidDataProvider() {
1054 return array(
1055 'empty string' => array(''),
1056 'http domain' => array('http://www.google.de/'),
1057 'https domain' => array('https://www.google.de/'),
1058 'relative path with XSS' => array('../typo3/whatever.php?argument=javascript:alert(0)'),
1059 );
1060 }
1061
1062 /**
1063 * @test
1064 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1065 */
1066 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
1067 $this->assertEquals('', t3lib_div::sanitizeLocalUrl($url));
1068 }
1069
1070 /**
1071 * @test
1072 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1073 */
1074 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
1075 $this->assertEquals('', t3lib_div::sanitizeLocalUrl(rawurlencode($url)));
1076 }
1077
1078
1079 //////////////////////////////////////
1080 // Tests concerning arrayDiffAssocRecursive
1081 //////////////////////////////////////
1082
1083 /**
1084 * @test
1085 */
1086 public function arrayDiffAssocRecursiveHandlesOneDimensionalArrays() {
1087 $array1 = array(
1088 'key1' => 'value1',
1089 'key2' => 'value2',
1090 'key3' => 'value3',
1091 );
1092 $array2 = array(
1093 'key1' => 'value1',
1094 'key3' => 'value3',
1095 );
1096 $expectedResult = array(
1097 'key2' => 'value2',
1098 );
1099 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
1100 $this->assertEquals($expectedResult, $actualResult);
1101 }
1102
1103 /**
1104 * @test
1105 */
1106 public function arrayDiffAssocRecursiveHandlesMultiDimensionalArrays() {
1107 $array1 = array(
1108 'key1' => 'value1',
1109 'key2' => array(
1110 'key21' => 'value21',
1111 'key22' => 'value22',
1112 'key23' => array(
1113 'key231' => 'value231',
1114 'key232' => 'value232',
1115 ),
1116 ),
1117 );
1118 $array2 = array(
1119 'key1' => 'value1',
1120 'key2' => array(
1121 'key21' => 'value21',
1122 'key23' => array(
1123 'key231' => 'value231',
1124 ),
1125 ),
1126 );
1127 $expectedResult = array(
1128 'key2' => array(
1129 'key22' => 'value22',
1130 'key23' => array(
1131 'key232' => 'value232',
1132 ),
1133 ),
1134 );
1135 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
1136 $this->assertEquals($expectedResult, $actualResult);
1137 }
1138
1139 /**
1140 * @test
1141 */
1142 public function arrayDiffAssocRecursiveHandlesMixedArrays() {
1143 $array1 = array(
1144 'key1' => array(
1145 'key11' => 'value11',
1146 'key12' => 'value12',
1147 ),
1148 'key2' => 'value2',
1149 'key3' => 'value3',
1150 );
1151 $array2 = array(
1152 'key1' => 'value1',
1153 'key2' => array(
1154 'key21' => 'value21',
1155 ),
1156 );
1157 $expectedResult = array(
1158 'key3' => 'value3',
1159 );
1160 $actualResult = t3lib_div::arrayDiffAssocRecursive($array1, $array2);
1161 $this->assertEquals($expectedResult, $actualResult);
1162 }
1163
1164
1165 //////////////////////////////////////
1166 // Tests concerning removeDotsFromTS
1167 //////////////////////////////////////
1168
1169 /**
1170 * @test
1171 */
1172 public function removeDotsFromTypoScriptSucceedsWithDottedArray() {
1173 $typoScript = array(
1174 'propertyA.' => array(
1175 'keyA.' => array(
1176 'valueA' => 1,
1177 ),
1178 'keyB' => 2,
1179 ),
1180 'propertyB' => 3,
1181 );
1182
1183 $expectedResult = array(
1184 'propertyA' => array(
1185 'keyA' => array(
1186 'valueA' => 1,
1187 ),
1188 'keyB' => 2,
1189 ),
1190 'propertyB' => 3,
1191 );
1192
1193 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
1194 }
1195
1196 /**
1197 * @test
1198 */
1199 public function removeDotsFromTypoScriptOverridesSubArray() {
1200 $typoScript = array(
1201 'propertyA.' => array(
1202 'keyA' => 'getsOverridden',
1203 'keyA.' => array(
1204 'valueA' => 1,
1205 ),
1206 'keyB' => 2,
1207 ),
1208 'propertyB' => 3,
1209 );
1210
1211 $expectedResult = array(
1212 'propertyA' => array(
1213 'keyA' => array(
1214 'valueA' => 1,
1215 ),
1216 'keyB' => 2,
1217 ),
1218 'propertyB' => 3,
1219 );
1220
1221 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
1222 }
1223
1224 /**
1225 * @test
1226 */
1227 public function removeDotsFromTypoScriptOverridesWithScalar() {
1228 $typoScript = array(
1229 'propertyA.' => array(
1230 'keyA.' => array(
1231 'valueA' => 1,
1232 ),
1233 'keyA' => 'willOverride',
1234 'keyB' => 2,
1235 ),
1236 'propertyB' => 3,
1237 );
1238
1239 $expectedResult = array(
1240 'propertyA' => array(
1241 'keyA' => 'willOverride',
1242 'keyB' => 2,
1243 ),
1244 'propertyB' => 3,
1245 );
1246
1247 $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
1248 }
1249
1250
1251 //////////////////////////////////////
1252 // Tests concerning get_dirs
1253 //////////////////////////////////////
1254
1255 /**
1256 * @test
1257 */
1258 public function getDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
1259 $path = PATH_t3lib;
1260 $directories = t3lib_div::get_dirs($path);
1261
1262 $this->assertType('array', $directories);
1263 }
1264
1265 /**
1266 * @test
1267 */
1268 public function getDirsReturnsStringErrorOnPathFailure() {
1269 $path = 'foo';
1270 $result = t3lib_div::get_dirs($path);
1271 $expectedResult = 'error';
1272
1273 $this->assertEquals($expectedResult, $result);
1274 }
1275
1276
1277 //////////////////////////////////
1278 // Tests concerning hmac
1279 //////////////////////////////////
1280
1281 /**
1282 * @test
1283 */
1284 public function hmacReturnsHashOfProperLength() {
1285 $hmac = t3lib_div::hmac('message');
1286 $this->assertTrue(!empty($hmac) && is_string($hmac));
1287 $this->assertTrue(strlen($hmac) == 40);
1288 }
1289
1290 /**
1291 * @test
1292 */
1293 public function hmacReturnsEqualHashesForEqualInput() {
1294 $msg0 = 'message';
1295 $msg1 = 'message';
1296 $this->assertEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
1297 }
1298
1299 /**
1300 * @test
1301 */
1302 public function hmacReturnsNoEqualHashesForNonEqualInput() {
1303 $msg0 = 'message0';
1304 $msg1 = 'message1';
1305 $this->assertNotEquals(t3lib_div::hmac($msg0), t3lib_div::hmac($msg1));
1306 }
1307
1308
1309 //////////////////////////////////
1310 // Tests concerning quoteJSvalue
1311 //////////////////////////////////
1312
1313 /**
1314 * @test
1315 */
1316 public function quoteJSvalueHtmlspecialcharsDataByDefault() {
1317 $this->assertContains(
1318 '&gt;',
1319 t3lib_div::quoteJSvalue('>')
1320 );
1321 }
1322
1323 /**
1324 * @test
1325 */
1326 public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
1327 $this->assertContains(
1328 '&gt;',
1329 t3lib_div::quoteJSvalue('>', false)
1330 );
1331 }
1332
1333 /**
1334 * @test
1335 */
1336 public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
1337 $this->assertContains(
1338 '>',
1339 t3lib_div::quoteJSvalue('>', true)
1340 );
1341 }
1342
1343 /**
1344 * @test
1345 */
1346 public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
1347 $this->assertEquals(
1348 "''",
1349 t3lib_div::quoteJSvalue("", true)
1350 );
1351 }
1352
1353 /**
1354 * @test
1355 */
1356 public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
1357 $this->assertEquals(
1358 "'Hello world!'",
1359 t3lib_div::quoteJSvalue("Hello world!", true)
1360 );
1361 }
1362
1363 /**
1364 * @test
1365 */
1366 public function quoteJSvalueEscapesSingleQuote() {
1367 $this->assertEquals(
1368 "'\\''",
1369 t3lib_div::quoteJSvalue("'", true)
1370 );
1371 }
1372
1373 /**
1374 * @test
1375 */
1376 public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
1377 $this->assertEquals(
1378 "'\\\"'",
1379 t3lib_div::quoteJSvalue('"', true)
1380 );
1381 }
1382
1383 /**
1384 * @test
1385 */
1386 public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
1387 $this->assertEquals(
1388 "'\\&quot;'",
1389 t3lib_div::quoteJSvalue('"', false)
1390 );
1391 }
1392
1393 /**
1394 * @test
1395 */
1396 public function quoteJSvalueEscapesTab() {
1397 $this->assertEquals(
1398 "'" . '\t' . "'",
1399 t3lib_div::quoteJSvalue(TAB)
1400 );
1401 }
1402
1403 /**
1404 * @test
1405 */
1406 public function quoteJSvalueEscapesLinefeed() {
1407 $this->assertEquals(
1408 "'" . '\n' . "'",
1409 t3lib_div::quoteJSvalue(LF)
1410 );
1411 }
1412
1413 /**
1414 * @test
1415 */
1416 public function quoteJSvalueEscapesCarriageReturn() {
1417 $this->assertEquals(
1418 "'" . '\r' . "'",
1419 t3lib_div::quoteJSvalue(CR)
1420 );
1421 }
1422
1423 /**
1424 * @test
1425 */
1426 public function quoteJSvalueEscapesBackslah() {
1427 $this->assertEquals(
1428 "'\\\\'",
1429 t3lib_div::quoteJSvalue('\\')
1430 );
1431 }
1432
1433 //////////////////////////////////
1434 // Tests concerning readLLfile
1435 //////////////////////////////////
1436
1437 /**
1438 * @test
1439 */
1440 public function readLLfileHandlesLocallangXMLOverride() {
1441 $unique = uniqid('locallangXMLOverrideTest');
1442
1443 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
1444 <T3locallang>
1445 <data type="array">
1446 <languageKey index="default" type="array">
1447 <label index="buttons.logout">EXIT</label>
1448 </languageKey>
1449 </data>
1450 </T3locallang>';
1451
1452 $file = PATH_site . 'typo3temp/' . $unique . '.xml';
1453 t3lib_div::writeFileToTypo3tempDir($file, $xml);
1454
1455 // Get default value
1456 $defaultLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1457
1458 // Set override file
1459 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
1460
1461 // Get override value
1462 $overrideLL = t3lib_div::readLLfile('EXT:lang/locallang_core.xml', 'default');
1463
1464 // Clean up again
1465 unlink($file);
1466
1467 $this->assertNotEquals($overrideLL['default']['buttons.logout'], '');
1468 $this->assertNotEquals($defaultLL['default']['buttons.logout'], $overrideLL['default']['buttons.logout']);
1469 $this->assertEquals($overrideLL['default']['buttons.logout'], 'EXIT');
1470 }
1471
1472
1473 ///////////////////////////////
1474 // Tests concerning _GETset()
1475 ///////////////////////////////
1476
1477 /**
1478 * @test
1479 */
1480 public function getSetWritesArrayToGetSystemVariable() {
1481 $_GET = array();
1482 $GLOBALS['HTTP_GET_VARS'] = array();
1483
1484 $getParameters = array('foo' => 'bar');
1485 t3lib_div::_GETset($getParameters);
1486 $this->assertSame($getParameters, $_GET);
1487 }
1488
1489 /**
1490 * @test
1491 */
1492 public function getSetWritesArrayToGlobalsHttpGetVars() {
1493 $_GET = array();
1494 $GLOBALS['HTTP_GET_VARS'] = array();
1495
1496 $getParameters = array('foo' => 'bar');
1497 t3lib_div::_GETset($getParameters);
1498 $this->assertSame($getParameters, $GLOBALS['HTTP_GET_VARS']);
1499 }
1500
1501 /**
1502 * @test
1503 */
1504 public function getSetForArrayDropsExistingValues() {
1505 $_GET = array();
1506 $GLOBALS['HTTP_GET_VARS'] = array();
1507
1508 t3lib_div::_GETset(array('foo' => 'bar'));
1509
1510 t3lib_div::_GETset(array('oneKey' => 'oneValue'));
1511
1512 $this->assertEquals(
1513 array('oneKey' => 'oneValue'),
1514 $GLOBALS['HTTP_GET_VARS']
1515 );
1516 }
1517
1518 /**
1519 * @test
1520 */
1521 public function getSetAssignsOneValueToOneKey() {
1522 $_GET = array();
1523 $GLOBALS['HTTP_GET_VARS'] = array();
1524
1525 t3lib_div::_GETset('oneValue', 'oneKey');
1526
1527 $this->assertEquals(
1528 'oneValue',
1529 $GLOBALS['HTTP_GET_VARS']['oneKey']
1530 );
1531 }
1532
1533 /**
1534 * @test
1535 */
1536 public function getSetForOneValueDoesNotDropUnrelatedValues() {
1537 $_GET = array();
1538 $GLOBALS['HTTP_GET_VARS'] = array();
1539
1540 t3lib_div::_GETset(array('foo' => 'bar'));
1541 t3lib_div::_GETset('oneValue', 'oneKey');
1542
1543 $this->assertEquals(
1544 array('foo' => 'bar', 'oneKey' => 'oneValue'),
1545 $GLOBALS['HTTP_GET_VARS']
1546 );
1547 }
1548
1549 /**
1550 * @test
1551 */
1552 public function getSetCanAssignsAnArrayToASpecificArrayElement() {
1553 $_GET = array();
1554 $GLOBALS['HTTP_GET_VARS'] = array();
1555
1556 t3lib_div::_GETset(array('childKey' => 'oneValue'), 'parentKey');
1557
1558 $this->assertEquals(
1559 array('parentKey' => array('childKey' => 'oneValue')),
1560 $GLOBALS['HTTP_GET_VARS']
1561 );
1562 }
1563
1564 /**
1565 * @test
1566 */
1567 public function getSetCanAssignAStringValueToASpecificArrayChildElement() {
1568 $_GET = array();
1569 $GLOBALS['HTTP_GET_VARS'] = array();
1570
1571 t3lib_div::_GETset('oneValue', 'parentKey|childKey');
1572
1573 $this->assertEquals(
1574 array('parentKey' => array('childKey' => 'oneValue')),
1575 $GLOBALS['HTTP_GET_VARS']
1576 );
1577 }
1578
1579 /**
1580 * @test
1581 */
1582 public function getSetCanAssignAnArrayToASpecificArrayChildElement() {
1583 $_GET = array();
1584 $GLOBALS['HTTP_GET_VARS'] = array();
1585
1586 t3lib_div::_GETset(
1587 array('key1' => 'value1', 'key2' => 'value2'),
1588 'parentKey|childKey'
1589 );
1590
1591 $this->assertEquals(
1592 array(
1593 'parentKey' => array(
1594 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
1595 )
1596 ),
1597 $GLOBALS['HTTP_GET_VARS']
1598 );
1599 }
1600
1601
1602 ///////////////////////////////
1603 // Tests concerning fixPermissions
1604 ///////////////////////////////
1605
1606 /**
1607 * @test
1608 */
1609 public function fixPermissionsCorrectlySetsPermissionsToFile() {
1610 if (TYPO3_OS == 'WIN') {
1611 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1612 }
1613
1614 // Create and prepare test file
1615 $filename = PATH_site . 'typo3temp/' . uniqid('test_');
1616 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1617 chmod($filename, 0742);
1618
1619 // Set target permissions and run method
1620 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1621 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1622 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1623
1624 // Get actual permissions and clean up
1625 clearstatcache();
1626 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1627 $resultFileGroup = filegroup($filename);
1628 unlink($filename);
1629
1630 // Test if everything was ok
1631 $this->assertTrue($fixPermissionsResult);
1632 $this->assertEquals($resultFilePermissions, '0660');
1633 $this->assertEquals($resultFileGroup, posix_getegid());
1634 }
1635
1636 /**
1637 * @test
1638 */
1639 public function fixPermissionsCorrectlySetsPermissionsToHiddenFile() {
1640 if (TYPO3_OS == 'WIN') {
1641 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1642 }
1643
1644 // Create and prepare test file
1645 $filename = PATH_site . 'typo3temp/' . uniqid('.test_');
1646 t3lib_div::writeFileToTypo3tempDir($filename, '42');
1647 chmod($filename, 0742);
1648
1649 // Set target permissions and run method
1650 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1651 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1652 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1653
1654 // Get actual permissions and clean up
1655 clearstatcache();
1656 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1657 $resultFileGroup = filegroup($filename);
1658 unlink($filename);
1659
1660 // Test if everything was ok
1661 $this->assertTrue($fixPermissionsResult);
1662 $this->assertEquals($resultFilePermissions, '0660');
1663 $this->assertEquals($resultFileGroup, posix_getegid());
1664 }
1665
1666 /**
1667 * @test
1668 */
1669 public function fixPermissionsCorrectlySetsPermissionsToDirectory() {
1670 if (TYPO3_OS == 'WIN') {
1671 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1672 }
1673
1674 // Create and prepare test directory
1675 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1676 t3lib_div::mkdir($directory);
1677 chmod($directory, 1551);
1678
1679 // Set target permissions and run method
1680 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1681 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1682 $fixPermissionsResult = t3lib_div::fixPermissions($directory . '/');
1683
1684 // Get actual permissions and clean up
1685 clearstatcache();
1686 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1687 $resultDirectoryGroup = filegroup($directory);
1688 t3lib_div::rmdir($directory);
1689
1690 // Test if everything was ok
1691 $this->assertTrue($fixPermissionsResult);
1692 $this->assertEquals($resultDirectoryPermissions, '0770');
1693 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1694 }
1695
1696 /**
1697 * @test
1698 */
1699 public function fixPermissionsCorrectlySetsPermissionsToHiddenDirectory() {
1700 if (TYPO3_OS == 'WIN') {
1701 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1702 }
1703
1704 // Create and prepare test directory
1705 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1706 t3lib_div::mkdir($directory);
1707 chmod($directory, 1551);
1708
1709 // Set target permissions and run method
1710 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1711 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1712 $fixPermissionsResult = t3lib_div::fixPermissions($directory);
1713
1714 // Get actual permissions and clean up
1715 clearstatcache();
1716 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
1717 $resultDirectoryGroup = filegroup($directory);
1718 t3lib_div::rmdir($directory);
1719
1720 // Test if everything was ok
1721 $this->assertTrue($fixPermissionsResult);
1722 $this->assertEquals($resultDirectoryPermissions, '0770');
1723 $this->assertEquals($resultDirectoryGroup, posix_getegid());
1724 }
1725
1726 /**
1727 * @test
1728 */
1729 public function fixPermissionsCorrectlySetsPermissionsRecursive() {
1730 if (TYPO3_OS == 'WIN') {
1731 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1732 }
1733
1734 // Create and prepare test directory and file structure
1735 $baseDirectory = PATH_site . 'typo3temp/' . uniqid('test_');
1736 t3lib_div::mkdir($baseDirectory);
1737 chmod($baseDirectory, 1751);
1738 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
1739 chmod($baseDirectory . '/file', 0742);
1740 t3lib_div::mkdir($baseDirectory . '/foo');
1741 chmod($baseDirectory . '/foo', 1751);
1742 t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
1743 chmod($baseDirectory . '/foo/file', 0742);
1744 t3lib_div::mkdir($baseDirectory . '/.bar');
1745 chmod($baseDirectory . '/.bar', 1751);
1746 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
1747 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
1748 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
1749 touch($baseDirectory . '/.bar/.file', '42');
1750 chmod($baseDirectory . '/.bar/.file', 0742);
1751 touch($baseDirectory . '/.bar/..file2', '42');
1752 chmod($baseDirectory . '/.bar/..file2', 0742);
1753
1754 // Set target permissions and run method
1755 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1756 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
1757 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = posix_getegid();
1758 $fixPermissionsResult = t3lib_div::fixPermissions($baseDirectory, TRUE);
1759
1760 // Get actual permissions
1761 clearstatcache();
1762 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
1763 $resultBaseDirectoryGroup = filegroup($baseDirectory);
1764 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
1765 $resultBaseFileGroup = filegroup($baseDirectory . '/file');
1766 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
1767 $resultFooDirectoryGroup = filegroup($baseDirectory . '/foo');
1768 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
1769 $resultFooFileGroup = filegroup($baseDirectory . '/foo/file');
1770 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
1771 $resultBarDirectoryGroup = filegroup($baseDirectory . '/.bar');
1772 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
1773 $resultBarFileGroup = filegroup($baseDirectory . '/.bar/.file');
1774 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
1775 $resultBarFile2Group = filegroup($baseDirectory . '/.bar/..file2');
1776
1777 // Clean up
1778 unlink($baseDirectory . '/file');
1779 unlink($baseDirectory . '/foo/file');
1780 unlink($baseDirectory . '/.bar/.file');
1781 unlink($baseDirectory . '/.bar/..file2');
1782 t3lib_div::rmdir($baseDirectory . '/foo');
1783 t3lib_div::rmdir($baseDirectory . '/.bar');
1784 t3lib_div::rmdir($baseDirectory);
1785
1786 // Test if everything was ok
1787 $this->assertTrue($fixPermissionsResult);
1788 $this->assertEquals($resultBaseDirectoryPermissions, '0770');
1789 $this->assertEquals($resultBaseDirectoryGroup, posix_getegid());
1790 $this->assertEquals($resultBaseFilePermissions, '0660');
1791 $this->assertEquals($resultBaseFileGroup, posix_getegid());
1792 $this->assertEquals($resultFooDirectoryPermissions, '0770');
1793 $this->assertEquals($resultFooDirectoryGroup, posix_getegid());
1794 $this->assertEquals($resultFooFilePermissions, '0660');
1795 $this->assertEquals($resultFooFileGroup, posix_getegid());
1796 $this->assertEquals($resultBarDirectoryPermissions, '0770');
1797 $this->assertEquals($resultBarDirectoryGroup, posix_getegid());
1798 $this->assertEquals($resultBarFilePermissions, '0660');
1799 $this->assertEquals($resultBarFileGroup, posix_getegid());
1800 $this->assertEquals($resultBarFile2Permissions, '0660');
1801 $this->assertEquals($resultBarFile2Group, posix_getegid());
1802 }
1803
1804 /**
1805 * @test
1806 */
1807 public function fixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
1808 if (TYPO3_OS == 'WIN') {
1809 $this->markTestSkipped('fixPermissions() tests not available on Windows');
1810 }
1811
1812 // Create and prepare test file
1813 $filename = PATH_site . 'typo3temp/../typo3temp/' . uniqid('test_');
1814 touch($filename);
1815 chmod($filename, 0742);
1816
1817 // Set target permissions and run method
1818 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
1819 $fixPermissionsResult = t3lib_div::fixPermissions($filename);
1820
1821 // Get actual permissions and clean up
1822 clearstatcache();
1823 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
1824 unlink($filename);
1825
1826 // Test if everything was ok
1827 $this->assertFalse($fixPermissionsResult);
1828 $this->assertEquals($resultFilePermissions, '0742');
1829 }
1830
1831
1832 ///////////////////////////////
1833 // Tests concerning mkdir
1834 ///////////////////////////////
1835
1836 /**
1837 * @test
1838 */
1839 public function mkdirCorrectlyCreatesDirectory() {
1840 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1841 $mkdirResult = t3lib_div::mkdir($directory);
1842 $directoryCreated = is_dir($directory);
1843 t3lib_div::rmdir($directory);
1844 $this->assertTrue($mkdirResult);
1845 $this->assertTrue($directoryCreated);
1846 }
1847
1848 /**
1849 * @test
1850 */
1851 public function mkdirCorrectlyCreatesHiddenDirectory() {
1852 $directory = PATH_site . 'typo3temp/' . uniqid('.test_');
1853 $mkdirResult = t3lib_div::mkdir($directory);
1854 $directoryCreated = is_dir($directory);
1855 t3lib_div::rmdir($directory);
1856 $this->assertTrue($mkdirResult);
1857 $this->assertTrue($directoryCreated);
1858 }
1859
1860 /**
1861 * @test
1862 */
1863 public function mkdirCorrectlyCreatesDirectoryWithTrailingSlash() {
1864 $directory = PATH_site . 'typo3temp/' . uniqid('test_');
1865 $mkdirResult = t3lib_div::mkdir($directory);
1866 $directoryCreated = is_dir($directory);
1867 t3lib_div::rmdir($directory);
1868 $this->assertTrue($mkdirResult);
1869 $this->assertTrue($directoryCreated);
1870 }
1871
1872 /**
1873 * Data provider for ImageMagick shell commands
1874 * @see explodeAndUnquoteImageMagickCommands
1875 */
1876 public function imageMagickCommandsDataProvider() {
1877 return array(
1878 // Some theoretical tests first
1879 array(
1880 'aa bb "cc" "dd"',
1881 array('aa', 'bb', '"cc"', '"dd"'),
1882 array('aa', 'bb', 'cc', 'dd'),
1883 ),
1884 array(
1885 'aa bb "cc dd"',
1886 array('aa', 'bb', '"cc dd"'),
1887 array('aa', 'bb', 'cc dd'),
1888 ),
1889 array(
1890 '\'aa bb\' "cc dd"',
1891 array('\'aa bb\'', '"cc dd"'),
1892 array('aa bb', 'cc dd'),
1893 ),
1894 array(
1895 '\'aa bb\' cc "dd"',
1896 array('\'aa bb\'', 'cc', '"dd"'),
1897 array('aa bb', 'cc', 'dd'),
1898 ),
1899 // Now test against some real world examples
1900 array(
1901 '/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"',
1902 array(
1903 '/opt/local/bin/gm.exe',
1904 'convert',
1905 '+profile',
1906 '\'*\'',
1907 '-geometry',
1908 '170x136!',
1909 '-negate',
1910 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1911 '"C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1912 ),
1913 array(
1914 '/opt/local/bin/gm.exe',
1915 'convert',
1916 '+profile',
1917 '*',
1918 '-geometry',
1919 '170x136!',
1920 '-negate',
1921 'C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
1922 'C:/Users/Someuser.Domain/Documents/Htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
1923 ),
1924 ),
1925 array(
1926 '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"',
1927 array(
1928 'C:/opt/local/bin/gm.exe',
1929 'convert',
1930 '+profile',
1931 '\'*\'',
1932 '-geometry',
1933 '170x136!',
1934 '-negate',
1935 '"C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1936 '"C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1937 ),
1938 array(
1939 'C:/opt/local/bin/gm.exe',
1940 'convert',
1941 '+profile',
1942 '*',
1943 '-geometry',
1944 '170x136!',
1945 '-negate',
1946 'C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
1947 'C:/Program Files/Apache2/htdocs/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
1948 ),
1949 ),
1950 array(
1951 '/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"',
1952 array(
1953 '/usr/bin/gm',
1954 'convert',
1955 '+profile',
1956 '\'*\'',
1957 '-geometry',
1958 '170x136!',
1959 '-negate',
1960 '"/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1961 '"/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1962 ),
1963 array(
1964 '/usr/bin/gm',
1965 'convert',
1966 '+profile',
1967 '*',
1968 '-geometry',
1969 '170x136!',
1970 '-negate',
1971 '/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
1972 '/Shared Items/Data/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
1973 ),
1974 ),
1975 array(
1976 '/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"',
1977 array(
1978 '/usr/bin/gm',
1979 'convert',
1980 '+profile',
1981 '\'*\'',
1982 '-geometry',
1983 '170x136!',
1984 '-negate',
1985 '"/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]"',
1986 '"/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif"'
1987 ),
1988 array(
1989 '/usr/bin/gm',
1990 'convert',
1991 '+profile',
1992 '*',
1993 '-geometry',
1994 '170x136!',
1995 '-negate',
1996 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
1997 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
1998 ),
1999 ),
2000 array(
2001 '/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\'',
2002 array(
2003 '/usr/bin/gm',
2004 'convert',
2005 '+profile',
2006 '\'*\'',
2007 '-geometry',
2008 '170x136!',
2009 '-negate',
2010 '\'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]\'',
2011 '\'/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif\''
2012 ),
2013 array(
2014 '/usr/bin/gm',
2015 'convert',
2016 '+profile',
2017 '*',
2018 '-geometry',
2019 '170x136!',
2020 '-negate',
2021 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif[0]',
2022 '/Network/Servers/server01.internal/Projects/typo3temp/temp/61401f5c16c63d58e1d92e8a2449f2fe_maskNT.gif'
2023 ),
2024 ),
2025 );
2026 }
2027
2028 /**
2029 * Tests if the commands are exploded and unquoted correctly
2030 *
2031 * @dataProvider imageMagickCommandsDataProvider
2032 * @test
2033 */
2034 public function explodeAndUnquoteImageMagickCommands($source, $expectedQuoted, $expectedUnquoted) {
2035 $actualQuoted = t3lib_div::unQuoteFilenames($source);
2036 $acutalUnquoted = t3lib_div::unQuoteFilenames($source, TRUE);
2037
2038 $this->assertEquals($expectedQuoted, $actualQuoted, 'The exploded command does not match the expected');
2039 $this->assertEquals($expectedUnquoted, $acutalUnquoted, 'The exploded and unquoted command does not match the expected');
2040 }
2041
2042
2043 ///////////////////////////////
2044 // Tests concerning split_fileref
2045 ///////////////////////////////
2046
2047 /**
2048 * @test
2049 */
2050 public function splitFileRefReturnsFileTypeNotForFolders(){
2051 $directoryName = uniqid('test_') . '.com';
2052 $directoryPath = PATH_site . 'typo3temp/';
2053 $directory = $directoryPath . $directoryName;
2054 mkdir($directory, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']));
2055
2056 $fileInfo = t3lib_div::split_fileref($directory);
2057
2058 $directoryCreated = is_dir($directory);
2059 rmdir($directory);
2060
2061 $this->assertTrue($directoryCreated);
2062 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
2063 $this->assertEquals($directoryPath, $fileInfo['path']);
2064 $this->assertEquals($directoryName, $fileInfo['file']);
2065 $this->assertEquals($directoryName, $fileInfo['filebody']);
2066 $this->assertEquals('', $fileInfo['fileext']);
2067 $this->assertArrayNotHasKey('realFileext', $fileInfo);
2068 }
2069
2070 /**
2071 * @test
2072 */
2073 public function splitFileRefReturnsFileTypeForFilesWithoutPathSite() {
2074 $testFile = 'fileadmin/media/someFile.png';
2075
2076 $fileInfo = t3lib_div::split_fileref($testFile);
2077 $this->assertType(PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $fileInfo);
2078 $this->assertEquals('fileadmin/media/', $fileInfo['path']);
2079 $this->assertEquals('someFile.png', $fileInfo['file']);
2080 $this->assertEquals('someFile', $fileInfo['filebody']);
2081 $this->assertEquals('png', $fileInfo['fileext']);
2082 }
2083
2084
2085 /////////////////////////////
2086 // Tests concerning dirname
2087 /////////////////////////////
2088
2089 /**
2090 * @see dirnameWithDataProvider
2091 *
2092 * @return array<array>
2093 */
2094 public function dirnameDataProvider() {
2095 return array(
2096 'absolute path with multiple part and file' => array('/dir1/dir2/script.php', '/dir1/dir2'),
2097 'absolute path with one part' => array('/dir1/', '/dir1'),
2098 'absolute path to file without extension' => array('/dir1/something', '/dir1'),
2099 'relative path with one part and file' => array('dir1/script.php', 'dir1'),
2100 'relative one-character path with one part and file' => array('d/script.php', 'd'),
2101 'absolute zero-part path with file' => array('/script.php', ''),
2102 'empty string' => array('', ''),
2103 );
2104 }
2105
2106 /**
2107 * @test
2108 *
2109 * @dataProvider dirnameDataProvider
2110 *
2111 * @param string $input the input for dirname
2112 * @param string $expectedValue the expected return value expected from dirname
2113 */
2114 public function dirnameWithDataProvider($input, $expectedValue) {
2115 $this->assertEquals(
2116 $expectedValue,
2117 t3lib_div::dirname($input)
2118 );
2119 }
2120
2121
2122 /////////////////////////////////////////////////////////////////////////////////////
2123 // Tests concerning makeInstance, setSingletonInstance, addInstance, purgeInstances
2124 /////////////////////////////////////////////////////////////////////////////////////
2125
2126 /**
2127 * @test
2128 *
2129 * @expectedException InvalidArgumentException
2130 */
2131 public function makeInstanceWithEmptyClassNameThrowsException() {
2132 t3lib_div::makeInstance('');
2133 }
2134
2135 /**
2136 * @test
2137 */
2138 public function makeInstanceReturnsClassInstance() {
2139 $className = get_class($this->getMock('foo'));
2140
2141 $this->assertTrue(
2142 t3lib_div::makeInstance($className) instanceof $className
2143 );
2144 }
2145
2146 /**
2147 * @test
2148 */
2149 public function makeInstancePassesParametersToConstructor() {
2150 $className = 'testingClass' . uniqid();
2151 if (!class_exists($className, FALSE)) {
2152 eval(
2153 'class ' . $className . ' {' .
2154 ' public $constructorParameter1;' .
2155 ' public $constructorParameter2;' .
2156 ' public function __construct($parameter1, $parameter2) {' .
2157 ' $this->constructorParameter1 = $parameter1;' .
2158 ' $this->constructorParameter2 = $parameter2;' .
2159 ' }' .
2160 '}'
2161 );
2162 }
2163
2164 $instance = t3lib_div::makeInstance($className, 'one parameter', 'another parameter');
2165
2166 $this->assertEquals(
2167 'one parameter',
2168 $instance->constructorParameter1,
2169 'The first constructor parameter has not been set.'
2170 );
2171 $this->assertEquals(
2172 'another parameter',
2173 $instance->constructorParameter2,
2174 'The second constructor parameter has not been set.'
2175 );
2176 }
2177
2178 /**
2179 * @test
2180 */
2181 public function makeInstanceCalledTwoTimesForNonSingletonClassReturnsDifferentInstances() {
2182 $className = get_class($this->getMock('foo'));
2183
2184 $this->assertNotSame(
2185 t3lib_div::makeInstance($className),
2186 t3lib_div::makeInstance($className)
2187 );
2188 }
2189
2190 /**
2191 * @test
2192 */
2193 public function makeInstanceCalledTwoTimesForSingletonClassReturnsSameInstance() {
2194 $className = get_class($this->getMock('t3lib_Singleton'));
2195
2196 $this->assertSame(
2197 t3lib_div::makeInstance($className),
2198 t3lib_div::makeInstance($className)
2199 );
2200 }
2201
2202 /**
2203 * @test
2204 */
2205 public function makeInstanceCalledTwoTimesForSingletonClassWithPurgeInstancesInbetweenReturnsDifferentInstances() {
2206 $className = get_class($this->getMock('t3lib_Singleton'));
2207
2208 $instance = t3lib_div::makeInstance($className);
2209 t3lib_div::purgeInstances();
2210
2211 $this->assertNotSame(
2212 $instance,
2213 t3lib_div::makeInstance($className)
2214 );
2215 }
2216
2217 /**
2218 * @test
2219 * @expectedException InvalidArgumentException
2220 */
2221 public function setSingletonInstanceForEmptyClassNameThrowsException() {
2222 $instance = $this->getMock('t3lib_Singleton');
2223
2224 t3lib_div::setSingletonInstance('', $instance);
2225 }
2226
2227 /**
2228 * @test
2229 * @expectedException InvalidArgumentException
2230 */
2231 public function setSingletonInstanceForClassThatIsNoSubclassOfProvidedClassThrowsException() {
2232 $instance = $this->getMock('t3lib_Singleton', array('foo'));
2233 $singletonClassName = get_class($this->getMock('t3lib_Singleton'));
2234
2235 t3lib_div::setSingletonInstance($singletonClassName, $instance);
2236 }
2237
2238 /**
2239 * @test
2240 */
2241 public function setSingletonInstanceMakesMakeInstanceReturnThatInstance() {
2242 $instance = $this->getMock('t3lib_Singleton');
2243 $singletonClassName = get_class($instance);
2244
2245 t3lib_div::setSingletonInstance($singletonClassName, $instance);
2246
2247 $this->assertSame(
2248 $instance,
2249 t3lib_div::makeInstance($singletonClassName)
2250 );
2251 }
2252
2253 /**
2254 * @test
2255 */
2256 public function setSingletonInstanceCalledTwoTimesMakesMakeInstanceReturnLastSetInstance() {
2257 $instance1 = $this->getMock('t3lib_Singleton');
2258 $singletonClassName = get_class($instance1);
2259 $instance2 = new $singletonClassName();
2260
2261 t3lib_div::setSingletonInstance($singletonClassName, $instance1);
2262 t3lib_div::setSingletonInstance($singletonClassName, $instance2);
2263
2264 $this->assertSame(
2265 $instance2,
2266 t3lib_div::makeInstance($singletonClassName)
2267 );
2268 }
2269
2270 /**
2271 * @test
2272 * @expectedException InvalidArgumentException
2273 */
2274 public function addInstanceForEmptyClassNameThrowsException() {
2275 $instance = $this->getMock('foo');
2276
2277 t3lib_div::addInstance('', $instance);
2278 }
2279
2280 /**
2281 * @test
2282 * @expectedException InvalidArgumentException
2283 */
2284 public function addInstanceForClassThatIsNoSubclassOfProvidedClassThrowsException() {
2285 $instance = $this->getMock('foo', array('bar'));
2286 $singletonClassName = get_class($this->getMock('foo'));
2287
2288 t3lib_div::addInstance($singletonClassName, $instance);
2289 }
2290
2291 /**
2292 * @test
2293 * @expectedException InvalidArgumentException
2294 */
2295 public function addInstanceWithSingletonInstanceThrowsException() {
2296 $instance = $this->getMock('t3lib_Singleton');
2297
2298 t3lib_div::addInstance(get_class($instance), $instance);
2299 }
2300
2301 /**
2302 * @test
2303 */
2304 public function addInstanceMakesMakeInstanceReturnThatInstance() {
2305 $instance = $this->getMock('foo');
2306 $className = get_class($instance);
2307
2308 t3lib_div::addInstance($className, $instance);
2309
2310 $this->assertSame(
2311 $instance,
2312 t3lib_div::makeInstance($className)
2313 );
2314 }
2315
2316 /**
2317 * @test
2318 */
2319 public function makeInstanceCalledTwoTimesAfterAddInstanceReturnTwoDifferentInstances() {
2320 $instance = $this->getMock('foo');
2321 $className = get_class($instance);
2322
2323 t3lib_div::addInstance($className, $instance);
2324
2325 $this->assertNotSame(
2326 t3lib_div::makeInstance($className),
2327 t3lib_div::makeInstance($className)
2328 );
2329 }
2330
2331 /**
2332 * @test
2333 */
2334 public function addInstanceCalledTwoTimesMakesMakeInstanceReturnBothInstancesInAddingOrder() {
2335 $instance1 = $this->getMock('foo');
2336 $className = get_class($instance1);
2337 t3lib_div::addInstance($className, $instance1);
2338
2339 $instance2 = new $className();
2340 t3lib_div::addInstance($className, $instance2);
2341
2342 $this->assertSame(
2343 $instance1,
2344 t3lib_div::makeInstance($className),
2345 'The first returned instance does not match the first added instance.'
2346 );
2347 $this->assertSame(
2348 $instance2,
2349 t3lib_div::makeInstance($className),
2350 'The second returned instance does not match the second added instance.'
2351 );
2352 }
2353
2354 /**
2355 * @test
2356 */
2357 public function purgeInstancesDropsAddedInstance() {
2358 $instance = $this->getMock('foo');
2359 $className = get_class($instance);
2360
2361 t3lib_div::addInstance($className, $instance);
2362 t3lib_div::purgeInstances();
2363
2364 $this->assertNotSame(
2365 $instance,
2366 t3lib_div::makeInstance($className)
2367 );
2368 }
2369
2370 /**
2371 * Data provider for validPathStrDetectsInvalidCharacters.
2372 *
2373 * @return array
2374 */
2375 public function validPathStrInvalidCharactersDataProvider() {
2376 return array(
2377 'double slash in path' => array('path//path'),
2378 'backslash in path' => array('path\\path'),
2379 'directory up in path' => array('path/../path'),
2380 'directory up at the beginning' => array('../path'),
2381 'NUL character in path' => array("path\x00path"),
2382 'BS character in path' => array("path\x08path"),
2383 );
2384 }
2385
2386 /**
2387 * Tests whether invalid characters are detected.
2388 *
2389 * @param string $path
2390 * @dataProvider validPathStrInvalidCharactersDataProvider
2391 * @test
2392 */
2393 public function validPathStrDetectsInvalidCharacters($path) {
2394 $this->assertNull(t3lib_div::validPathStr($path));
2395 }
2396
2397 /**
2398 * Tests whether verifyFilenameAgainstDenyPattern detects the null character.
2399 *
2400 * @test
2401 */
2402 public function verifyFilenameAgainstDenyPatternDetectsNullCharacter() {
2403 $this->assertFalse(t3lib_div::verifyFilenameAgainstDenyPattern("image\x00.gif"));
2404 }
2405
2406
2407 /////////////////////////////////////////////////////////////////////////////////////
2408 // Tests concerning sysLog
2409 /////////////////////////////////////////////////////////////////////////////////////
2410
2411 /**
2412 * @test
2413 */
2414 public function syslogFixesPermissionsOnFileIfUsingFileLogging() {
2415 if (TYPO3_OS == 'WIN') {
2416 $this->markTestSkipped('syslogFixesPermissionsOnFileIfUsingFileLogging() test not available on Windows.');
2417 }
2418
2419 // Fake all required settings
2420 $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLogLevel'] = 0;
2421 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLogInit'] = TRUE;
2422 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['systemLog']);
2423 $testLogFilename = PATH_site . 'typo3temp/' . uniqid('test_') . '.txt';
2424 $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLog'] = 'file,' . $testLogFilename . ',0';
2425 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
2426
2427 // Call method, get actual permissions and clean up
2428 t3lib_div::syslog('testLog', 'test', 1);
2429 clearstatcache();
2430 $resultFilePermissions = substr(decoct(fileperms($testLogFilename)), 2);
2431 t3lib_div::unlink_tempfile($testLogFilename);
2432
2433 $this->assertEquals($resultFilePermissions, '0777');
2434 }
2435
2436 /**
2437 * @test
2438 */
2439 public function deprecationLogFixesPermissionsOnLogFile() {
2440 if (TYPO3_OS == 'WIN') {
2441 $this->markTestSkipped('deprecationLogFixesPermissionsOnLogFile() test not available on Windows.');
2442 }
2443
2444 // Fake all required settings and get an unique logfilename
2445 $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = uniqid('test_');
2446 $deprecationLogFilename = t3lib_div::getDeprecationLogFileName();
2447 $GLOBALS['TYPO3_CONF_VARS']['SYS']['enableDeprecationLog'] = TRUE;
2448 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
2449
2450 // Call method, get actual permissions and clean up
2451 t3lib_div::deprecationLog('foo');
2452 clearstatcache();
2453 $resultFilePermissions = substr(decoct(fileperms($deprecationLogFilename)), 2);
2454 @unlink($deprecationLogFilename);
2455
2456 $this->assertEquals($resultFilePermissions, '0777');
2457 }
2458 }
2459 ?>