[TASk] Add tests for t3lib_div addSlashesOnArray and stripSlashesOnArray
[Packages/TYPO3.CMS.git] / tests / t3lib / class.t3lib_divTest.php
index 73a0ca6..3c21f21 100644 (file)
@@ -215,11 +215,11 @@ class t3lib_divTest extends tx_phpunit_testcase {
         */
        public static function invalidIpDataProvider() {
                return array(
-                       'null' => array(null),
+                       'null' => array(NULL),
                        'zero' => array(0),
                        'string' => array('test'),
                        'string empty' => array(''),
-                       'string null' => array('null'),
+                       'string NULL' => array('NULL'),
                        'out of bounds IPv4' => array('300.300.300.300'),
                        'dotted decimal notation with only two dots' => array('127.0.1'),
                );
@@ -339,17 +339,17 @@ class t3lib_divTest extends tx_phpunit_testcase {
                        'no case sensitive string match' => array('hello world', 'Hello'),
                        'array is not part of string' => array('string', array()),
                        'string is not part of array' => array(array(), 'string'),
-                       'null is not part of string' => array('string', NULL),
+                       'NULL is not part of string' => array('string', NULL),
                        'string is not part of array' => array(NULL, 'string'),
-                       'null is not part of array' => array(array(), NULL),
+                       'NULL is not part of array' => array(array(), NULL),
                        'array is not part of string' => array(NULL, array()),
                        'empty string is not part of empty string' => array('', ''),
-                       'null is not part of empty string' => array('', NULL),
+                       'NULL is not part of empty string' => array('', NULL),
                        'false is not part of empty string' => array('', FALSE),
-                       'empty string is not part of null' => array(NULL, ''),
+                       'empty string is not part of NULL' => array(NULL, ''),
                        'empty string is not part of false' => array(FALSE, ''),
                        'empty string is not part of zero integer' => array(0, ''),
-                       'zero integer is not part of null' => array(NULL, 0),
+                       'zero integer is not part of NULL' => array(NULL, 0),
                        'zero integer is not part of empty string' => array('', 0),
                );
        }
@@ -1111,6 +1111,66 @@ class t3lib_divTest extends tx_phpunit_testcase {
 
 
        //////////////////////////////////////
+       // Tests concerning addSlashesOnArray
+       //////////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function addSlashesOnArrayAddsSlashesRecursive() {
+               $inputArray = array(
+                       'key1' => array(
+                               'key11' => "val'ue1",
+                               'key12' => 'val"ue2',
+                       ),
+                       'key2' => 'val\ue3',
+               );
+               $expectedResult = array(
+                       'key1' => array(
+                               'key11' => "val\'ue1",
+                               'key12' => 'val\"ue2',
+                       ),
+                       'key2' => 'val\\\\ue3',
+               );
+               t3lib_div::addSlashesOnArray($inputArray);
+               $this->assertEquals(
+                       $expectedResult,
+                       $inputArray
+               );
+       }
+
+
+       //////////////////////////////////////
+       // Tests concerning addSlashesOnArray
+       //////////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function stripSlashesOnArrayStripsSlashesRecursive() {
+               $inputArray = array(
+                       'key1' => array(
+                               'key11' => "val\'ue1",
+                               'key12' => 'val\"ue2',
+                       ),
+                       'key2' => 'val\\\\ue3',
+               );
+               $expectedResult = array(
+                       'key1' => array(
+                               'key11' => "val'ue1",
+                               'key12' => 'val"ue2',
+                       ),
+                       'key2' => 'val\ue3',
+               );
+               t3lib_div::stripSlashesOnArray($inputArray);
+               $this->assertEquals(
+                       $expectedResult,
+                       $inputArray
+               );
+       }
+
+
+       //////////////////////////////////////
        // Tests concerning arrayDiffAssocRecursive
        //////////////////////////////////////
 
@@ -1281,6 +1341,119 @@ class t3lib_divTest extends tx_phpunit_testcase {
                $this->assertEquals($expectedResult, t3lib_div::removeDotsFromTS($typoScript));
        }
 
