[TASK] Improve performance of trimExplode
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / GeneralUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
18 use TYPO3\CMS\Core\Utility;
19 use \org\bovigo\vfs\vfsStream;
20 use \org\bovigo\vfs\vfsStreamDirectory;
21 use \org\bovigo\vfs\vfsStreamWrapper;
22 use TYPO3\CMS\Core\Tests\FileStreamWrapper;
23
24 /**
25 * Testcase for class \TYPO3\CMS\Core\Utility\GeneralUtility
26 *
27 * @author Ingo Renner <ingo@typo3.org>
28 * @author Oliver Klee <typo3-coding@oliverklee.de>
29 */
30 class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
31
32 /**
33 * @var array A backup of registered singleton instances
34 */
35 protected $singletonInstances = array();
36
37 protected function setUp() {
38 GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount = 0;
39 GeneralUtilityFixture::setAllowHostHeaderValue(FALSE);
40 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = Utility\GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL;
41 $this->singletonInstances = Utility\GeneralUtility::getSingletonInstances();
42 }
43
44 protected function tearDown() {
45 Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
46 parent::tearDown();
47 }
48
49 /**
50 * Helper method to test for an existing internet connection.
51 * Some tests are skipped if there is no working uplink.
52 *
53 * @return bool $isConnected
54 */
55 public function isConnected() {
56 $isConnected = FALSE;
57 $connected = @fsockopen('typo3.org', 80);
58 if ($connected) {
59 $isConnected = TRUE;
60 fclose($connected);
61 }
62 return $isConnected;
63 }
64
65 ///////////////////////////
66 // Tests concerning _GP
67 ///////////////////////////
68 /**
69 * @test
70 * @dataProvider gpDataProvider
71 */
72 public function canRetrieveValueWithGP($key, $get, $post, $expected) {
73 $_GET = $get;
74 $_POST = $post;
75 $this->assertSame($expected, Utility\GeneralUtility::_GP($key));
76 }
77
78 /**
79 * Data provider for canRetrieveValueWithGP.
80 * All test values also check whether slashes are stripped properly.
81 *
82 * @return array
83 */
84 public function gpDataProvider() {
85 return array(
86 'No key parameter' => array(NULL, array(), array(), NULL),
87 'Key not found' => array('cake', array(), array(), NULL),
88 'Value only in GET' => array('cake', array('cake' => 'li\\e'), array(), 'lie'),
89 'Value only in POST' => array('cake', array(), array('cake' => 'l\\ie'), 'lie'),
90 'Value from POST preferred over GET' => array('cake', array('cake' => 'is a'), array('cake' => '\\lie'), 'lie'),
91 'Value can be an array' => array(
92 'cake',
93 array('cake' => array('is a' => 'l\\ie')),
94 array(),
95 array('is a' => 'lie')
96 )
97 );
98 }
99
100 ///////////////////////////
101 // Tests concerning _GPmerged
102 ///////////////////////////
103 /**
104 * @test
105 * @dataProvider gpMergedDataProvider
106 */
107 public function gpMergedWillMergeArraysFromGetAndPost($get, $post, $expected) {
108 $_POST = $post;
109 $_GET = $get;
110 $this->assertEquals($expected, Utility\GeneralUtility::_GPmerged('cake'));
111 }
112
113 /**
114 * Data provider for gpMergedWillMergeArraysFromGetAndPost
115 *
116 * @return array
117 */
118 public function gpMergedDataProvider() {
119 $fullDataArray = array('cake' => array('a' => 'is a', 'b' => 'lie'));
120 $postPartData = array('cake' => array('b' => 'lie'));
121 $getPartData = array('cake' => array('a' => 'is a'));
122 $getPartDataModified = array('cake' => array('a' => 'is not a'));
123 return array(
124 'Key doesn\' exist' => array(array('foo'), array('bar'), array()),
125 'No POST data' => array($fullDataArray, array(), $fullDataArray['cake']),
126 'No GET data' => array(array(), $fullDataArray, $fullDataArray['cake']),
127 'POST and GET are merged' => array($getPartData, $postPartData, $fullDataArray['cake']),
128 'POST is preferred over GET' => array($getPartDataModified, $fullDataArray, $fullDataArray['cake'])
129 );
130 }
131
132 ///////////////////////////////
133 // Tests concerning _GET / _POST
134 ///////////////////////////////
135 /**
136 * Data provider for canRetrieveGlobalInputsThroughGet
137 * and canRetrieveGlobalInputsThroughPost
138 *
139 * @return array
140 */
141 public function getAndPostDataProvider() {
142 return array(
143 'Requested input data doesn\'t exist' => array('cake', array(), NULL),
144 'No key will return entire input data' => array(NULL, array('cake' => 'l\\ie'), array('cake' => 'lie')),
145 'Can retrieve specific input' => array('cake', array('cake' => 'li\\e', 'foo'), 'lie'),
146 'Can retrieve nested input data' => array('cake', array('cake' => array('is a' => 'l\\ie')), array('is a' => 'lie'))
147 );
148 }
149
150 /**
151 * @test
152 * @dataProvider getAndPostDataProvider
153 */
154 public function canRetrieveGlobalInputsThroughGet($key, $get, $expected) {
155 $_GET = $get;
156 $this->assertSame($expected, Utility\GeneralUtility::_GET($key));
157 }
158
159 /**
160 * @test
161 * @dataProvider getAndPostDataProvider
162 */
163 public function canRetrieveGlobalInputsThroughPost($key, $post, $expected) {
164 $_POST = $post;
165 $this->assertSame($expected, Utility\GeneralUtility::_POST($key));
166 }
167
168 ///////////////////////////////
169 // Tests concerning _GETset
170 ///////////////////////////////
171 /**
172 * @test
173 * @dataProvider getSetDataProvider
174 */
175 public function canSetNewGetInputValues($input, $key, $expected, $getPreset = array()) {
176 $_GET = $getPreset;
177 Utility\GeneralUtility::_GETset($input, $key);
178 $this->assertSame($expected, $_GET);
179 }
180
181 /**
182 * Data provider for canSetNewGetInputValues
183 *
184 * @return array
185 */
186 public function getSetDataProvider() {
187 return array(
188 'No input data used without target key' => array(NULL, NULL, array()),
189 'No input data used with target key' => array(NULL, 'cake', array('cake' => '')),
190 'No target key used with string input data' => array('data', NULL, array()),
191 'No target key used with array input data' => array(array('cake' => 'lie'), NULL, array('cake' => 'lie')),
192 'Target key and string input data' => array('lie', 'cake', array('cake' => 'lie')),
193 'Replace existing GET data' => array('lie', 'cake', array('cake' => 'lie'), array('cake' => 'is a lie')),
194 'Target key pointing to sublevels and string input data' => array('lie', 'cake|is', array('cake' => array('is' => 'lie'))),
195 'Target key pointing to sublevels and array input data' => array(array('a' => 'lie'), 'cake|is', array('cake' => array('is' => array('a' => 'lie'))))
196 );
197 }
198
199 ///////////////////////////
200 // Tests concerning cmpIPv4
201 ///////////////////////////
202 /**
203 * Data provider for cmpIPv4ReturnsTrueForMatchingAddress
204 *
205 * @return array Data sets
206 */
207 static public function cmpIPv4DataProviderMatching() {
208 return array(
209 'host with full IP address' => array('127.0.0.1', '127.0.0.1'),
210 'host with two wildcards at the end' => array('127.0.0.1', '127.0.*.*'),
211 'host with wildcard at third octet' => array('127.0.0.1', '127.0.*.1'),
212 'host with wildcard at second octet' => array('127.0.0.1', '127.*.0.1'),
213 '/8 subnet' => array('127.0.0.1', '127.1.1.1/8'),
214 '/32 subnet (match only name)' => array('127.0.0.1', '127.0.0.1/32'),
215 '/30 subnet' => array('10.10.3.1', '10.10.3.3/30'),
216 'host with wildcard in list with IPv4/IPv6 addresses' => array('192.168.1.1', '127.0.0.1, 1234:5678::/126, 192.168.*'),
217 'host in list with IPv4/IPv6 addresses' => array('192.168.1.1', '::1, 1234:5678::/126, 192.168.1.1'),
218 );
219 }
220
221 /**
222 * @test
223 * @dataProvider cmpIPv4DataProviderMatching
224 */
225 public function cmpIPv4ReturnsTrueForMatchingAddress($ip, $list) {
226 $this->assertTrue(Utility\GeneralUtility::cmpIPv4($ip, $list));
227 }
228
229 /**
230 * Data provider for cmpIPv4ReturnsFalseForNotMatchingAddress
231 *
232 * @return array Data sets
233 */
234 static public function cmpIPv4DataProviderNotMatching() {
235 return array(
236 'single host' => array('127.0.0.1', '127.0.0.2'),
237 'single host with wildcard' => array('127.0.0.1', '127.*.1.1'),
238 'single host with /32 subnet mask' => array('127.0.0.1', '127.0.0.2/32'),
239 '/31 subnet' => array('127.0.0.1', '127.0.0.2/31'),
240 'list with IPv4/IPv6 addresses' => array('127.0.0.1', '10.0.2.3, 192.168.1.1, ::1'),
241 'list with only IPv6 addresses' => array('10.20.30.40', '::1, 1234:5678::/127')
242 );
243 }
244
245 /**
246 * @test
247 * @dataProvider cmpIPv4DataProviderNotMatching
248 */
249 public function cmpIPv4ReturnsFalseForNotMatchingAddress($ip, $list) {
250 $this->assertFalse(Utility\GeneralUtility::cmpIPv4($ip, $list));
251 }
252
253 ///////////////////////////
254 // Tests concerning cmpIPv6
255 ///////////////////////////
256 /**
257 * Data provider for cmpIPv6ReturnsTrueForMatchingAddress
258 *
259 * @return array Data sets
260 */
261 static public function cmpIPv6DataProviderMatching() {
262 return array(
263 'empty address' => array('::', '::'),
264 'empty with netmask in list' => array('::', '::/0'),
265 'empty with netmask 0 and host-bits set in list' => array('::', '::123/0'),
266 'localhost' => array('::1', '::1'),
267 'localhost with leading zero blocks' => array('::1', '0:0::1'),
268 'host with submask /128' => array('::1', '0:0::1/128'),
269 '/16 subnet' => array('1234::1', '1234:5678::/16'),
270 '/126 subnet' => array('1234:5678::3', '1234:5678::/126'),
271 '/126 subnet with host-bits in list set' => array('1234:5678::3', '1234:5678::2/126'),
272 'list with IPv4/IPv6 addresses' => array('1234:5678::3', '::1, 127.0.0.1, 1234:5678::/126, 192.168.1.1')
273 );
274 }
275
276 /**
277 * @test
278 * @dataProvider cmpIPv6DataProviderMatching
279 */
280 public function cmpIPv6ReturnsTrueForMatchingAddress($ip, $list) {
281 $this->assertTrue(Utility\GeneralUtility::cmpIPv6($ip, $list));
282 }
283
284 /**
285 * Data provider for cmpIPv6ReturnsFalseForNotMatchingAddress
286 *
287 * @return array Data sets
288 */
289 static public function cmpIPv6DataProviderNotMatching() {
290 return array(
291 'empty against localhost' => array('::', '::1'),
292 'empty against localhost with /128 netmask' => array('::', '::1/128'),
293 'localhost against different host' => array('::1', '::2'),
294 'localhost against host with prior bits set' => array('::1', '::1:1'),
295 'host against different /17 subnet' => array('1234::1', '1234:f678::/17'),
296 'host against different /127 subnet' => array('1234:5678::3', '1234:5678::/127'),
297 'host against IPv4 address list' => array('1234:5678::3', '127.0.0.1, 192.168.1.1'),
298 'host against mixed list with IPv6 host in different subnet' => array('1234:5678::3', '::1, 1234:5678::/127')
299 );
300 }
301
302 /**
303 * @test
304 * @dataProvider cmpIPv6DataProviderNotMatching
305 */
306 public function cmpIPv6ReturnsFalseForNotMatchingAddress($ip, $list) {
307 $this->assertFalse(Utility\GeneralUtility::cmpIPv6($ip, $list));
308 }
309
310 ///////////////////////////////
311 // Tests concerning IPv6Hex2Bin
312 ///////////////////////////////
313 /**
314 * Data provider for IPv6Hex2BinCorrect
315 *
316 * @return array Data sets
317 */
318 static public function IPv6Hex2BinDataProviderCorrect() {
319 return array(
320 'empty 1' => array('::', str_pad('', 16, "\x00")),
321 'empty 2, already normalized' => array('0000:0000:0000:0000:0000:0000:0000:0000', str_pad('', 16, "\x00")),
322 'already normalized' => array('0102:0304:0000:0000:0000:0000:0506:0078', "\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78"),
323 'expansion in middle 1' => array('1::2', "\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02"),
324 'expansion in middle 2' => array('beef::fefa', "\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa"),
325 );
326 }
327
328 /**
329 * @test
330 * @dataProvider IPv6Hex2BinDataProviderCorrect
331 */
332 public function IPv6Hex2BinCorrectlyConvertsAddresses($hex, $binary) {
333 $this->assertTrue(Utility\GeneralUtility::IPv6Hex2Bin($hex) === $binary);
334 }
335
336 ///////////////////////////////
337 // Tests concerning IPv6Bin2Hex
338 ///////////////////////////////
339 /**
340 * Data provider for IPv6Bin2HexCorrect
341 *
342 * @return array Data sets
343 */
344 static public function IPv6Bin2HexDataProviderCorrect() {
345 return array(
346 'empty' => array(str_pad('', 16, "\x00"), '::'),
347 'non-empty front' => array("\x01" . str_pad('', 15, "\x00"), '100::'),
348 'non-empty back' => array(str_pad('', 15, "\x00") . "\x01", '::1'),
349 'normalized' => array("\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78", '102:304::506:78'),
350 'expansion in middle 1' => array("\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02", '1::2'),
351 'expansion in middle 2' => array("\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa", 'beef::fefa'),
352 );
353 }
354
355 /**
356 * @test
357 * @dataProvider IPv6Bin2HexDataProviderCorrect
358 */
359 public function IPv6Bin2HexCorrectlyConvertsAddresses($binary, $hex) {
360 $this->assertEquals(Utility\GeneralUtility::IPv6Bin2Hex($binary), $hex);
361 }
362
363 ////////////////////////////////////////////////
364 // Tests concerning normalizeIPv6 / compressIPv6
365 ////////////////////////////////////////////////
366 /**
367 * Data provider for normalizeIPv6ReturnsCorrectlyNormalizedFormat
368 *
369 * @return array Data sets
370 */
371 static public function normalizeCompressIPv6DataProviderCorrect() {
372 return array(
373 'empty' => array('::', '0000:0000:0000:0000:0000:0000:0000:0000'),
374 'localhost' => array('::1', '0000:0000:0000:0000:0000:0000:0000:0001'),
375 'expansion in middle 1' => array('1::2', '0001:0000:0000:0000:0000:0000:0000:0002'),
376 'expansion in middle 2' => array('1:2::3', '0001:0002:0000:0000:0000:0000:0000:0003'),
377 'expansion in middle 3' => array('1::2:3', '0001:0000:0000:0000:0000:0000:0002:0003'),
378 'expansion in middle 4' => array('1:2::3:4:5', '0001:0002:0000:0000:0000:0003:0004:0005')
379 );
380 }
381
382 /**
383 * @test
384 * @dataProvider normalizeCompressIPv6DataProviderCorrect
385 */
386 public function normalizeIPv6CorrectlyNormalizesAddresses($compressed, $normalized) {
387 $this->assertEquals($normalized, Utility\GeneralUtility::normalizeIPv6($compressed));
388 }
389
390 /**
391 * @test
392 * @dataProvider normalizeCompressIPv6DataProviderCorrect
393 */
394 public function compressIPv6CorrectlyCompressesAdresses($compressed, $normalized) {
395 $this->assertEquals($compressed, Utility\GeneralUtility::compressIPv6($normalized));
396 }
397
398 /**
399 * @test
400 */
401 public function compressIPv6CorrectlyCompressesAdressWithSomeAddressOnRightSide() {
402 if (strtolower(PHP_OS) === 'darwin') {
403 $this->markTestSkipped('This test does not work on OSX / Darwin OS.');
404 }
405 $this->assertEquals('::f0f', Utility\GeneralUtility::compressIPv6('0000:0000:0000:0000:0000:0000:0000:0f0f'));
406 }
407
408 ///////////////////////////////
409 // Tests concerning validIP
410 ///////////////////////////////
411 /**
412 * Data provider for checkValidIpReturnsTrueForValidIp
413 *
414 * @return array Data sets
415 */
416 static public function validIpDataProvider() {
417 return array(
418 '0.0.0.0' => array('0.0.0.0'),
419 'private IPv4 class C' => array('192.168.0.1'),
420 'private IPv4 class A' => array('10.0.13.1'),
421 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb')
422 );
423 }
424
425 /**
426 * @test
427 * @dataProvider validIpDataProvider
428 */
429 public function validIpReturnsTrueForValidIp($ip) {
430 $this->assertTrue(Utility\GeneralUtility::validIP($ip));
431 }
432
433 /**
434 * Data provider for checkValidIpReturnsFalseForInvalidIp
435 *
436 * @return array Data sets
437 */
438 static public function invalidIpDataProvider() {
439 return array(
440 'null' => array(NULL),
441 'zero' => array(0),
442 'string' => array('test'),
443 'string empty' => array(''),
444 'string NULL' => array('NULL'),
445 'out of bounds IPv4' => array('300.300.300.300'),
446 'dotted decimal notation with only two dots' => array('127.0.1')
447 );
448 }
449
450 /**
451 * @test
452 * @dataProvider invalidIpDataProvider
453 */
454 public function validIpReturnsFalseForInvalidIp($ip) {
455 $this->assertFalse(Utility\GeneralUtility::validIP($ip));
456 }
457
458 ///////////////////////////////
459 // Tests concerning cmpFQDN
460 ///////////////////////////////
461 /**
462 * Data provider for cmpFqdnReturnsTrue
463 *
464 * @return array Data sets
465 */
466 static public function cmpFqdnValidDataProvider() {
467 return array(
468 'localhost should usually resolve, IPv4' => array('127.0.0.1', '*'),
469 'localhost should usually resolve, IPv6' => array('::1', '*'),
470 // other testcases with resolving not possible since it would
471 // require a working IPv4/IPv6-connectivity
472 'aaa.bbb.ccc.ddd.eee, full' => array('aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.ddd.eee'),
473 'aaa.bbb.ccc.ddd.eee, wildcard first' => array('aaa.bbb.ccc.ddd.eee', '*.ccc.ddd.eee'),
474 'aaa.bbb.ccc.ddd.eee, wildcard last' => array('aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.*'),
475 'aaa.bbb.ccc.ddd.eee, wildcard middle' => array('aaa.bbb.ccc.ddd.eee', 'aaa.*.eee'),
476 'list-matches, 1' => array('aaa.bbb.ccc.ddd.eee', 'xxx, yyy, zzz, aaa.*.eee'),
477 'list-matches, 2' => array('aaa.bbb.ccc.ddd.eee', '127:0:0:1,,aaa.*.eee,::1')
478 );
479 }
480
481 /**
482 * @test
483 * @dataProvider cmpFqdnValidDataProvider
484 */
485 public function cmpFqdnReturnsTrue($baseHost, $list) {
486 $this->assertTrue(Utility\GeneralUtility::cmpFQDN($baseHost, $list));
487 }
488
489 /**
490 * Data provider for cmpFqdnReturnsFalse
491 *
492 * @return array Data sets
493 */
494 static public function cmpFqdnInvalidDataProvider() {
495 return array(
496 'num-parts of hostname to check can only be less or equal than hostname, 1' => array('aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.ddd.eee.fff'),
497 'num-parts of hostname to check can only be less or equal than hostname, 2' => array('aaa.bbb.ccc.ddd.eee', 'aaa.*.bbb.ccc.ddd.eee')
498 );
499 }
500
501 /**
502 * @test
503 * @dataProvider cmpFqdnInvalidDataProvider
504 */
505 public function cmpFqdnReturnsFalse($baseHost, $list) {
506 $this->assertFalse(Utility\GeneralUtility::cmpFQDN($baseHost, $list));
507 }
508
509 ///////////////////////////////
510 // Tests concerning inList
511 ///////////////////////////////
512 /**
513 * @test
514 * @param string $haystack
515 * @dataProvider inListForItemContainedReturnsTrueDataProvider
516 */
517 public function inListForItemContainedReturnsTrue($haystack) {
518 $this->assertTrue(Utility\GeneralUtility::inList($haystack, 'findme'));
519 }
520
521 /**
522 * Data provider for inListForItemContainedReturnsTrue.
523 *
524 * @return array
525 */
526 public function inListForItemContainedReturnsTrueDataProvider() {
527 return array(
528 'Element as second element of four items' => array('one,findme,three,four'),
529 'Element at beginning of list' => array('findme,one,two'),
530 'Element at end of list' => array('one,two,findme'),
531 'One item list' => array('findme')
532 );
533 }
534
535 /**
536 * @test
537 * @param string $haystack
538 * @dataProvider inListForItemNotContainedReturnsFalseDataProvider
539 */
540 public function inListForItemNotContainedReturnsFalse($haystack) {
541 $this->assertFalse(Utility\GeneralUtility::inList($haystack, 'findme'));
542 }
543
544 /**
545 * Data provider for inListForItemNotContainedReturnsFalse.
546 *
547 * @return array
548 */
549 public function inListForItemNotContainedReturnsFalseDataProvider() {
550 return array(
551 'Four item list' => array('one,two,three,four'),
552 'One item list' => array('one'),
553 'Empty list' => array('')
554 );
555 }
556
557 ///////////////////////////////
558 // Tests concerning rmFromList
559 ///////////////////////////////
560 /**
561 * @test
562 * @param string $initialList
563 * @param string $listWithElementRemoved
564 * @dataProvider rmFromListRemovesElementsFromCommaSeparatedListDataProvider
565 */
566 public function rmFromListRemovesElementsFromCommaSeparatedList($initialList, $listWithElementRemoved) {
567 $this->assertSame($listWithElementRemoved, Utility\GeneralUtility::rmFromList('removeme', $initialList));
568 }
569
570 /**
571 * Data provider for rmFromListRemovesElementsFromCommaSeparatedList
572 *
573 * @return array
574 */
575 public function rmFromListRemovesElementsFromCommaSeparatedListDataProvider() {
576 return array(
577 'Element as second element of three' => array('one,removeme,two', 'one,two'),
578 'Element at beginning of list' => array('removeme,one,two', 'one,two'),
579 'Element at end of list' => array('one,two,removeme', 'one,two'),
580 'One item list' => array('removeme', ''),
581 'Element not contained in list' => array('one,two,three', 'one,two,three'),
582 'Empty element survives' => array('one,,three,,removeme', 'one,,three,'),
583 'Empty element survives at start' => array(',removeme,three,removeme', ',three'),
584 'Empty element survives at end' => array('removeme,three,removeme,', 'three,'),
585 'Empty list' => array('', ''),
586 'List contains removeme multiple times' => array('removeme,notme,removeme,removeme', 'notme'),
587 'List contains removeme multiple times nothing else' => array('removeme,removeme,removeme', ''),
588 'List contains removeme multiple times nothing else 2x' => array('removeme,removeme', ''),
589 'List contains removeme multiple times nothing else 3x' => array('removeme,removeme,removeme', ''),
590 'List contains removeme multiple times nothing else 4x' => array('removeme,removeme,removeme,removeme', ''),
591 'List contains removeme multiple times nothing else 5x' => array('removeme,removeme,removeme,removeme,removeme', ''),
592 );
593 }
594
595 ///////////////////////////////
596 // Tests concerning expandList
597 ///////////////////////////////
598 /**
599 * @test
600 * @param string $list
601 * @param string $expectation
602 * @dataProvider expandListExpandsIntegerRangesDataProvider
603 */
604 public function expandListExpandsIntegerRanges($list, $expectation) {
605 $this->assertSame($expectation, Utility\GeneralUtility::expandList($list));
606 }
607
608 /**
609 * Data provider for expandListExpandsIntegerRangesDataProvider
610 *
611 * @return array
612 */
613 public function expandListExpandsIntegerRangesDataProvider() {
614 return array(
615 'Expand for the same number' => array('1,2-2,7', '1,2,7'),
616 'Small range expand with parameters reversed ignores reversed items' => array('1,5-3,7', '1,7'),
617 'Small range expand' => array('1,3-5,7', '1,3,4,5,7'),
618 'Expand at beginning' => array('3-5,1,7', '3,4,5,1,7'),
619 'Expand at end' => array('1,7,3-5', '1,7,3,4,5'),
620 'Multiple small range expands' => array('1,3-5,7-10,12', '1,3,4,5,7,8,9,10,12'),
621 'One item list' => array('1-5', '1,2,3,4,5'),
622 'Nothing to expand' => array('1,2,3,4', '1,2,3,4'),
623 'Empty list' => array('', '')
624 );
625 }
626
627 /**
628 * @test
629 */
630 public function expandListExpandsForTwoThousandElementsExpandsOnlyToThousandElementsMaximum() {
631 $list = Utility\GeneralUtility::expandList('1-2000');
632 $this->assertSame(1000, count(explode(',', $list)));
633 }
634
635 ///////////////////////////////
636 // Tests concerning uniqueList
637 ///////////////////////////////
638 /**
639 * @test
640 * @param string $initialList
641 * @param string $unifiedList
642 * @dataProvider uniqueListUnifiesCommaSeparatedListDataProvider
643 */
644 public function uniqueListUnifiesCommaSeparatedList($initialList, $unifiedList) {
645 $this->assertSame($unifiedList, Utility\GeneralUtility::uniqueList($initialList));
646 }
647
648 /**
649 * Data provider for uniqueListUnifiesCommaSeparatedList
650 *
651 * @return array
652 */
653 public function uniqueListUnifiesCommaSeparatedListDataProvider() {
654 return array(
655 'List without duplicates' => array('one,two,three', 'one,two,three'),
656 'List with two consecutive duplicates' => array('one,two,two,three,three', 'one,two,three'),
657 'List with non-consecutive duplicates' => array('one,two,three,two,three', 'one,two,three'),
658 'One item list' => array('one', 'one'),
659 'Empty list' => array('', '')
660 );
661 }
662
663 ///////////////////////////////
664 // Tests concerning isFirstPartOfStr
665 ///////////////////////////////
666 /**
667 * Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
668 *
669 * @return array
670 */
671 public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider() {
672 return array(
673 'match first part of string' => array('hello world', 'hello'),
674 'match whole string' => array('hello', 'hello'),
675 'integer is part of string with same number' => array('24', 24),
676 'string is part of integer with same number' => array(24, '24'),
677 'integer is part of string starting with same number' => array('24 beer please', 24)
678 );
679 }
680
681 /**
682 * @test
683 * @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
684 */
685 public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part) {
686 $this->assertTrue(Utility\GeneralUtility::isFirstPartOfStr($string, $part));
687 }
688
689 /**
690 * Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
691 *
692 * @return array
693 */
694 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider() {
695 return array(
696 'no string match' => array('hello', 'bye'),
697 'no case sensitive string match' => array('hello world', 'Hello'),
698 'array is not part of string' => array('string', array()),
699 'string is not part of array' => array(array(), 'string'),
700 'NULL is not part of string' => array('string', NULL),
701 'string is not part of NULL' => array(NULL, 'string'),
702 'NULL is not part of array' => array(array(), NULL),
703 'array is not part of NULL' => array(NULL, array()),
704 'empty string is not part of empty string' => array('', ''),
705 'NULL is not part of empty string' => array('', NULL),
706 'false is not part of empty string' => array('', FALSE),
707 'empty string is not part of NULL' => array(NULL, ''),
708 'empty string is not part of false' => array(FALSE, ''),
709 'empty string is not part of zero integer' => array(0, ''),
710 'zero integer is not part of NULL' => array(NULL, 0),
711 'zero integer is not part of empty string' => array('', 0)
712 );
713 }
714
715 /**
716 * @test
717 * @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
718 */
719 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part) {
720 $this->assertFalse(Utility\GeneralUtility::isFirstPartOfStr($string, $part));
721 }
722
723 ///////////////////////////////
724 // Tests concerning formatSize
725 ///////////////////////////////
726 /**
727 * @test
728 * @dataProvider formatSizeDataProvider
729 */
730 public function formatSizeTranslatesBytesToHigherOrderRepresentation($size, $label, $expected) {
731 $this->assertEquals($expected, Utility\GeneralUtility::formatSize($size, $label));
732 }
733
734 /**
735 * Data provider for formatSizeTranslatesBytesToHigherOrderRepresentation
736 *
737 * @return array
738 */
739 public function formatSizeDataProvider() {
740 return array(
741 'Bytes keep beeing bytes (min)' => array(1, '', '1 '),
742 'Bytes keep beeing bytes (max)' => array(899, '', '899 '),
743 'Kilobytes are detected' => array(1024, '', '1.0 K'),
744 'Megabytes are detected' => array(1048576, '', '1.0 M'),
745 'Gigabytes are detected' => array(1073741824, '', '1.0 G'),
746 'Decimal is omitted for large kilobytes' => array(31080, '', '30 K'),
747 'Decimal is omitted for large megabytes' => array(31458000, '', '30 M'),
748 'Decimal is omitted for large gigabytes' => array(32212254720, '', '30 G'),
749 'Label for bytes can be exchanged' => array(1, ' Foo|||', '1 Foo'),
750 'Label for kilobytes can be exchanged' => array(1024, '| Foo||', '1.0 Foo'),
751 'Label for megabyes can be exchanged' => array(1048576, '|| Foo|', '1.0 Foo'),
752 'Label for gigabytes can be exchanged' => array(1073741824, '||| Foo', '1.0 Foo')
753 );
754 }
755
756 ///////////////////////////////
757 // Tests concerning splitCalc
758 ///////////////////////////////
759 /**
760 * Data provider for splitCalc
761 *
762 * @return array expected values, arithmetic expression
763 */
764 public function splitCalcDataProvider() {
765 return array(
766 'empty string returns empty array' => array(
767 array(),
768 ''
769 ),
770 'number without operator returns array with plus and number' => array(
771 array(array('+', 42)),
772 '42'
773 ),
774 'two numbers with asterisk return first number with plus and second number with asterisk' => array(
775 array(array('+', 42), array('*', 31)),
776 '42 * 31'
777 )
778 );
779 }
780
781 /**
782 * @test
783 * @dataProvider splitCalcDataProvider
784 */
785 public function splitCalcCorrectlySplitsExpression($expected, $expression) {
786 $this->assertEquals($expected, Utility\GeneralUtility::splitCalc($expression, '+-*/'));
787 }
788
789 ///////////////////////////////
790 // Tests concerning htmlspecialchars_decode
791 ///////////////////////////////
792 /**
793 * @test
794 */
795 public function htmlspecialcharsDecodeReturnsDecodedString() {
796 $string = '<typo3 version="6.0">&nbsp;</typo3>';
797 $encoded = htmlspecialchars($string);
798 $decoded = htmlspecialchars_decode($encoded);
799 $this->assertEquals($string, $decoded);
800 }
801
802 ///////////////////////////////
803 // Tests concerning deHSCentities
804 ///////////////////////////////
805 /**
806 * @test
807 * @dataProvider deHSCentitiesReturnsDecodedStringDataProvider
808 */
809 public function deHSCentitiesReturnsDecodedString($input, $expected) {
810 $this->assertEquals($expected, Utility\GeneralUtility::deHSCentities($input));
811 }
812
813 /**
814 * Data provider for deHSCentitiesReturnsDecodedString
815 *
816 * @return array
817 */
818 public function deHSCentitiesReturnsDecodedStringDataProvider() {
819 return array(
820 'Empty string' => array('', ''),
821 'Double encoded &' => array('&amp;amp;', '&amp;'),
822 'Double encoded numeric entity' => array('&amp;#1234;', '&#1234;'),
823 'Double encoded hexadecimal entity' => array('&amp;#x1b;', '&#x1b;'),
824 'Single encoded entities are not touched' => array('&amp; &#1234; &#x1b;', '&amp; &#1234; &#x1b;')
825 );
826 }
827
828 //////////////////////////////////
829 // Tests concerning slashJS
830 //////////////////////////////////
831 /**
832 * @test
833 * @dataProvider slashJsDataProvider
834 */
835 public function slashJsEscapesSingleQuotesAndSlashes($input, $extended, $expected) {
836 $this->assertEquals($expected, Utility\GeneralUtility::slashJS($input, $extended));
837 }
838
839 /**
840 * Data provider for slashJsEscapesSingleQuotesAndSlashes
841 *
842 * @return array
843 */
844 public function slashJsDataProvider() {
845 return array(
846 'Empty string is not changed' => array('', FALSE, ''),
847 'Normal string is not changed' => array('The cake is a lie √', FALSE, 'The cake is a lie √'),
848 'String with single quotes' => array('The \'cake\' is a lie', FALSE, 'The \\\'cake\\\' is a lie'),
849 'String with single quotes and backslashes - just escape single quotes' => array('The \\\'cake\\\' is a lie', FALSE, 'The \\\\\'cake\\\\\' is a lie'),
850 'String with single quotes and backslashes - escape both' => array('The \\\'cake\\\' is a lie', TRUE, 'The \\\\\\\'cake\\\\\\\' is a lie')
851 );
852 }
853
854 //////////////////////////////////
855 // Tests concerning rawUrlEncodeJS
856 //////////////////////////////////
857 /**
858 * @test
859 */
860 public function rawUrlEncodeJsPreservesWhitespaces() {
861 $input = 'Encode \'me\', but leave my spaces √';
862 $expected = 'Encode %27me%27%2C but leave my spaces %E2%88%9A';
863 $this->assertEquals($expected, Utility\GeneralUtility::rawUrlEncodeJS($input));
864 }
865
866 //////////////////////////////////
867 // Tests concerning rawUrlEncodeJS
868 //////////////////////////////////
869 /**
870 * @test
871 */
872 public function rawUrlEncodeFpPreservesSlashes() {
873 $input = 'Encode \'me\', but leave my / √';
874 $expected = 'Encode%20%27me%27%2C%20but%20leave%20my%20/%20%E2%88%9A';
875 $this->assertEquals($expected, Utility\GeneralUtility::rawUrlEncodeFP($input));
876 }
877
878 //////////////////////////////////
879 // Tests concerning strtoupper / strtolower
880 //////////////////////////////////
881 /**
882 * Data provider for strtoupper and strtolower
883 *
884 * @return array
885 */
886 public function strtouppperDataProvider() {
887 return array(
888 'Empty string' => array('', ''),
889 'String containing only latin characters' => array('the cake is a lie.', 'THE CAKE IS A LIE.'),
890 'String with umlauts and accent characters' => array('the càkê is ä lie.', 'THE CàKê IS ä LIE.')
891 );
892 }
893
894 /**
895 * @test
896 * @dataProvider strtouppperDataProvider
897 */
898 public function strtoupperConvertsOnlyLatinCharacters($input, $expected) {
899 $this->assertEquals($expected, Utility\GeneralUtility::strtoupper($input));
900 }
901
902 /**
903 * @test
904 * @dataProvider strtouppperDataProvider
905 */
906 public function strtolowerConvertsOnlyLatinCharacters($expected, $input) {
907 $this->assertEquals($expected, Utility\GeneralUtility::strtolower($input));
908 }
909
910 //////////////////////////////////
911 // Tests concerning validEmail
912 //////////////////////////////////
913 /**
914 * Data provider for valid validEmail's
915 *
916 * @return array Valid email addresses
917 */
918 public function validEmailValidDataProvider() {
919 return array(
920 'short mail address' => array('a@b.c'),
921 'simple mail address' => array('test@example.com'),
922 'uppercase characters' => array('QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET'),
923 'equal sign in local part' => array('test=mail@example.com'),
924 'dash in local part' => array('test-mail@example.com'),
925 'plus in local part' => array('test+mail@example.com'),
926 'question mark in local part' => array('test?mail@example.com'),
927 'slash in local part' => array('foo/bar@example.com'),
928 'hash in local part' => array('foo#bar@example.com'),
929 'dot in local part' => array('firstname.lastname@employee.2something.com'),
930 'dash as local part' => array('-@foo.com'),
931 'umlauts in domain part' => array('foo@äöüfoo.com')
932 );
933 }
934
935 /**
936 * @test
937 * @dataProvider validEmailValidDataProvider
938 */
939 public function validEmailReturnsTrueForValidMailAddress($address) {
940 $this->assertTrue(Utility\GeneralUtility::validEmail($address));
941 }
942
943 /**
944 * Data provider for invalid validEmail's
945 *
946 * @return array Invalid email addresses
947 */
948 public function validEmailInvalidDataProvider() {
949 return array(
950 'empty string' => array(''),
951 'empty array' => array(array()),
952 'integer' => array(42),
953 'float' => array(42.23),
954 'array' => array(array('foo')),
955 'object' => array(new \stdClass()),
956 '@ sign only' => array('@'),
957 'string longer than 320 characters' => array(str_repeat('0123456789', 33)),
958 'duplicate @' => array('test@@example.com'),
959 'duplicate @ combined with further special characters in local part' => array('test!.!@#$%^&*@example.com'),
960 'opening parenthesis in local part' => array('foo(bar@example.com'),
961 'closing parenthesis in local part' => array('foo)bar@example.com'),
962 'opening square bracket in local part' => array('foo[bar@example.com'),
963 'closing square bracket as local part' => array(']@example.com'),
964 'top level domain only' => array('test@com'),
965 'dash as second level domain' => array('foo@-.com'),
966 'domain part starting with dash' => array('foo@-foo.com'),
967 'domain part ending with dash' => array('foo@foo-.com'),
968 'number as top level domain' => array('foo@bar.123'),
969 'dot at beginning of domain part' => array('test@.com'),
970 'local part ends with dot' => array('e.x.a.m.p.l.e.@example.com'),
971 'umlauts in local part' => array('äöüfoo@bar.com'),
972 'trailing whitespace' => array('test@example.com '),
973 'trailing carriage return' => array('test@example.com' . CR),
974 'trailing linefeed' => array('test@example.com' . LF),
975 'trailing carriage return linefeed' => array('test@example.com' . CRLF),
976 'trailing tab' => array('test@example.com' . TAB)
977 );
978 }
979
980 /**
981 * @test
982 * @dataProvider validEmailInvalidDataProvider
983 */
984 public function validEmailReturnsFalseForInvalidMailAddress($address) {
985 $this->assertFalse(Utility\GeneralUtility::validEmail($address));
986 }
987
988 //////////////////////////////////
989 // Tests concerning intExplode
990 //////////////////////////////////
991 /**
992 * @test
993 */
994 public function intExplodeConvertsStringsToInteger() {
995 $testString = '1,foo,2';
996 $expectedArray = array(1, 0, 2);
997 $actualArray = Utility\GeneralUtility::intExplode(',', $testString);
998 $this->assertEquals($expectedArray, $actualArray);
999 }
1000
1001 //////////////////////////////////
1002 // Tests concerning implodeArrayForUrl / explodeUrl2Array
1003 //////////////////////////////////
1004 /**
1005 * Data provider for implodeArrayForUrlBuildsValidParameterString and
1006 * explodeUrl2ArrayTransformsParameterStringToArray
1007 *
1008 * @return array
1009 */
1010 public function implodeArrayForUrlDataProvider() {
1011 $valueArray = array('one' => '√', 'two' => 2);
1012 return array(
1013 'Empty input' => array('foo', array(), ''),
1014 'String parameters' => array('foo', $valueArray, '&foo[one]=%E2%88%9A&foo[two]=2'),
1015 'Nested array parameters' => array('foo', array($valueArray), '&foo[0][one]=%E2%88%9A&foo[0][two]=2'),
1016 'Keep blank parameters' => array('foo', array('one' => '√', ''), '&foo[one]=%E2%88%9A&foo[0]=')
1017 );
1018 }
1019
1020 /**
1021 * @test
1022 * @dataProvider implodeArrayForUrlDataProvider
1023 */
1024 public function implodeArrayForUrlBuildsValidParameterString($name, $input, $expected) {
1025 $this->assertSame($expected, Utility\GeneralUtility::implodeArrayForUrl($name, $input));
1026 }
1027
1028 /**
1029 * @test
1030 */
1031 public function implodeArrayForUrlCanSkipEmptyParameters() {
1032 $input = array('one' => '√', '');
1033 $expected = '&foo[one]=%E2%88%9A';
1034 $this->assertSame($expected, Utility\GeneralUtility::implodeArrayForUrl('foo', $input, '', TRUE));
1035 }
1036
1037 /**
1038 * @test
1039 */
1040 public function implodeArrayForUrlCanUrlEncodeKeyNames() {
1041 $input = array('one' => '√', '');
1042 $expected = '&foo%5Bone%5D=%E2%88%9A&foo%5B0%5D=';
1043 $this->assertSame($expected, Utility\GeneralUtility::implodeArrayForUrl('foo', $input, '', FALSE, TRUE));
1044 }
1045
1046 /**
1047 * @test
1048 * @dataProvider implodeArrayForUrlDataProvider
1049 */
1050 public function explodeUrl2ArrayTransformsParameterStringToNestedArray($name, $array, $input) {
1051 $expected = $array ? array($name => $array) : array();
1052 $this->assertEquals($expected, Utility\GeneralUtility::explodeUrl2Array($input, TRUE));
1053 }
1054
1055 /**
1056 * @test
1057 * @dataProvider explodeUrl2ArrayDataProvider
1058 */
1059 public function explodeUrl2ArrayTransformsParameterStringToFlatArray($input, $expected) {
1060 $this->assertEquals($expected, Utility\GeneralUtility::explodeUrl2Array($input, FALSE));
1061 }
1062
1063 /**
1064 * Data provider for explodeUrl2ArrayTransformsParameterStringToFlatArray
1065 *
1066 * @return array
1067 */
1068 public function explodeUrl2ArrayDataProvider() {
1069 return array(
1070 'Empty string' => array('', array()),
1071 'Simple parameter string' => array('&one=%E2%88%9A&two=2', array('one' => '√', 'two' => 2)),
1072 'Nested parameter string' => array('&foo[one]=%E2%88%9A&two=2', array('foo[one]' => '√', 'two' => 2))
1073 );
1074 }
1075
1076 //////////////////////////////////
1077 // Tests concerning compileSelectedGetVarsFromArray
1078 //////////////////////////////////
1079 /**
1080 * @test
1081 */
1082 public function compileSelectedGetVarsFromArrayFiltersIncomingData() {
1083 $filter = 'foo,bar';
1084 $getArray = array('foo' => 1, 'cake' => 'lie');
1085 $expected = array('foo' => 1);
1086 $result = Utility\GeneralUtility::compileSelectedGetVarsFromArray($filter, $getArray, FALSE);
1087 $this->assertSame($expected, $result);
1088 }
1089
1090 /**
1091 * @test
1092 */
1093 public function compileSelectedGetVarsFromArrayUsesGetPostDataFallback() {
1094 $_GET['bar'] = '2';
1095 $filter = 'foo,bar';
1096 $getArray = array('foo' => 1, 'cake' => 'lie');
1097 $expected = array('foo' => 1, 'bar' => '2');
1098 $result = Utility\GeneralUtility::compileSelectedGetVarsFromArray($filter, $getArray, TRUE);
1099 $this->assertSame($expected, $result);
1100 }
1101
1102 //////////////////////////////////
1103 // Tests concerning array_merge
1104 //////////////////////////////////
1105 /**
1106 * Test demonstrating array_merge. This is actually
1107 * a native PHP operator, therefore this test is mainly used to
1108 * show how this function can be used.
1109 *
1110 * @test
1111 */
1112 public function arrayMergeKeepsIndexesAfterMerge() {
1113 $array1 = array(10 => 'FOO', '20' => 'BAR');
1114 $array2 = array('5' => 'PLONK');
1115 $expected = array('5' => 'PLONK', 10 => 'FOO', '20' => 'BAR');
1116 $this->assertEquals($expected, Utility\GeneralUtility::array_merge($array1, $array2));
1117 }
1118
1119 //////////////////////////////////
1120 // Tests concerning revExplode
1121 //////////////////////////////////
1122
1123 /**
1124 * @return array
1125 */
1126 public function revExplodeDataProvider() {
1127 return array(
1128 'limit 0 should return unexploded string' => array(
1129 ':',
1130 'my:words:here',
1131 0,
1132 array('my:words:here')
1133 ),
1134 'limit 1 should return unexploded string' => array(
1135 ':',
1136 'my:words:here',
1137 1,
1138 array('my:words:here')
1139 ),
1140 'limit 2 should return two pieces' => array(
1141 ':',
1142 'my:words:here',
1143 2,
1144 array('my:words', 'here')
1145 ),
1146 'limit 3 should return unexploded string' => array(
1147 ':',
1148 'my:words:here',
1149 3,
1150 array('my', 'words', 'here')
1151 ),
1152 'limit 0 should return unexploded string if no delimiter is contained' => array(
1153 ':',
1154 'mywordshere',
1155 0,
1156 array('mywordshere')
1157 ),
1158 'limit 1 should return unexploded string if no delimiter is contained' => array(
1159 ':',
1160 'mywordshere',
1161 1,
1162 array('mywordshere')
1163 ),
1164 'limit 2 should return unexploded string if no delimiter is contained' => array(
1165 ':',
1166 'mywordshere',
1167 2,
1168 array('mywordshere')
1169 ),
1170 'limit 3 should return unexploded string if no delimiter is contained' => array(
1171 ':',
1172 'mywordshere',
1173 3,
1174 array('mywordshere')
1175 ),
1176 'multi character delimiter is handled properly with limit 2' => array(
1177 '[]',
1178 'a[b][c][d]',
1179 2,
1180 array('a[b][c', 'd]')
1181 ),
1182 'multi character delimiter is handled properly with limit 3' => array(
1183 '[]',
1184 'a[b][c][d]',
1185 3,
1186 array('a[b', 'c', 'd]')
1187 ),
1188 );
1189 }
1190
1191 /**
1192 * @test
1193 * @dataProvider revExplodeDataProvider
1194 */
1195 public function revExplodeCorrectlyExplodesStringForGivenPartsCount($delimiter, $testString, $count, $expectedArray) {
1196 $actualArray = Utility\GeneralUtility::revExplode($delimiter, $testString, $count);
1197 $this->assertEquals($expectedArray, $actualArray);
1198 }
1199
1200 /**
1201 * @test
1202 */
1203 public function revExplodeRespectsLimitThreeWhenExploding() {
1204 $testString = 'even:more:of:my:words:here';
1205 $expectedArray = array('even:more:of:my', 'words', 'here');
1206 $actualArray = Utility\GeneralUtility::revExplode(':', $testString, 3);
1207 $this->assertEquals($expectedArray, $actualArray);
1208 }
1209
1210 //////////////////////////////////
1211 // Tests concerning trimExplode
1212 //////////////////////////////////
1213 /**
1214 * @test
1215 */
1216 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
1217 $testString = ' a , b , c ,d ,, e,f,';
1218 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
1219 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString);
1220 $this->assertEquals($expectedArray, $actualArray);
1221 }
1222
1223 /**
1224 * @test
1225 */
1226 public function checkTrimExplodeRemovesNewLines() {
1227 $testString = ' a , b , ' . LF . ' ,d ,, e,f,';
1228 $expectedArray = array('a', 'b', 'd', 'e', 'f');
1229 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE);
1230 $this->assertEquals($expectedArray, $actualArray);
1231 }
1232
1233 /**
1234 * @test
1235 */
1236 public function checkTrimExplodeRemovesEmptyElements() {
1237 $testString = 'a , b , c , ,d ,, ,e,f,';
1238 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
1239 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE);
1240 $this->assertEquals($expectedArray, $actualArray);
1241 }
1242
1243 /**
1244 * @test
1245 */
1246 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
1247 $testString = ' a , b , c , , d,, ,e ';
1248 $expectedArray = array('a', 'b', 'c,,d,,,e');
1249 // Limiting returns the rest of the string as the last element
1250 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, FALSE, 3);
1251 $this->assertEquals($expectedArray, $actualArray);
1252 }
1253
1254 /**
1255 * @test
1256 */
1257 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
1258 $testString = ' a , b , c , , d,, ,e ';
1259 $expectedArray = array('a', 'b', 'c,d,e');
1260 // Limiting returns the rest of the string as the last element
1261 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE, 3);
1262 $this->assertEquals($expectedArray, $actualArray);
1263 }
1264
1265 /**
1266 * @test
1267 */
1268 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
1269 $testString = ' a , b , c , d, ,e, f , , ';
1270 $expectedArray = array('a', 'b', 'c', 'd', '', 'e');
1271 // limiting returns the rest of the string as the last element
1272 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, FALSE, -3);
1273 $this->assertEquals($expectedArray, $actualArray);
1274 }
1275
1276 /**
1277 * @test
1278 */
1279 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
1280 $testString = ' a , b , c , d, ,e, f , , ';
1281 $expectedArray = array('a', 'b', 'c');
1282 // Limiting returns the rest of the string as the last element
1283 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE, -3);
1284 $this->assertEquals($expectedArray, $actualArray);
1285 }
1286
1287 /**
1288 * @test
1289 */
1290 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
1291 $testString = ' a , b , , c , , , ';
1292 $expectedArray = array('a', 'b', 'c');
1293 // Limiting returns the rest of the string as the last element
1294 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE, 4);
1295 $this->assertEquals($expectedArray, $actualArray);
1296 }
1297
1298 /**
1299 * @test
1300 */
1301 public function checkTrimExplodeKeepsZeroAsString() {
1302 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
1303 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
1304 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE);
1305 $this->assertEquals($expectedArray, $actualArray);
1306 }
1307
1308 /**
1309 * @test
1310 */
1311 public function checkTrimExplodeKeepsWhitespaceInsideElements() {
1312 $testString = 'a , b , c , ,d ,, ,e,f, g h ,';
1313 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g h');
1314 $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE);
1315 $this->assertEquals($expectedArray, $actualArray);
1316 }
1317
1318 /**
1319 * @test
1320 */
1321 public function checkTrimExplodeCanUseInternalRegexDelimiterAsExplodeDelimiter() {
1322 $testString = 'a / b / c / /d // /e/f/ g h /';
1323 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g h');
1324 $actualArray = Utility\GeneralUtility::trimExplode('/', $testString, TRUE);
1325 $this->assertEquals($expectedArray, $actualArray);
1326 }
1327
1328 //////////////////////////////////
1329 // Tests concerning getBytesFromSizeMeasurement
1330 //////////////////////////////////
1331 /**
1332 * Data provider for getBytesFromSizeMeasurement
1333 *
1334 * @return array expected value, input string
1335 */
1336 public function getBytesFromSizeMeasurementDataProvider() {
1337 return array(
1338 '100 kilo Bytes' => array('102400', '100k'),
1339 '100 mega Bytes' => array('104857600', '100m'),
1340 '100 giga Bytes' => array('107374182400', '100g')
1341 );
1342 }
1343
1344 /**
1345 * @test
1346 * @dataProvider getBytesFromSizeMeasurementDataProvider
1347 */
1348 public function getBytesFromSizeMeasurementCalculatesCorrectByteValue($expected, $byteString) {
1349 $this->assertEquals($expected, Utility\GeneralUtility::getBytesFromSizeMeasurement($byteString));
1350 }
1351
1352 //////////////////////////////////
1353 // Tests concerning getIndpEnv
1354 //////////////////////////////////
1355 /**
1356 * @test
1357 */
1358 public function getIndpEnvTypo3SitePathReturnNonEmptyString() {
1359 $this->assertTrue(strlen(Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH')) >= 1);
1360 }
1361
1362 /**
1363 * @test
1364 */
1365 public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() {
1366 if (TYPO3_OS === 'WIN') {
1367 $this->markTestSkipped('Test not available on Windows OS.');
1368 }
1369 $result = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
1370 $this->assertEquals('/', $result[0]);
1371 }
1372
1373 /**
1374 * @test
1375 */
1376 public function getIndpEnvTypo3SitePathReturnsStringStartingWithDrive() {
1377 if (TYPO3_OS !== 'WIN') {
1378 $this->markTestSkipped('Test available only on Windows OS.');
1379 }
1380 $result = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
1381 $this->assertRegExp('/^[a-z]:\//i', $result);
1382 }
1383
1384 /**
1385 * @test
1386 */
1387 public function getIndpEnvTypo3SitePathReturnsStringEndingWithSlash() {
1388 $result = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
1389 $this->assertEquals('/', $result[strlen($result) - 1]);
1390 }
1391
1392 /**
1393 * @return array
1394 */
1395 static public function hostnameAndPortDataProvider() {
1396 return array(
1397 'localhost ipv4 without port' => array('127.0.0.1', '127.0.0.1', ''),
1398 'localhost ipv4 with port' => array('127.0.0.1:81', '127.0.0.1', '81'),
1399 'localhost ipv6 without port' => array('[::1]', '[::1]', ''),
1400 'localhost ipv6 with port' => array('[::1]:81', '[::1]', '81'),
1401 'ipv6 without port' => array('[2001:DB8::1]', '[2001:DB8::1]', ''),
1402 'ipv6 with port' => array('[2001:DB8::1]:81', '[2001:DB8::1]', '81'),
1403 'hostname without port' => array('lolli.did.this', 'lolli.did.this', ''),
1404 'hostname with port' => array('lolli.did.this:42', 'lolli.did.this', '42')
1405 );
1406 }
1407
1408 /**
1409 * @test
1410 * @dataProvider hostnameAndPortDataProvider
1411 */
1412 public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
1413 $_SERVER['HTTP_HOST'] = $httpHost;
1414 $this->assertEquals($expectedIp, Utility\GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY'));
1415 }
1416
1417 /**
1418 * @test
1419 */
1420 public function isAllowedHostHeaderValueReturnsFalseIfTrusedHostsIsNotConfigured() {
1421 unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern']);
1422 $this->assertFalse(GeneralUtilityFixture::isAllowedHostHeaderValue('evil.foo.bar'));
1423 }
1424
1425 /**
1426 * @return array
1427 */
1428 static public function hostnamesMatchingTrustedHostsConfigurationDataProvider() {
1429 return array(
1430 'hostname without port matching' => array('lolli.did.this', '.*\.did\.this'),
1431 'other hostname without port matching' => array('helmut.did.this', '.*\.did\.this'),
1432 'two different hostnames without port matching 1st host' => array('helmut.is.secure', '(helmut\.is\.secure|lolli\.is\.secure)'),
1433 'two different hostnames without port matching 2nd host' => array('lolli.is.secure', '(helmut\.is\.secure|lolli\.is\.secure)'),
1434 'hostname with port matching' => array('lolli.did.this:42', '.*\.did\.this:42'),
1435 'hostnames are case insensitive 1' => array('lolli.DID.this:42', '.*\.did.this:42'),
1436 'hostnames are case insensitive 2' => array('lolli.did.this:42', '.*\.DID.this:42'),
1437 );
1438 }
1439
1440 /**
1441 * @return array
1442 */
1443 static public function hostnamesNotMatchingTrustedHostsConfigurationDataProvider() {
1444 return array(
1445 'hostname without port' => array('lolli.did.this', 'helmut\.did\.this'),
1446 'hostname with port, but port not allowed' => array('lolli.did.this:42', 'helmut\.did\.this'),
1447 'two different hostnames in pattern but host header starts with differnet value #1' => array('sub.helmut.is.secure', '(helmut\.is\.secure|lolli\.is\.secure)'),
1448 'two different hostnames in pattern but host header starts with differnet value #2' => array('sub.lolli.is.secure', '(helmut\.is\.secure|lolli\.is\.secure)'),
1449 'two different hostnames in pattern but host header ends with differnet value #1' => array('helmut.is.secure.tld', '(helmut\.is\.secure|lolli\.is\.secure)'),
1450 'two different hostnames in pattern but host header ends with differnet value #2' => array('lolli.is.secure.tld', '(helmut\.is\.secure|lolli\.is\.secure)'),
1451 );
1452 }
1453
1454 /**
1455 * @param string $httpHost HTTP_HOST string
1456 * @param string $hostNamePattern trusted hosts pattern
1457 * @test
1458 * @dataProvider hostnamesMatchingTrustedHostsConfigurationDataProvider
1459 */
1460 public function isAllowedHostHeaderValueReturnsTrueIfHostValueMatches($httpHost, $hostNamePattern) {
1461 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = $hostNamePattern;
1462 $this->assertTrue(GeneralUtilityFixture::isAllowedHostHeaderValue($httpHost));
1463 }
1464
1465 /**
1466 * @param string $httpHost HTTP_HOST string
1467 * @param string $hostNamePattern trusted hosts pattern
1468 * @test
1469 * @dataProvider hostnamesNotMatchingTrustedHostsConfigurationDataProvider
1470 */
1471 public function isAllowedHostHeaderValueReturnsFalseIfHostValueMatches($httpHost, $hostNamePattern) {
1472 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = $hostNamePattern;
1473 $this->assertFalse(GeneralUtilityFixture::isAllowedHostHeaderValue($httpHost));
1474 }
1475
1476 public function serverNamePatternDataProvider() {
1477 return array(
1478 'host value matches server name and server port is default http' => array(
1479 'httpHost' => 'secure.web.server',
1480 'serverName' => 'secure.web.server',
1481 'isAllowed' => TRUE,
1482 'serverPort' => '80',
1483 'ssl' => 'Off',
1484 ),
1485 'host value matches server name if compared case insensitive 1' => array(
1486 'httpHost' => 'secure.web.server',
1487 'serverName' => 'secure.WEB.server',
1488 'isAllowed' => TRUE,
1489 ),
1490 'host value matches server name if compared case insensitive 2' => array(
1491 'httpHost' => 'secure.WEB.server',
1492 'serverName' => 'secure.web.server',
1493 'isAllowed' => TRUE,
1494 ),
1495 'host value matches server name and server port is default https' => array(
1496 'httpHost' => 'secure.web.server',
1497 'serverName' => 'secure.web.server',
1498 'isAllowed' => TRUE,
1499 'serverPort' => '443',
1500 'ssl' => 'On',
1501 ),
1502 'host value matches server name and server port' => array(
1503 'httpHost' => 'secure.web.server:88',
1504 'serverName' => 'secure.web.server',
1505 'isAllowed' => TRUE,
1506 'serverPort' => '88',
1507 ),
1508 'host value matches server name case insensitive 1 and server port' => array(
1509 'httpHost' => 'secure.WEB.server:88',
1510 'serverName' => 'secure.web.server',
1511 'isAllowed' => TRUE,
1512 'serverPort' => '88',
1513 ),
1514 'host value matches server name case insensitive 2 and server port' => array(
1515 'httpHost' => 'secure.web.server:88',
1516 'serverName' => 'secure.WEB.server',
1517 'isAllowed' => TRUE,
1518 'serverPort' => '88',
1519 ),
1520 'host value is ipv6 but matches server name and server port' => array(
1521 'httpHost' => '[::1]:81',
1522 'serverName' => '[::1]',
1523 'isAllowed' => TRUE,
1524 'serverPort' => '81',
1525 ),
1526 'host value does not match server name' => array(
1527 'httpHost' => 'insecure.web.server',
1528 'serverName' => 'secure.web.server',
1529 'isAllowed' => FALSE,
1530 ),
1531 'host value does not match server port' => array(
1532 'httpHost' => 'secure.web.server:88',
1533 'serverName' => 'secure.web.server',
1534 'isAllowed' => FALSE,
1535 'serverPort' => '89',
1536 ),
1537 'host value has default port that does not match server port' => array(
1538 'httpHost' => 'secure.web.server',
1539 'serverName' => 'secure.web.server',
1540 'isAllowed' => FALSE,
1541 'serverPort' => '81',
1542 'ssl' => 'Off',
1543 ),
1544 'host value has default port that does not match server ssl port' => array(
1545 'httpHost' => 'secure.web.server',
1546 'serverName' => 'secure.web.server',
1547 'isAllowed' => FALSE,
1548 'serverPort' => '444',
1549 'ssl' => 'On',
1550 ),
1551 );
1552 }
1553
1554 /**
1555 * @param string $httpHost
1556 * @param string $serverName
1557 * @param bool $isAllowed
1558 * @param string $serverPort
1559 * @param string $ssl
1560 *
1561 * @test
1562 * @dataProvider serverNamePatternDataProvider
1563 */
1564 public function isAllowedHostHeaderValueWorksCorrectlyWithWithServerNamePattern($httpHost, $serverName, $isAllowed, $serverPort = '80', $ssl = 'Off') {
1565 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = Utility\GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_SERVER_NAME;
1566 $_SERVER['SERVER_NAME'] = $serverName;
1567 $_SERVER['SERVER_PORT'] = $serverPort;
1568 $_SERVER['HTTPS'] = $ssl;
1569 $this->assertSame($isAllowed, GeneralUtilityFixture::isAllowedHostHeaderValue($httpHost));
1570 }
1571
1572 /**
1573 * @test
1574 */
1575 public function allGetIndpEnvCallsRelatedToHostNamesCallIsAllowedHostHeaderValue() {
1576 GeneralUtilityFixture::getIndpEnv('HTTP_HOST');
1577 GeneralUtilityFixture::getIndpEnv('TYPO3_HOST_ONLY');
1578 GeneralUtilityFixture::getIndpEnv('TYPO3_REQUEST_HOST');
1579 GeneralUtilityFixture::getIndpEnv('TYPO3_REQUEST_URL');
1580 $this->assertSame(4, GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount);
1581 }
1582
1583 /**
1584 * @param string $httpHost HTTP_HOST string
1585 * @param string $hostNamePattern trusted hosts pattern
1586 * @test
1587 * @dataProvider hostnamesNotMatchingTrustedHostsConfigurationDataProvider
1588 * @expectedException \UnexpectedValueException
1589 * @expectedExceptionCode 1396795884
1590 */
1591 public function getIndpEnvForHostThrowsExceptionForNotAllowedHostnameValues($httpHost, $hostNamePattern) {
1592 $_SERVER['HTTP_HOST'] = $httpHost;
1593 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = $hostNamePattern;
1594 GeneralUtilityFixture::getIndpEnv('HTTP_HOST');
1595 }
1596
1597 /**
1598 * @param string $httpHost HTTP_HOST string
1599 * @param string $hostNamePattern trusted hosts pattern (not used in this test currently)
1600 * @test
1601 * @dataProvider hostnamesNotMatchingTrustedHostsConfigurationDataProvider
1602 */
1603 public function getIndpEnvForHostAllowsAllHostnameValuesIfHostPatternIsSetToAllowAll($httpHost, $hostNamePattern) {
1604 $_SERVER['HTTP_HOST'] = $httpHost;
1605 $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = Utility\GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL;
1606 $this->assertSame($httpHost, Utility\GeneralUtility::getIndpEnv('HTTP_HOST'));
1607 }
1608
1609 /**
1610 * @test
1611 * @dataProvider hostnameAndPortDataProvider
1612 */
1613 public function getIndpEnvTypo3PortParsesHostnamesAndIpAdresses($httpHost, $dummy, $expectedPort) {
1614 $_SERVER['HTTP_HOST'] = $httpHost;
1615 $this->assertEquals($expectedPort, Utility\GeneralUtility::getIndpEnv('TYPO3_PORT'));
1616 }
1617
1618 //////////////////////////////////
1619 // Tests concerning underscoredToUpperCamelCase
1620 //////////////////////////////////
1621 /**
1622 * Data provider for underscoredToUpperCamelCase
1623 *
1624 * @return array expected, input string
1625 */
1626 public function underscoredToUpperCamelCaseDataProvider() {
1627 return array(
1628 'single word' => array('Blogexample', 'blogexample'),
1629 'multiple words' => array('BlogExample', 'blog_example')
1630 );
1631 }
1632
1633 /**
1634 * @test
1635 * @dataProvider underscoredToUpperCamelCaseDataProvider
1636 */
1637 public function underscoredToUpperCamelCase($expected, $inputString) {
1638 $this->assertEquals($expected, Utility\GeneralUtility::underscoredToUpperCamelCase($inputString));
1639 }
1640
1641 //////////////////////////////////
1642 // Tests concerning underscoredToLowerCamelCase
1643 //////////////////////////////////
1644 /**
1645 * Data provider for underscoredToLowerCamelCase
1646 *
1647 * @return array expected, input string
1648 */
1649 public function underscoredToLowerCamelCaseDataProvider() {
1650 return array(
1651 'single word' => array('minimalvalue', 'minimalvalue'),
1652 'multiple words' => array('minimalValue', 'minimal_value')
1653 );
1654 }
1655
1656 /**
1657 * @test
1658 * @dataProvider underscoredToLowerCamelCaseDataProvider
1659 */
1660 public function underscoredToLowerCamelCase($expected, $inputString) {
1661 $this->assertEquals($expected, Utility\GeneralUtility::underscoredToLowerCamelCase($inputString));
1662 }
1663
1664 //////////////////////////////////
1665 // Tests concerning camelCaseToLowerCaseUnderscored
1666 //////////////////////////////////
1667 /**
1668 * Data provider for camelCaseToLowerCaseUnderscored
1669 *
1670 * @return array expected, input string
1671 */
1672 public function camelCaseToLowerCaseUnderscoredDataProvider() {
1673 return array(
1674 'single word' => array('blogexample', 'blogexample'),
1675 'single word starting upper case' => array('blogexample', 'Blogexample'),
1676 'two words starting lower case' => array('minimal_value', 'minimalValue'),
1677 'two words starting upper case' => array('blog_example', 'BlogExample')
1678 );
1679 }
1680
1681 /**
1682 * @test
1683 * @dataProvider camelCaseToLowerCaseUnderscoredDataProvider
1684 */
1685 public function camelCaseToLowerCaseUnderscored($expected, $inputString) {
1686 $this->assertEquals($expected, Utility\GeneralUtility::camelCaseToLowerCaseUnderscored($inputString));
1687 }
1688
1689 //////////////////////////////////
1690 // Tests concerning lcFirst
1691 //////////////////////////////////
1692 /**
1693 * Data provider for lcFirst
1694 *
1695 * @return array expected, input string
1696 */
1697 public function lcfirstDataProvider() {
1698 return array(
1699 'single word' => array('blogexample', 'blogexample'),
1700 'single Word starting upper case' => array('blogexample', 'Blogexample'),
1701 'two words' => array('blogExample', 'BlogExample')
1702 );
1703 }
1704
1705 /**
1706 * @test
1707 * @dataProvider lcfirstDataProvider
1708 */
1709 public function lcFirst($expected, $inputString) {
1710 $this->assertEquals($expected, Utility\GeneralUtility::lcfirst($inputString));
1711 }
1712
1713 //////////////////////////////////
1714 // Tests concerning encodeHeader
1715 //////////////////////////////////
1716 /**
1717 * @test
1718 */
1719 public function encodeHeaderEncodesWhitespacesInQuotedPrintableMailHeader() {
1720 $this->assertEquals('=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=', Utility\GeneralUtility::encodeHeader('We test whether the copyright character © is encoded correctly', 'quoted-printable', 'utf-8'));
1721 }
1722
1723 /**
1724 * @test
1725 */
1726 public function encodeHeaderEncodesQuestionmarksInQuotedPrintableMailHeader() {
1727 $this->assertEquals('=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=', Utility\GeneralUtility::encodeHeader('Is the copyright character © really encoded correctly? Really?', 'quoted-printable', 'utf-8'));
1728 }
1729
1730 //////////////////////////////////
1731 // Tests concerning isValidUrl
1732 //////////////////////////////////
1733 /**
1734 * Data provider for valid isValidUrl's
1735 *
1736 * @return array Valid resource
1737 */
1738 public function validUrlValidResourceDataProvider() {
1739 return array(
1740 'http' => array('http://www.example.org/'),
1741 'http without trailing slash' => array('http://qwe'),
1742 'http directory with trailing slash' => array('http://www.example/img/dir/'),
1743 'http directory without trailing slash' => array('http://www.example/img/dir'),
1744 'http index.html' => array('http://example.com/index.html'),
1745 'http index.php' => array('http://www.example.com/index.php'),
1746 'http test.png' => array('http://www.example/img/test.png'),
1747 'http username password querystring and ancher' => array('https://user:pw@www.example.org:80/path?arg=value#fragment'),
1748 'file' => array('file:///tmp/test.c'),
1749 'file directory' => array('file://foo/bar'),
1750 'ftp directory' => array('ftp://ftp.example.com/tmp/'),
1751 'mailto' => array('mailto:foo@bar.com'),
1752 'news' => array('news:news.php.net'),
1753 'telnet' => array('telnet://192.0.2.16:80/'),
1754 'ldap' => array('ldap://[2001:db8::7]/c=GB?objectClass?one'),
1755 'http punycode domain name' => array('http://www.xn--bb-eka.at'),
1756 'http punicode subdomain' => array('http://xn--h-zfa.oebb.at'),
1757 'http domain-name umlauts' => array('http://www.öbb.at'),
1758 'http subdomain umlauts' => array('http://äh.oebb.at'),
1759 );
1760 }
1761
1762 /**
1763 * @test
1764 * @dataProvider validUrlValidResourceDataProvider
1765 */
1766 public function validURLReturnsTrueForValidResource($url) {
1767 $this->assertTrue(Utility\GeneralUtility::isValidUrl($url));
1768 }
1769
1770 /**
1771 * Data provider for invalid isValidUrl's
1772 *
1773 * @return array Invalid ressource
1774 */
1775 public function isValidUrlInvalidRessourceDataProvider() {
1776 return array(
1777 'http missing colon' => array('http//www.example/wrong/url/'),
1778 'http missing slash' => array('http:/www.example'),
1779 'hostname only' => array('www.example.org/'),
1780 'file missing protocol specification' => array('/tmp/test.c'),
1781 'slash only' => array('/'),
1782 'string http://' => array('http://'),
1783 'string http:/' => array('http:/'),
1784 'string http:' => array('http:'),
1785 'string http' => array('http'),
1786 'empty string' => array(''),
1787 'string -1' => array('-1'),
1788 'string array()' => array('array()'),
1789 'random string' => array('qwe'),
1790 'http directory umlauts' => array('http://www.oebb.at/äöü/'),
1791 );
1792 }
1793
1794 /**
1795 * @test
1796 * @dataProvider isValidUrlInvalidRessourceDataProvider
1797 */
1798 public function validURLReturnsFalseForInvalidRessoure($url) {
1799 $this->assertFalse(Utility\GeneralUtility::isValidUrl($url));
1800 }
1801
1802 //////////////////////////////////
1803 // Tests concerning isOnCurrentHost
1804 //////////////////////////////////
1805 /**
1806 * @test
1807 */
1808 public function isOnCurrentHostReturnsTrueWithCurrentHost() {
1809 $testUrl = Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
1810 $this->assertTrue(Utility\GeneralUtility::isOnCurrentHost($testUrl));
1811 }
1812
1813 /**
1814 * Data provider for invalid isOnCurrentHost's
1815 *
1816 * @return array Invalid Hosts
1817 */
1818 public function checkisOnCurrentHostInvalidHosts() {
1819 return array(
1820 'empty string' => array(''),
1821 'arbitrary string' => array('arbitrary string'),
1822 'localhost IP' => array('127.0.0.1'),
1823 'relative path' => array('./relpath/file.txt'),
1824 'absolute path' => array('/abspath/file.txt?arg=value'),
1825 'differnt host' => array(Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org')
1826 );
1827 }
1828
1829 ////////////////////////////////////////
1830 // Tests concerning sanitizeLocalUrl
1831 ////////////////////////////////////////
1832 /**
1833 * Data provider for valid sanitizeLocalUrl's
1834 *
1835 * @return array Valid url
1836 */
1837 public function sanitizeLocalUrlValidUrlDataProvider() {
1838 $subDirectory = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
1839 $typo3SiteUrl = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
1840 $typo3RequestHost = Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
1841 return array(
1842 'alt_intro.php' => array('alt_intro.php'),
1843 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
1844 $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
1845 $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
1846 '../index.php' => array('../index.php'),
1847 '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
1848 '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
1849 '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
1850 PATH_site . 'typo3/alt_intro.php' => array(PATH_site . 'typo3/alt_intro.php'),
1851 $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
1852 $typo3RequestHost . $subDirectory . '/index.php' => array($typo3RequestHost . $subDirectory . '/index.php')
1853 );
1854 }
1855
1856 /**
1857 * @test
1858 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1859 */
1860 public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
1861 $this->assertEquals($url, Utility\GeneralUtility::sanitizeLocalUrl($url));
1862 }
1863
1864 /**
1865 * @test
1866 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1867 */
1868 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
1869 $this->assertEquals(rawurlencode($url), Utility\GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
1870 }
1871
1872 /**
1873 * Data provider for invalid sanitizeLocalUrl's
1874 *
1875 * @return array Valid url
1876 */
1877 public function sanitizeLocalUrlInvalidDataProvider() {
1878 return array(
1879 'empty string' => array(''),
1880 'http domain' => array('http://www.google.de/'),
1881 'https domain' => array('https://www.google.de/'),
1882 'relative path with XSS' => array('../typo3/whatever.php?argument=javascript:alert(0)')
1883 );
1884 }
1885
1886 /**
1887 * @test
1888 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1889 */
1890 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
1891 $this->assertEquals('', Utility\GeneralUtility::sanitizeLocalUrl($url));
1892 }
1893
1894 /**
1895 * @test
1896 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1897 */
1898 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
1899 $this->assertEquals('', Utility\GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
1900 }
1901
1902 ////////////////////////////////////////
1903 // Tests concerning unlink_tempfile
1904 ////////////////////////////////////////
1905
1906 /**
1907 * @test
1908 */
1909 public function unlink_tempfileRemovesValidFileInTypo3temp() {
1910 $fixtureFile = __DIR__ . '/Fixtures/clear.gif';
1911 $testFilename = PATH_site . 'typo3temp/' . $this->getUniqueId('test_') . '.gif';
1912 @copy($fixtureFile, $testFilename);
1913 Utility\GeneralUtility::unlink_tempfile($testFilename);
1914 $fileExists = file_exists($testFilename);
1915 $this->assertFalse($fileExists);
1916 }
1917
1918 /**
1919 * @test
1920 */
1921 public function unlink_tempfileRemovesHiddenFile() {
1922 $fixtureFile = __DIR__ . '/Fixtures/clear.gif';
1923 $testFilename = PATH_site . 'typo3temp/' . $this->getUniqueId('.test_') . '.gif';
1924 @copy($fixtureFile, $testFilename);
1925 Utility\GeneralUtility::unlink_tempfile($testFilename);
1926 $fileExists = file_exists($testFilename);
1927 $this->assertFalse($fileExists);
1928 }
1929
1930 /**
1931 * @test
1932 */
1933 public function unlink_tempfileReturnsTrueIfFileWasRemoved() {
1934 $fixtureFile = __DIR__ . '/Fixtures/clear.gif';
1935 $testFilename = PATH_site . 'typo3temp/' . $this->getUniqueId('test_') . '.gif';
1936 @copy($fixtureFile, $testFilename);
1937 $returnValue = Utility\GeneralUtility::unlink_tempfile($testFilename);
1938 $this->assertTrue($returnValue);
1939 }
1940
1941 /**
1942 * @test
1943 */
1944 public function unlink_tempfileReturnsNullIfFileDoesNotExist() {
1945 $returnValue = Utility\GeneralUtility::unlink_tempfile(PATH_site . 'typo3temp/' . $this->getUniqueId('i_do_not_exist'));
1946 $this->assertNull($returnValue);
1947 }
1948
1949 /**
1950 * @test
1951 */
1952 public function unlink_tempfileReturnsNullIfFileIsNowWithinTypo3temp() {
1953 $returnValue = Utility\GeneralUtility::unlink_tempfile('/tmp/typo3-unit-test-unlink_tempfile');
1954 $this->assertNull($returnValue);
1955 }
1956
1957 //////////////////////////////////////
1958 // Tests concerning tempnam
1959 //////////////////////////////////////
1960
1961 /**
1962 * @test
1963 */
1964 public function tempnamReturnsPathStartingWithGivenPrefix() {
1965 $filePath = Utility\GeneralUtility::tempnam('foo');
1966 $fileName = basename($filePath);
1967 $this->assertStringStartsWith('foo', $fileName);
1968 }
1969
1970 /**
1971 * @test
1972 */
1973 public function tempnamReturnsPathWithoutBackslashes() {
1974 $filePath = Utility\GeneralUtility::tempnam('foo');
1975 $this->assertNotContains('\\', $filePath);
1976 }
1977
1978 /**
1979 * @test
1980 */
1981 public function tempnamReturnsAbsolutePathInsideDocumentRoot() {
1982 $filePath = Utility\GeneralUtility::tempnam('foo');
1983 $this->assertStringStartsWith(PATH_site, $filePath);
1984 }
1985
1986 //////////////////////////////////////
1987 // Tests concerning addSlashesOnArray
1988 //////////////////////////////////////
1989 /**
1990 * @test
1991 */
1992 public function addSlashesOnArrayAddsSlashesRecursive() {
1993 $inputArray = array(
1994 'key1' => array(
1995 'key11' => 'val\'ue1',
1996 'key12' => 'val"ue2'
1997 ),
1998 'key2' => 'val\\ue3'
1999 );
2000 $expectedResult = array(
2001 'key1' => array(
2002 'key11' => 'val\\\'ue1',
2003 'key12' => 'val\\"ue2'
2004 ),
2005 'key2' => 'val\\\\ue3'
2006 );
2007 Utility\GeneralUtility::addSlashesOnArray($inputArray);
2008 $this->assertEquals($expectedResult, $inputArray);
2009 }
2010
2011 //////////////////////////////////////
2012 // Tests concerning addSlashesOnArray
2013 //////////////////////////////////////
2014 /**
2015 * @test
2016 */
2017 public function stripSlashesOnArrayStripsSlashesRecursive() {
2018 $inputArray = array(
2019 'key1' => array(
2020 'key11' => 'val\\\'ue1',
2021 'key12' => 'val\\"ue2'
2022 ),
2023 'key2' => 'val\\\\ue3'
2024 );
2025 $expectedResult = array(
2026 'key1' => array(
2027 'key11' => 'val\'ue1',
2028 'key12' => 'val"ue2'
2029 ),
2030 'key2' => 'val\\ue3'
2031 );
2032 Utility\GeneralUtility::stripSlashesOnArray($inputArray);
2033 $this->assertEquals($expectedResult, $inputArray);
2034 }
2035
2036 //////////////////////////////////////
2037 // Tests concerning removeDotsFromTS
2038 //////////////////////////////////////
2039 /**
2040 * @test
2041 */
2042 public function removeDotsFromTypoScriptSucceedsWithDottedArray() {
2043 $typoScript = array(
2044 'propertyA.' => array(
2045 'keyA.' => array(
2046 'valueA' => 1
2047 ),
2048 'keyB' => 2
2049 ),
2050 'propertyB' => 3
2051 );
2052 $expectedResult = array(
2053 'propertyA' => array(
2054 'keyA' => array(
2055 'valueA' => 1
2056 ),
2057 'keyB' => 2
2058 ),
2059 'propertyB' => 3
2060 );
2061 $this->assertEquals($expectedResult, Utility\GeneralUtility::removeDotsFromTS($typoScript));
2062 }
2063
2064 /**
2065 * @test
2066 */
2067 public function removeDotsFromTypoScriptOverridesSubArray() {
2068 $typoScript = array(
2069 'propertyA.' => array(
2070 'keyA' => 'getsOverridden',
2071 'keyA.' => array(
2072 'valueA' => 1
2073 ),
2074 'keyB' => 2
2075 ),
2076 'propertyB' => 3
2077 );
2078 $expectedResult = array(
2079 'propertyA' => array(
2080 'keyA' => array(
2081 'valueA' => 1
2082 ),
2083 'keyB' => 2
2084 ),
2085 'propertyB' => 3
2086 );
2087 $this->assertEquals($expectedResult, Utility\GeneralUtility::removeDotsFromTS($typoScript));
2088 }
2089
2090 /**
2091 * @test
2092 */
2093 public function removeDotsFromTypoScriptOverridesWithScalar() {
2094 $typoScript = array(
2095 'propertyA.' => array(
2096 'keyA.' => array(
2097 'valueA' => 1
2098 ),
2099 'keyA' => 'willOverride',
2100 'keyB' => 2
2101 ),
2102 'propertyB' => 3
2103 );
2104 $expectedResult = array(
2105 'propertyA' => array(
2106 'keyA' => 'willOverride',
2107 'keyB' => 2
2108 ),
2109 'propertyB' => 3
2110 );
2111 $this->assertEquals($expectedResult, Utility\GeneralUtility::removeDotsFromTS($typoScript));
2112 }
2113
2114 //////////////////////////////////////
2115 // Tests concerning get_dirs
2116 //////////////////////////////////////
2117 /**
2118 * @test
2119 */
2120 public function getDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
2121 $path = PATH_typo3conf;
2122 $directories = Utility\GeneralUtility::get_dirs($path);
2123 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY, $directories);
2124 }
2125
2126 /**
2127 * @test
2128 */
2129 public function getDirsReturnsStringErrorOnPathFailure() {
2130 $path = 'foo';
2131 $result = Utility\GeneralUtility::get_dirs($path);
2132 $expectedResult = 'error';
2133 $this->assertEquals($expectedResult, $result);
2134 }
2135
2136 //////////////////////////////////
2137 // Tests concerning hmac
2138 //////////////////////////////////
2139 /**
2140 * @test
2141 */
2142 public function hmacReturnsHashOfProperLength() {
2143 $hmac = Utility\GeneralUtility::hmac('message');
2144 $this->assertTrue(!empty($hmac) && is_string($hmac));
2145 $this->assertTrue(strlen($hmac) == 40);
2146 }
2147
2148 /**
2149 * @test
2150 */
2151 public function hmacReturnsEqualHashesForEqualInput() {
2152 $msg0 = 'message';
2153 $msg1 = 'message';
2154 $this->assertEquals(Utility\GeneralUtility::hmac($msg0), Utility\GeneralUtility::hmac($msg1));
2155 }
2156
2157 /**
2158 * @test
2159 */
2160 public function hmacReturnsNoEqualHashesForNonEqualInput() {
2161 $msg0 = 'message0';
2162 $msg1 = 'message1';
2163 $this->assertNotEquals(Utility\GeneralUtility::hmac($msg0), Utility\GeneralUtility::hmac($msg1));
2164 }
2165
2166 //////////////////////////////////
2167 // Tests concerning quoteJSvalue
2168 //////////////////////////////////
2169 /**
2170 * Data provider for quoteJSvalueTest.
2171 *
2172 * @return array
2173 */
2174 public function quoteJsValueDataProvider() {
2175 return array(
2176 'Immune characters are returned as is' => array(
2177 '._,',
2178 '._,'
2179 ),
2180 'Alphanumerical characters are returned as is' => array(
2181 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
2182 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
2183 ),
2184 'Angle brackets and ampersand are encoded' => array(
2185 '<>&',
2186 '\\u003C\\u003E\\u0026'
2187 ),
2188 'Quotes and backslashes are encoded' => array(
2189 '"\'\\',
2190 '\\u0022\\u0027\\u005C'
2191 ),
2192 'Forward slashes are escaped' => array(
2193 '</script>',
2194 '\\u003C\\/script\\u003E'
2195 ),
2196 'Empty string stays empty' => array(
2197 '',
2198 ''
2199 ),
2200 'Exclamation mark and space are properly encoded' => array(
2201 'Hello World!',
2202 'Hello\\u0020World\\u0021'
2203 ),
2204 'Whitespaces are properly encoded' => array(
2205 TAB . LF . CR . ' ',
2206 '\\u0009\\u000A\\u000D\\u0020'
2207 ),
2208 'Null byte is properly encoded' => array(
2209 chr(0),
2210 '\\u0000'
2211 ),
2212 'Umlauts are properly encoded' => array(
2213 'ÜüÖöÄä',
2214 '\\u00dc\\u00fc\\u00d6\\u00f6\\u00c4\\u00e4'
2215 )
2216 );
2217 }
2218
2219 /**
2220 * @test
2221 * @param string $input
2222 * @param string $expected
2223 * @dataProvider quoteJsValueDataProvider
2224 */
2225 public function quoteJsValueTest($input, $expected) {
2226 $this->assertSame('\'' . $expected . '\'', Utility\GeneralUtility::quoteJSvalue($input));
2227 }
2228
2229 //////////////////////////////////
2230 // Tests concerning readLLfile
2231 //////////////////////////////////
2232 /**
2233 * @test
2234 */
2235 public function readLLfileHandlesLocallangXMLOverride() {
2236 $unique = 'locallangXMLOverrideTest' . substr($this->getUniqueId(), 0, 10);
2237 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2238 <T3locallang>
2239 <data type="array">
2240 <languageKey index="default" type="array">
2241 <label index="buttons.logout">EXIT</label>
2242 </languageKey>
2243 </data>
2244 </T3locallang>';
2245 $file = PATH_site . 'typo3temp/' . $unique . '.xml';
2246 Utility\GeneralUtility::writeFileToTypo3tempDir($file, $xml);
2247 // Make sure there is no cached version of the label
2248 Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
2249 // Get default value
2250 $defaultLL = Utility\GeneralUtility::readLLfile('EXT:lang/locallang_core.xlf', 'default');
2251 // Clear language cache again
2252 Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('l10n')->flush();
2253 // Set override file
2254 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xlf'][$unique] = $file;
2255 /** @var $store \TYPO3\CMS\Core\Localization\LanguageStore */
2256 $store = Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageStore::class);
2257 $store->flushData('EXT:lang/locallang_core.xlf');
2258 // Get override value
2259 $overrideLL = Utility\GeneralUtility::readLLfile('EXT:lang/locallang_core.xlf', 'default');
2260 // Clean up again
2261 unlink($file);
2262 $this->assertNotEquals($overrideLL['default']['buttons.logout'][0]['target'], '');
2263 $this->assertNotEquals($defaultLL['default']['buttons.logout'][0]['target'], $overrideLL['default']['buttons.logout'][0]['target']);
2264 $this->assertEquals($overrideLL['default']['buttons.logout'][0]['target'], 'EXIT');
2265 }
2266
2267 ///////////////////////////////
2268 // Tests concerning _GETset()
2269 ///////////////////////////////
2270 /**
2271 * @test
2272 */
2273 public function getSetWritesArrayToGetSystemVariable() {
2274 $_GET = array();
2275 $GLOBALS['HTTP_GET_VARS'] = array();
2276 $getParameters = array('foo' => 'bar');
2277 Utility\GeneralUtility::_GETset($getParameters);
2278 $this->assertSame($getParameters, $_GET);
2279 }
2280
2281 /**
2282 * @test
2283 */
2284 public function getSetWritesArrayToGlobalsHttpGetVars() {
2285 $_GET = array();
2286 $GLOBALS['HTTP_GET_VARS'] = array();
2287 $getParameters = array('foo' => 'bar');
2288 Utility\GeneralUtility::_GETset($getParameters);
2289 $this->assertSame($getParameters, $GLOBALS['HTTP_GET_VARS']);
2290 }
2291
2292 /**
2293 * @test
2294 */
2295 public function getSetForArrayDropsExistingValues() {
2296 $_GET = array();
2297 $GLOBALS['HTTP_GET_VARS'] = array();
2298 Utility\GeneralUtility::_GETset(array('foo' => 'bar'));
2299 Utility\GeneralUtility::_GETset(array('oneKey' => 'oneValue'));
2300 $this->assertEquals(array('oneKey' => 'oneValue'), $GLOBALS['HTTP_GET_VARS']);
2301 }
2302
2303 /**
2304 * @test
2305 */
2306 public function getSetAssignsOneValueToOneKey() {
2307 $_GET = array();
2308 $GLOBALS['HTTP_GET_VARS'] = array();
2309 Utility\GeneralUtility::_GETset('oneValue', 'oneKey');
2310 $this->assertEquals('oneValue', $GLOBALS['HTTP_GET_VARS']['oneKey']);
2311 }
2312
2313 /**
2314 * @test
2315 */
2316 public function getSetForOneValueDoesNotDropUnrelatedValues() {
2317 $_GET = array();
2318 $GLOBALS['HTTP_GET_VARS'] = array();
2319 Utility\GeneralUtility::_GETset(array('foo' => 'bar'));
2320 Utility\GeneralUtility::_GETset('oneValue', 'oneKey');
2321 $this->assertEquals(array('foo' => 'bar', 'oneKey' => 'oneValue'), $GLOBALS['HTTP_GET_VARS']);
2322 }
2323
2324 /**
2325 * @test
2326 */
2327 public function getSetCanAssignsAnArrayToASpecificArrayElement() {
2328 $_GET = array();
2329 $GLOBALS['HTTP_GET_VARS'] = array();
2330 Utility\GeneralUtility::_GETset(array('childKey' => 'oneValue'), 'parentKey');
2331 $this->assertEquals(array('parentKey' => array('childKey' => 'oneValue')), $GLOBALS['HTTP_GET_VARS']);
2332 }
2333
2334 /**
2335 * @test
2336 */
2337 public function getSetCanAssignAStringValueToASpecificArrayChildElement() {
2338 $_GET = array();
2339 $GLOBALS['HTTP_GET_VARS'] = array();
2340 Utility\GeneralUtility::_GETset('oneValue', 'parentKey|childKey');
2341 $this->assertEquals(array('parentKey' => array('childKey' => 'oneValue')), $GLOBALS['HTTP_GET_VARS']);
2342 }
2343
2344 /**
2345 * @test
2346 */
2347 public function getSetCanAssignAnArrayToASpecificArrayChildElement() {
2348 $_GET = array();
2349 $GLOBALS['HTTP_GET_VARS'] = array();
2350 Utility\GeneralUtility::_GETset(array('key1' => 'value1', 'key2' => 'value2'), 'parentKey|childKey');
2351 $this->assertEquals(array(
2352 'parentKey' => array(
2353 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
2354 )
2355 ), $GLOBALS['HTTP_GET_VARS']);
2356 }
2357
2358 ///////////////////////////
2359 // Tests concerning minifyJavaScript
2360 ///////////////////////////
2361 /**
2362 * @test
2363 */
2364 public function minifyJavaScriptReturnsInputStringIfNoHookIsRegistered() {
2365 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript']);
2366 $testString = $this->getUniqueId('string');
2367 $this->assertSame($testString, Utility\GeneralUtility::minifyJavaScript($testString));
2368 }
2369
2370 /**
2371 * Create an own hook callback class, register as hook, and check
2372 * if given string to compress is given to hook method
2373 *
2374 * @test
2375 */
2376 public function minifyJavaScriptCallsRegisteredHookWithInputString() {
2377 $hookClassName = $this->getUniqueId('tx_coretest');
2378 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2379 $functionName = '&' . $hookClassName . '->minify';
2380 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2381 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2382 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2383 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2384 $minifyHookMock->expects($this->once())->method('minify')->will($this->returnCallback(array($this, 'isMinifyJavaScriptHookCalledCallback')));
2385 Utility\GeneralUtility::minifyJavaScript('foo');
2386 }
2387
2388 /**
2389 * Callback function used in minifyJavaScriptCallsRegisteredHookWithInputString test
2390 *
2391 * @param array $params
2392 */
2393 public function isMinifyJavaScriptHookCalledCallback(array $params) {
2394 // We can not throw an exception here, because that would be caught by the
2395 // minifyJavaScript method under test itself. Thus, we just die if the
2396 // input string is not ok.
2397 if ($params['script'] !== 'foo') {
2398 die('broken');
2399 }
2400 }
2401
2402 /**
2403 * Create a hook callback, use callback to throw an exception and check
2404 * if the exception is given as error parameter to the calling method.
2405 *
2406 * @test
2407 */
2408 public function minifyJavaScriptReturnsErrorStringOfHookException() {
2409 $hookClassName = $this->getUniqueId('tx_coretest');
2410 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2411 $functionName = '&' . $hookClassName . '->minify';
2412 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2413 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2414 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2415 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2416 $minifyHookMock->expects($this->any())->method('minify')->will($this->returnCallback(array($this, 'minifyJavaScriptErroneousCallback')));
2417 $error = '';
2418 Utility\GeneralUtility::minifyJavaScript('string to compress', $error);
2419 $this->assertSame('Error minifying java script: foo', $error);
2420 }
2421
2422 /**
2423 * Check if the error message that is returned by the hook callback
2424 * is logged to \TYPO3\CMS\Core\Utility\GeneralUtility::devLog.
2425 *
2426 * @test
2427 */
2428 public function minifyJavaScriptWritesExceptionMessageToDevLog() {
2429 $t3libDivMock = $this->getUniqueId('GeneralUtility');
2430 eval('namespace ' . __NAMESPACE__ . '; class ' . $t3libDivMock . ' extends \\TYPO3\\CMS\\Core\\Utility\\GeneralUtility {' . ' public static function devLog($errorMessage) {' . ' if (!($errorMessage === \'Error minifying java script: foo\')) {' . ' throw new \\UnexpectedValue(\'broken\');' . ' }' . ' throw new \\RuntimeException();' . ' }' . '}');
2431 $t3libDivMock = __NAMESPACE__ . '\\' . $t3libDivMock;
2432 $hookClassName = $this->getUniqueId('tx_coretest');
2433 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2434 $functionName = '&' . $hookClassName . '->minify';
2435 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2436 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2437 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2438 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2439 $minifyHookMock->expects($this->any())->method('minify')->will($this->returnCallback(array($this, 'minifyJavaScriptErroneousCallback')));
2440 $this->setExpectedException('\\RuntimeException');
2441 $t3libDivMock::minifyJavaScript('string to compress');
2442 }
2443
2444 /**
2445 * Callback function used in
2446 * minifyJavaScriptReturnsErrorStringOfHookException and
2447 * minifyJavaScriptWritesExceptionMessageToDevLog
2448 *
2449 * @throws \RuntimeException
2450 */
2451 public function minifyJavaScriptErroneousCallback() {
2452 throw new \RuntimeException('foo', 1344888548);
2453 }
2454
2455 ///////////////////////////////
2456 // Tests concerning fixPermissions
2457 ///////////////////////////////
2458 /**
2459 * @test
2460 */
2461 public function fixPermissionsSetsGroup() {
2462 if (TYPO3_OS == 'WIN') {
2463 $this->markTestSkipped('fixPermissionsSetsGroup() tests not available on Windows');
2464 }
2465 if (!function_exists('posix_getegid')) {
2466 $this->markTestSkipped('Function posix_getegid() not available, fixPermissionsSetsGroup() tests skipped');
2467 }
2468 if (posix_getegid() === -1) {
2469 $this->markTestSkipped('The fixPermissionsSetsGroup() is not available on Mac OS because posix_getegid() always returns -1 on Mac OS.');
2470 }
2471 // Create and prepare test file
2472 $filename = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2473 Utility\GeneralUtility::writeFileToTypo3tempDir($filename, '42');
2474 $this->testFilesToDelete[] = $filename;
2475 $currentGroupId = posix_getegid();
2476 // Set target group and run method
2477 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = $currentGroupId;
2478 Utility\GeneralUtility::fixPermissions($filename);
2479 clearstatcache();
2480 $this->assertEquals($currentGroupId, filegroup($filename));
2481 }
2482
2483 /**
2484 * @test
2485 */
2486 public function fixPermissionsSetsPermissionsToFile() {
2487 if (TYPO3_OS == 'WIN') {
2488 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2489 }
2490 // Create and prepare test file
2491 $filename = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2492 Utility\GeneralUtility::writeFileToTypo3tempDir($filename, '42');
2493 $this->testFilesToDelete[] = $filename;
2494 chmod($filename, 482);
2495 // Set target permissions and run method
2496 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2497 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($filename);
2498 clearstatcache();
2499 $this->assertTrue($fixPermissionsResult);
2500 $this->assertEquals('0660', substr(decoct(fileperms($filename)), 2));
2501 }
2502
2503 /**
2504 * @test
2505 */
2506 public function fixPermissionsSetsPermissionsToHiddenFile() {
2507 if (TYPO3_OS == 'WIN') {
2508 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2509 }
2510 // Create and prepare test file
2511 $filename = PATH_site . 'typo3temp/' . $this->getUniqueId('.test_');
2512 Utility\GeneralUtility::writeFileToTypo3tempDir($filename, '42');
2513 $this->testFilesToDelete[] = $filename;
2514 chmod($filename, 482);
2515 // Set target permissions and run method
2516 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2517 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($filename);
2518 clearstatcache();
2519 $this->assertTrue($fixPermissionsResult);
2520 $this->assertEquals('0660', substr(decoct(fileperms($filename)), 2));
2521 }
2522
2523 /**
2524 * @test
2525 */
2526 public function fixPermissionsSetsPermissionsToDirectory() {
2527 if (TYPO3_OS == 'WIN') {
2528 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2529 }
2530 // Create and prepare test directory
2531 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2532 Utility\GeneralUtility::mkdir($directory);
2533 $this->testFilesToDelete[] = $directory;
2534 chmod($directory, 1551);
2535 // Set target permissions and run method
2536 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2537 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($directory);
2538 clearstatcache();
2539 $this->assertTrue($fixPermissionsResult);
2540 $this->assertEquals('0770', substr(decoct(fileperms($directory)), 1));
2541 }
2542
2543 /**
2544 * @test
2545 */
2546 public function fixPermissionsSetsPermissionsToDirectoryWithTrailingSlash() {
2547 if (TYPO3_OS == 'WIN') {
2548 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2549 }
2550 // Create and prepare test directory
2551 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2552 Utility\GeneralUtility::mkdir($directory);
2553 $this->testFilesToDelete[] = $directory;
2554 chmod($directory, 1551);
2555 // Set target permissions and run method
2556 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2557 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($directory . '/');
2558 // Get actual permissions and clean up
2559 clearstatcache();
2560 $this->assertTrue($fixPermissionsResult);
2561 $this->assertEquals('0770', substr(decoct(fileperms($directory)), 1));
2562 }
2563
2564 /**
2565 * @test
2566 */
2567 public function fixPermissionsSetsPermissionsToHiddenDirectory() {
2568 if (TYPO3_OS == 'WIN') {
2569 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2570 }
2571 // Create and prepare test directory
2572 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('.test_');
2573 Utility\GeneralUtility::mkdir($directory);
2574 $this->testFilesToDelete[] = $directory;
2575 chmod($directory, 1551);
2576 // Set target permissions and run method
2577 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2578 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($directory);
2579 // Get actual permissions and clean up
2580 clearstatcache();
2581 $this->assertTrue($fixPermissionsResult);
2582 $this->assertEquals('0770', substr(decoct(fileperms($directory)), 1));
2583 }
2584
2585 /**
2586 * @test
2587 */
2588 public function fixPermissionsCorrectlySetsPermissionsRecursive() {
2589 if (TYPO3_OS == 'WIN') {
2590 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2591 }
2592 // Create and prepare test directory and file structure
2593 $baseDirectory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2594 Utility\GeneralUtility::mkdir($baseDirectory);
2595 $this->testFilesToDelete[] = $baseDirectory;
2596 chmod($baseDirectory, 1751);
2597 Utility\GeneralUtility::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
2598 chmod($baseDirectory . '/file', 482);
2599 Utility\GeneralUtility::mkdir($baseDirectory . '/foo');
2600 chmod($baseDirectory . '/foo', 1751);
2601 Utility\GeneralUtility::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
2602 chmod($baseDirectory . '/foo/file', 482);
2603 Utility\GeneralUtility::mkdir($baseDirectory . '/.bar');
2604 chmod($baseDirectory . '/.bar', 1751);
2605 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
2606 // \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
2607 // \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
2608 touch($baseDirectory . '/.bar/.file', '42');
2609 chmod($baseDirectory . '/.bar/.file', 482);
2610 touch($baseDirectory . '/.bar/..file2', '42');
2611 chmod($baseDirectory . '/.bar/..file2', 482);
2612 // Set target permissions and run method
2613 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2614 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2615 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($baseDirectory, TRUE);
2616 // Get actual permissions
2617 clearstatcache();
2618 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
2619 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
2620 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
2621 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
2622 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
2623 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
2624 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
2625 // Test if everything was ok
2626 $this->assertTrue($fixPermissionsResult);
2627 $this->assertEquals('0770', $resultBaseDirectoryPermissions);
2628 $this->assertEquals('0660', $resultBaseFilePermissions);
2629 $this->assertEquals('0770', $resultFooDirectoryPermissions);
2630 $this->assertEquals('0660', $resultFooFilePermissions);
2631 $this->assertEquals('0770', $resultBarDirectoryPermissions);
2632 $this->assertEquals('0660', $resultBarFilePermissions);
2633 $this->assertEquals('0660', $resultBarFile2Permissions);
2634 }
2635
2636 /**
2637 * @test
2638 */
2639 public function fixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
2640 if (TYPO3_OS == 'WIN') {
2641 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2642 }
2643 // Create and prepare test file
2644 $filename = PATH_site . 'typo3temp/../typo3temp/' . $this->getUniqueId('test_');
2645 touch($filename);
2646 chmod($filename, 482);
2647 // Set target permissions and run method
2648 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2649 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($filename);
2650 clearstatcache();
2651 $this->testFilesToDelete[] = $filename;
2652 $this->assertFalse($fixPermissionsResult);
2653 }
2654
2655 /**
2656 * @test
2657 */
2658 public function fixPermissionsSetsPermissionsWithRelativeFileReference() {
2659 if (TYPO3_OS == 'WIN') {
2660 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2661 }
2662 $filename = 'typo3temp/' . $this->getUniqueId('test_');
2663 Utility\GeneralUtility::writeFileToTypo3tempDir(PATH_site . $filename, '42');
2664 $this->testFilesToDelete[] = PATH_site . $filename;
2665 chmod(PATH_site . $filename, 482);
2666 // Set target permissions and run method
2667 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2668 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($filename);
2669 clearstatcache();
2670 $this->assertTrue($fixPermissionsResult);
2671 $this->assertEquals('0660', substr(decoct(fileperms(PATH_site . $filename)), 2));
2672 }
2673
2674 /**
2675 * @test
2676 */
2677 public function fixPermissionsSetsDefaultPermissionsToFile() {
2678 if (TYPO3_OS == 'WIN') {
2679 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2680 }
2681 $filename = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2682 Utility\GeneralUtility::writeFileToTypo3tempDir($filename, '42');
2683 $this->testFilesToDelete[] = $filename;
2684 chmod($filename, 482);
2685 unset($GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask']);
2686 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($filename);
2687 clearstatcache();
2688 $this->assertTrue($fixPermissionsResult);
2689 $this->assertEquals('0644', substr(decoct(fileperms($filename)), 2));
2690 }
2691
2692 /**
2693 * @test
2694 */
2695 public function fixPermissionsSetsDefaultPermissionsToDirectory() {
2696 if (TYPO3_OS == 'WIN') {
2697 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2698 }
2699 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2700 Utility\GeneralUtility::mkdir($directory);
2701 $this->testFilesToDelete[] = $directory;
2702 chmod($directory, 1551);
2703 unset($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']);
2704 $fixPermissionsResult = Utility\GeneralUtility::fixPermissions($directory);
2705 clearstatcache();
2706 $this->assertTrue($fixPermissionsResult);
2707 $this->assertEquals('0755', substr(decoct(fileperms($directory)), 1));
2708 }
2709
2710 ///////////////////////////////
2711 // Tests concerning mkdir
2712 ///////////////////////////////
2713 /**
2714 * @test
2715 */
2716 public function mkdirCreatesDirectory() {
2717 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2718 $mkdirResult = Utility\GeneralUtility::mkdir($directory);
2719 $this->testFilesToDelete[] = $directory;
2720 clearstatcache();
2721 $this->assertTrue($mkdirResult);
2722 $this->assertTrue(is_dir($directory));
2723 }
2724
2725 /**
2726 * @test
2727 */
2728 public function mkdirCreatesHiddenDirectory() {
2729 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('.test_');
2730 $mkdirResult = Utility\GeneralUtility::mkdir($directory);
2731 $this->testFilesToDelete[] = $directory;
2732 clearstatcache();
2733 $this->assertTrue($mkdirResult);
2734 $this->assertTrue(is_dir($directory));
2735 }
2736
2737 /**
2738 * @test
2739 */
2740 public function mkdirCreatesDirectoryWithTrailingSlash() {
2741 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_') . '/';
2742 $mkdirResult = Utility\GeneralUtility::mkdir($directory);
2743 $this->testFilesToDelete[] = $directory;
2744 clearstatcache();
2745 $this->assertTrue($mkdirResult);
2746 $this->assertTrue(is_dir($directory));
2747 }
2748
2749 /**
2750 * @test
2751 */
2752 public function mkdirSetsPermissionsOfCreatedDirectory() {
2753 if (TYPO3_OS == 'WIN') {
2754 $this->markTestSkipped('mkdirSetsPermissionsOfCreatedDirectory() test not available on Windows');
2755 }
2756 $directory = PATH_site . 'typo3temp/' . $this->getUniqueId('test_');
2757 $oldUmask = umask(19);
2758 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0772';
2759 Utility\GeneralUtility::mkdir($directory);
2760 $this->testFilesToDelete[] = $directory;
2761 clearstatcache();
2762 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
2763 umask($oldUmask);
2764 $this->assertEquals($resultDirectoryPermissions, '0772');
2765 }
2766
2767 /**
2768 * @test
2769 */
2770 public function mkdirSetsGroupOwnershipOfCreatedDirectory() {
2771 if (!function_exists('posix_getegid')) {
2772 $this->markTestSkipped('Function posix_getegid() not available, mkdirSetsGroupOwnershipOfCreatedDirectory() tests skipped');
2773 }
2774 if (posix_getegid() === -1) {
2775 $this->markTestSkipped('The mkdirSetsGroupOwnershipOfCreatedDirectory() is not available on Mac OS because posix_getegid() always returns -1 on Mac OS.');
2776 }
2777 $swapGroup = $this->checkGroups(__FUNCTION__);
2778 if ($swapGroup !== FALSE) {
2779 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = $swapGroup;
2780 $directory = $this->getUniqueId('mkdirtest_');
2781 Utility\GeneralUtility::mkdir(PATH_site . 'typo3temp/' . $directory);
2782 $this->testFilesToDelete[] = PATH_site . 'typo3temp/' . $directory;
2783 clearstatcache();
2784 $resultDirectoryGroupInfo = posix_getgrgid(filegroup(PATH_site . 'typo3temp/' . $directory));
2785 $resultDirectoryGroup = $resultDirectoryGroupInfo['name'];
2786 $this->assertEquals($resultDirectoryGroup, $swapGroup);
2787 }
2788 }
2789
2790 ///////////////////////////////
2791 // Helper function for filesystem ownership tests
2792 ///////////////////////////////
2793 /**
2794 * Check if test on filesystem group ownership can be done in this environment
2795 * If so, return second group of webserver user
2796 *
2797 * @param string calling method name
2798 * @return mixed FALSE if test cannot be run, string name of the second group of webserver user
2799 */
2800 private function checkGroups($methodName) {
2801 if (TYPO3_OS == 'WIN') {
2802 $this->markTestSkipped($methodName . '() test not available on Windows.');
2803 return FALSE;
2804 }
2805 if (!function_exists('posix_getgroups')) {
2806 $this->markTestSkipped('Function posix_getgroups() not available, ' . $methodName . '() tests skipped');
2807 }
2808 $groups = posix_getgroups();
2809 if (count($groups) <= 1) {
2810 $this->markTestSkipped($methodName . '() test cannot be done when the web server user is only member of 1 group.');
2811 return FALSE;
2812 }
2813 $uname = strtolower(php_uname());
2814 $groupOffset = 1;
2815 if (strpos($uname, 'darwin') !== FALSE) {
2816 // We are on OSX and it seems that the first group needs to be fetched since Mavericks
2817 $groupOffset = 0;
2818 }
2819 $groupInfo = posix_getgrgid($groups[$groupOffset]);
2820 return $groupInfo['name'];
2821 }
2822
2823 ///////////////////////////////
2824 // Tests concerning mkdir_deep
2825 ///////////////////////////////
2826 /**
2827 * @test
2828 */
2829 public function mkdirDeepCreatesDirectory() {
2830 $directory = 'typo3temp/' . $this->getUniqueId('test_');
2831 Utility\GeneralUtility::mkdir_deep(PATH_site, $directory);
2832 $this->testFilesToDelete[] = PATH_site . $directory;
2833 $this->assertTrue(is_dir(PATH_site . $directory));
2834 }
2835
2836 /**
2837 * @test
2838 */
2839 public function mkdirDeepCreatesSubdirectoriesRecursive() {
2840 $directory = 'typo3temp/' . $this->getUniqueId('test_');
2841 $subDirectory = $directory . '/foo';
2842 Utility\GeneralUtility::mkdir_deep(PATH_site, $subDirectory);
2843 $this->testFilesToDelete[] = PATH_site . $directory;
2844 $this->assertTrue(is_dir(PATH_site . $subDirectory));
2845 }
2846
2847 /**
2848 * Data provider for mkdirDeepCreatesDirectoryWithDoubleSlashes.
2849 * @return array
2850 */
2851 public function mkdirDeepCreatesDirectoryWithAndWithoutDoubleSlashesDataProvider() {
2852 return array(
2853 'no double slash if concatenated with PATH_site' => array('fileadmin/testDir1'),
2854 'double slash if concatenated with PATH_site' => array('/fileadmin/testDir2'),
2855 );
2856 }
2857
2858 /**
2859 * @test
2860 * @dataProvider mkdirDeepCreatesDirectoryWithAndWithoutDoubleSlashesDataProvider
2861 */
2862 public function mkdirDeepCreatesDirectoryWithDoubleSlashes($directoryToCreate) {
2863 vfsStream::setup();
2864 // Load fixture files and folders from disk
2865 FileStreamWrapper::init(PATH_site);
2866 FileStreamWrapper::registerOverlayPath('fileadmin', 'vfs://root/fileadmin', TRUE);
2867 Utility\GeneralUtility::mkdir_deep(PATH_site, $directoryToCreate);
2868 $this->assertTrue(is_dir(PATH_site . $directoryToCreate));
2869 FileStreamWrapper::destroy();
2870 }
2871
2872 /**
2873 * @test
2874 */
2875 public function mkdirDeepFixesPermissionsOfCreatedDirectory() {
2876 if (TYPO3_OS == 'WIN') {
2877 $this->markTestSkipped('mkdirDeepFixesPermissionsOfCreatedDirectory() test not available on Windows.');
2878 }
2879 $directory = $this->getUniqueId('mkdirdeeptest_');