2 namespace TYPO3\CMS\Core\Tests\Unit\Utility
;
4 /***************************************************************
7 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
28 * Testcase for class \TYPO3\CMS\Core\Utility\GeneralUtility
30 * @author Ingo Renner <ingo@typo3.org>
31 * @author Oliver Klee <typo3-coding@oliverklee.de>
35 class GeneralUtilityTest
extends \tx_phpunit_testcase
{
38 * Enable backup of global and system variables
42 protected $backupGlobals = TRUE;
45 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
46 * because resource types cannot be handled during serializing
50 protected $backupGlobalsBlacklist = array('TYPO3_DB');
52 public function tearDown() {
53 \TYPO3\CMS\Core\Utility\GeneralUtility
::purgeInstances();
56 ///////////////////////////
57 // Tests concerning _GP
58 ///////////////////////////
61 * @dataProvider gpDataProvider
63 public function canRetrieveValueWithGP($key, $get, $post, $expected) {
66 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::_GP($key));
70 * Data provider for canRetrieveValueWithGP.
71 * All test values also check whether slashes are stripped properly.
75 public function gpDataProvider() {
77 'No key parameter' => array(NULL, array(), array(), NULL),
78 'Key not found' => array('cake', array(), array(), NULL),
79 'Value only in GET' => array('cake', array('cake' => 'li\\e'), array(), 'lie'),
80 'Value only in POST' => array('cake', array(), array('cake' => 'l\\ie'), 'lie'),
81 'Value from POST preferred over GET' => array('cake', array('cake' => 'is a'), array('cake' => '\\lie'), 'lie'),
82 'Value can be an array' => array(
84 array('cake' => array('is a' => 'l\\ie')),
86 array('is a' => 'lie')
91 ///////////////////////////
92 // Tests concerning _GPmerged
93 ///////////////////////////
96 * @dataProvider gpMergedDataProvider
98 public function gpMergedWillMergeArraysFromGetAndPost($get, $post, $expected) {
101 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::_GPmerged('cake'));
105 * Data provider for gpMergedWillMergeArraysFromGetAndPost
109 public function gpMergedDataProvider() {
110 $fullDataArray = array('cake' => array('a' => 'is a', 'b' => 'lie'));
111 $postPartData = array('cake' => array('b' => 'lie'));
112 $getPartData = array('cake' => array('a' => 'is a'));
113 $getPartDataModified = array('cake' => array('a' => 'is not a'));
115 'Key doesn\' exist' => array(array('foo'), array('bar'), array()),
116 'No POST data' => array($fullDataArray, array(), $fullDataArray['cake']),
117 'No GET data' => array(array(), $fullDataArray, $fullDataArray['cake']),
118 'POST and GET are merged' => array($getPartData, $postPartData, $fullDataArray['cake']),
119 'POST is preferred over GET' => array($getPartDataModified, $fullDataArray, $fullDataArray['cake'])
123 ///////////////////////////////
124 // Tests concerning _GET / _POST
125 ///////////////////////////////
127 * Data provider for canRetrieveGlobalInputsThroughGet
128 * and canRetrieveGlobalInputsThroughPost
132 public function getAndPostDataProvider() {
134 'Requested input data doesn\'t exist' => array('cake', array(), NULL),
135 'No key will return entire input data' => array(NULL, array('cake' => 'l\\ie'), array('cake' => 'lie')),
136 'Can retrieve specific input' => array('cake', array('cake' => 'li\\e', 'foo'), 'lie'),
137 'Can retrieve nested input data' => array('cake', array('cake' => array('is a' => 'l\\ie')), array('is a' => 'lie'))
143 * @dataProvider getAndPostDataProvider
145 public function canRetrieveGlobalInputsThroughGet($key, $get, $expected) {
147 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::_GET($key));
152 * @dataProvider getAndPostDataProvider
154 public function canRetrieveGlobalInputsThroughPost($key, $post, $expected) {
156 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::_POST($key));
159 ///////////////////////////////
160 // Tests concerning _GETset
161 ///////////////////////////////
164 * @dataProvider getSetDataProvider
166 public function canSetNewGetInputValues($input, $key, $expected, $getPreset = array()) {
168 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset($input, $key);
169 $this->assertSame($expected, $_GET);
173 * Data provider for canSetNewGetInputValues
177 public function getSetDataProvider() {
179 'No input data used without target key' => array(NULL, NULL, array()),
180 'No input data used with target key' => array(NULL, 'cake', array('cake' => '')),
181 'No target key used with string input data' => array('data', NULL, array()),
182 'No target key used with array input data' => array(array('cake' => 'lie'), NULL, array('cake' => 'lie')),
183 'Target key and string input data' => array('lie', 'cake', array('cake' => 'lie')),
184 'Replace existing GET data' => array('lie', 'cake', array('cake' => 'lie'), array('cake' => 'is a lie')),
185 'Target key pointing to sublevels and string input data' => array('lie', 'cake|is', array('cake' => array('is' => 'lie'))),
186 'Target key pointing to sublevels and array input data' => array(array('a' => 'lie'), 'cake|is', array('cake' => array('is' => array('a' => 'lie'))))
190 ///////////////////////////////
191 // Tests concerning gif_compress
192 ///////////////////////////////
196 public function gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() {
197 if (TYPO3_OS
== 'WIN') {
198 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
200 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] ||
!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
201 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available without imagemagick setup.');
203 $testFinder = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('Tx_Phpunit_Service_TestFinder');
204 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 'Unit/t3lib/fixtures/clear.gif';
205 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress'] = TRUE;
206 // Copy file to unique filename in typo3temp, set target permissions and run method
207 $testFilename = ((PATH_site
. 'typo3temp/') . uniqid('test_')) . '.gif';
208 @copy
($fixtureGifFile, $testFilename);
209 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
210 \TYPO3\CMS\Core\Utility\GeneralUtility
::gif_compress($testFilename, 'IM');
211 // Get actual permissions and clean up
213 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
214 \TYPO3\CMS\Core\Utility\GeneralUtility
::unlink_tempfile($testFilename);
215 $this->assertEquals($resultFilePermissions, '0777');
221 public function gifCompressFixesPermissionOfConvertedFileIfUsingGd() {
222 if (TYPO3_OS
== 'WIN') {
223 $this->markTestSkipped('gifCompressFixesPermissionOfConvertedFileIfUsingImagemagick() test not available on Windows.');
225 $testFinder = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('Tx_Phpunit_Service_TestFinder');
226 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 'Unit/t3lib/fixtures/clear.gif';
227 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'] = TRUE;
228 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] = FALSE;
229 $GLOBALS['TYPO3_CONF_VARS']['GFX']['gif_compress'] = TRUE;
230 // Copy file to unique filename in typo3temp, set target permissions and run method
231 $testFilename = ((PATH_site
. 'typo3temp/') . uniqid('test_')) . '.gif';
232 @copy
($fixtureGifFile, $testFilename);
233 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
234 \TYPO3\CMS\Core\Utility\GeneralUtility
::gif_compress($testFilename, 'GD');
235 // Get actual permissions and clean up
237 $resultFilePermissions = substr(decoct(fileperms($testFilename)), 2);
238 \TYPO3\CMS\Core\Utility\GeneralUtility
::unlink_tempfile($testFilename);
239 $this->assertEquals($resultFilePermissions, '0777');
242 ///////////////////////////////
243 // Tests concerning png_to_gif_by_imagemagick
244 ///////////////////////////////
248 public function pngToGifByImagemagickFixesPermissionsOfConvertedFile() {
249 if (TYPO3_OS
== 'WIN') {
250 $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available on Windows.');
252 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] ||
!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']) {
253 $this->markTestSkipped('pngToGifByImagemagickFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
255 $testFinder = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('Tx_Phpunit_Service_TestFinder');
256 $fixturePngFile = $testFinder->getAbsoluteCoreTestsPath() . 'Unit/t3lib/fixtures/clear.png';
257 $GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif'] = TRUE;
258 // Copy file to unique filename in typo3temp, set target permissions and run method
259 $testFilename = ((PATH_site
. 'typo3temp/') . uniqid('test_')) . '.png';
260 @copy
($fixturePngFile, $testFilename);
261 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
262 $newGifFile = \TYPO3\CMS\Core\Utility\GeneralUtility
::png_to_gif_by_imagemagick($testFilename);
263 // Get actual permissions and clean up
265 $resultFilePermissions = substr(decoct(fileperms($newGifFile)), 2);
266 \TYPO3\CMS\Core\Utility\GeneralUtility
::unlink_tempfile($newGifFile);
267 $this->assertEquals($resultFilePermissions, '0777');
270 ///////////////////////////////
271 // Tests concerning read_png_gif
272 ///////////////////////////////
276 public function readPngGifFixesPermissionsOfConvertedFile() {
277 if (TYPO3_OS
== 'WIN') {
278 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available on Windows.');
280 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
281 $this->markTestSkipped('readPngGifFixesPermissionsOfConvertedFile() test not available without imagemagick setup.');
283 $testFinder = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('Tx_Phpunit_Service_TestFinder');
284 $testGifFile = $testFinder->getAbsoluteCoreTestsPath() . 'Unit/t3lib/fixtures/clear.gif';
285 // Set target permissions and run method
286 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
287 $newPngFile = \TYPO3\CMS\Core\Utility\GeneralUtility
::read_png_gif($testGifFile, TRUE);
288 // Get actual permissions and clean up
290 $resultFilePermissions = substr(decoct(fileperms($newPngFile)), 2);
291 \TYPO3\CMS\Core\Utility\GeneralUtility
::unlink_tempfile($newPngFile);
292 $this->assertEquals($resultFilePermissions, '0777');
295 ///////////////////////////
296 // Tests concerning cmpIPv4
297 ///////////////////////////
299 * Data provider for cmpIPv4ReturnsTrueForMatchingAddress
301 * @return array Data sets
303 static public function cmpIPv4DataProviderMatching() {
305 'host with full IP address' => array('127.0.0.1', '127.0.0.1'),
306 'host with two wildcards at the end' => array('127.0.0.1', '127.0.*.*'),
307 'host with wildcard at third octet' => array('127.0.0.1', '127.0.*.1'),
308 'host with wildcard at second octet' => array('127.0.0.1', '127.*.0.1'),
309 '/8 subnet' => array('127.0.0.1', '127.1.1.1/8'),
310 '/32 subnet (match only name)' => array('127.0.0.1', '127.0.0.1/32'),
311 '/30 subnet' => array('10.10.3.1', '10.10.3.3/30'),
312 'host with wildcard in list with IPv4/IPv6 addresses' => array('192.168.1.1', '127.0.0.1, 1234:5678::/126, 192.168.*'),
313 'host in list with IPv4/IPv6 addresses' => array('192.168.1.1', '::1, 1234:5678::/126, 192.168.1.1'),
319 * @dataProvider cmpIPv4DataProviderMatching
321 public function cmpIPv4ReturnsTrueForMatchingAddress($ip, $list) {
322 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpIPv4($ip, $list));
326 * Data provider for cmpIPv4ReturnsFalseForNotMatchingAddress
328 * @return array Data sets
330 static public function cmpIPv4DataProviderNotMatching() {
332 'single host' => array('127.0.0.1', '127.0.0.2'),
333 'single host with wildcard' => array('127.0.0.1', '127.*.1.1'),
334 'single host with /32 subnet mask' => array('127.0.0.1', '127.0.0.2/32'),
335 '/31 subnet' => array('127.0.0.1', '127.0.0.2/31'),
336 'list with IPv4/IPv6 addresses' => array('127.0.0.1', '10.0.2.3, 192.168.1.1, ::1'),
337 'list with only IPv6 addresses' => array('10.20.30.40', '::1, 1234:5678::/127')
343 * @dataProvider cmpIPv4DataProviderNotMatching
345 public function cmpIPv4ReturnsFalseForNotMatchingAddress($ip, $list) {
346 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpIPv4($ip, $list));
349 ///////////////////////////
350 // Tests concerning cmpIPv6
351 ///////////////////////////
353 * Data provider for cmpIPv6ReturnsTrueForMatchingAddress
355 * @return array Data sets
357 static public function cmpIPv6DataProviderMatching() {
359 'empty address' => array('::', '::'),
360 'empty with netmask in list' => array('::', '::/0'),
361 'empty with netmask 0 and host-bits set in list' => array('::', '::123/0'),
362 'localhost' => array('::1', '::1'),
363 'localhost with leading zero blocks' => array('::1', '0:0::1'),
364 'host with submask /128' => array('::1', '0:0::1/128'),
365 '/16 subnet' => array('1234::1', '1234:5678::/16'),
366 '/126 subnet' => array('1234:5678::3', '1234:5678::/126'),
367 '/126 subnet with host-bits in list set' => array('1234:5678::3', '1234:5678::2/126'),
368 'list with IPv4/IPv6 addresses' => array('1234:5678::3', '::1, 127.0.0.1, 1234:5678::/126, 192.168.1.1')
374 * @dataProvider cmpIPv6DataProviderMatching
376 public function cmpIPv6ReturnsTrueForMatchingAddress($ip, $list) {
377 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpIPv6($ip, $list));
381 * Data provider for cmpIPv6ReturnsFalseForNotMatchingAddress
383 * @return array Data sets
385 static public function cmpIPv6DataProviderNotMatching() {
387 'empty against localhost' => array('::', '::1'),
388 'empty against localhost with /128 netmask' => array('::', '::1/128'),
389 'localhost against different host' => array('::1', '::2'),
390 'localhost against host with prior bits set' => array('::1', '::1:1'),
391 'host against different /17 subnet' => array('1234::1', '1234:f678::/17'),
392 'host against different /127 subnet' => array('1234:5678::3', '1234:5678::/127'),
393 'host against IPv4 address list' => array('1234:5678::3', '127.0.0.1, 192.168.1.1'),
394 'host against mixed list with IPv6 host in different subnet' => array('1234:5678::3', '::1, 1234:5678::/127')
400 * @dataProvider cmpIPv6DataProviderNotMatching
402 public function cmpIPv6ReturnsFalseForNotMatchingAddress($ip, $list) {
403 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpIPv6($ip, $list));
406 ///////////////////////////////
407 // Tests concerning IPv6Hex2Bin
408 ///////////////////////////////
410 * Data provider for IPv6Hex2BinCorrect
412 * @return array Data sets
414 static public function IPv6Hex2BinDataProviderCorrect() {
416 'empty 1' => array('::', str_pad('', 16, "\x00")),
417 'empty 2, already normalized' => array('0000:0000:0000:0000:0000:0000:0000:0000', str_pad('', 16, "\x00")),
418 'already normalized' => array('0102:0304:0000:0000:0000:0000:0506:0078', "\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78"),
419 'expansion in middle 1' => array('1::2', "\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02"),
420 'expansion in middle 2' => array('beef::fefa', "\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa"),
426 * @dataProvider IPv6Hex2BinDataProviderCorrect
428 public function IPv6Hex2BinCorrectlyConvertsAddresses($hex, $binary) {
429 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::IPv6Hex2Bin($hex) === $binary);
432 ///////////////////////////////
433 // Tests concerning IPv6Bin2Hex
434 ///////////////////////////////
436 * Data provider for IPv6Bin2HexCorrect
438 * @return array Data sets
440 static public function IPv6Bin2HexDataProviderCorrect() {
442 'empty' => array(str_pad('', 16, "\x00"), '::'),
443 'non-empty front' => array("\x01" . str_pad('', 15, "\x00"), '100::'),
444 'non-empty back' => array(str_pad('', 15, "\x00") . "\x01", '::1'),
445 'normalized' => array("\x01\x02\x03\x04" . str_pad('', 8, "\x00") . "\x05\x06\x00\x78", '102:304::506:78'),
446 'expansion in middle 1' => array("\x00\x01" . str_pad('', 12, "\x00") . "\x00\x02", '1::2'),
447 'expansion in middle 2' => array("\xbe\xef" . str_pad('', 12, "\x00") . "\xfe\xfa", 'beef::fefa'),
453 * @dataProvider IPv6Bin2HexDataProviderCorrect
455 public function IPv6Bin2HexCorrectlyConvertsAddresses($binary, $hex) {
456 if (!class_exists('t3lib_diff')) {
457 $this->markTestSkipped('The current version of phpunit relies on t3lib_diff which is removed in 6.0 and thus this test fails. Move t3lib_diff to phpunit and reenable this test.');
459 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility
::IPv6Bin2Hex($binary), $hex);
462 ////////////////////////////////////////////////
463 // Tests concerning normalizeIPv6 / compressIPv6
464 ////////////////////////////////////////////////
466 * Data provider for normalizeIPv6ReturnsCorrectlyNormalizedFormat
468 * @return array Data sets
470 static public function normalizeCompressIPv6DataProviderCorrect() {
472 'empty' => array('::', '0000:0000:0000:0000:0000:0000:0000:0000'),
473 'localhost' => array('::1', '0000:0000:0000:0000:0000:0000:0000:0001'),
474 'some address on right side' => array('::f0f', '0000:0000:0000:0000:0000:0000:0000:0f0f'),
475 'expansion in middle 1' => array('1::2', '0001:0000:0000:0000:0000:0000:0000:0002'),
476 'expansion in middle 2' => array('1:2::3', '0001:0002:0000:0000:0000:0000:0000:0003'),
477 'expansion in middle 3' => array('1::2:3', '0001:0000:0000:0000:0000:0000:0002:0003'),
478 'expansion in middle 4' => array('1:2::3:4:5', '0001:0002:0000:0000:0000:0003:0004:0005')
484 * @dataProvider normalizeCompressIPv6DataProviderCorrect
486 public function normalizeIPv6CorrectlyNormalizesAddresses($compressed, $normalized) {
487 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility
::normalizeIPv6($compressed), $normalized);
492 * @dataProvider normalizeCompressIPv6DataProviderCorrect
494 public function compressIPv6CorrectlyCompressesAdresses($compressed, $normalized) {
495 if (!class_exists('t3lib_diff')) {
496 $this->markTestSkipped('The current version of phpunit relies on t3lib_diff which is removed in 6.0 and thus this test fails. Move t3lib_diff to phpunit and reenable this test.');
498 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility
::compressIPv6($normalized), $compressed);
501 ///////////////////////////////
502 // Tests concerning validIP
503 ///////////////////////////////
505 * Data provider for checkValidIpReturnsTrueForValidIp
507 * @return array Data sets
509 static public function validIpDataProvider() {
511 '0.0.0.0' => array('0.0.0.0'),
512 'private IPv4 class C' => array('192.168.0.1'),
513 'private IPv4 class A' => array('10.0.13.1'),
514 'private IPv6' => array('fe80::daa2:5eff:fe8b:7dfb')
520 * @dataProvider validIpDataProvider
522 public function validIpReturnsTrueForValidIp($ip) {
523 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::validIP($ip));
527 * Data provider for checkValidIpReturnsFalseForInvalidIp
529 * @return array Data sets
531 static public function invalidIpDataProvider() {
533 'null' => array(NULL),
535 'string' => array('test'),
536 'string empty' => array(''),
537 'string NULL' => array('NULL'),
538 'out of bounds IPv4' => array('300.300.300.300'),
539 'dotted decimal notation with only two dots' => array('127.0.1')
545 * @dataProvider invalidIpDataProvider
547 public function validIpReturnsFalseForInvalidIp($ip) {
548 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::validIP($ip));
551 ///////////////////////////////
552 // Tests concerning cmpFQDN
553 ///////////////////////////////
555 * Data provider for cmpFqdnReturnsTrue
557 * @return array Data sets
559 static public function cmpFqdnValidDataProvider() {
561 'localhost should usually resolve, IPv4' => array('127.0.0.1', '*'),
562 'localhost should usually resolve, IPv6' => array('::1', '*'),
563 // other testcases with resolving not possible since it would
564 // require a working IPv4/IPv6-connectivity
565 'aaa.bbb.ccc.ddd.eee, full' => array('aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.ddd.eee'),
566 'aaa.bbb.ccc.ddd.eee, wildcard first' => array('aaa.bbb.ccc.ddd.eee', '*.ccc.ddd.eee'),
567 'aaa.bbb.ccc.ddd.eee, wildcard last' => array('aaa.bbb.ccc.ddd.eee', 'aaa.bbb.ccc.*'),
568 'aaa.bbb.ccc.ddd.eee, wildcard middle' => array('aaa.bbb.ccc.ddd.eee', 'aaa.*.eee'),
569 'list-matches, 1' => array('aaa.bbb.ccc.ddd.eee', 'xxx, yyy, zzz, aaa.*.eee'),
570 'list-matches, 2' => array('aaa.bbb.ccc.ddd.eee', '127:0:0:1,,aaa.*.eee,::1')
576 * @dataProvider cmpFqdnValidDataProvider
578 public function cmpFqdnReturnsTrue($baseHost, $list) {
579 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpFQDN($baseHost, $list));
583 * Data provider for cmpFqdnReturnsFalse
585 * @return array Data sets
587 static public function cmpFqdnInvalidDataProvider() {
589 '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'),
590 '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')
596 * @dataProvider cmpFqdnInvalidDataProvider
598 public function cmpFqdnReturnsFalse($baseHost, $list) {
599 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::cmpFQDN($baseHost, $list));
602 ///////////////////////////////
603 // Tests concerning inList
604 ///////////////////////////////
607 * @param string $haystack
608 * @dataProvider inListForItemContainedReturnsTrueDataProvider
610 public function inListForItemContainedReturnsTrue($haystack) {
611 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::inList($haystack, 'findme'));
615 * Data provider for inListForItemContainedReturnsTrue.
619 public function inListForItemContainedReturnsTrueDataProvider() {
621 'Element as second element of four items' => array('one,findme,three,four'),
622 'Element at beginning of list' => array('findme,one,two'),
623 'Element at end of list' => array('one,two,findme'),
624 'One item list' => array('findme')
630 * @param string $haystack
631 * @dataProvider inListForItemNotContainedReturnsFalseDataProvider
633 public function inListForItemNotContainedReturnsFalse($haystack) {
634 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::inList($haystack, 'findme'));
638 * Data provider for inListForItemNotContainedReturnsFalse.
642 public function inListForItemNotContainedReturnsFalseDataProvider() {
644 'Four item list' => array('one,two,three,four'),
645 'One item list' => array('one'),
646 'Empty list' => array('')
650 ///////////////////////////////
651 // Tests concerning rmFromList
652 ///////////////////////////////
655 * @param string $initialList
656 * @param string $listWithElementRemoved
657 * @dataProvider rmFromListRemovesElementsFromCommaSeparatedListDataProvider
659 public function rmFromListRemovesElementsFromCommaSeparatedList($initialList, $listWithElementRemoved) {
660 $this->assertSame($listWithElementRemoved, \TYPO3\CMS\Core\Utility\GeneralUtility
::rmFromList('removeme', $initialList));
664 * Data provider for rmFromListRemovesElementsFromCommaSeparatedList
668 public function rmFromListRemovesElementsFromCommaSeparatedListDataProvider() {
670 'Element as second element of three' => array('one,removeme,two', 'one,two'),
671 'Element at beginning of list' => array('removeme,one,two', 'one,two'),
672 'Element at end of list' => array('one,two,removeme', 'one,two'),
673 'One item list' => array('removeme', ''),
674 'Element not contained in list' => array('one,two,three', 'one,two,three'),
675 'Empty list' => array('', '')
679 ///////////////////////////////
680 // Tests concerning expandList
681 ///////////////////////////////
684 * @param string $list
685 * @param string $expectation
686 * @dataProvider expandListExpandsIntegerRangesDataProvider
688 public function expandListExpandsIntegerRanges($list, $expectation) {
689 $this->assertSame($expectation, \TYPO3\CMS\Core\Utility\GeneralUtility
::expandList($list));
693 * Data provider for expandListExpandsIntegerRangesDataProvider
697 public function expandListExpandsIntegerRangesDataProvider() {
699 'Expand for the same number' => array('1,2-2,7', '1,2,7'),
700 'Small range expand with parameters reversed ignores reversed items' => array('1,5-3,7', '1,7'),
701 'Small range expand' => array('1,3-5,7', '1,3,4,5,7'),
702 'Expand at beginning' => array('3-5,1,7', '3,4,5,1,7'),
703 'Expand at end' => array('1,7,3-5', '1,7,3,4,5'),
704 'Multiple small range expands' => array('1,3-5,7-10,12', '1,3,4,5,7,8,9,10,12'),
705 'One item list' => array('1-5', '1,2,3,4,5'),
706 'Nothing to expand' => array('1,2,3,4', '1,2,3,4'),
707 'Empty list' => array('', '')
714 public function expandListExpandsForTwoThousandElementsExpandsOnlyToThousandElementsMaximum() {
715 $list = \TYPO3\CMS\Core\Utility\GeneralUtility
::expandList('1-2000');
716 $this->assertSame(1000, count(explode(',', $list)));
719 ///////////////////////////////
720 // Tests concerning uniqueList
721 ///////////////////////////////
724 * @param string $initialList
725 * @param string $unifiedList
726 * @dataProvider uniqueListUnifiesCommaSeparatedListDataProvider
728 public function uniqueListUnifiesCommaSeparatedList($initialList, $unifiedList) {
729 $this->assertSame($unifiedList, \TYPO3\CMS\Core\Utility\GeneralUtility
::uniqueList($initialList));
733 * Data provider for uniqueListUnifiesCommaSeparatedList
737 public function uniqueListUnifiesCommaSeparatedListDataProvider() {
739 'List without duplicates' => array('one,two,three', 'one,two,three'),
740 'List with two consecutive duplicates' => array('one,two,two,three,three', 'one,two,three'),
741 'List with non-consecutive duplicates' => array('one,two,three,two,three', 'one,two,three'),
742 'One item list' => array('one', 'one'),
743 'Empty list' => array('', '')
747 ///////////////////////////////
748 // Tests concerning isFirstPartOfStr
749 ///////////////////////////////
751 * Data provider for isFirstPartOfStrReturnsTrueForMatchingFirstParts
755 public function isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider() {
757 'match first part of string' => array('hello world', 'hello'),
758 'match whole string' => array('hello', 'hello'),
759 'integer is part of string with same number' => array('24', 24),
760 'string is part of integer with same number' => array(24, '24'),
761 'integer is part of string starting with same number' => array('24 beer please', 24)
767 * @dataProvider isFirstPartOfStrReturnsTrueForMatchingFirstPartDataProvider
769 public function isFirstPartOfStrReturnsTrueForMatchingFirstPart($string, $part) {
770 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::isFirstPartOfStr($string, $part));
774 * Data provider for checkIsFirstPartOfStrReturnsFalseForNotMatchingFirstParts
778 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider() {
780 'no string match' => array('hello', 'bye'),
781 'no case sensitive string match' => array('hello world', 'Hello'),
782 'array is not part of string' => array('string', array()),
783 'string is not part of array' => array(array(), 'string'),
784 'NULL is not part of string' => array('string', NULL),
785 'string is not part of array' => array(NULL, 'string'),
786 'NULL is not part of array' => array(array(), NULL),
787 'array is not part of string' => array(NULL, array()),
788 'empty string is not part of empty string' => array('', ''),
789 'NULL is not part of empty string' => array('', NULL),
790 'false is not part of empty string' => array('', FALSE),
791 'empty string is not part of NULL' => array(NULL, ''),
792 'empty string is not part of false' => array(FALSE, ''),
793 'empty string is not part of zero integer' => array(0, ''),
794 'zero integer is not part of NULL' => array(NULL, 0),
795 'zero integer is not part of empty string' => array('', 0)
801 * @dataProvider isFirstPartOfStrReturnsFalseForNotMatchingFirstPartDataProvider
803 public function isFirstPartOfStrReturnsFalseForNotMatchingFirstPart($string, $part) {
804 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::isFirstPartOfStr($string, $part));
807 ///////////////////////////////
808 // Tests concerning formatSize
809 ///////////////////////////////
812 * @dataProvider formatSizeDataProvider
814 public function formatSizeTranslatesBytesToHigherOrderRepresentation($size, $label, $expected) {
815 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::formatSize($size, $label));
819 * Data provider for formatSizeTranslatesBytesToHigherOrderRepresentation
823 public function formatSizeDataProvider() {
825 'Bytes keep beeing bytes (min)' => array(1, '', '1 '),
826 'Bytes keep beeing bytes (max)' => array(899, '', '899 '),
827 'Kilobytes are detected' => array(1024, '', '1.0 K'),
828 'Megabytes are detected' => array(1048576, '', '1.0 M'),
829 'Gigabytes are detected' => array(1073741824, '', '1.0 G'),
830 'Decimal is omitted for large kilobytes' => array(31080, '', '30 K'),
831 'Decimal is omitted for large megabytes' => array(31458000, '', '30 M'),
832 'Decimal is omitted for large gigabytes' => array(32212254720, '', '30 G'),
833 'Label for bytes can be exchanged' => array(1, ' Foo|||', '1 Foo'),
834 'Label for kilobytes can be exchanged' => array(1024, '| Foo||', '1.0 Foo'),
835 'Label for megabyes can be exchanged' => array(1048576, '|| Foo|', '1.0 Foo'),
836 'Label for gigabytes can be exchanged' => array(1073741824, '||| Foo', '1.0 Foo')
840 ///////////////////////////////
841 // Tests concerning splitCalc
842 ///////////////////////////////
844 * Data provider for splitCalc
846 * @return array expected values, arithmetic expression
848 public function splitCalcDataProvider() {
850 'empty string returns empty array' => array(
854 'number without operator returns array with plus and number' => array(
855 array(array('+', 42)),
858 'two numbers with asterisk return first number with plus and second number with asterisk' => array(
859 array(array('+', 42), array('*', 31)),
867 * @dataProvider splitCalcDataProvider
869 public function splitCalcCorrectlySplitsExpression($expected, $expression) {
870 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::splitCalc($expression, '+-*/'));
873 ///////////////////////////////
874 // Tests concerning htmlspecialchars_decode
875 ///////////////////////////////
879 public function htmlspecialcharsDecodeReturnsDecodedString() {
880 $string = '<typo3 version="6.0"> </typo3>';
881 $encoded = htmlspecialchars($string);
882 $decoded = \TYPO3\CMS\Core\Utility\GeneralUtility
::htmlspecialchars_decode($encoded);
883 $this->assertEquals($string, $decoded);
886 ///////////////////////////////
887 // Tests concerning deHSCentities
888 ///////////////////////////////
891 * @dataProvider deHSCentitiesReturnsDecodedStringDataProvider
893 public function deHSCentitiesReturnsDecodedString($input, $expected) {
894 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::deHSCentities($input));
898 * Data provider for deHSCentitiesReturnsDecodedString
902 public function deHSCentitiesReturnsDecodedStringDataProvider() {
904 'Empty string' => array('', ''),
905 'Double encoded &' => array('&amp;', '&'),
906 'Double encoded numeric entity' => array('&#1234;', 'Ӓ'),
907 'Double encoded hexadecimal entity' => array('&#x1b;', ''),
908 'Single encoded entities are not touched' => array('& Ӓ ', '& Ӓ ')
912 //////////////////////////////////
913 // Tests concerning slashJS
914 //////////////////////////////////
917 * @dataProvider slashJsDataProvider
919 public function slashJsEscapesSingleQuotesAndSlashes($input, $extended, $expected) {
920 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::slashJS($input, $extended));
924 * Data provider for slashJsEscapesSingleQuotesAndSlashes
928 public function slashJsDataProvider() {
930 'Empty string is not changed' => array('', FALSE, ''),
931 'Normal string is not changed' => array('The cake is a lie √', FALSE, 'The cake is a lie √'),
932 'String with single quotes' => array('The \'cake\' is a lie', FALSE, 'The \\\'cake\\\' is a lie'),
933 'String with single quotes and backslashes - just escape single quotes' => array('The \\\'cake\\\' is a lie', FALSE, 'The \\\\\'cake\\\\\' is a lie'),
934 'String with single quotes and backslashes - escape both' => array('The \\\'cake\\\' is a lie', TRUE, 'The \\\\\\\'cake\\\\\\\' is a lie')
938 //////////////////////////////////
939 // Tests concerning rawUrlEncodeJS
940 //////////////////////////////////
944 public function rawUrlEncodeJsPreservesWhitespaces() {
945 $input = 'Encode \'me\', but leave my spaces √';
946 $expected = 'Encode %27me%27%2C but leave my spaces %E2%88%9A';
947 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::rawUrlEncodeJS($input));
950 //////////////////////////////////
951 // Tests concerning rawUrlEncodeJS
952 //////////////////////////////////
956 public function rawUrlEncodeFpPreservesSlashes() {
957 $input = 'Encode \'me\', but leave my / √';
958 $expected = 'Encode%20%27me%27%2C%20but%20leave%20my%20/%20%E2%88%9A';
959 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::rawUrlEncodeFP($input));
962 //////////////////////////////////
963 // Tests concerning strtoupper / strtolower
964 //////////////////////////////////
966 * Data provider for strtoupper and strtolower
970 public function strtouppperDataProvider() {
972 'Empty string' => array('', ''),
973 'String containing only latin characters' => array('the cake is a lie.', 'THE CAKE IS A LIE.'),
974 'String with umlauts and accent characters' => array('the cà kê is ä lie.', 'THE Cà Kê IS ä LIE.')
980 * @dataProvider strtouppperDataProvider
982 public function strtoupperConvertsOnlyLatinCharacters($input, $expected) {
983 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::strtoupper($input));
988 * @dataProvider strtouppperDataProvider
990 public function strtolowerConvertsOnlyLatinCharacters($expected, $input) {
991 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::strtolower($input));
994 //////////////////////////////////
995 // Tests concerning validEmail
996 //////////////////////////////////
998 * Data provider for valid validEmail's
1000 * @return array Valid email addresses
1002 public function validEmailValidDataProvider() {
1004 'short mail address' => array('a@b.c'),
1005 'simple mail address' => array('test@example.com'),
1006 'uppercase characters' => array('QWERTYUIOPASDFGHJKLZXCVBNM@QWERTYUIOPASDFGHJKLZXCVBNM.NET'),
1007 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
1008 // 'equal sign in local part' => array('test=mail@example.com'),
1009 'dash in local part' => array('test-mail@example.com'),
1010 'plus in local part' => array('test+mail@example.com'),
1011 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
1012 // 'question mark in local part' => array('test?mail@example.com'),
1013 'slash in local part' => array('foo/bar@example.com'),
1014 'hash in local part' => array('foo#bar@example.com'),
1015 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6 and 5.3.2 but fails with 5.3.0 on windows
1016 // 'dot in local part' => array('firstname.lastname@employee.2something.com'),
1017 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
1018 // 'dash as local part' => array('-@foo.com'),
1019 'umlauts in local part' => array('äöüfoo@bar.com'),
1020 'umlauts in domain part' => array('foo@äöüfoo.com')
1026 * @dataProvider validEmailValidDataProvider
1028 public function validEmailReturnsTrueForValidMailAddress($address) {
1029 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::validEmail($address));
1033 * Data provider for invalid validEmail's
1035 * @return array Invalid email addresses
1037 public function validEmailInvalidDataProvider() {
1039 '@ sign only' => array('@'),
1040 'duplicate @' => array('test@@example.com'),
1041 'duplicate @ combined with further special characters in local part' => array('test!.!@#$%^&*@example.com'),
1042 'opening parenthesis in local part' => array('foo(bar@example.com'),
1043 'closing parenthesis in local part' => array('foo)bar@example.com'),
1044 'opening square bracket in local part' => array('foo[bar@example.com'),
1045 'closing square bracket as local part' => array(']@example.com'),
1046 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
1047 // 'top level domain only' => array('test@com'),
1048 'dash as second level domain' => array('foo@-.com'),
1049 'domain part starting with dash' => array('foo@-foo.com'),
1050 'domain part ending with dash' => array('foo@foo-.com'),
1051 'number as top level domain' => array('foo@bar.123'),
1052 // Fix / change if TYPO3 php requirement changed: Address not ok with 5.2.6, but ok with 5.3.2 (?)
1053 // 'dash as top level domain' => array('foo@bar.-'),
1054 'dot at beginning of domain part' => array('test@.com'),
1055 // Fix / change if TYPO3 php requirement changed: Address ok with 5.2.6, but not ok with 5.3.2
1056 // 'local part ends with dot' => array('e.x.a.m.p.l.e.@example.com'),
1057 'trailing whitespace' => array('test@example.com '),
1058 'trailing carriage return' => array('test@example.com' . CR
),
1059 'trailing linefeed' => array('test@example.com' . LF
),
1060 'trailing carriage return linefeed' => array('test@example.com' . CRLF
),
1061 'trailing tab' => array('test@example.com' . TAB
)
1067 * @dataProvider validEmailInvalidDataProvider
1069 public function validEmailReturnsFalseForInvalidMailAddress($address) {
1070 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::validEmail($address));
1073 //////////////////////////////////
1074 // Tests concerning inArray
1075 //////////////////////////////////
1078 * @dataProvider inArrayDataProvider
1080 public function inArrayChecksStringExistenceWithinArray($array, $item, $expected) {
1081 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::inArray($array, $item));
1085 * Data provider for inArrayChecksStringExistenceWithinArray
1089 public function inArrayDataProvider() {
1091 'Empty array' => array(array(), 'search', FALSE),
1092 'One item array no match' => array(array('one'), 'two', FALSE),
1093 'One item array match' => array(array('one'), 'one', TRUE),
1094 'Multiple items array no match' => array(array('one', 2, 'three', 4), 'four', FALSE),
1095 'Multiple items array match' => array(array('one', 2, 'three', 4), 'three', TRUE),
1096 'Integer search items can match string values' => array(array('0', '1', '2'), 1, TRUE),
1097 'Search item is not casted to integer for a match' => array(array(4), '4a', FALSE),
1098 'Empty item won\'t match - in contrast to the php-builtin ' => array(array(0, 1, 2), '', FALSE)
1102 //////////////////////////////////
1103 // Tests concerning intExplode
1104 //////////////////////////////////
1108 public function intExplodeConvertsStringsToInteger() {
1109 $testString = '1,foo,2';
1110 $expectedArray = array(1, 0, 2);
1111 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::intExplode(',', $testString);
1112 $this->assertEquals($expectedArray, $actualArray);
1115 //////////////////////////////////
1116 // Tests concerning keepItemsInArray
1117 //////////////////////////////////
1120 * @dataProvider keepItemsInArrayWorksWithOneArgumentDataProvider
1122 public function keepItemsInArrayWorksWithOneArgument($search, $array, $expected) {
1123 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::keepItemsInArray($array, $search));
1127 * Data provider for keepItemsInArrayWorksWithOneArgument
1131 public function keepItemsInArrayWorksWithOneArgumentDataProvider() {
1138 'Empty argument will match "all" elements' => array(NULL, $array, $array),
1139 'No match' => array('four', $array, array()),
1140 'One match' => array('two', $array, array('two' => 'two')),
1141 'Multiple matches' => array('two,one', $array, array('one' => 'one', 'two' => 'two')),
1142 'Argument can be an array' => array(array('three'), $array, array('three' => 'three'))
1147 * Shows the exmaple from the doc comment where
1148 * a function is used to reduce the sub arrays to one item which
1149 * is then used for the matching.
1153 public function keepItemsInArrayCanUseCallbackOnSearchArray() {
1155 'aa' => array('first', 'second'),
1156 'bb' => array('third', 'fourth'),
1157 'cc' => array('fifth', 'sixth')
1159 $expected = array('bb' => array('third', 'fourth'));
1160 $keepItems = 'third';
1161 $getValueFunc = create_function('$value', 'return $value[0];');
1162 $match = \TYPO3\CMS\Core\Utility\GeneralUtility
::keepItemsInArray($array, $keepItems, $getValueFunc);
1163 $this->assertEquals($expected, $match);
1166 //////////////////////////////////
1167 // Tests concerning implodeArrayForUrl / explodeUrl2Array
1168 //////////////////////////////////
1170 * Data provider for implodeArrayForUrlBuildsValidParameterString and
1171 * explodeUrl2ArrayTransformsParameterStringToArray
1175 public function implodeArrayForUrlDataProvider() {
1176 $valueArray = array('one' => '√', 'two' => 2);
1178 'Empty input' => array('foo', array(), ''),
1179 'String parameters' => array('foo', $valueArray, '&foo[one]=%E2%88%9A&foo[two]=2'),
1180 'Nested array parameters' => array('foo', array($valueArray), '&foo[0][one]=%E2%88%9A&foo[0][two]=2'),
1181 'Keep blank parameters' => array('foo', array('one' => '√', ''), '&foo[one]=%E2%88%9A&foo[0]=')
1187 * @dataProvider implodeArrayForUrlDataProvider
1189 public function implodeArrayForUrlBuildsValidParameterString($name, $input, $expected) {
1190 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::implodeArrayForUrl($name, $input));
1196 public function implodeArrayForUrlCanSkipEmptyParameters() {
1197 $input = array('one' => '√', '');
1198 $expected = '&foo[one]=%E2%88%9A';
1199 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::implodeArrayForUrl('foo', $input, '', TRUE));
1205 public function implodeArrayForUrlCanUrlEncodeKeyNames() {
1206 $input = array('one' => '√', '');
1207 $expected = '&foo%5Bone%5D=%E2%88%9A&foo%5B0%5D=';
1208 $this->assertSame($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::implodeArrayForUrl('foo', $input, '', FALSE, TRUE));
1213 * @dataProvider implodeArrayForUrlDataProvider
1215 public function explodeUrl2ArrayTransformsParameterStringToNestedArray($name, $array, $input) {
1216 $expected = $array ?
array($name => $array) : array();
1217 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::explodeUrl2Array($input, TRUE));
1222 * @dataProvider explodeUrl2ArrayDataProvider
1224 public function explodeUrl2ArrayTransformsParameterStringToFlatArray($input, $expected) {
1225 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::explodeUrl2Array($input, FALSE));
1229 * Data provider for explodeUrl2ArrayTransformsParameterStringToFlatArray
1233 public function explodeUrl2ArrayDataProvider() {
1235 'Empty string' => array('', array()),
1236 'Simple parameter string' => array('&one=%E2%88%9A&two=2', array('one' => '√', 'two' => 2)),
1237 'Nested parameter string' => array('&foo[one]=%E2%88%9A&two=2', array('foo[one]' => '√', 'two' => 2))
1241 //////////////////////////////////
1242 // Tests concerning compileSelectedGetVarsFromArray
1243 //////////////////////////////////
1247 public function compileSelectedGetVarsFromArrayFiltersIncomingData() {
1248 $filter = 'foo,bar';
1249 $getArray = array('foo' => 1, 'cake' => 'lie');
1250 $expected = array('foo' => 1);
1251 $result = \TYPO3\CMS\Core\Utility\GeneralUtility
::compileSelectedGetVarsFromArray($filter, $getArray, FALSE);
1252 $this->assertSame($expected, $result);
1258 public function compileSelectedGetVarsFromArrayUsesGetPostDataFallback() {
1260 $filter = 'foo,bar';
1261 $getArray = array('foo' => 1, 'cake' => 'lie');
1262 $expected = array('foo' => 1, 'bar' => '2');
1263 $result = \TYPO3\CMS\Core\Utility\GeneralUtility
::compileSelectedGetVarsFromArray($filter, $getArray, TRUE);
1264 $this->assertSame($expected, $result);
1267 //////////////////////////////////
1268 // Tests concerning remapArrayKeys
1269 //////////////////////////////////
1273 public function remapArrayKeysExchangesKeysWithGivenMapping() {
1279 $keyMapping = array(
1288 \TYPO3\CMS\Core\Utility\GeneralUtility
::remapArrayKeys($array, $keyMapping);
1289 $this->assertEquals($expected, $array);
1292 //////////////////////////////////
1293 // Tests concerning array_merge
1294 //////////////////////////////////
1296 * Test demonstrating array_merge. This is actually
1297 * a native PHP operator, therefore this test is mainly used to
1298 * show how this function can be used.
1302 public function arrayMergeKeepsIndexesAfterMerge() {
1303 $array1 = array(10 => 'FOO', '20' => 'BAR');
1304 $array2 = array('5' => 'PLONK');
1305 $expected = array('5' => 'PLONK', 10 => 'FOO', '20' => 'BAR');
1306 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::array_merge($array1, $array2));
1309 //////////////////////////////////
1310 // Tests concerning int_from_ver
1311 //////////////////////////////////
1313 * Data Provider for intFromVerConvertsVersionNumbersToIntegersDataProvider
1317 public function intFromVerConvertsVersionNumbersToIntegersDataProvider() {
1319 array('4003003', '4.3.3'),
1320 array('4012003', '4.12.3'),
1321 array('5000000', '5.0.0'),
1322 array('3008001', '3.8.1'),
1323 array('1012', '0.1.12')
1329 * @dataProvider intFromVerConvertsVersionNumbersToIntegersDataProvider
1331 public function intFromVerConvertsVersionNumbersToIntegers($expected, $version) {
1332 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::int_from_ver($version));
1335 //////////////////////////////////
1336 // Tests concerning revExplode
1337 //////////////////////////////////
1341 public function revExplodeExplodesString() {
1342 $testString = 'my:words:here';
1343 $expectedArray = array('my:words', 'here');
1344 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::revExplode(':', $testString, 2);
1345 $this->assertEquals($expectedArray, $actualArray);
1348 //////////////////////////////////
1349 // Tests concerning trimExplode
1350 //////////////////////////////////
1354 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
1355 $testString = ' a , b , c ,d ,, e,f,';
1356 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
1357 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString);
1358 $this->assertEquals($expectedArray, $actualArray);
1364 public function checkTrimExplodeRemovesNewLines() {
1365 $testString = (' a , b , ' . LF
) . ' ,d ,, e,f,';
1366 $expectedArray = array('a', 'b', 'd', 'e', 'f');
1367 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE);
1368 $this->assertEquals($expectedArray, $actualArray);
1374 public function checkTrimExplodeRemovesEmptyElements() {
1375 $testString = 'a , b , c , ,d ,, ,e,f,';
1376 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
1377 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE);
1378 $this->assertEquals($expectedArray, $actualArray);
1384 public function checkTrimExplodeKeepsRemainingResultsWithEmptyItemsAfterReachingLimitWithPositiveParameter() {
1385 $testString = ' a , b , c , , d,, ,e ';
1386 $expectedArray = array('a', 'b', 'c,,d,,,e');
1387 // Limiting returns the rest of the string as the last element
1388 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, FALSE, 3);
1389 $this->assertEquals($expectedArray, $actualArray);
1395 public function checkTrimExplodeKeepsRemainingResultsWithoutEmptyItemsAfterReachingLimitWithPositiveParameter() {
1396 $testString = ' a , b , c , , d,, ,e ';
1397 $expectedArray = array('a', 'b', 'c,d,e');
1398 // Limiting returns the rest of the string as the last element
1399 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE, 3);
1400 $this->assertEquals($expectedArray, $actualArray);
1406 public function checkTrimExplodeKeepsRamainingResultsWithEmptyItemsAfterReachingLimitWithNegativeParameter() {
1407 $testString = ' a , b , c , d, ,e, f , , ';
1408 $expectedArray = array('a', 'b', 'c', 'd', '', 'e');
1409 // limiting returns the rest of the string as the last element
1410 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, FALSE, -3);
1411 $this->assertEquals($expectedArray, $actualArray);
1417 public function checkTrimExplodeKeepsRamainingResultsWithoutEmptyItemsAfterReachingLimitWithNegativeParameter() {
1418 $testString = ' a , b , c , d, ,e, f , , ';
1419 $expectedArray = array('a', 'b', 'c');
1420 // Limiting returns the rest of the string as the last element
1421 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE, -3);
1422 $this->assertEquals($expectedArray, $actualArray);
1428 public function checkTrimExplodeReturnsExactResultsWithoutReachingLimitWithPositiveParameter() {
1429 $testString = ' a , b , , c , , , ';
1430 $expectedArray = array('a', 'b', 'c');
1431 // Limiting returns the rest of the string as the last element
1432 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE, 4);
1433 $this->assertEquals($expectedArray, $actualArray);
1439 public function checkTrimExplodeKeepsZeroAsString() {
1440 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
1441 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
1442 $actualArray = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $testString, TRUE);
1443 $this->assertEquals($expectedArray, $actualArray);
1446 //////////////////////////////////
1447 // Tests concerning removeArrayEntryByValue
1448 //////////////////////////////////
1452 public function checkRemoveArrayEntryByValueRemovesEntriesFromOneDimensionalArray() {
1453 $inputArray = array(
1459 $compareValue = 'test2';
1460 $expectedResult = array(
1464 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::removeArrayEntryByValue($inputArray, $compareValue);
1465 $this->assertEquals($expectedResult, $actualResult);
1471 public function checkRemoveArrayEntryByValueRemovesEntriesFromMultiDimensionalArray() {
1472 $inputArray = array(
1479 $compareValue = 'bar';
1480 $expectedResult = array(
1484 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::removeArrayEntryByValue($inputArray, $compareValue);
1485 $this->assertEquals($expectedResult, $actualResult);
1491 public function checkRemoveArrayEntryByValueRemovesEntryWithEmptyString() {
1492 $inputArray = array(
1498 $expectedResult = array(
1502 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::removeArrayEntryByValue($inputArray, $compareValue);
1503 $this->assertEquals($expectedResult, $actualResult);
1506 //////////////////////////////////
1507 // Tests concerning getBytesFromSizeMeasurement
1508 //////////////////////////////////
1510 * Data provider for getBytesFromSizeMeasurement
1512 * @return array expected value, input string
1514 public function getBytesFromSizeMeasurementDataProvider() {
1516 '100 kilo Bytes' => array('102400', '100k'),
1517 '100 mega Bytes' => array('104857600', '100m'),
1518 '100 giga Bytes' => array('107374182400', '100g')
1524 * @dataProvider getBytesFromSizeMeasurementDataProvider
1526 public function getBytesFromSizeMeasurementCalculatesCorrectByteValue($expected, $byteString) {
1527 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::getBytesFromSizeMeasurement($byteString));
1530 //////////////////////////////////
1531 // Tests concerning getIndpEnv
1532 //////////////////////////////////
1536 public function getIndpEnvTypo3SitePathReturnNonEmptyString() {
1537 $this->assertTrue(strlen(\TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_PATH')) >= 1);
1543 public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() {
1544 $result = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_PATH');
1545 $this->assertEquals('/', $result[0]);
1551 public function getIndpEnvTypo3SitePathReturnsStringEndingWithSlash() {
1552 $result = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_PATH');
1553 $this->assertEquals('/', $result[strlen($result) - 1]);
1559 static public function hostnameAndPortDataProvider() {
1561 'localhost ipv4 without port' => array('127.0.0.1', '127.0.0.1', ''),
1562 'localhost ipv4 with port' => array('127.0.0.1:81', '127.0.0.1', '81'),
1563 'localhost ipv6 without port' => array('[::1]', '[::1]', ''),
1564 'localhost ipv6 with port' => array('[::1]:81', '[::1]', '81'),
1565 'ipv6 without port' => array('[2001:DB8::1]', '[2001:DB8::1]', ''),
1566 'ipv6 with port' => array('[2001:DB8::1]:81', '[2001:DB8::1]', '81'),
1567 'hostname without port' => array('lolli.did.this', 'lolli.did.this', ''),
1568 'hostname with port' => array('lolli.did.this:42', 'lolli.did.this', '42')
1574 * @dataProvider hostnameAndPortDataProvider
1576 public function getIndpEnvTypo3HostOnlyParsesHostnamesAndIpAdresses($httpHost, $expectedIp) {
1577 $_SERVER['HTTP_HOST'] = $httpHost;
1578 $this->assertEquals($expectedIp, \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_HOST_ONLY'));
1583 * @dataProvider hostnameAndPortDataProvider
1585 public function getIndpEnvTypo3PortParsesHostnamesAndIpAdresses($httpHost, $dummy, $expectedPort) {
1586 $_SERVER['HTTP_HOST'] = $httpHost;
1587 $this->assertEquals($expectedPort, \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_PORT'));
1590 //////////////////////////////////
1591 // Tests concerning underscoredToUpperCamelCase
1592 //////////////////////////////////
1594 * Data provider for underscoredToUpperCamelCase
1596 * @return array expected, input string
1598 public function underscoredToUpperCamelCaseDataProvider() {
1600 'single word' => array('Blogexample', 'blogexample'),
1601 'multiple words' => array('BlogExample', 'blog_example')
1607 * @dataProvider underscoredToUpperCamelCaseDataProvider
1609 public function underscoredToUpperCamelCase($expected, $inputString) {
1610 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::underscoredToUpperCamelCase($inputString));
1613 //////////////////////////////////
1614 // Tests concerning underscoredToLowerCamelCase
1615 //////////////////////////////////
1617 * Data provider for underscoredToLowerCamelCase
1619 * @return array expected, input string
1621 public function underscoredToLowerCamelCaseDataProvider() {
1623 'single word' => array('minimalvalue', 'minimalvalue'),
1624 'multiple words' => array('minimalValue', 'minimal_value')
1630 * @dataProvider underscoredToLowerCamelCaseDataProvider
1632 public function underscoredToLowerCamelCase($expected, $inputString) {
1633 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::underscoredToLowerCamelCase($inputString));
1636 //////////////////////////////////
1637 // Tests concerning camelCaseToLowerCaseUnderscored
1638 //////////////////////////////////
1640 * Data provider for camelCaseToLowerCaseUnderscored
1642 * @return array expected, input string
1644 public function camelCaseToLowerCaseUnderscoredDataProvider() {
1646 'single word' => array('blogexample', 'blogexample'),
1647 'single word starting upper case' => array('blogexample', 'Blogexample'),
1648 'two words starting lower case' => array('minimal_value', 'minimalValue'),
1649 'two words starting upper case' => array('blog_example', 'BlogExample')
1655 * @dataProvider camelCaseToLowerCaseUnderscoredDataProvider
1657 public function camelCaseToLowerCaseUnderscored($expected, $inputString) {
1658 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::camelCaseToLowerCaseUnderscored($inputString));
1661 //////////////////////////////////
1662 // Tests concerning lcFirst
1663 //////////////////////////////////
1665 * Data provider for lcFirst
1667 * @return array expected, input string
1669 public function lcfirstDataProvider() {
1671 'single word' => array('blogexample', 'blogexample'),
1672 'single Word starting upper case' => array('blogexample', 'Blogexample'),
1673 'two words' => array('blogExample', 'BlogExample')
1679 * @dataProvider lcfirstDataProvider
1681 public function lcFirst($expected, $inputString) {
1682 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\GeneralUtility
::lcfirst($inputString));
1685 //////////////////////////////////
1686 // Tests concerning encodeHeader
1687 //////////////////////////////////
1691 public function encodeHeaderEncodesWhitespacesInQuotedPrintableMailHeader() {
1692 $this->assertEquals('=?utf-8?Q?We_test_whether_the_copyright_character_=C2=A9_is_encoded_correctly?=', \TYPO3\CMS\Core\Utility\GeneralUtility
::encodeHeader('We test whether the copyright character © is encoded correctly', 'quoted-printable', 'utf-8'));
1698 public function encodeHeaderEncodesQuestionmarksInQuotedPrintableMailHeader() {
1699 $this->assertEquals('=?utf-8?Q?Is_the_copyright_character_=C2=A9_really_encoded_correctly=3F_Really=3F?=', \TYPO3\CMS\Core\Utility\GeneralUtility
::encodeHeader('Is the copyright character © really encoded correctly? Really?', 'quoted-printable', 'utf-8'));
1702 //////////////////////////////////
1703 // Tests concerning isValidUrl
1704 //////////////////////////////////
1706 * Data provider for valid isValidUrl's
1708 * @return array Valid ressource
1710 public function validUrlValidRessourceDataProvider() {
1712 'http' => array('http://www.example.org/'),
1713 'http without trailing slash' => array('http://qwe'),
1714 'http directory with trailing slash' => array('http://www.example/img/dir/'),
1715 'http directory without trailing slash' => array('http://www.example/img/dir'),
1716 'http index.html' => array('http://example.com/index.html'),
1717 'http index.php' => array('http://www.example.com/index.php'),
1718 'http test.png' => array('http://www.example/img/test.png'),
1719 'http username password querystring and ancher' => array('https://user:pw@www.example.org:80/path?arg=value#fragment'),
1720 'file' => array('file:///tmp/test.c'),
1721 'file directory' => array('file://foo/bar'),
1722 'ftp directory' => array('ftp://ftp.example.com/tmp/'),
1723 'mailto' => array('mailto:foo@bar.com'),
1724 'news' => array('news:news.php.net'),
1725 'telnet' => array('telnet://192.0.2.16:80/'),
1726 'ldap' => array('ldap://[2001:db8::7]/c=GB?objectClass?one'),
1727 'http punycode domain name' => array('http://www.xn--bb-eka.at'),
1728 'http punicode subdomain' => array('http://xn--h-zfa.oebb.at'),
1729 'http domain-name umlauts' => array('http://www.öbb.at'),
1730 'http subdomain umlauts' => array('http://äh.oebb.at'),
1731 'http directory umlauts' => array('http://www.oebb.at/äöü/')
1737 * @dataProvider validUrlValidRessourceDataProvider
1739 public function validURLReturnsTrueForValidRessource($url) {
1740 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::isValidUrl($url));
1744 * Data provider for invalid isValidUrl's
1746 * @return array Invalid ressource
1748 public function isValidUrlInvalidRessourceDataProvider() {
1750 'http missing colon' => array('http//www.example/wrong/url/'),
1751 'http missing slash' => array('http:/www.example'),
1752 'hostname only' => array('www.example.org/'),
1753 'file missing protocol specification' => array('/tmp/test.c'),
1754 'slash only' => array('/'),
1755 'string http://' => array('http://'),
1756 'string http:/' => array('http:/'),
1757 'string http:' => array('http:'),
1758 'string http' => array('http'),
1759 'empty string' => array(''),
1760 'string -1' => array('-1'),
1761 'string array()' => array('array()'),
1762 'random string' => array('qwe')
1768 * @dataProvider isValidUrlInvalidRessourceDataProvider
1770 public function validURLReturnsFalseForInvalidRessoure($url) {
1771 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::isValidUrl($url));
1774 //////////////////////////////////
1775 // Tests concerning isOnCurrentHost
1776 //////////////////////////////////
1780 public function isOnCurrentHostReturnsTrueWithCurrentHost() {
1781 $testUrl = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_REQUEST_URL');
1782 $this->assertTrue(\TYPO3\CMS\Core\Utility\GeneralUtility
::isOnCurrentHost($testUrl));
1786 * Data provider for invalid isOnCurrentHost's
1788 * @return array Invalid Hosts
1790 public function checkisOnCurrentHostInvalidHosts() {
1792 'empty string' => array(''),
1793 'arbitrary string' => array('arbitrary string'),
1794 'localhost IP' => array('127.0.0.1'),
1795 'relative path' => array('./relpath/file.txt'),
1796 'absolute path' => array('/abspath/file.txt?arg=value'),
1797 'differnt host' => array(\TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_REQUEST_HOST') . '.example.org')
1801 ////////////////////////////////////////
1802 // Tests concerning sanitizeLocalUrl
1803 ////////////////////////////////////////
1805 * Data provider for valid sanitizeLocalUrl's
1807 * @return array Valid url
1809 public function sanitizeLocalUrlValidUrlDataProvider() {
1810 $subDirectory = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_PATH');
1811 $typo3SiteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_SITE_URL');
1812 $typo3RequestHost = \TYPO3\CMS\Core\Utility\GeneralUtility
::getIndpEnv('TYPO3_REQUEST_HOST');
1814 'alt_intro.php' => array('alt_intro.php'),
1815 'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
1816 $subDirectory . 'typo3/alt_intro.php' => array($subDirectory . 'typo3/alt_intro.php'),
1817 $subDirectory . 'index.php' => array($subDirectory . 'index.php'),
1818 '../index.php' => array('../index.php'),
1819 '../typo3/alt_intro.php' => array('../typo3/alt_intro.php'),
1820 '../~userDirectory/index.php' => array('../~userDirectory/index.php'),
1821 '../typo3/mod.php?var1=test-case&var2=~user' => array('../typo3/mod.php?var1=test-case&var2=~user'),
1822 PATH_site
. 'typo3/alt_intro.php' => array(PATH_site
. 'typo3/alt_intro.php'),
1823 $typo3SiteUrl . 'typo3/alt_intro.php' => array($typo3SiteUrl . 'typo3/alt_intro.php'),
1824 ($typo3RequestHost . $subDirectory) . '/index.php' => array(($typo3RequestHost . $subDirectory) . '/index.php')
1830 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1832 public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
1833 $this->assertEquals($url, \TYPO3\CMS\Core\Utility\GeneralUtility
::sanitizeLocalUrl($url));
1838 * @dataProvider sanitizeLocalUrlValidUrlDataProvider
1840 public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
1841 $this->assertEquals(rawurlencode($url), \TYPO3\CMS\Core\Utility\GeneralUtility
::sanitizeLocalUrl(rawurlencode($url)));
1845 * Data provider for invalid sanitizeLocalUrl's
1847 * @return array Valid url
1849 public function sanitizeLocalUrlInvalidDataProvider() {
1851 'empty string' => array(''),
1852 'http domain' => array('http://www.google.de/'),
1853 'https domain' => array('https://www.google.de/'),
1854 'relative path with XSS' => array('../typo3/whatever.php?argument=javascript:alert(0)')
1860 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1862 public function sanitizeLocalUrlDeniesPlainInvalidUrls($url) {
1863 $this->assertEquals('', \TYPO3\CMS\Core\Utility\GeneralUtility
::sanitizeLocalUrl($url));
1868 * @dataProvider sanitizeLocalUrlInvalidDataProvider
1870 public function sanitizeLocalUrlDeniesEncodedInvalidUrls($url) {
1871 $this->assertEquals('', \TYPO3\CMS\Core\Utility\GeneralUtility
::sanitizeLocalUrl(rawurlencode($url)));
1874 //////////////////////////////////////
1875 // Tests concerning addSlashesOnArray
1876 //////////////////////////////////////
1880 public function addSlashesOnArrayAddsSlashesRecursive() {
1881 $inputArray = array(
1883 'key11' => 'val\'ue1',
1884 'key12' => 'val"ue2'
1886 'key2' => 'val\\ue3'
1888 $expectedResult = array(
1890 'key11' => 'val\\\'ue1',
1891 'key12' => 'val\\"ue2'
1893 'key2' => 'val\\\\ue3'
1895 \TYPO3\CMS\Core\Utility\GeneralUtility
::addSlashesOnArray($inputArray);
1896 $this->assertEquals($expectedResult, $inputArray);
1899 //////////////////////////////////////
1900 // Tests concerning addSlashesOnArray
1901 //////////////////////////////////////
1905 public function stripSlashesOnArrayStripsSlashesRecursive() {
1906 $inputArray = array(
1908 'key11' => 'val\\\'ue1',
1909 'key12' => 'val\\"ue2'
1911 'key2' => 'val\\\\ue3'
1913 $expectedResult = array(
1915 'key11' => 'val\'ue1',
1916 'key12' => 'val"ue2'
1918 'key2' => 'val\\ue3'
1920 \TYPO3\CMS\Core\Utility\GeneralUtility
::stripSlashesOnArray($inputArray);
1921 $this->assertEquals($expectedResult, $inputArray);
1924 //////////////////////////////////////
1925 // Tests concerning arrayDiffAssocRecursive
1926 //////////////////////////////////////
1930 public function arrayDiffAssocRecursiveHandlesOneDimensionalArrays() {
1940 $expectedResult = array(
1943 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::arrayDiffAssocRecursive($array1, $array2);
1944 $this->assertEquals($expectedResult, $actualResult);
1950 public function arrayDiffAssocRecursiveHandlesMultiDimensionalArrays() {
1954 'key21' => 'value21',
1955 'key22' => 'value22',
1957 'key231' => 'value231',
1958 'key232' => 'value232'
1965 'key21' => 'value21',
1967 'key231' => 'value231'
1971 $expectedResult = array(
1973 'key22' => 'value22',
1975 'key232' => 'value232'
1979 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::arrayDiffAssocRecursive($array1, $array2);
1980 $this->assertEquals($expectedResult, $actualResult);
1986 public function arrayDiffAssocRecursiveHandlesMixedArrays() {
1989 'key11' => 'value11',
1990 'key12' => 'value12'
1998 'key21' => 'value21'
2001 $expectedResult = array(
2004 $actualResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::arrayDiffAssocRecursive($array1, $array2);
2005 $this->assertEquals($expectedResult, $actualResult);
2008 //////////////////////////////////////
2009 // Tests concerning removeDotsFromTS
2010 //////////////////////////////////////
2014 public function removeDotsFromTypoScriptSucceedsWithDottedArray() {
2015 $typoScript = array(
2016 'propertyA.' => array(
2024 $expectedResult = array(
2025 'propertyA' => array(
2033 $this->assertEquals($expectedResult, \TYPO3\CMS\Core\Utility\GeneralUtility
::removeDotsFromTS($typoScript));
2039 public function removeDotsFromTypoScriptOverridesSubArray() {
2040 $typoScript = array(
2041 'propertyA.' => array(
2042 'keyA' => 'getsOverridden',
2050 $expectedResult = array(
2051 'propertyA' => array(
2059 $this->assertEquals($expectedResult, \TYPO3\CMS\Core\Utility\GeneralUtility
::removeDotsFromTS($typoScript));
2065 public function removeDotsFromTypoScriptOverridesWithScalar() {
2066 $typoScript = array(
2067 'propertyA.' => array(
2071 'keyA' => 'willOverride',
2076 $expectedResult = array(
2077 'propertyA' => array(
2078 'keyA' => 'willOverride',
2083 $this->assertEquals($expectedResult, \TYPO3\CMS\Core\Utility\GeneralUtility
::removeDotsFromTS($typoScript));
2086 //////////////////////////////////////
2087 // Tests concerning naturalKeySortRecursive
2088 //////////////////////////////////////
2092 public function naturalKeySortRecursiveReturnsFalseIfInputIsNotAnArray() {
2093 $testValues = array(
2098 foreach ($testValues as $testValue) {
2099 $this->assertFalse(\TYPO3\CMS\Core\Utility\GeneralUtility
::naturalKeySortRecursive($testValue));
2106 public function naturalKeySortRecursiveSortsOneDimensionalArrayByNaturalOrder() {
2120 $expectedResult = array(
2133 \TYPO3\CMS\Core\Utility\GeneralUtility
::naturalKeySortRecursive($testArray);
2134 $this->assertEquals($expectedResult, array_values($testArray));
2140 public function naturalKeySortRecursiveSortsMultiDimensionalArrayByNaturalOrder() {
2178 $expectedResult = array(
2191 \TYPO3\CMS\Core\Utility\GeneralUtility
::naturalKeySortRecursive($testArray);
2192 $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa']['bad'])));
2193 $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa'])));
2194 $this->assertEquals($expectedResult, array_values(array_keys($testArray)));
2197 //////////////////////////////////////
2198 // Tests concerning get_dirs
2199 //////////////////////////////////////
2203 public function getDirsReturnsArrayOfDirectoriesFromGivenDirectory() {
2205 $directories = \TYPO3\CMS\Core\Utility\GeneralUtility
::get_dirs($path);
2206 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType
::TYPE_ARRAY
, $directories);
2212 public function getDirsReturnsStringErrorOnPathFailure() {
2214 $result = \TYPO3\CMS\Core\Utility\GeneralUtility
::get_dirs($path);
2215 $expectedResult = 'error';
2216 $this->assertEquals($expectedResult, $result);
2219 //////////////////////////////////
2220 // Tests concerning hmac
2221 //////////////////////////////////
2225 public function hmacReturnsHashOfProperLength() {
2226 $hmac = \TYPO3\CMS\Core\Utility\GeneralUtility
::hmac('message');
2227 $this->assertTrue(!empty($hmac) && is_string($hmac));
2228 $this->assertTrue(strlen($hmac) == 40);
2234 public function hmacReturnsEqualHashesForEqualInput() {
2237 $this->assertEquals(\TYPO3\CMS\Core\Utility\GeneralUtility
::hmac($msg0), \TYPO3\CMS\Core\Utility\GeneralUtility
::hmac($msg1));
2243 public function hmacReturnsNoEqualHashesForNonEqualInput() {
2246 $this->assertNotEquals(\TYPO3\CMS\Core\Utility\GeneralUtility
::hmac($msg0), \TYPO3\CMS\Core\Utility\GeneralUtility
::hmac($msg1));
2249 //////////////////////////////////
2250 // Tests concerning quoteJSvalue
2251 //////////////////////////////////
2253 * Data provider for quoteJSvalueTest.
2257 public function quoteJsValueDataProvider() {
2259 'Immune characters are returned as is' => array(
2263 'Alphanumerical characters are returned as is' => array(
2264 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
2265 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
2267 'Angel brackets and ampersand are encoded' => array(
2271 'Quotes and slashes are encoded' => array(
2273 '\\x22\\x27\\x5C\\x2F'
2275 'Empty string stays empty' => array(
2279 'Exclamation mark and space are properly encoded' => array(
2281 'Hello\\x20World\\x21'
2283 'Whitespaces are properly encoded' => array(
2284 ((TAB
. LF
) . CR
) . ' ',
2285 '\\x09\\x0A\\x0D\\x20'
2287 'Null byte is properly encoded' => array(
2291 'Umlauts are properly encoded' => array(
2293 '\\xDC\\xFC\\xD6\\xF6\\xC4\\xE4'
2300 * @param string $input
2301 * @param string $expected
2302 * @dataProvider quoteJsValueDataProvider
2304 public function quoteJsValueTest($input, $expected) {
2305 $this->assertSame(('\'' . $expected) . '\'', \TYPO3\CMS\Core\Utility\GeneralUtility
::quoteJSvalue($input));
2308 //////////////////////////////////
2309 // Tests concerning readLLfile
2310 //////////////////////////////////
2314 public function readLLfileHandlesLocallangXMLOverride() {
2315 $unique = uniqid('locallangXMLOverrideTest');
2316 $xml = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
2319 <languageKey index="default" type="array">
2320 <label index="buttons.logout">EXIT</label>
2324 $file = ((PATH_site
. 'typo3temp/') . $unique) . '.xml';
2325 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($file, $xml);
2326 // Make sure there is no cached version of the label
2327 $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n')->flush();
2328 // Get default value
2329 $defaultLL = \TYPO3\CMS\Core\Utility\GeneralUtility
::readLLfile('EXT:lang/locallang_core.xml', 'default');
2330 // Clear language cache again
2331 $GLOBALS['typo3CacheManager']->getCache('t3lib_l10n')->flush();
2332 // Set override file
2333 $GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['EXT:lang/locallang_core.xml'][$unique] = $file;
2334 /** @var $store \TYPO3\CMS\Core\Localization\LanguageStore */
2335 $store = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Localization\\LanguageStore');
2336 $store->flushData('EXT:lang/locallang_core.xml');
2337 // Get override value
2338 $overrideLL = \TYPO3\CMS\Core\Utility\GeneralUtility
::readLLfile('EXT:lang/locallang_core.xml', 'default');
2341 $this->assertNotEquals($overrideLL['default']['buttons.logout'][0]['target'], '');
2342 $this->assertNotEquals($defaultLL['default']['buttons.logout'][0]['target'], $overrideLL['default']['buttons.logout'][0]['target']);
2343 $this->assertEquals($overrideLL['default']['buttons.logout'][0]['target'], 'EXIT');
2346 ///////////////////////////////
2347 // Tests concerning _GETset()
2348 ///////////////////////////////
2352 public function getSetWritesArrayToGetSystemVariable() {
2354 $GLOBALS['HTTP_GET_VARS'] = array();
2355 $getParameters = array('foo' => 'bar');
2356 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset($getParameters);
2357 $this->assertSame($getParameters, $_GET);
2363 public function getSetWritesArrayToGlobalsHttpGetVars() {
2365 $GLOBALS['HTTP_GET_VARS'] = array();
2366 $getParameters = array('foo' => 'bar');
2367 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset($getParameters);
2368 $this->assertSame($getParameters, $GLOBALS['HTTP_GET_VARS']);
2374 public function getSetForArrayDropsExistingValues() {
2376 $GLOBALS['HTTP_GET_VARS'] = array();
2377 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset(array('foo' => 'bar'));
2378 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset(array('oneKey' => 'oneValue'));
2379 $this->assertEquals(array('oneKey' => 'oneValue'), $GLOBALS['HTTP_GET_VARS']);
2385 public function getSetAssignsOneValueToOneKey() {
2387 $GLOBALS['HTTP_GET_VARS'] = array();
2388 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset('oneValue', 'oneKey');
2389 $this->assertEquals('oneValue', $GLOBALS['HTTP_GET_VARS']['oneKey']);
2395 public function getSetForOneValueDoesNotDropUnrelatedValues() {
2397 $GLOBALS['HTTP_GET_VARS'] = array();
2398 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset(array('foo' => 'bar'));
2399 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset('oneValue', 'oneKey');
2400 $this->assertEquals(array('foo' => 'bar', 'oneKey' => 'oneValue'), $GLOBALS['HTTP_GET_VARS']);
2406 public function getSetCanAssignsAnArrayToASpecificArrayElement() {
2408 $GLOBALS['HTTP_GET_VARS'] = array();
2409 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset(array('childKey' => 'oneValue'), 'parentKey');
2410 $this->assertEquals(array('parentKey' => array('childKey' => 'oneValue')), $GLOBALS['HTTP_GET_VARS']);
2416 public function getSetCanAssignAStringValueToASpecificArrayChildElement() {
2418 $GLOBALS['HTTP_GET_VARS'] = array();
2419 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset('oneValue', 'parentKey|childKey');
2420 $this->assertEquals(array('parentKey' => array('childKey' => 'oneValue')), $GLOBALS['HTTP_GET_VARS']);
2426 public function getSetCanAssignAnArrayToASpecificArrayChildElement() {
2428 $GLOBALS['HTTP_GET_VARS'] = array();
2429 \TYPO3\CMS\Core\Utility\GeneralUtility
::_GETset(array('key1' => 'value1', 'key2' => 'value2'), 'parentKey|childKey');
2430 $this->assertEquals(array(
2431 'parentKey' => array(
2432 'childKey' => array('key1' => 'value1', 'key2' => 'value2')
2434 ), $GLOBALS['HTTP_GET_VARS']);
2437 ///////////////////////////
2438 // Tests concerning minifyJavaScript
2439 ///////////////////////////
2443 public function minifyJavaScriptReturnsInputStringIfNoHookIsRegistered() {
2444 unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript']);
2445 $testString = uniqid('string');
2446 $this->assertSame($testString, \TYPO3\CMS\Core\Utility\GeneralUtility
::minifyJavaScript($testString));
2450 * Create an own hook callback class, register as hook, and check
2451 * if given string to compress is given to hook method
2455 public function minifyJavaScriptCallsRegisteredHookWithInputString() {
2456 $hookClassName = uniqid('tx_coretest');
2457 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2458 $functionName = ('&' . $hookClassName) . '->minify';
2459 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2460 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2461 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2462 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2463 $minifyHookMock->expects($this->once())->method('minify')->will($this->returnCallback(array($this, 'isMinifyJavaScriptHookCalledCallback')));
2464 \TYPO3\CMS\Core\Utility\GeneralUtility
::minifyJavaScript('foo');
2468 * Callback function used in minifyJavaScriptCallsRegisteredHookWithInputString test
2470 * @param array $params
2472 public function isMinifyJavaScriptHookCalledCallback(array $params) {
2473 // We can not throw an exception here, because that would be caught by the
2474 // minifyJavaScript method under test itself. Thus, we just die if the
2475 // input string is not ok.
2476 if ($params['script'] !== 'foo') {
2482 * Create a hook callback, use callback to throw an exception and check
2483 * if the exception is given as error parameter to the calling method.
2487 public function minifyJavaScriptReturnsErrorStringOfHookException() {
2488 $hookClassName = uniqid('tx_coretest');
2489 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2490 $functionName = ('&' . $hookClassName) . '->minify';
2491 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2492 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2493 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2494 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2495 $minifyHookMock->expects($this->any())->method('minify')->will($this->returnCallback(array($this, 'minifyJavaScriptErroneousCallback')));
2497 \TYPO3\CMS\Core\Utility\GeneralUtility
::minifyJavaScript('string to compress', $error);
2498 $this->assertSame('Error minifying java script: foo', $error);
2502 * Check if the error message that is returned by the hook callback
2503 * is logged to t3lib_div::devLog.
2507 public function minifyJavaScriptWritesExceptionMessageToDevLog() {
2508 $namespace = 'TYPO3\\CMS\\Core\\Utility';
2509 $t3libDivMock = uniqid('GeneralUtility');
2510 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();') . ' }') . '}');
2511 $t3libDivMock = $namespace . '\\' . $t3libDivMock;
2512 $hookClassName = uniqid('tx_coretest');
2513 $minifyHookMock = $this->getMock('stdClass', array('minify'), array(), $hookClassName);
2514 $functionName = ('&' . $hookClassName) . '->minify';
2515 $GLOBALS['T3_VAR']['callUserFunction'][$functionName] = array();
2516 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['obj'] = $minifyHookMock;
2517 $GLOBALS['T3_VAR']['callUserFunction'][$functionName]['method'] = 'minify';
2518 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_div.php']['minifyJavaScript'][] = $functionName;
2519 $minifyHookMock->expects($this->any())->method('minify')->will($this->returnCallback(array($this, 'minifyJavaScriptErroneousCallback')));
2520 $this->setExpectedException('\\RuntimeException');
2521 $t3libDivMock::minifyJavaScript('string to compress');
2525 * Callback function used in
2526 * minifyJavaScriptReturnsErrorStringOfHookException and
2527 * minifyJavaScriptWritesExceptionMessageToDevLog
2529 * @throws \RuntimeException
2531 public function minifyJavaScriptErroneousCallback() {
2532 throw new \
RuntimeException('foo', 1344888548);
2535 ///////////////////////////
2536 // Tests concerning getUrl
2537 ///////////////////////////
2541 public function getUrlWithAdditionalRequestHeadersProvidesHttpHeaderOnError() {
2542 $url = 'http://typo3.org/i-do-not-exist-' . time();
2544 \TYPO3\CMS\Core\Utility\GeneralUtility
::getUrl($url, 0, array(), $report);
2545 $this->assertContains('404', $report['message']);
2551 public function getUrlProvidesWithoutAdditionalRequestHeadersHttpHeaderOnError() {
2552 $url = 'http://typo3.org/i-do-not-exist-' . time();
2554 \TYPO3\CMS\Core\Utility\GeneralUtility
::getUrl($url, 0, FALSE, $report);
2555 $this->assertContains('404', $report['message'], 'Did not provide the HTTP response header when requesting a failing URL.');
2558 ///////////////////////////////
2559 // Tests concerning fixPermissions
2560 ///////////////////////////////
2564 public function fixPermissionsSetsGroup() {
2565 if (TYPO3_OS
== 'WIN') {
2566 $this->markTestSkipped('fixPermissionsSetsGroup() tests not available on Windows');
2568 if (!function_exists('posix_getegid')) {
2569 $this->markTestSkipped('Function posix_getegid() not available, fixPermissionsSetsGroup() tests skipped');
2571 if (posix_getegid() === -1) {
2572 $this->markTestSkipped('The fixPermissionsSetsGroup() is not available on Mac OS because posix_getegid() always returns -1 on Mac OS.');
2574 // Create and prepare test file
2575 $filename = (PATH_site
. 'typo3temp/') . uniqid('test_');
2576 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($filename, '42');
2577 $currentGroupId = posix_getegid();
2578 // Set target group and run method
2579 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = $currentGroupId;
2580 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($filename);
2582 $resultFileGroup = filegroup($filename);
2584 $this->assertEquals($currentGroupId, $resultFileGroup);
2590 public function fixPermissionsSetsPermissionsToFile() {
2591 if (TYPO3_OS
== 'WIN') {
2592 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2594 // Create and prepare test file
2595 $filename = (PATH_site
. 'typo3temp/') . uniqid('test_');
2596 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($filename, '42');
2597 chmod($filename, 482);
2598 // Set target permissions and run method
2599 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2600 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($filename);
2601 // Get actual permissions and clean up
2603 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
2605 // Test if everything was ok
2606 $this->assertTrue($fixPermissionsResult);
2607 $this->assertEquals($resultFilePermissions, '0660');
2613 public function fixPermissionsSetsPermissionsToHiddenFile() {
2614 if (TYPO3_OS
== 'WIN') {
2615 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2617 // Create and prepare test file
2618 $filename = (PATH_site
. 'typo3temp/') . uniqid('.test_');
2619 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($filename, '42');
2620 chmod($filename, 482);
2621 // Set target permissions and run method
2622 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2623 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($filename);
2624 // Get actual permissions and clean up
2626 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
2628 // Test if everything was ok
2629 $this->assertTrue($fixPermissionsResult);
2630 $this->assertEquals($resultFilePermissions, '0660');
2636 public function fixPermissionsSetsPermissionsToDirectory() {
2637 if (TYPO3_OS
== 'WIN') {
2638 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2640 // Create and prepare test directory
2641 $directory = (PATH_site
. 'typo3temp/') . uniqid('test_');
2642 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2643 chmod($directory, 1551);
2644 // Set target permissions and run method
2645 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2646 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($directory);
2647 // Get actual permissions and clean up
2649 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
2650 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($directory);
2651 // Test if everything was ok
2652 $this->assertTrue($fixPermissionsResult);
2653 $this->assertEquals($resultDirectoryPermissions, '0770');
2659 public function fixPermissionsSetsPermissionsToDirectoryWithTrailingSlash() {
2660 if (TYPO3_OS
== 'WIN') {
2661 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2663 // Create and prepare test directory
2664 $directory = (PATH_site
. 'typo3temp/') . uniqid('test_');
2665 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2666 chmod($directory, 1551);
2667 // Set target permissions and run method
2668 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2669 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($directory . '/');
2670 // Get actual permissions and clean up
2672 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
2673 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($directory);
2674 // Test if everything was ok
2675 $this->assertTrue($fixPermissionsResult);
2676 $this->assertEquals($resultDirectoryPermissions, '0770');
2682 public function fixPermissionsSetsPermissionsToHiddenDirectory() {
2683 if (TYPO3_OS
== 'WIN') {
2684 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2686 // Create and prepare test directory
2687 $directory = (PATH_site
. 'typo3temp/') . uniqid('.test_');
2688 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2689 chmod($directory, 1551);
2690 // Set target permissions and run method
2691 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2692 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($directory);
2693 // Get actual permissions and clean up
2695 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
2696 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($directory);
2697 // Test if everything was ok
2698 $this->assertTrue($fixPermissionsResult);
2699 $this->assertEquals($resultDirectoryPermissions, '0770');
2705 public function fixPermissionsCorrectlySetsPermissionsRecursive() {
2706 if (TYPO3_OS
== 'WIN') {
2707 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2709 // Create and prepare test directory and file structure
2710 $baseDirectory = (PATH_site
. 'typo3temp/') . uniqid('test_');
2711 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($baseDirectory);
2712 chmod($baseDirectory, 1751);
2713 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($baseDirectory . '/file', '42');
2714 chmod($baseDirectory . '/file', 482);
2715 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($baseDirectory . '/foo');
2716 chmod($baseDirectory . '/foo', 1751);
2717 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir($baseDirectory . '/foo/file', '42');
2718 chmod($baseDirectory . '/foo/file', 482);
2719 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($baseDirectory . '/.bar');
2720 chmod($baseDirectory . '/.bar', 1751);
2721 // Use this if writeFileToTypo3tempDir is fixed to create hidden files in subdirectories
2722 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/.file', '42');
2723 // t3lib_div::writeFileToTypo3tempDir($baseDirectory . '/.bar/..file2', '42');
2724 touch($baseDirectory . '/.bar/.file', '42');
2725 chmod($baseDirectory . '/.bar/.file', 482);
2726 touch($baseDirectory . '/.bar/..file2', '42');
2727 chmod($baseDirectory . '/.bar/..file2', 482);
2728 // Set target permissions and run method
2729 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2730 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0770';
2731 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($baseDirectory, TRUE);
2732 // Get actual permissions
2734 $resultBaseDirectoryPermissions = substr(decoct(fileperms($baseDirectory)), 1);
2735 $resultBaseFilePermissions = substr(decoct(fileperms($baseDirectory . '/file')), 2);
2736 $resultFooDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/foo')), 1);
2737 $resultFooFilePermissions = substr(decoct(fileperms($baseDirectory . '/foo/file')), 2);
2738 $resultBarDirectoryPermissions = substr(decoct(fileperms($baseDirectory . '/.bar')), 1);
2739 $resultBarFilePermissions = substr(decoct(fileperms($baseDirectory . '/.bar/.file')), 2);
2740 $resultBarFile2Permissions = substr(decoct(fileperms($baseDirectory . '/.bar/..file2')), 2);
2742 unlink($baseDirectory . '/file');
2743 unlink($baseDirectory . '/foo/file');
2744 unlink($baseDirectory . '/.bar/.file');
2745 unlink($baseDirectory . '/.bar/..file2');
2746 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($baseDirectory . '/foo');
2747 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($baseDirectory . '/.bar');
2748 \TYPO3\CMS\Core\Utility\GeneralUtility
::rmdir($baseDirectory);
2749 // Test if everything was ok
2750 $this->assertTrue($fixPermissionsResult);
2751 $this->assertEquals($resultBaseDirectoryPermissions, '0770');
2752 $this->assertEquals($resultBaseFilePermissions, '0660');
2753 $this->assertEquals($resultFooDirectoryPermissions, '0770');
2754 $this->assertEquals($resultFooFilePermissions, '0660');
2755 $this->assertEquals($resultBarDirectoryPermissions, '0770');
2756 $this->assertEquals($resultBarFilePermissions, '0660');
2757 $this->assertEquals($resultBarFile2Permissions, '0660');
2763 public function fixPermissionsDoesNotSetPermissionsToNotAllowedPath() {
2764 if (TYPO3_OS
== 'WIN') {
2765 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2767 // Create and prepare test file
2768 $filename = (PATH_site
. 'typo3temp/../typo3temp/') . uniqid('test_');
2770 chmod($filename, 482);
2771 // Set target permissions and run method
2772 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2773 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($filename);
2774 // Get actual permissions and clean up
2776 $resultFilePermissions = substr(decoct(fileperms($filename)), 2);
2778 // Test if everything was ok
2779 $this->assertFalse($fixPermissionsResult);
2785 public function fixPermissionsSetsPermissionsWithRelativeFileReference() {
2786 if (TYPO3_OS
== 'WIN') {
2787 $this->markTestSkipped('fixPermissions() tests not available on Windows');
2789 $filename = 'typo3temp/' . uniqid('test_');
2790 \TYPO3\CMS\Core\Utility\GeneralUtility
::writeFileToTypo3tempDir(PATH_site
. $filename, '42');
2791 chmod(PATH_site
. $filename, 482);
2792 // Set target permissions and run method
2793 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
2794 $fixPermissionsResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::fixPermissions($filename);
2795 // Get actual permissions and clean up
2797 $resultFilePermissions = substr(decoct(fileperms(PATH_site
. $filename)), 2);
2798 unlink(PATH_site
. $filename);
2799 // Test if everything was ok
2800 $this->assertTrue($fixPermissionsResult);
2801 $this->assertEquals($resultFilePermissions, '0660');
2804 ///////////////////////////////
2805 // Tests concerning mkdir
2806 ///////////////////////////////
2810 public function mkdirCreatesDirectory() {
2811 $directory = (PATH_site
. 'typo3temp/') . uniqid('test_');
2812 $mkdirResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2814 $directoryCreated = is_dir($directory);
2816 $this->assertTrue($mkdirResult);
2817 $this->assertTrue($directoryCreated);
2823 public function mkdirCreatesHiddenDirectory() {
2824 $directory = (PATH_site
. 'typo3temp/') . uniqid('.test_');
2825 $mkdirResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2827 $directoryCreated = is_dir($directory);
2829 $this->assertTrue($mkdirResult);
2830 $this->assertTrue($directoryCreated);
2836 public function mkdirCreatesDirectoryWithTrailingSlash() {
2837 $directory = ((PATH_site
. 'typo3temp/') . uniqid('test_')) . '/';
2838 $mkdirResult = \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2840 $directoryCreated = is_dir($directory);
2842 $this->assertTrue($mkdirResult);
2843 $this->assertTrue($directoryCreated);
2849 public function mkdirSetsPermissionsOfCreatedDirectory() {
2850 if (TYPO3_OS
== 'WIN') {
2851 $this->markTestSkipped('mkdirSetsPermissionsOfCreatedDirectory() test not available on Windows');
2853 $directory = (PATH_site
. 'typo3temp/') . uniqid('test_');
2854 $oldUmask = umask(19);
2855 $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0772';
2856 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir($directory);
2858 $resultDirectoryPermissions = substr(decoct(fileperms($directory)), 1);
2861 $this->assertEquals($resultDirectoryPermissions, '0772');
2867 public function mkdirSetsGroupOwnershipOfCreatedDirectory() {
2868 if (!function_exists('posix_getegid')) {
2869 $this->markTestSkipped('Function posix_getegid() not available, mkdirSetsGroupOwnershipOfCreatedDirectory() tests skipped');
2871 if (posix_getegid() === -1) {
2872 $this->markTestSkipped('The mkdirSetsGroupOwnershipOfCreatedDirectory() is not available on Mac OS because posix_getegid() always returns -1 on Mac OS.');
2874 $swapGroup = $this->checkGroups(__FUNCTION__
);
2875 if ($swapGroup !== FALSE) {
2876 $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup'] = $swapGroup;
2877 $directory = uniqid('mkdirtest_');
2878 \TYPO3\CMS\Core\Utility\GeneralUtility
::mkdir((PATH_site
. 'typo3temp/') . $directory);
2880 $resultDirectoryGroupInfo = posix_getgrgid(filegroup((PATH_site
. 'typo3temp/') . $directory));
2881 $resultDirectoryGroup = $resultDirectoryGroupInfo['name'];