+       //////////////////////////////////////
+       // Tests concerning naturalKeySortRecursive
+       //////////////////////////////////////
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveReturnsFalseIfInputIsNotAnArray() {
+               $testValues = array(
+                       1,
+                       'string',
+                       FALSE
+               );
+               foreach($testValues as $testValue) {
+                       $this->assertFalse(t3lib_div::naturalKeySortRecursive($testValue));
+               }
+       }
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveSortsOneDimensionalArrayByNaturalOrder() {
+               $testArray = array(
+                       'bb' => 'bb',
+                       'ab' => 'ab',
+                       '123' => '123',
+                       'aaa' => 'aaa',
+                       'abc' => 'abc',
+                       '23' => '23',
+                       'ba' => 'ba',
+                       'bad' => 'bad',
+                       '2' => '2',
+                       'zap' => 'zap',
+                       '210' => '210'
+               );
+               $expectedResult = array(
+                       '2',
+                       '23',
+                       '123',
+                       '210',
+                       'aaa',
+                       'ab',
+                       'abc',
+                       'ba',
+                       'bad',
+                       'bb',
+                       'zap'
+               );
+               t3lib_div::naturalKeySortRecursive($testArray);
+               $this->assertEquals($expectedResult, array_values($testArray));
+       }
+
+       /**
+        * @test
+        */
+       public function naturalKeySortRecursiveSortsMultiDimensionalArrayByNaturalOrder() {
+               $testArray = array(
+                       '2' => '2',
+                       'bb' => 'bb',
+                       'ab' => 'ab',
+                       '23' => '23',
+                       'aaa' => array(
+                               'bb' => 'bb',
+                               'ab' => 'ab',
+                               '123' => '123',
+                               'aaa' => 'aaa',
+                               '2' => '2',
+                               'abc' => 'abc',
+                               'ba' => 'ba',
+                               '23' => '23',
+                               'bad' => array(
+                                       'bb' => 'bb',
+                                       'ab' => 'ab',
+                                       '123' => '123',
+                                       'aaa' => 'aaa',
+                                       'abc' => 'abc',
+                                       '23' => '23',
+                                       'ba' => 'ba',
+                                       'bad' => 'bad',
+                                       '2' => '2',
+                                       'zap' => 'zap',
+                                       '210' => '210'
+                               ),
+                               '210' => '210',
+                               'zap' => 'zap'
+                       ),
+                       'abc' => 'abc',
+                       'ba' => 'ba',
+                       '210' => '210',
+                       'bad' => 'bad',
+                       '123' => '123',
+                       'zap' => 'zap'
+               );
+
+               $expectedResult = array(
+                       '2',
+                       '23',
+                       '123',
+                       '210',
+                       'aaa',
+                       'ab',
+                       'abc',
+                       'ba',
+                       'bad',
+                       'bb',
+                       'zap'
+               );
+               t3lib_div::naturalKeySortRecursive($testArray);
+
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa']['bad'])));
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray['aaa'])));
+               $this->assertEquals($expectedResult, array_values(array_keys($testArray)));
+       }
 
        //////////////////////////////////////
        // Tests concerning get_dirs
@@ -1360,7 +1533,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvaluetHtmlspecialcharsDataWithinCDataSetToFalse() {
                $this->assertContains(
                        '>',
-                       t3lib_div::quoteJSvalue('>', false)
+                       t3lib_div::quoteJSvalue('>', FALSE)
                );
        }
 
@@ -1370,7 +1543,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvaluetNotHtmlspecialcharsDataWithinCDataSetToTrue() {
                $this->assertContains(
                        '>',
-                       t3lib_div::quoteJSvalue('>', true)
+                       t3lib_div::quoteJSvalue('>', TRUE)
                );
        }
 
@@ -1380,7 +1553,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvalueReturnsEmptyStringQuotedInSingleQuotes() {
                $this->assertEquals(
                        "''",
-                       t3lib_div::quoteJSvalue("", true)
+                       t3lib_div::quoteJSvalue("", TRUE)
                );
        }
 
