[FEATURE] Setting a scheme for a URI
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Sat, 11 Jun 2011 14:02:03 +0000 (16:02 +0200)
committerBastian Waidelich <bastian@typo3.org>
Tue, 13 Sep 2011 13:01:42 +0000 (15:01 +0200)
Allow specifying an URI-scheme like "https".
Usage:
$uriBuilder
  ->setCreateAbsoluteUri(TRUE)
  ->setAbsoluteUriScheme('https')

Change-Id: Ifff775b188328b7caf5bdf4bde8d5f5b8834e3a9
Resolves: #9969

typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php
typo3/sysext/extbase/Tests/Unit/MVC/Web/Routing/UriBuilderTest.php

index f2f6483..98cf4a5 100644 (file)
@@ -66,6 +66,11 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
        protected $createAbsoluteUri = FALSE;
 
        /**
+        * @var string
+        */
+       protected $absoluteUriScheme = NULL;
+
+       /**
         * @var boolean
         */
        protected $addQueryString = FALSE;
@@ -198,7 +203,7 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
        /**
         * Specifies the format of the target (e.g. "html" or "xml")
         *
-        * @param string $section
+        * @param string $format
         * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
         * @api
         */
@@ -236,6 +241,24 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
        }
 
        /**
+        * @return string
+        */
+       public function getAbsoluteUriScheme() {
+               return $this->absoluteUriScheme;
+       }
+
+       /**
+        * Sets the scheme that should be used for absolute URIs in FE mode
+        *
+        * @param string $absoluteUriScheme the scheme to be used for absolute URIs
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        */
+       public function setAbsoluteUriScheme($absoluteUriScheme) {
+               $this->absoluteUriScheme = $absoluteUriScheme;
+               return $this;
+       }
+
+       /**
         * If set, the current query parameters will be merged with $this->arguments. Defaults to FALSE.
         *
         * @param boolean $addQueryString
@@ -544,6 +567,9 @@ class Tx_Extbase_MVC_Web_Routing_UriBuilder {
 
                if ($this->createAbsoluteUri === TRUE) {
                        $typolinkConfiguration['forceAbsoluteUrl'] = TRUE;
+                       if ($this->absoluteUriScheme !== NULL) {
+                               $typolinkConfiguration['forceAbsoluteUrl.']['scheme'] = $this->absoluteUriScheme;
+                       }
                }
 
                $uri = $this->contentObject->typoLink_URL($typolinkConfiguration);
index 48f3d87..b1b936a 100644 (file)
@@ -91,6 +91,7 @@ class Tx_Extbase_Tests_Unit_MVC_Web_Routing_UriBuilderTest extends Tx_Extbase_Te
                        ->setSection('testSection')
                        ->setFormat('testFormat')
                        ->setCreateAbsoluteUri(TRUE)
+                       ->setAbsoluteUriScheme('https')
                        ->setAddQueryString(TRUE)
                        ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
                        ->setArgumentPrefix('testArgumentPrefix')
@@ -104,6 +105,7 @@ class Tx_Extbase_Tests_Unit_MVC_Web_Routing_UriBuilderTest extends Tx_Extbase_Te
                $this->assertEquals('testSection', $this->uriBuilder->getSection());
                $this->assertEquals('testFormat', $this->uriBuilder->getFormat());
                $this->assertEquals(TRUE, $this->uriBuilder->getCreateAbsoluteUri());
+               $this->assertEquals('https', $this->uriBuilder->getAbsoluteUriScheme());
                $this->assertEquals(TRUE, $this->uriBuilder->getAddQueryString());
                $this->assertEquals(array('test' => 'addQueryStringExcludeArguments'), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
                $this->assertEquals('testArgumentPrefix', $this->uriBuilder->getArgumentPrefix());
@@ -354,6 +356,40 @@ class Tx_Extbase_Tests_Unit_MVC_Web_Routing_UriBuilderTest extends Tx_Extbase_Te
        /**
         * @test
         */
+       public function buildFrontendUriSetsAbsoluteUriSchemeIfSpecified() {
+               $uriBuilder = $this->getAccessibleMock('Tx_Extbase_MVC_Web_Routing_UriBuilder', array('buildTypolinkConfiguration'));
+               $uriBuilder->_set('contentObject', $this->mockContentObject);
+               $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
+
+               $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar', 'forceAbsoluteUrl' => TRUE, 'forceAbsoluteUrl.' => array('scheme' => 'someScheme')))->will($this->returnValue('http://baseuri/relative/uri'));
+               $uriBuilder->setCreateAbsoluteUri(TRUE);
+               $uriBuilder->setAbsoluteUriScheme('someScheme');
+
+               $expectedResult = 'http://baseuri/relative/uri';
+               $actualResult = $uriBuilder->buildFrontendUri();
+               $this->assertSame($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function buildFrontendUriDoesNotSetAbsoluteUriSchemeIfCreateAbsoluteUriIsFalse() {
+               $uriBuilder = $this->getAccessibleMock('Tx_Extbase_MVC_Web_Routing_UriBuilder', array('buildTypolinkConfiguration'));
+               $uriBuilder->_set('contentObject', $this->mockContentObject);
+               $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('foo' => 'bar')));
+
+               $this->mockContentObject->expects($this->once())->method('typoLink_URL')->with(array('foo' => 'bar'))->will($this->returnValue('http://baseuri/relative/uri'));
+               $uriBuilder->setCreateAbsoluteUri(FALSE);
+               $uriBuilder->setAbsoluteUriScheme('someScheme');
+
+               $expectedResult = 'http://baseuri/relative/uri';
+               $actualResult = $uriBuilder->buildFrontendUri();
+               $this->assertSame($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
        public function resetSetsAllOptionsToTheirDefaultValue() {
                $this->uriBuilder
                        ->setArguments(array('test' => 'arguments'))