[BUGFIX] Streamline Unit Tests for GeneralUtility::sanitizeLocalUrl 88/42988/2
authorNicole Cordes <typo3@cordes.co>
Wed, 2 Sep 2015 13:06:12 +0000 (15:06 +0200)
committerNicole Cordes <typo3@cordes.co>
Thu, 3 Sep 2015 06:48:51 +0000 (08:48 +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/42988
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index dacbfcf..085e3e8 100644 (file)
@@ -2014,14 +2014,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 = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
                $typo3SiteUrl = Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
                $typo3RequestHost = Utility\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'),
@@ -2039,17 +2040,78 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 
        /**
         * @test
-        * @dataProvider sanitizeLocalUrlValidUrlDataProvider
+        * @param string $path
+        * @dataProvider sanitizeLocalUrlValidPathsDataProvider
+        */
+       public function sanitizeLocalUrlAcceptsNotEncodedValidPaths($path) {
+               $this->assertEquals($path, Utility\GeneralUtility::sanitizeLocalUrl($path));
+       }
+
+       /**
+        * @test
+        * @param string $path
+        * @dataProvider sanitizeLocalUrlValidPathsDataProvider
+        */
+       public function sanitizeLocalUrlAcceptsEncodedValidPaths($path) {
+               $this->assertEquals(rawurlencode($path), Utility\GeneralUtility::sanitizeLocalUrl(rawurlencode($path)));
+       }
+
+       /**
+        * Data provider for valid sanitizeLocalUrl's
+        *
+        * @return array Valid 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) {
+       public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url, $host, $subDirectory) {
+               $_SERVER['HTTP_HOST'] = $host;
+               $_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php';
                $this->assertEquals($url, Utility\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';
                $this->assertEquals(rawurlencode($url), Utility\GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
        }