@@ -1390,7 +1563,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvalueNotModifiesStringWithoutSpecialCharacters() {
                $this->assertEquals(
                        "'Hello world!'",
-                       t3lib_div::quoteJSvalue("Hello world!", true)
+                       t3lib_div::quoteJSvalue("Hello world!", TRUE)
                );
        }
 
@@ -1400,7 +1573,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvalueEscapesSingleQuote() {
                $this->assertEquals(
                        "'\\''",
-                       t3lib_div::quoteJSvalue("'", true)
+                       t3lib_div::quoteJSvalue("'", TRUE)
                );
        }
 
@@ -1410,7 +1583,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvalueEscapesDoubleQuoteWithinCDataSetToTrue() {
                $this->assertEquals(
                        "'\\\"'",
-                       t3lib_div::quoteJSvalue('"', true)
+                       t3lib_div::quoteJSvalue('"', TRUE)
                );
        }
 
@@ -1420,7 +1593,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        public function quoteJSvalueEscapesAndHtmlspecialcharsDoubleQuoteWithinCDataSetToFalse() {
                $this->assertEquals(
                        "'\\"'",
-                       t3lib_div::quoteJSvalue('"', false)
+                       t3lib_div::quoteJSvalue('"', FALSE)
                );
        }
 
@@ -1897,7 +2070,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
 
                $filename = 'typo3temp/' . uniqid('test_');
                t3lib_div::writeFileToTypo3tempDir(PATH_site . $filename, '42');
-               chmod($filename, 0742);
+               chmod(PATH_site . $filename, 0742);
 
                        // Set target permissions and run method
                $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0660';
@@ -1976,6 +2149,111 @@ class t3lib_divTest extends tx_phpunit_testcase {
 
 
        ///////////////////////////////
+       // Tests concerning mkdir_deep
+       ///////////////////////////////
+
+       /**
+        * @test
+        */
+       public function mkdirDeepCreatesDirectory() {
+               $directory = 'typo3temp/' . uniqid('test_');
+               t3lib_div::mkdir_deep(PATH_site, $directory);
+               $isDirectoryCreated = is_dir(PATH_site . $directory);
+               rmdir(PATH_site . $directory);
+               $this->assertTrue($isDirectoryCreated);
+       }
+
+       /**
+        * @test
+        */
+       public function mkdirDeepCreatesSubdirectoriesRecursive() {
+               $directory = 'typo3temp/' . uniqid('test_');
+               $subDirectory = $directory . '/foo';
+               t3lib_div::mkdir_deep(PATH_site, $subDirectory);
+               $isDirectoryCreated = is_dir(PATH_site . $subDirectory);
+               rmdir(PATH_site . $subDirectory);
+               rmdir(PATH_site . $directory);
+               $this->assertTrue($isDirectoryCreated);
+       }
+
+       /**
+        * @test
+        */
+       public function mkdirDeepFixesPermissionsOnNewDirectory() {
+               if (TYPO3_OS == 'WIN') {
+                       $this->markTestSkipped('mkdirDeepFixesPermissionsOnNewDirectory() test not available on Windows.');
+               }
+
+               $directory = uniqid('mkdirdeeptest_');
+               $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] = '0750';
+               t3lib_div::mkdir_deep(PATH_site . 'typo3temp/', $directory);
+               clearstatcache();
+               $resultDirectoryPermissions = substr(decoct(fileperms(PATH_site . 'typo3temp/' . $directory)), -3, 3);
+               @rmdir(PATH_site . 'typo3temp/' . $directory);
+               $this->assertEquals($resultDirectoryPermissions, '750');
+       }
+
+       /**
+        * @test
+        */
+       public function mkdirDeepDoesNotChangePermissionsOfExistingSubDirectories() {
+               if (TYPO3_OS == 'WIN') {
+                       $this->markTestSkipped('mkdirDeepDoesNotChangePermissionsOfExistingSubDirectories() test not available on Windows.');
+               }
+
+               $baseDirectory = PATH_site . 'typo3temp/';
+               $existingDirectory = uniqid('test_existing_') . '/';
+               $newSubDirectory = uniqid('test_new_');
+               @mkdir($baseDirectory . $existingDirectory);
+               chmod($baseDirectory . $existingDirectory, 0742);
+               t3lib_div::mkdir_deep($baseDirectory, $existingDirectory . $newSubDirectory);
+               $resultExistingDirectoryPermissions = substr(decoct(fileperms($baseDirectory . $existingDirectory)), 2);
+               @rmdir($baseDirectory, $existingDirectory . $newSubDirectory);
+               @rmdir($baseDirectory, $existingDirectory);
+               $this->assertEquals($resultExistingDirectoryPermissions, '0742');
+       }
+
+       /**
+        * @test
+        */
+       public function mkdirDeepCreatesDirectoryInVfsStream() {
+               if (!class_exists('\vfsStreamWrapper')) {
+                       $this->markTestSkipped('mkdirDeepCreatesDirectoryInVfsStream() test not available with this phpunit version.');
+               }
+
+               \vfsStreamWrapper::register();
+               $baseDirectory = uniqid('test_');
+               \vfsStreamWrapper::setRoot(new \vfsStreamDirectory($baseDirectory));
+               t3lib_div::mkdir_deep('vfs://' . $baseDirectory . '/', 'sub');
+               $this->assertTrue(is_dir('vfs://' . $baseDirectory . '/sub'));
+       }
+
+       /**
+        * @test
+        * @expectedException \RuntimeException
+        */
+       public function mkdirDeepThrowsExceptionIfDirectoryCreationFails() {
+               t3lib_div::mkdir_deep('http://localhost');
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function mkdirDeepThrowsExceptionIfBaseDirectoryIsNotOfTypeString() {
+               t3lib_div::mkdir_deep(array());
+       }
+
+       /**
+        * @test
+        * @expectedException \InvalidArgumentException
+        */
+       public function mkdirDeepThrowsExceptionIfDeepDirectoryIsNotOfTypeString() {
+               t3lib_div::mkdir_deep(PATH_site . 'typo3temp/foo', array());
+       }
+
+
+       ///////////////////////////////
        // Tests concerning unQuoteFilenames
        ///////////////////////////////
 
@@ -2563,7 +2841,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
        }
 
        /**
-        * Tests whether verifyFilenameAgainstDenyPattern detects the null character.
+        * Tests whether verifyFilenameAgainstDenyPattern detects the NULL character.
         *
         * @test
         */
