[BUGFIX] Streamline Unit Tests for GeneralUtility::sanitizeLocalUrl 82/42982/3
authorNicole Cordes <typo3@cordes.co>
Wed, 2 Sep 2015 13:06:12 +0000 (15:06 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Thu, 3 Sep 2015 06:24:24 +0000 (08:24 +0200)
Currently the Unit Tests for the function just work by accident. As
HTTP_HOST an absolute path is set which is used to get some other values
to compare the given strings. To add further tests we should streamline
the tests and set/add expectations for host and script_name.

Resolves: #69475
Releases: master, 6.2
Change-Id: If139da7bd22c1ebf2c538cd3412fb0abf56dc668
Reviewed-on: http://review.typo3.org/42982
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index ecb1a6c..7ddf570 100644 (file)
@@ -1975,14 +1975,15 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        // Tests concerning sanitizeLocalUrl
        ////////////////////////////////////////
        /**
-        * Data provider for valid sanitizeLocalUrl's
+        * Data provider for valid sanitizeLocalUrl paths
         *
         * @return array Valid url
         */
-       public function sanitizeLocalUrlValidUrlDataProvider() {
+       public function sanitizeLocalUrlValidPathsDataProvider() {
                $subDirectory = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
                $typo3SiteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
                $typo3RequestHost = GeneralUtility::getIndpEnv('TYPO3_REQUEST_HOST');
+
                return array(
                        'alt_intro.php' => array('alt_intro.php'),
                        'alt_intro.php?foo=1&bar=2' => array('alt_intro.php?foo=1&bar=2'),
@@ -2000,17 +2001,80 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @test
-        * @dataProvider sanitizeLocalUrlValidUrlDataProvider
+        * @param string $path
+        * @dataProvider sanitizeLocalUrlValidPathsDataProvider
+        */
+       public function sanitizeLocalUrlAcceptsNotEncodedValidPaths($path) {
+               $this->assertEquals($path, GeneralUtility::sanitizeLocalUrl($path));
+       }
+
+       /**
+        * @test
+        * @param string $path
+        * @dataProvider sanitizeLocalUrlValidPathsDataProvider
+        */
+       public function sanitizeLocalUrlAcceptsEncodedValidPaths($path) {
+               $this->assertEquals(rawurlencode($path), GeneralUtility::sanitizeLocalUrl(rawurlencode($path)));
+       }
+
+       /**
+        * Data provider for valid sanitizeLocalUrl's
+        *
+        * @return array Valid url
         */
-       public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url) {
+       public function sanitizeLocalUrlValidUrlsDataProvider() {
+               $host = 'localhost';
+               $subDirectory = '/cms/';
+
+               return array(
+                       $subDirectory . 'typo3/alt_intro.php' => array(
+                               $subDirectory . 'typo3/alt_intro.php',
+                               $host,
+                               $subDirectory,
+                       ),
+                       $subDirectory . 'index.php' => array(
+                               $subDirectory . 'index.php',
+                               $host,
+                               $subDirectory,
+                       ),
+                       'http://' . $host . '/typo3/alt_intro.php' => array(
+                               'http://' . $host . '/typo3/alt_intro.php',
+                               $host,
+                               '',
+                       ),
+                       'http://' . $host . $subDirectory . 'typo3/alt_intro.php' => array(
+                               'http://' . $host . $subDirectory . 'typo3/alt_intro.php',
+                               $host,
+                               $subDirectory,
+                       ),
+               );
+       }
+
+       /**
+        * @test
+        * @param string $url
+        * @param string $host
+        * @param string $subDirectory
+        * @dataProvider sanitizeLocalUrlValidUrlsDataProvider
+        */
+       public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url, $host, $subDirectory) {
+               $_SERVER['HTTP_HOST'] = $host;
+               $_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php';
+               GeneralUtility::flushInternalRuntimeCaches();
                $this->assertEquals($url, GeneralUtility::sanitizeLocalUrl($url));
        }
 
        /**
         * @test
-        * @dataProvider sanitizeLocalUrlValidUrlDataProvider
+        * @param string $url
+        * @param string $host
+        * @param string $subDirectory
+        * @dataProvider sanitizeLocalUrlValidUrlsDataProvider
         */
-       public function sanitizeLocalUrlAcceptsEncodedValidUrls($url) {
+       public function sanitizeLocalUrlAcceptsEncodedValidUrls($url, $host, $subDirectory) {
+               $_SERVER['HTTP_HOST'] = $host;
+               $_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php';
+               GeneralUtility::flushInternalRuntimeCaches();
                $this->assertEquals(rawurlencode($url), GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
        }