[TASK] Add unit tests for forceAbsoluteUrl() 78/32678/2
authorMarkus Klein <klein.t3@reelworx.at>
Sun, 27 Jul 2014 23:00:41 +0000 (01:00 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 9 Sep 2014 11:10:36 +0000 (13:10 +0200)
ContentObjectRenderer::forceAbsoluteUrl() is not covered by unit tests
at all.
Add unit tests and improve mocking in other existing tests.

Resolves: #60629
Releases: 6.3, 6.2
Change-Id: Ie3c03b0cff2118bf7ef38ca03135f2a0a6f120cd
Reviewed-on: http://review.typo3.org/32678
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 9460da3..a889079 100644 (file)
@@ -6219,6 +6219,7 @@ class ContentObjectRenderer {
                                        'host' => $matches[3],
                                        'path' => $matches[4]
                                );
+                               $isUrlModified = FALSE;
                                // Set scheme and host if not yet part of the URL:
                                if (empty($urlParts['host'])) {
                                        $urlParts['scheme'] = 'http';
index 96c190a..429a917 100644 (file)
@@ -54,7 +54,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['TYPO3\\CMS\\Core\\Charset\\CharsetConverter_utils'] = 'mbstring';
                $this->cObj = $this->getAccessibleMock(
                        'TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer',
-                       array('getResourceFactory')
+                       array('getResourceFactory', 'getEnvironmentVariable')
                );
                $this->cObj->start(array(), 'tt_content');
        }
@@ -199,7 +199,9 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getQueryArgumentsExcludesParameters() {
-               $_SERVER['QUERY_STRING'] = 'key1=value1' . '&key2=value2' . '&key3[key31]=value31' . '&key3[key32][key321]=value321' . '&key3[key32][key322]=value322';
+               $this->cObj->expects($this->any())->method('getEnvironmentVariable')->with($this->equalTo('QUERY_STRING'))->will(
+                       $this->returnValue('key1=value1&key2=value2&key3[key31]=value31&key3[key32][key321]=value321&key3[key32][key322]=value322')
+               );
                $getQueryArgumentsConfiguration = array();
                $getQueryArgumentsConfiguration['exclude'] = array();
                $getQueryArgumentsConfiguration['exclude'][] = 'key1';
@@ -242,7 +244,9 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getQueryArgumentsOverrulesSingleParameter() {
-               $_SERVER['QUERY_STRING'] = 'key1=value1';
+               $this->cObj->expects($this->any())->method('getEnvironmentVariable')->with($this->equalTo('QUERY_STRING'))->will(
+                       $this->returnValue('key1=value1')
+               );
                $getQueryArgumentsConfiguration = array();
                $overruleArguments = array(
                        // Should be overridden
@@ -300,7 +304,9 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getQueryArgumentsOverrulesMultiDimensionalForcedParameters() {
-               $_SERVER['QUERY_STRING'] = 'key1=value1' . '&key2=value2' . '&key3[key31]=value31' . '&key3[key32][key321]=value321' . '&key3[key32][key322]=value322';
+               $this->cObj->expects($this->any())->method('getEnvironmentVariable')->with($this->equalTo('QUERY_STRING'))->will(
+                       $this->returnValue('key1=value1&key2=value2&key3[key31]=value31&key3[key32][key321]=value321&key3[key32][key322]=value322')
+               );
                $_POST = array(
                        'key1' => 'value1',
                        'key2' => 'value2',
@@ -1688,7 +1694,9 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function getDataWithTypeGetindpenv() {
-               $this->assertEquals(PATH_thisScript, $this->cObj->getData('getindpenv:SCRIPT_FILENAME'));
+               $this->cObj->expects($this->once())->method('getEnvironmentVariable')
+                       ->with($this->equalTo('SCRIPT_FILENAME'))->will($this->returnValue('dummyPath'));
+               $this->assertEquals('dummyPath', $this->cObj->getData('getindpenv:SCRIPT_FILENAME'));
        }
 
        /**
@@ -2574,4 +2582,77 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertTrue(is_array($sourceConfiguration));
                return 'isGetOneSourceCollectionCalledCallback';
        }
+
+       /**
+        * @param string $expected The expected URL
+        * @param string $url The URL to parse and manipulate
+        * @param array $configuration The configuration array
+        * @test
+        * @dataProvider forceAbsoluteUrlReturnsCorrectAbsoluteUrlDataProvider
+        */
+       public function forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration) {
+               // Force hostname
+               $this->cObj->expects($this->any())->method('getEnvironmentVariable')->will($this->returnValueMap(
+                       array(
+                               array('HTTP_HOST', 'localhost'),
+                               array('TYPO3_SITE_PATH', '/'),
+                       )
+               ));
+
+               $this->assertEquals($expected, $this->cObj->_call('forceAbsoluteUrl', $url, $configuration));
+       }
+
+       /**
+        * @return array The test data for forceAbsoluteUrlReturnsAbsoluteUrl
+        */
+       public function forceAbsoluteUrlReturnsCorrectAbsoluteUrlDataProvider() {
+               return array(
+                       'Missing forceAbsoluteUrl leaves URL untouched' => array(
+                               'foo',
+                               'foo',
+                               array()
+                       ),
+                       'Absolute URL stays unchanged' => array(
+                               'http://example.org/',
+                               'http://example.org/',
+                               array(
+                                       'forceAbsoluteUrl' => '1'
+                               )
+                       ),
+                       'Absolute URL stays unchanged 2' => array(
+                               'http://example.org/resource.html',
+                               'http://example.org/resource.html',
+                               array(
+                                       'forceAbsoluteUrl' => '1'
+                               )
+                       ),
+                       'Scheme and host w/o ending slash stays unchanged' => array(
+                               'http://example.org',
+                               'http://example.org',
+                               array(
+                                       'forceAbsoluteUrl' => '1'
+                               )
+                       ),
+                       'Scheme can be forced' => array(
+                               'typo3://example.org',
+                               'http://example.org',
+                               array(
+                                       'forceAbsoluteUrl' => '1',
+                                       'forceAbsoluteUrl.' => array(
+                                               'scheme' => 'typo3'
+                                       )
+                               )
+                       ),
+                       'Scheme can be forced with relative path' => array(
+                               'typo3://localhost/fileadmin/dummy.txt',
+                               '/fileadmin/dummy.txt', // this leading slash is weird, but we need it to really get an absolute link
+                               array(
+                                       'forceAbsoluteUrl' => '1',
+                                       'forceAbsoluteUrl.' => array(
+                                               'scheme' => 'typo3'
+                                       )
+                               )
+                       ),
+               );
+       }
 }