[+FEATURE] Fluid (ViewHelpers): prepend URIs with scheme if it's not specified in...
authorBastian Waidelich <bastian@typo3.org>
Thu, 30 Dec 2010 17:17:43 +0000 (17:17 +0000)
committerBastian Waidelich <bastian@typo3.org>
Thu, 30 Dec 2010 17:17:43 +0000 (17:17 +0000)
Resolves #10401

typo3/sysext/fluid/Classes/ViewHelpers/Link/ExternalViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Uri/ExternalViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Link/ExternalViewHelperTest.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Uri/ExternalViewHelperTest.php

index 5ca3ee1..bf54b31 100644 (file)
@@ -59,11 +59,16 @@ class Tx_Fluid_ViewHelpers_Link_ExternalViewHelper extends Tx_Fluid_Core_ViewHel
 
        /**
         * @param string $uri the URI that will be put in the href attribute of the rendered link tag
+        * @param string $defaultScheme scheme the href attribute will be prefixed with if specified $uri does not contain a scheme already
         * @return string Rendered link
         * @author Bastian Waidelich <bastian@typo3.org>
         * @api
         */
-       public function render($uri) {
+       public function render($uri, $defaultScheme = 'http') {
+               $scheme = parse_url($uri, PHP_URL_SCHEME);
+               if ($scheme === NULL && $defaultScheme !== '') {
+                       $uri = $defaultScheme . '://' . $uri;
+               }
                $this->tag->addAttribute('href', $uri);
                $this->tag->setContent($this->renderChildren());
 
index 8261795..1d87418 100644 (file)
@@ -40,11 +40,16 @@ class Tx_Fluid_ViewHelpers_Uri_ExternalViewHelper extends Tx_Fluid_Core_ViewHelp
 
        /**
         * @param string $uri the target URI
+        * @param string $defaultScheme scheme the result will be prefixed with if specified $uri does not contain a scheme already
         * @return string rendered URI
         * @author Bastian Waidelich <bastian@typo3.org>
         * @api
         */
-       public function render($uri) {
+       public function render($uri, $defaultScheme = 'http') {
+               $scheme = parse_url($uri, PHP_URL_SCHEME);
+               if ($scheme === NULL && $defaultScheme !== '') {
+                       $uri = $defaultScheme . '://' . $uri;
+               }
                return $uri;
        }
 }
index fa94d24..4130d11 100644 (file)
@@ -48,13 +48,64 @@ class Tx_Fluid_Tests_Unit_ViewHelpers_Link_ExternalViewHelperTest extends Tx_Flu
                $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
                $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'http://www.some-domain.tld');
                $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
-               $this->viewHelper->injectTagBuilder($mockTagBuilder);
+               $this->viewHelper->_set('tag', $mockTagBuilder);
 
                $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
 
                $this->viewHelper->initialize();
                $this->viewHelper->render('http://www.some-domain.tld');
        }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsHttpPrefixIfSpecifiedUriDoesNotContainScheme() {
+               $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute', 'setContent'));
+               $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
+               $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'http://www.some-domain.tld');
+               $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
+               $this->viewHelper->_set('tag', $mockTagBuilder);
+
+               $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
+
+               $this->viewHelper->initialize();
+               $this->viewHelper->render('www.some-domain.tld');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsSpecifiedSchemeIfUriDoesNotContainScheme() {
+               $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute', 'setContent'));
+               $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
+               $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'ftp://some-domain.tld');
+               $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
+               $this->viewHelper->_set('tag', $mockTagBuilder);
+
+               $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
+
+               $this->viewHelper->initialize();
+               $this->viewHelper->render('some-domain.tld', 'ftp');
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderDoesNotAddEmptyScheme() {
+               $mockTagBuilder = $this->getMock('Tx_Fluid_Core_ViewHelper_TagBuilder', array('setTagName', 'addAttribute', 'setContent'));
+               $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
+               $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'some-domain.tld');
+               $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
+               $this->viewHelper->_set('tag', $mockTagBuilder);
+
+               $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
+
+               $this->viewHelper->initialize();
+               $this->viewHelper->render('some-domain.tld', '');
+       }
 }
 
 ?>
\ No newline at end of file
index c06d397..59cb609 100644 (file)
@@ -51,6 +51,39 @@ class Tx_Fluid_Tests_Unit_ViewHelpers_Uri_ExternalViewHelperTest extends Tx_Flui
 
                $this->assertEquals('http://www.some-domain.tld', $actualResult);
        }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsHttpPrefixIfSpecifiedUriDoesNotContainScheme() {
+               $this->viewHelper->initialize();
+               $actualResult = $this->viewHelper->render('www.some-domain.tld');
+
+               $this->assertEquals('http://www.some-domain.tld', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderAddsSpecifiedSchemeIfUriDoesNotContainScheme() {
+               $this->viewHelper->initialize();
+               $actualResult = $this->viewHelper->render('some-domain.tld', 'ftp');
+
+               $this->assertEquals('ftp://some-domain.tld', $actualResult);
+       }
+
+       /**
+        * @test
+        * @author Bastian Waidelich <bastian@typo3.org>
+        */
+       public function renderDoesNotAddEmptyScheme() {
+               $this->viewHelper->initialize();
+               $actualResult = $this->viewHelper->render('some-domain.tld', '');
+
+               $this->assertEquals('some-domain.tld', $actualResult);
+       }
 }