@@ -2623,5 +2901,66 @@ class t3lib_divTest extends tx_phpunit_testcase {
 
                $this->assertEquals($resultFilePermissions, '0777');
        }
+
+       ///////////////////////////////////////////////////
+       // Tests concerning hasValidClassPrefix
+       ///////////////////////////////////////////////////
+
+       /**
+        * @return array
+        */
+       public function validClassPrefixDataProvider() {
+               return array(
+                       array('tx_foo'),
+                       array('tx_foo_bar'),
+                       array('Tx_foo'),
+                       array($GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix'] . 'foo'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider validClassPrefixDataProvider
+        * @param string $className Class name to test
+        */
+       public function hasValidClassPrefixAcceptsValidPrefixes($className) {
+               $this->assertTrue(
+                       t3lib_div::hasValidClassPrefix($className)
+               );
+       }
+
+       /**
+        * @return array
+        */
+       public function invalidClassPrefixDataProvider() {
+               return array(
+                       array(''),
+                       array('ab_c'),
+                       array('txfoo'),
+                       array('Txfoo'),
+                       array('userfoo'),
+                       array('User_foo'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider invalidClassPrefixDataProvider
+        * @param string $className Class name to test
+        */
+       public function hasValidClassPrefixRefusesInvalidPrefixes($className) {
+               $this->assertFalse(
+                       t3lib_div::hasValidClassPrefix($className)
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function hasValidClassPrefixAcceptsAdditionalPrefixes() {
+               $this->assertTrue(
+                       t3lib_div::hasValidClassPrefix('customPrefix_foo', array('customPrefix_'))
+               );
+       }
 }
 ?>
\ No newline at end of file