!!! [~TASK] Extbase (MVC): Rewrite of URIBuilder. Renaming to "UriBuilder" and big...
authorBastian Waidelich <bastian@typo3.org>
Mon, 31 Aug 2009 19:04:51 +0000 (19:04 +0000)
committerBastian Waidelich <bastian@typo3.org>
Mon, 31 Aug 2009 19:04:51 +0000 (19:04 +0000)
typo3/sysext/extbase/Classes/MVC/Controller/AbstractController.php
typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php
typo3/sysext/extbase/Classes/MVC/Controller/ControllerContext.php
typo3/sysext/extbase/Classes/MVC/Web/Routing/URIBuilder_renamed.php [deleted file]
typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php [new file with mode: 0644]
typo3/sysext/extbase/Tests/MVC/Controller/AbstractController_testcase.php
typo3/sysext/extbase/Tests/MVC/Web/Routing/URIBuilder_testcase.php
typo3/sysext/extbase/ext_autoload.php

index 07a3af6..d305999 100755 (executable)
@@ -40,9 +40,9 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
        protected $objectManager;
 
        /**
-        * @var Tx_Extbase_MVC_Web_Routing_URIBuilder
+        * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
         */
-       protected $URIBuilder;
+       protected $uriBuilder;
 
        /**
         * @var string Key of the extension this controller belongs to
@@ -188,8 +188,8 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
                $this->request->setDispatched(TRUE);
                $this->response = $response;
 
-               $this->URIBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Routing_URIBuilder');
-               $this->URIBuilder->setRequest($request);
+               $this->uriBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Routing_UriBuilder');
+               $this->uriBuilder->setRequest($request);
 
                $this->initializeControllerArgumentsBaseValidators();
                $this->mapRequestArgumentsToControllerArguments();
@@ -211,7 +211,7 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
                if ($this->argumentsMappingResults !== NULL) {
                        $controllerContext->setArgumentsMappingResults($this->argumentsMappingResults);
                }
-               $controllerContext->setURIBuilder($this->URIBuilder);
+               $controllerContext->setUriBuilder($this->uriBuilder);
                return $controllerContext;
        }
 
@@ -263,7 +263,9 @@ abstract class Tx_Extbase_MVC_Controller_AbstractController implements Tx_Extbas
                        $pageUid = $GLOBALS['TSFE']->id;
                }
 
-               $uri = $this->URIBuilder->URIFor($pageUid, $actionName, $arguments, $controllerName, $extensionName);
+               $this->uriBuilder->reset();
+               $this->uriBuilder->setTargetPageUid($pageUid);
+               $uri = $this->uriBuilder->uriFor($actionName, $arguments, $controllerName, $extensionName);
                $this->redirectToURI($uri, $delay, $statusCode);
        }
 
index 61ab3b0..f70e228 100644 (file)
@@ -118,8 +118,8 @@ class Tx_Extbase_MVC_Controller_ActionController extends Tx_Extbase_MVC_Controll
                $this->request->setDispatched(TRUE);
                $this->response = $response;
 
-               $this->URIBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Routing_URIBuilder');
-               $this->URIBuilder->setRequest($request);
+               $this->uriBuilder = t3lib_div::makeInstance('Tx_Extbase_MVC_Web_Routing_UriBuilder');
+               $this->uriBuilder->setRequest($request);
 
                $this->actionMethodName = $this->resolveActionMethodName();
 
index a8b46af..5ce0ace 100644 (file)
@@ -57,9 +57,9 @@ class Tx_Extbase_MVC_Controller_ControllerContext {
        protected $argumentsMappingResults;
 
        /**
-        * @var Tx_Extbase_MVC_Web_Routing_URIBuilder
+        * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
         */
-       protected $URIBuilder;
+       protected $uriBuilder;
 
        /**
         * Set the request of the controller
@@ -142,19 +142,19 @@ class Tx_Extbase_MVC_Controller_ControllerContext {
        }
 
        /**
-        * Tx_Extbase_MVC_Web_Routing_URIBuilder $URIBuilder
+        * Tx_Extbase_MVC_Web_Routing_UriBuilder $uriBuilder
         * @return void
         */
-       public function setURIBuilder(Tx_Extbase_MVC_Web_Routing_URIBuilder $URIBuilder) {
-               $this->URIBuilder = $URIBuilder;
+       public function setUriBuilder(Tx_Extbase_MVC_Web_Routing_UriBuilder $uriBuilder) {
+               $this->uriBuilder = $uriBuilder;
        }
 
        /**
-        * @return Tx_Extbase_MVC_Web_Routing_URIBuilder
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder
         * @api
         */
-       public function getURIBuilder() {
-               return $this->URIBuilder;
+       public function getUriBuilder() {
+               return $this->uriBuilder;
        }
 
 }
diff --git a/typo3/sysext/extbase/Classes/MVC/Web/Routing/URIBuilder_renamed.php b/typo3/sysext/extbase/Classes/MVC/Web/Routing/URIBuilder_renamed.php
deleted file mode 100644 (file)
index 268fb66..0000000
+++ /dev/null
@@ -1,503 +0,0 @@
-<?php
-/*                                                                        *
- * This script is part of the TYPO3 project - inspiring people to share!  *
- *                                                                        *
- * TYPO3 is free software; you can redistribute it and/or modify it under *
- * the terms of the GNU General Public License version 2 as published by  *
- * the Free Software Foundation.                                          *
- *                                                                        *
- * This script is distributed in the hope that it will be useful, but     *
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
- * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
- * Public License for more details.                                       *
- *                                                                        */
-
-/**
- * An URI Builder
- *
- * @package Extbase
- * @subpackage MVC\Web\Routing
- * @version $Id$
- */
-class Tx_Extbase_MVC_Web_Routing_UriBuilder {
-
-       /**
-        * An instance of tslib_cObj
-        *
-        * @var tslib_cObj
-        */
-       protected $contentObject;
-
-       /**
-        * @var Tx_Extbase_MVC_Web_Request
-        */
-       protected $request;
-
-       /**
-        * @var array
-        */
-       protected $arguments = array();
-
-       /**
-        * @var string
-        */
-       protected $section = '';
-
-       /**
-        * @var boolean
-        */
-       protected $createAbsoluteUri = FALSE;
-
-       /**
-        * @var boolean
-        */
-       protected $addQueryString = FALSE;
-
-       /**
-        * @var array
-        */
-       protected $argumentsToBeExcludedFromQueryString = array();
-
-       /**
-        * @var boolean
-        */
-       protected $linkAccessRestrictedPages = FALSE;
-
-       /**
-        * @var integer
-        */
-       protected $targetPageUid = NULL;
-
-       /**
-        * @var integer
-        */
-       protected $targetPageType = 0;
-
-       /**
-        * @var boolean
-        */
-       protected $noCache = FALSE;
-
-       /**
-        * @var boolean
-        */
-       protected $useCacheHash = TRUE;
-
-
-       /**
-        * Constructs this URI Helper
-        *
-        * @param tslib_cObj $contentObject
-        */
-       public function __construct(tslib_cObj $contentObject = NULL) {
-               $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
-       }
-
-       /**
-        * Sets the current request
-        *
-        * @param Tx_Extbase_MVC_Web_Request $request
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        */
-       public function setRequest(Tx_Extbase_MVC_Web_Request $request) {
-               $this->request = $request;
-               return $this;
-       }
-
-       /**
-        * @return Tx_Extbase_MVC_Web_Request
-        */
-       public function getRequest() {
-               return $this->request;
-       }
-
-       /**
-        * Additional query parameters.
-        * If you want to "prefix" arguments, you can pass in multidimensional arrays:
-        * array('prefix1' => array('foo' => 'bar')) gets "&prefix1[foo]=bar"
-        *
-        * @param array $arguments
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setArguments(array $arguments) {
-               $this->arguments = $arguments;
-               return $this;
-       }
-
-       /**
-        * @return array
-        * #@api
-        */
-       public function getArguments() {
-               return $this->arguments;
-       }
-
-       /**
-        * If specified, adds a given HTML anchor to the URI (#...)
-        *
-        * @param string $section
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setSection($section) {
-               $this->section = $section;
-               return $this;
-       }
-
-       /**
-        * @return string
-        * @api
-        */
-       public function getSection() {
-               return $this->section;
-       }
-
-       /**
-        * If set, the URI is prepended with the current base URI. Defaults to FALSE.
-        *
-        * @param boolean $createAbsoluteUri
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setCreateAbsoluteUri($createAbsoluteUri) {
-               $this->createAbsoluteUri = $createAbsoluteUri;
-               return $this;
-       }
-
-       /**
-        * @return boolean
-        * @api
-        */
-       public function getCreateAbsoluteUri() {
-               return $this->createAbsoluteUri;
-       }
-
-       /**
-        * If set, the current query parameters will be merged with $this->arguments. Defaults to FALSE.
-        *
-        * @param boolean $addQueryString
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        * @see TSref/typolink.addQueryString
-        */
-       public function setAddQueryString($addQueryString) {
-               $this->addQueryString = (boolean)$addQueryString;
-               return $this;
-       }
-
-       /**
-        * @return boolean
-        * @api
-        */
-       public function getAddQueryString() {
-               return $this->addQueryString;
-       }
-
-       /**
-        * A list of arguments to be excluded from the query parameters
-        * Only active if addQueryString is set
-        *
-        * @param array $argumentsToBeExcludedFromQueryString
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        * @see TSref/typolink.addQueryString.exclude
-        * @see setAddQueryString()
-        */
-       public function setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString) {
-               $this->argumentsToBeExcludedFromQueryString = $argumentsToBeExcludedFromQueryString;
-               return $this;
-       }
-
-       /**
-        * @return array
-        * @api
-        */
-       public function getArgumentsToBeExcludedFromQueryString() {
-               return $this->argumentsToBeExcludedFromQueryString;
-       }
-
-       /**
-        * If set, URIs for pages without access permissions will be created
-        *
-        * @param boolean $linkAccessRestrictedPages
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setLinkAccessRestrictedPages($linkAccessRestrictedPages) {
-               $this->linkAccessRestrictedPages = (boolean)$linkAccessRestrictedPages;
-               return $this;
-       }
-
-       /**
-        * @return boolean
-        * @api
-        */
-       public function getLinkAccessRestrictedPages() {
-               return $this->linkAccessRestrictedPages;
-       }
-
-       /**
-        * Uid of the target page
-        *
-        * @param integer $pageUid
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setTargetPageUid($targetPageUid) {
-               $this->targetPageUid = $targetPageUid;
-               return $this;
-       }
-
-       /**
-        * returns $this->targetPageUid.
-        *
-        * @return integer
-        * @api
-        */
-       public function getTargetPageUid() {
-               return $this->targetPageUid;
-       }
-
-       /**
-        * Sets the page type of the target URI. Defaults to 0
-        *
-        * @param integer $pageType
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setTargetPageType($targetPageType) {
-               $this->targetPageType = (integer)$targetPageType;
-               return $this;
-       }
-
-       /**
-        * @return integer
-        * @api
-        */
-       public function getTargetPageType() {
-               return $this->targetPageType;
-       }
-
-       /**
-        * by default FALSE; if TRUE, &no_cache=1 will be appended to the URI
-        * This overrules the useCacheHash setting
-        *
-        * @param boolean $noCache
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setNoCache($noCache) {
-               $this->noCache = (boolean)$noCache;
-               return $this;
-       }
-
-       /**
-        * @return boolean
-        * @api
-        */
-       public function getNoCache() {
-               return $this->noCache;
-       }
-
-       /**
-        * by default TRUE; if FALSE, no cHash parameter will be appended to the URI
-        * If noCache is set, this setting will be ignored.
-        *
-        * @param boolean $useCacheHash
-        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
-        * @api
-        */
-       public function setUseCacheHash($useCacheHash) {
-               $this->useCacheHash = (boolean)$useCacheHash;
-               return $this;
-       }
-
-       /**
-        * @return boolean
-        * @api
-        */
-       public function getUseCacheHash() {
-               return $this->useCacheHash;
-       }
-
-       /**
-        * Creates an URI by making use of the typolink mechanism.
-        *
-        * @param integer $pageUid uid of the target page
-        * @param string $actionName Name of the action to be called
-        * @param array $controllerArguments Additional query parameters, will be "namespaced"
-        * @param string $controllerName Name of the target controller
-        * @param string $extensionName Name of the target extension, without underscores. If NULL current ExtensionName is used.
-        * @param string $pluginName Name of the target plugin.  If NULL current PluginName is used.
-        * @return string the typolink URI
-        * @api
-        */
-       public function uriFor($actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL) {
-               if ($actionName !== NULL) {
-                       $controllerArguments['action'] = $actionName;
-               }
-               if ($controllerName !== NULL) {
-                       $controllerArguments['controller'] = $controllerName;
-               } else {
-                       $controllerArguments['controller'] = $this->request->getControllerName();
-               }
-               if ($extensionName === NULL) {
-                       $extensionName = $this->request->getControllerExtensionName();
-               }
-               if ($pluginName === NULL) {
-                       $pluginName = $this->request->getPluginName();
-               }
-               $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
-               $prefixedControllerArguments = (count($controllerArguments) > 0) ? array($argumentPrefix => $controllerArguments) : array();
-               $this->arguments = t3lib_div::array_merge_recursive_overrule($this->arguments, $prefixedControllerArguments);
-
-               return $this->build();
-       }
-
-       /**
-        * Builds the URI
-        * Depending on the current context this calls buildBackendUri() or buildFrontendUri()
-        *
-        * @return string The URI
-        * @api
-        * @see buildBackendUri()
-        * @see buildFrontendUri()
-        */
-       public function build() {
-               if (TYPO3_MODE === 'BE') {
-                       return $this->buildBackendUri();
-               } else {
-                       return $this->buildFrontendUri();
-               }
-       }
-
-       /**
-        * Builds the URI, backend flavour
-        * The resulting URI is relative and starts with "mod.php".
-        * The settings pageUid, pageType, noCache, useCacheHash, linkAccessRestrictedPages, createAbsoluteUri
-        * will be ignored in the backend.
-        *
-        * @return string The URI
-        */
-       public function buildBackendUri() {
-               if ($this->addQueryString === TRUE) {
-                       $arguments = t3lib_div::_GET();
-                       foreach($this->argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
-                               unset($arguments[$argumentToBeExcluded]);
-                       }
-               } else {
-                       $arguments = array(
-                               'M' => t3lib_div::_GET('M'),
-                               'id' => t3lib_div::_GET('id')
-                       );
-               }
-               $arguments = t3lib_div::array_merge_recursive_overrule($arguments, $this->arguments);
-               $arguments = $this->convertDomainObjectsToIdentityArrays($arguments);
-               $uri = 'mod.php?' . http_build_query($arguments, NULL, '&');
-               if ($this->section !== '') {
-                       $uri .= '#' . $this->section;
-               }
-               return $uri;
-       }
-
-       /**
-        * Builds the URI, frontend flavour
-        *
-        * @return string The URI
-        * @see buildTypolinkConfiguration()
-        */
-       public function buildFrontendUri() {
-               $typolinkConfiguration = $this->buildTypolinkConfiguration();
-
-               $uri = $this->contentObject->typoLink_URL($typolinkConfiguration);
-               if ($this->createAbsoluteUri === TRUE) {
-                       $uri = $this->request->getBaseURI() . $uri;
-               }
-               return $uri;
-       }
-
-       /**
-        * Resets all UriBuilder options to their default value
-        *
-        * @return void
-        * @api
-        */
-       public function reset() {
-               $this->arguments = array();
-               $this->section = '';
-               $this->createAbsoluteUri = FALSE;
-               $this->addQueryString = FALSE;
-               $this->argumentsToBeExcludedFromQueryString = array();
-               $this->linkAccessRestrictedPages = FALSE;
-               $this->targetPageUid = NULL;
-               $this->targetPageType = 0;
-               $this->noCache = FALSE;
-               $this->useCacheHash = TRUE;
-       }
-
-       /**
-        * Builds a TypoLink configuration array from the current settings
-        *
-        * @return array typolink configuration array
-        * @see TSref/typolink
-        */
-       protected function buildTypolinkConfiguration() {
-               $typolinkConfiguration = array();
-
-               $typolinkConfiguration['parameter'] = $this->targetPageUid !== NULL ? $this->targetPageUid : $GLOBALS['TSFE']->id;
-               if ($this->targetPageType !== 0) {
-                       $typolinkConfiguration['parameter'] .= ',' . $this->targetPageType;
-               }
-
-               if (count($this->arguments) > 0) {
-                       $arguments = $this->convertDomainObjectsToIdentityArrays($this->arguments);
-                       $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments, NULL, '&');
-               }
-
-               if ($this->addQueryString === TRUE) {
-                       $typolinkConfiguration['addQueryString'] = 1;
-                       if (count($this->argumentsToBeExcludedFromQueryString) > 0) {
-                               $typolinkConfiguration['addQueryString.'] = array(
-                                       'exclude' => implode(',', $this->argumentsToBeExcludedFromQueryString)
-                               );
-                       }
-               }
-
-               if ($this->noCache === TRUE) {
-                       $typolinkConfiguration['no_cache'] = 1;
-               } elseif ($this->useCacheHash) {
-                       $typolinkConfiguration['useCacheHash'] = 1;
-               }
-
-               if ($this->section !== '') {
-                       $typolinkConfiguration['section'] = $this->section;
-               }
-
-               if ($this->linkAccessRestrictedPages === TRUE) {
-                       $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
-               }
-
-               return $typolinkConfiguration;
-       }
-
-       /**
-        * Recursively iterates through the specified arguments and turns instances of type Tx_Extbase_DomainObject_AbstractEntity
-        * into an arrays containing the uid of the domain object.
-        *
-        * @param array $arguments The arguments to be iterated
-        * @return array The modified arguments array
-        */
-       protected function convertDomainObjectsToIdentityArrays(array $arguments) {
-               foreach ($arguments as $argumentKey => $argumentValue) {
-                       if ($argumentValue instanceof Tx_Extbase_DomainObject_AbstractEntity) {
-                               $arguments[$argumentKey] = array('uid' => $argumentValue->getUid());
-                       } elseif (is_array($argumentValue)) {
-                               $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
-                       }
-               }
-               return $arguments;
-       }
-
-}
-?>
\ No newline at end of file
diff --git a/typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php b/typo3/sysext/extbase/Classes/MVC/Web/Routing/UriBuilder.php
new file mode 100644 (file)
index 0000000..268fb66
--- /dev/null
@@ -0,0 +1,503 @@
+<?php
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * An URI Builder
+ *
+ * @package Extbase
+ * @subpackage MVC\Web\Routing
+ * @version $Id$
+ */
+class Tx_Extbase_MVC_Web_Routing_UriBuilder {
+
+       /**
+        * An instance of tslib_cObj
+        *
+        * @var tslib_cObj
+        */
+       protected $contentObject;
+
+       /**
+        * @var Tx_Extbase_MVC_Web_Request
+        */
+       protected $request;
+
+       /**
+        * @var array
+        */
+       protected $arguments = array();
+
+       /**
+        * @var string
+        */
+       protected $section = '';
+
+       /**
+        * @var boolean
+        */
+       protected $createAbsoluteUri = FALSE;
+
+       /**
+        * @var boolean
+        */
+       protected $addQueryString = FALSE;
+
+       /**
+        * @var array
+        */
+       protected $argumentsToBeExcludedFromQueryString = array();
+
+       /**
+        * @var boolean
+        */
+       protected $linkAccessRestrictedPages = FALSE;
+
+       /**
+        * @var integer
+        */
+       protected $targetPageUid = NULL;
+
+       /**
+        * @var integer
+        */
+       protected $targetPageType = 0;
+
+       /**
+        * @var boolean
+        */
+       protected $noCache = FALSE;
+
+       /**
+        * @var boolean
+        */
+       protected $useCacheHash = TRUE;
+
+
+       /**
+        * Constructs this URI Helper
+        *
+        * @param tslib_cObj $contentObject
+        */
+       public function __construct(tslib_cObj $contentObject = NULL) {
+               $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
+       }
+
+       /**
+        * Sets the current request
+        *
+        * @param Tx_Extbase_MVC_Web_Request $request
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        */
+       public function setRequest(Tx_Extbase_MVC_Web_Request $request) {
+               $this->request = $request;
+               return $this;
+       }
+
+       /**
+        * @return Tx_Extbase_MVC_Web_Request
+        */
+       public function getRequest() {
+               return $this->request;
+       }
+
+       /**
+        * Additional query parameters.
+        * If you want to "prefix" arguments, you can pass in multidimensional arrays:
+        * array('prefix1' => array('foo' => 'bar')) gets "&prefix1[foo]=bar"
+        *
+        * @param array $arguments
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setArguments(array $arguments) {
+               $this->arguments = $arguments;
+               return $this;
+       }
+
+       /**
+        * @return array
+        * #@api
+        */
+       public function getArguments() {
+               return $this->arguments;
+       }
+
+       /**
+        * If specified, adds a given HTML anchor to the URI (#...)
+        *
+        * @param string $section
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setSection($section) {
+               $this->section = $section;
+               return $this;
+       }
+
+       /**
+        * @return string
+        * @api
+        */
+       public function getSection() {
+               return $this->section;
+       }
+
+       /**
+        * If set, the URI is prepended with the current base URI. Defaults to FALSE.
+        *
+        * @param boolean $createAbsoluteUri
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setCreateAbsoluteUri($createAbsoluteUri) {
+               $this->createAbsoluteUri = $createAbsoluteUri;
+               return $this;
+       }
+
+       /**
+        * @return boolean
+        * @api
+        */
+       public function getCreateAbsoluteUri() {
+               return $this->createAbsoluteUri;
+       }
+
+       /**
+        * If set, the current query parameters will be merged with $this->arguments. Defaults to FALSE.
+        *
+        * @param boolean $addQueryString
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        * @see TSref/typolink.addQueryString
+        */
+       public function setAddQueryString($addQueryString) {
+               $this->addQueryString = (boolean)$addQueryString;
+               return $this;
+       }
+
+       /**
+        * @return boolean
+        * @api
+        */
+       public function getAddQueryString() {
+               return $this->addQueryString;
+       }
+
+       /**
+        * A list of arguments to be excluded from the query parameters
+        * Only active if addQueryString is set
+        *
+        * @param array $argumentsToBeExcludedFromQueryString
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        * @see TSref/typolink.addQueryString.exclude
+        * @see setAddQueryString()
+        */
+       public function setArgumentsToBeExcludedFromQueryString(array $argumentsToBeExcludedFromQueryString) {
+               $this->argumentsToBeExcludedFromQueryString = $argumentsToBeExcludedFromQueryString;
+               return $this;
+       }
+
+       /**
+        * @return array
+        * @api
+        */
+       public function getArgumentsToBeExcludedFromQueryString() {
+               return $this->argumentsToBeExcludedFromQueryString;
+       }
+
+       /**
+        * If set, URIs for pages without access permissions will be created
+        *
+        * @param boolean $linkAccessRestrictedPages
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setLinkAccessRestrictedPages($linkAccessRestrictedPages) {
+               $this->linkAccessRestrictedPages = (boolean)$linkAccessRestrictedPages;
+               return $this;
+       }
+
+       /**
+        * @return boolean
+        * @api
+        */
+       public function getLinkAccessRestrictedPages() {
+               return $this->linkAccessRestrictedPages;
+       }
+
+       /**
+        * Uid of the target page
+        *
+        * @param integer $pageUid
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setTargetPageUid($targetPageUid) {
+               $this->targetPageUid = $targetPageUid;
+               return $this;
+       }
+
+       /**
+        * returns $this->targetPageUid.
+        *
+        * @return integer
+        * @api
+        */
+       public function getTargetPageUid() {
+               return $this->targetPageUid;
+       }
+
+       /**
+        * Sets the page type of the target URI. Defaults to 0
+        *
+        * @param integer $pageType
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setTargetPageType($targetPageType) {
+               $this->targetPageType = (integer)$targetPageType;
+               return $this;
+       }
+
+       /**
+        * @return integer
+        * @api
+        */
+       public function getTargetPageType() {
+               return $this->targetPageType;
+       }
+
+       /**
+        * by default FALSE; if TRUE, &no_cache=1 will be appended to the URI
+        * This overrules the useCacheHash setting
+        *
+        * @param boolean $noCache
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setNoCache($noCache) {
+               $this->noCache = (boolean)$noCache;
+               return $this;
+       }
+
+       /**
+        * @return boolean
+        * @api
+        */
+       public function getNoCache() {
+               return $this->noCache;
+       }
+
+       /**
+        * by default TRUE; if FALSE, no cHash parameter will be appended to the URI
+        * If noCache is set, this setting will be ignored.
+        *
+        * @param boolean $useCacheHash
+        * @return Tx_Extbase_MVC_Web_Routing_UriBuilder the current UriBuilder to allow method chaining
+        * @api
+        */
+       public function setUseCacheHash($useCacheHash) {
+               $this->useCacheHash = (boolean)$useCacheHash;
+               return $this;
+       }
+
+       /**
+        * @return boolean
+        * @api
+        */
+       public function getUseCacheHash() {
+               return $this->useCacheHash;
+       }
+
+       /**
+        * Creates an URI by making use of the typolink mechanism.
+        *
+        * @param integer $pageUid uid of the target page
+        * @param string $actionName Name of the action to be called
+        * @param array $controllerArguments Additional query parameters, will be "namespaced"
+        * @param string $controllerName Name of the target controller
+        * @param string $extensionName Name of the target extension, without underscores. If NULL current ExtensionName is used.
+        * @param string $pluginName Name of the target plugin.  If NULL current PluginName is used.
+        * @return string the typolink URI
+        * @api
+        */
+       public function uriFor($actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL) {
+               if ($actionName !== NULL) {
+                       $controllerArguments['action'] = $actionName;
+               }
+               if ($controllerName !== NULL) {
+                       $controllerArguments['controller'] = $controllerName;
+               } else {
+                       $controllerArguments['controller'] = $this->request->getControllerName();
+               }
+               if ($extensionName === NULL) {
+                       $extensionName = $this->request->getControllerExtensionName();
+               }
+               if ($pluginName === NULL) {
+                       $pluginName = $this->request->getPluginName();
+               }
+               $argumentPrefix = strtolower('tx_' . $extensionName . '_' . $pluginName);
+               $prefixedControllerArguments = (count($controllerArguments) > 0) ? array($argumentPrefix => $controllerArguments) : array();
+               $this->arguments = t3lib_div::array_merge_recursive_overrule($this->arguments, $prefixedControllerArguments);
+
+               return $this->build();
+       }
+
+       /**
+        * Builds the URI
+        * Depending on the current context this calls buildBackendUri() or buildFrontendUri()
+        *
+        * @return string The URI
+        * @api
+        * @see buildBackendUri()
+        * @see buildFrontendUri()
+        */
+       public function build() {
+               if (TYPO3_MODE === 'BE') {
+                       return $this->buildBackendUri();
+               } else {
+                       return $this->buildFrontendUri();
+               }
+       }
+
+       /**
+        * Builds the URI, backend flavour
+        * The resulting URI is relative and starts with "mod.php".
+        * The settings pageUid, pageType, noCache, useCacheHash, linkAccessRestrictedPages, createAbsoluteUri
+        * will be ignored in the backend.
+        *
+        * @return string The URI
+        */
+       public function buildBackendUri() {
+               if ($this->addQueryString === TRUE) {
+                       $arguments = t3lib_div::_GET();
+                       foreach($this->argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
+                               unset($arguments[$argumentToBeExcluded]);
+                       }
+               } else {
+                       $arguments = array(
+                               'M' => t3lib_div::_GET('M'),
+                               'id' => t3lib_div::_GET('id')
+                       );
+               }
+               $arguments = t3lib_div::array_merge_recursive_overrule($arguments, $this->arguments);
+               $arguments = $this->convertDomainObjectsToIdentityArrays($arguments);
+               $uri = 'mod.php?' . http_build_query($arguments, NULL, '&');
+               if ($this->section !== '') {
+                       $uri .= '#' . $this->section;
+               }
+               return $uri;
+       }
+
+       /**
+        * Builds the URI, frontend flavour
+        *
+        * @return string The URI
+        * @see buildTypolinkConfiguration()
+        */
+       public function buildFrontendUri() {
+               $typolinkConfiguration = $this->buildTypolinkConfiguration();
+
+               $uri = $this->contentObject->typoLink_URL($typolinkConfiguration);
+               if ($this->createAbsoluteUri === TRUE) {
+                       $uri = $this->request->getBaseURI() . $uri;
+               }
+               return $uri;
+       }
+
+       /**
+        * Resets all UriBuilder options to their default value
+        *
+        * @return void
+        * @api
+        */
+       public function reset() {
+               $this->arguments = array();
+               $this->section = '';
+               $this->createAbsoluteUri = FALSE;
+               $this->addQueryString = FALSE;
+               $this->argumentsToBeExcludedFromQueryString = array();
+               $this->linkAccessRestrictedPages = FALSE;
+               $this->targetPageUid = NULL;
+               $this->targetPageType = 0;
+               $this->noCache = FALSE;
+               $this->useCacheHash = TRUE;
+       }
+
+       /**
+        * Builds a TypoLink configuration array from the current settings
+        *
+        * @return array typolink configuration array
+        * @see TSref/typolink
+        */
+       protected function buildTypolinkConfiguration() {
+               $typolinkConfiguration = array();
+
+               $typolinkConfiguration['parameter'] = $this->targetPageUid !== NULL ? $this->targetPageUid : $GLOBALS['TSFE']->id;
+               if ($this->targetPageType !== 0) {
+                       $typolinkConfiguration['parameter'] .= ',' . $this->targetPageType;
+               }
+
+               if (count($this->arguments) > 0) {
+                       $arguments = $this->convertDomainObjectsToIdentityArrays($this->arguments);
+                       $typolinkConfiguration['additionalParams'] = '&' . http_build_query($arguments, NULL, '&');
+               }
+
+               if ($this->addQueryString === TRUE) {
+                       $typolinkConfiguration['addQueryString'] = 1;
+                       if (count($this->argumentsToBeExcludedFromQueryString) > 0) {
+                               $typolinkConfiguration['addQueryString.'] = array(
+                                       'exclude' => implode(',', $this->argumentsToBeExcludedFromQueryString)
+                               );
+                       }
+               }
+
+               if ($this->noCache === TRUE) {
+                       $typolinkConfiguration['no_cache'] = 1;
+               } elseif ($this->useCacheHash) {
+                       $typolinkConfiguration['useCacheHash'] = 1;
+               }
+
+               if ($this->section !== '') {
+                       $typolinkConfiguration['section'] = $this->section;
+               }
+
+               if ($this->linkAccessRestrictedPages === TRUE) {
+                       $typolinkConfiguration['linkAccessRestrictedPages'] = 1;
+               }
+
+               return $typolinkConfiguration;
+       }
+
+       /**
+        * Recursively iterates through the specified arguments and turns instances of type Tx_Extbase_DomainObject_AbstractEntity
+        * into an arrays containing the uid of the domain object.
+        *
+        * @param array $arguments The arguments to be iterated
+        * @return array The modified arguments array
+        */
+       protected function convertDomainObjectsToIdentityArrays(array $arguments) {
+               foreach ($arguments as $argumentKey => $argumentValue) {
+                       if ($argumentValue instanceof Tx_Extbase_DomainObject_AbstractEntity) {
+                               $arguments[$argumentKey] = array('uid' => $argumentValue->getUid());
+                       } elseif (is_array($argumentValue)) {
+                               $arguments[$argumentKey] = $this->convertDomainObjectsToIdentityArrays($argumentValue);
+                       }
+               }
+               return $arguments;
+       }
+
+}
+?>
\ No newline at end of file
index 1f9a6c8..d2662d1 100644 (file)
@@ -103,14 +103,15 @@ class Tx_Extbase_MVC_Controller_AbstractController_testcase extends Tx_Extbase_B
                $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
                $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response');
 
-               $mockURIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder');
-               $mockURIBuilder->expects($this->once())->method('URIFor')->with(123, 'theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
+               $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
+               $mockUriBuilder->expects($this->once())->method('setTargetPageUid')->with(123);
+               $mockUriBuilder->expects($this->once())->method('uriFor')->with('theActionName', $arguments, 'TheControllerName', 'TheExtensionName')->will($this->returnValue('the uri'));
 
                $controller = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_AbstractController'), array('redirectToURI'), array(), '', FALSE);
                $controller->expects($this->once())->method('redirectToURI')->with('the uri');
                $controller->_set('request', $mockRequest);
                $controller->_set('response', $mockResponse);
-               $controller->_set('URIBuilder', $mockURIBuilder);
+               $controller->_set('uriBuilder', $mockUriBuilder);
                $controller->_call('redirect', 'theActionName', 'TheControllerName', 'TheExtensionName', $arguments, 123);
        }
 
index 0eb9af5..49f0a12 100644 (file)
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/
 
-class Tx_Extbase_MVC_Web_Routing_URIBuilder_testcase extends Tx_Extbase_Base_testcase {
+class Tx_Extbase_MVC_Web_Routing_UriBuilder_testcase extends Tx_Extbase_Base_testcase {
 
+       /**
+        * @var tslib_fe
+        */
        protected $tsfeBackup;
 
+       /**
+        * @var array
+        */
+       protected $getBackup;
+
+       /**
+        * @var tslib_cObj
+        */
+       protected $contentObject;
+
+       /**
+        * @var Tx_Extbase_MVC_Web_Request
+        */
+       protected $request;
+
+       /**
+        * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
+        */
+       protected $uriBuilder;
+
        public function setUp() {
                $this->tsfeBackup = $GLOBALS['TSFE'];
                $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array(), array(), '', FALSE);
+
+               $this->getBackup = t3lib_div::_GET();
+
+               $this->contentObject = $this->getMock('tslib_cObj');
+               $this->request = $this->getMock('Tx_Extbase_MVC_Web_Request');
+
+               $this->uriBuilder = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Web_Routing_UriBuilder'), array('dummy'), array($this->contentObject));
+               $this->uriBuilder->setRequest($this->request);
        }
 
        public function tearDown() {
                $GLOBALS['TSFE'] = $this->tsfeBackup;
+               t3lib_div::_GETset($this->getBackup);
        }
 
        /**
         * @test
         */
-       public function uriForPrefixesArgumentsWithExtensionAndPluginNameAndSetsControllerArgument() {
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
-
-               $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, NULL, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')), 'SomeController', 'SomeExtension', 'SomePlugin');
+       public function settersAndGettersWorkAsExpected() {
+               $this->uriBuilder
+                       ->setArguments(array('test' => 'arguments'))
+                       ->setSection('testSection')
+                       ->setCreateAbsoluteUri(TRUE)
+                       ->setAddQueryString(TRUE)
+                       ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
+                       ->setLinkAccessRestrictedPages(TRUE)
+                       ->setTargetPageUid(123)
+                       ->setTargetPageType(321)
+                       ->setNoCache(TRUE)
+                       ->setUseCacheHash(FALSE);
+
+               $this->assertEquals(array('test' => 'arguments'), $this->uriBuilder->getArguments());
+               $this->assertEquals('testSection', $this->uriBuilder->getSection());
+               $this->assertEquals(TRUE, $this->uriBuilder->getCreateAbsoluteUri());
+               $this->assertEquals(TRUE, $this->uriBuilder->getAddQueryString());
+               $this->assertEquals(array('test' => 'addQueryStringExcludeArguments'), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
+               $this->assertEquals(TRUE, $this->uriBuilder->getLinkAccessRestrictedPages());
+               $this->assertEquals(123, $this->uriBuilder->getTargetPageUid());
+               $this->assertEquals(321, $this->uriBuilder->getTargetPageType());
+               $this->assertEquals(TRUE, $this->uriBuilder->getNoCache());
+               $this->assertEquals(FALSE, $this->uriBuilder->getUseCacheHash());
        }
 
        /**
         * @test
         */
-       public function additionalArgumentsOverruleArguments() {
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
+       public function uriForPrefixesArgumentsWithExtensionAndPluginNameAndSetsControllerArgument() {
+               $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'));
 
-               $arguments = array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'));
-               $additionalArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled'), 'additionalParam' => 'additionalValue');
-               $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, NULL, $arguments, 'SomeController', 'SomeExtension', 'SomePlugin', 0, FALSE, TRUE, '', FALSE, $additionalArguments);
+               $this->uriBuilder->uriFor(NULL, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')), 'SomeController', 'SomeExtension', 'SomePlugin');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
-       public function uriForForwardsAllParametersToTypolinkURI() {
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
+       public function uriForRecursivelyMergesAndOverrulesControllerArgumentsWithArguments() {
+               $arguments = array('tx_someextension_someplugin' => array('foo' => 'bar'), 'additionalParam' => 'additionalValue');
+               $controllerArguments = array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'));
+               $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
 
-               $expectedArguments = array('tx_someextension_someplugin' => array('action' => 'SomeAction', 'controller' => 'SomeController'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(123, $expectedArguments, 2, TRUE, FALSE, 'SomeSection', TRUE);
-               $URIBuilder->URIFor(123, 'SomeAction', array(), 'SomeController', 'SomeExtension', 'SomePlugin', 2, TRUE, FALSE, 'SomeSection', TRUE);
+               $this->uriBuilder->setArguments($arguments);
+               $this->uriBuilder->uriFor(NULL, $controllerArguments, 'SomeController', 'SomeExtension', 'SomePlugin');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
-       public function uriForSetsActionArgument() {
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
+       public function uriForOnlySetsActionArgumentIfSpecified() {
+               $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeController'));
 
-               $expectedArguments = array('tx_someextension_someplugin' => array('action' => 'SomeAction', 'controller' => 'SomeController'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, 'SomeAction', array(), 'SomeController', 'SomeExtension', 'SomePlugin');
+               $this->uriBuilder->uriFor(NULL, array(), 'SomeController', 'SomeExtension', 'SomePlugin');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
        public function uriForSetsControllerFromRequestIfControllerIsNotSet() {
-               $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
-               $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('SomeControllerFromRequest'));
-
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
-               $URIBuilder->setRequest($mockRequest);
+               $this->request->expects($this->once())->method('getControllerName')->will($this->returnValue('SomeControllerFromRequest'));
 
                $expectedArguments = array('tx_someextension_someplugin' => array('controller' => 'SomeControllerFromRequest'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, NULL, array(), NULL, 'SomeExtension', 'SomePlugin');
+
+               $this->uriBuilder->uriFor(NULL, array(), NULL, 'SomeExtension', 'SomePlugin');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
        public function uriForSetsExtensionNameFromRequestIfExtensionNameIsNotSet() {
-               $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
-               $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
-
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
-               $URIBuilder->setRequest($mockRequest);
+               $this->request->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionNameFromRequest'));
 
                $expectedArguments = array('tx_someextensionnamefromrequest_someplugin' => array('controller' => 'SomeController'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, NULL, array(), 'SomeController', NULL, 'SomePlugin');
+
+               $this->uriBuilder->uriFor(NULL, array(), 'SomeController', NULL, 'SomePlugin');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
        public function uriForSetsPluginNameFromRequestIfPluginNameIsNotSet() {
-               $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
-               $mockRequest->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePluginNameFromRequest'));
-
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'));
-               $URIBuilder->setRequest($mockRequest);
+               $this->request->expects($this->once())->method('getPluginName')->will($this->returnValue('SomePluginNameFromRequest'));
 
                $expectedArguments = array('tx_someextension_somepluginnamefromrequest' => array('controller' => 'SomeController'));
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, $expectedArguments, 0, FALSE, TRUE, '', FALSE);
-               $URIBuilder->URIFor(NULL, NULL, array(), 'SomeController', 'SomeExtension');
+
+               $this->uriBuilder->uriFor(NULL, array(), 'SomeController', 'SomeExtension');
+               $this->assertEquals($expectedArguments, $this->uriBuilder->getArguments());
        }
 
        /**
         * @test
         */
-       public function uriForCallsConvertDomainObjectsToIdentityArraysAfterArgumentsHaveBeenMerged() {
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI', 'convertDomainObjectsToIdentityArrays'));
+       public function buildBackendUriKeepsQueryParametersIfAddQueryStringIsSet() {
+               t3lib_div::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
 
-               $arguments = array('foo' => 'bar', 'baz' => array('extbase' => 'fluid'));
-               $additionalArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled'), 'additionalParam' => 'additionalValue');
-               $expectedArguments = array('tx_someextension_someplugin' => array('foo' => 'overruled', 'baz' => array('extbase' => 'fluid'), 'controller' => 'SomeController'), 'additionalParam' => 'additionalValue');
-               $URIBuilder->expects($this->once())->method('convertDomainObjectsToIdentityArrays')->with($expectedArguments)->will($this->returnValue(array()));;
-               $URIBuilder->URIFor(NULL, NULL, $arguments, 'SomeController', 'SomeExtension', 'SomePlugin', 0, FALSE, TRUE, '', FALSE, $additionalArguments);
+               $this->uriBuilder->setAddQueryString(TRUE);
+
+               $expectedResult = 'mod.php?M=moduleKey&id=pageId&foo=bar';
+               $actualResult = $this->uriBuilder->buildBackendUri();
+
+               $this->assertEquals($expectedResult, $actualResult);
        }
 
        /**
         * @test
         */
-       public function uriForPassesAllDefaultArgumentsToTypolinkURI() {
-               $mockRequest = $this->getMock('Tx_Extbase_MVC_Request');
-               $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue('SomeControllerName'));
-               $mockRequest->expects($this->any())->method('getControllerExtensionName')->will($this->returnValue('SomeExtensionName'));
-               $mockRequest->expects($this->any())->method('getPluginName')->will($this->returnValue('SomePluginName'));
+       public function buildBackendUriRemovesSpecifiedQueryParametersIfArgumentsToBeExcludedFromQueryStringIsSet() {
+               t3lib_div::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
 
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'), array($mockContentObject), '', FALSE);
-               $URIBuilder->setRequest($mockRequest);
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(NULL, array('tx_someextensionname_somepluginname' => array('controller' => 'SomeControllerName')), 0, FALSE, TRUE, '', FALSE, FALSE);
+               $this->uriBuilder->setAddQueryString(TRUE);
+               $this->uriBuilder->setArgumentsToBeExcludedFromQueryString(array('M', 'id'));
 
-               $URIBuilder->URIFor();
+               $expectedResult = 'mod.php?foo=bar';
+               $actualResult = $this->uriBuilder->buildBackendUri();
+
+               $this->assertEquals($expectedResult, $actualResult);
        }
 
        /**
         * @test
         */
-       public function uriForPassesAllSpecifiedArgumentsToTypolinkURI() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_URIBuilder', array('typolinkURI'), array($mockContentObject), '', FALSE);
-               $URIBuilder->expects($this->once())->method('typolinkURI')->with(123, array('tx_extensionname_pluginname' => array('some' => 'Argument', 'action' => 'actionName', 'controller' => 'controllerName'), 'additional' => 'Parameter'), 1, TRUE, FALSE, 'section', TRUE, TRUE);
+       public function buildBackendUriKeepsModuleQueryParametersIfAddQueryStringIsNotSet() {
+               t3lib_div::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
 
-               $URIBuilder->URIFor(123, 'actionName', array('some' => 'Argument'), 'controllerName', 'extensionName', 'pluginName', 1, TRUE, FALSE, 'section', TRUE, array('additional' => 'Parameter'), TRUE);
+               $expectedResult = 'mod.php?M=moduleKey&id=pageId';
+               $actualResult = $this->uriBuilder->buildBackendUri();
+
+               $this->assertEquals($expectedResult, $actualResult);
        }
 
        /**
         * @test
         */
-       public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
-               $mockDomainObject1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
-               $mockDomainObject1->_set('uid', '123');
+       public function buildBackendUriMergesAndOverrulesQueryParametersWithArguments() {
+               t3lib_div::_GETset(array('M' => 'moduleKey', 'id' => 'pageId', 'foo' => 'bar'));
 
-               $mockDomainObject2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
-               $mockDomainObject2->_set('uid', '321');
+               $this->uriBuilder->setArguments(array('M' => 'overwrittenModuleKey', 'somePrefix' => array('bar' => 'baz')));
 
-               $URIBuilder = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Web_Routing_URIBuilder'), array('dummy'));
+               $expectedResult = 'mod.php?M=overwrittenModuleKey&id=pageId&somePrefix%5Bbar%5D=baz';
+               $actualResult = $this->uriBuilder->buildBackendUri();
 
-               $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => array('uid' => '123'), 'second' => array('domainObject2' => array('uid' => '321')));
-               $actualResult = $URIBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function buildBackendUriConvertsDomainObjectsAfterArgumentsHaveBeenMerged() {
+               t3lib_div::_GETset(array('M' => 'moduleKey'));
+
+               $mockDomainObject = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
+               $mockDomainObject->_set('uid', '123');
+
+               $this->uriBuilder->setArguments(array('somePrefix' => array('someDomainObject' => $mockDomainObject)));
+
+               $expectedResult = 'mod.php?M=moduleKey&somePrefix%5BsomeDomainObject%5D%5Buid%5D=123';
+               $actualResult = $this->uriBuilder->buildBackendUri();
 
                $this->assertEquals($expectedResult, $actualResult);
        }
@@ -191,138 +242,220 @@ class Tx_Extbase_MVC_Web_Routing_URIBuilder_testcase extends Tx_Extbase_Base_tes
        /**
         * @test
         */
-       public function typolinkURILinksToCurrentPageIfPageUidIsNotSet() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildBackendUriRespectsSection() {
+               t3lib_div::_GETset(array('M' => 'moduleKey'));
 
-               $GLOBALS['TSFE']->id = 123;
-               $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $this->uriBuilder->setSection('someSection');
+
+               $expectedResult = 'mod.php?M=moduleKey#someSection';
+               $actualResult = $this->uriBuilder->buildBackendUri();
 
-               $URIBuilder->typolinkURI();
+               $this->assertEquals($expectedResult, $actualResult);
        }
 
        /**
         * @test
         */
-       public function typolinkURILinksToPageUidIfSet() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildFrontendUriCreatesTypoLink() {
+               $uriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder', array('buildTypolinkConfiguration'), array($this->contentObject));
+               $uriBuilder->expects($this->once())->method('buildTypolinkConfiguration')->will($this->returnValue(array('someTypoLinkConfiguration')));
+
+               $this->contentObject->expects($this->once())->method('typoLink_URL')->with(array('someTypoLinkConfiguration'));
+
+               $uriBuilder->buildFrontendUri();
+       }
+
+       /**
+        * @test
+        */
+       public function buildFrontendUriCreatesRelativeUrisByDefault() {
+               $this->contentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
+
+               $expectedResult = 'relative/uri';
+               $actualResult = $this->uriBuilder->buildFrontendUri();
+
+               $this->assertSame($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function buildFrontendUriCreatesAbsoluteUrisIfSpecified() {
+               $this->request->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://baseuri/'));
+               $this->contentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
+               $this->uriBuilder->setCreateAbsoluteUri(TRUE);
+
+               $expectedResult = 'http://baseuri/relative/uri';
+               $actualResult = $this->uriBuilder->buildFrontendUri();
+               $this->assertSame($expectedResult, $actualResult);
+       }
+
+               /**
+        * @test
+        */
+       public function resetSetsAllOptionsToTheirDefaultValue() {
+               $this->uriBuilder
+                       ->setArguments(array('test' => 'arguments'))
+                       ->setSection('testSection')
+                       ->setCreateAbsoluteUri(TRUE)
+                       ->setAddQueryString(TRUE)
+                       ->setArgumentsToBeExcludedFromQueryString(array('test' => 'addQueryStringExcludeArguments'))
+                       ->setLinkAccessRestrictedPages(TRUE)
+                       ->setTargetPageUid(123)
+                       ->setTargetPageType(321)
+                       ->setNoCache(TRUE)
+                       ->setUseCacheHash(FALSE);
+
+               $this->uriBuilder->reset();
+
+               $this->assertEquals(array(), $this->uriBuilder->getArguments());
+               $this->assertEquals('', $this->uriBuilder->getSection());
+               $this->assertEquals(FALSE, $this->uriBuilder->getCreateAbsoluteUri());
+               $this->assertEquals(FALSE, $this->uriBuilder->getAddQueryString());
+               $this->assertEquals(array(), $this->uriBuilder->getArgumentsToBeExcludedFromQueryString());
+               $this->assertEquals(FALSE, $this->uriBuilder->getLinkAccessRestrictedPages());
+               $this->assertEquals(NULL, $this->uriBuilder->getTargetPageUid());
+               $this->assertEquals(0, $this->uriBuilder->getTargetPageType());
+               $this->assertEquals(FALSE, $this->uriBuilder->getNoCache());
+               $this->assertEquals(TRUE, $this->uriBuilder->getUseCacheHash());
+       }
+
+       /**
+        * @test
+        */
+       public function buildTypolinkConfigurationRespectsSpecifiedTargetPageUid() {
+               $GLOBALS['TSFE']->id = 123;
+               $this->uriBuilder->setTargetPageUid(321);
 
                $expectedConfiguration = array('parameter' => 321, 'useCacheHash' => 1);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
+
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
+       }
 
-               $URIBuilder->typolinkURI(321);
+       /**
+        * @test
+        */
+       public function buildTypolinkConfigurationUsesCurrentPageUidIfTargetPageUidIsNotSet() {
+               $GLOBALS['TSFE']->id = 123;
+
+               $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
+
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURILinksProperlySetsAdditionalArguments() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationProperlySetsAdditionalArguments() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setArguments(array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
 
                $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&foo=bar&baz%5Bextbase%5D=fluid');
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $URIBuilder->typolinkURI(123, array('foo' => 'bar', 'baz' => array('extbase' => 'fluid')));
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURIConsidersPageType() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationConvertsDomainObjects() {
+               $mockDomainObject1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
+               $mockDomainObject1->_set('uid', '123');
 
-               $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $mockDomainObject2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
+               $mockDomainObject2->_set('uid', '321');
 
-               $URIBuilder->typolinkURI(123, array(), 2);
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setArguments(array('someDomainObject' => $mockDomainObject1, 'baz' => array('someOtherDomainObject' => $mockDomainObject2)));
+
+               $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'additionalParams' => '&someDomainObject%5Buid%5D=123&baz%5BsomeOtherDomainObject%5D%5Buid%5D=321');
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
+
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURIDisablesCacheHashIfNoCacheIsSet() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationConsidersPageType() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setTargetPageType(2);
 
-               $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $expectedConfiguration = array('parameter' => '123,2', 'useCacheHash' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $URIBuilder->typolinkURI(123, array(), 0, TRUE, TRUE);
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function cacheHashCanBeDisabled() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationDisablesCacheHashIfNoCacheIsSet() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setNoCache(TRUE);
 
-               $expectedConfiguration = array('parameter' => 123);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $expectedConfiguration = array('parameter' => 123, 'no_cache' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE);
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURIConsidersSection() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationDoesNotSetUseCacheHashOptionIfUseCacheHashIsDisabled() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setUseCacheHash(FALSE);
 
-               $expectedConfiguration = array('parameter' => 123, 'section' => 'SomeSection');
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $expectedConfiguration = array('parameter' => 123);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE, 'SomeSection');
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURIConsidersLinkAccessRestrictedPages() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationConsidersSection() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setSection('SomeSection');
 
-               $expectedConfiguration = array('parameter' => 123, 'linkAccessRestrictedPages' => 1);
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->with($expectedConfiguration);
+               $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'section' => 'SomeSection');
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $URIBuilder->typolinkURI(123, array(), 0, FALSE, FALSE, '', TRUE);
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURICreatesRelativeUrisByDefault() {
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
+       public function buildTypolinkConfigurationLinkAccessRestrictedPagesSetting() {
+               $this->uriBuilder->setTargetPageUid(123);
+               $this->uriBuilder->setLinkAccessRestrictedPages(TRUE);
 
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
+               $expectedConfiguration = array('parameter' => 123, 'useCacheHash' => 1, 'linkAccessRestrictedPages' => 1);
+               $actualConfiguration = $this->uriBuilder->_call('buildTypolinkConfiguration');
 
-               $expectedResult = 'relative/uri';
-               $actualResult = $URIBuilder->typolinkURI();
-               $this->assertSame('relative/uri', $actualResult);
+               $this->assertEquals($expectedConfiguration, $actualConfiguration);
        }
 
        /**
         * @test
         */
-       public function typolinkURICreatesAbsoluteUrisIfSpecified() {
-               $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
-               $mockRequest->expects($this->any())->method('getBaseURI')->will($this->returnValue('http://baseuri/'));
+       public function convertDomainObjectsToIdentityArraysConvertsDomainObjects() {
+               $mockDomainObject1 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
+               $mockDomainObject1->_set('uid', '123');
 
-               $mockContentObject = $this->getMock('tslib_cObj');
-               $URIBuilder = new Tx_Extbase_MVC_Web_Routing_URIBuilder($mockContentObject);
-               $URIBuilder->setRequest($mockRequest);
+               $mockDomainObject2 = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_DomainObject_AbstractEntity'), array('dummy'));
+               $mockDomainObject2->_set('uid', '321');
 
-               $mockContentObject->expects($this->once())->method('typoLink_URL')->will($this->returnValue('relative/uri'));
+               $expectedResult = array('foo' => array('bar' => 'baz'), 'domainObject1' => array('uid' => '123'), 'second' => array('domainObject2' => array('uid' => '321')));
+               $actualResult = $this->uriBuilder->_call('convertDomainObjectsToIdentityArrays', array('foo' => array('bar' => 'baz'), 'domainObject1' => $mockDomainObject1, 'second' => array('domainObject2' => $mockDomainObject2)));
 
-               $expectedResult = 'http://baseuri/relative/uri';
-               $actualResult = $URIBuilder->typolinkURI(NULL, array(), 0, FALSE, TRUE, '', FALSE, TRUE);
-               $this->assertSame($expectedResult, $actualResult);
+               $this->assertEquals($expectedResult, $actualResult);
        }
 
 }
index 2cc0b38..a2acba3 100644 (file)
@@ -85,7 +85,7 @@ return array(
        'tx_extbase_mvc_web_request' => $extensionClassesPath . 'MVC/Web/Request.php',
        'tx_extbase_mvc_web_requestbuilder' => $extensionClassesPath . 'MVC/Web/RequestBuilder.php',
        'tx_extbase_mvc_web_response' => $extensionClassesPath . 'MVC/Web/Response.php',
-       'tx_extbase_mvc_web_routing_uribuilder' => $extensionClassesPath . 'MVC/Web/Routing/URIBuilder.php',
+       'tx_extbase_mvc_web_routing_uribuilder' => $extensionClassesPath . 'MVC/Web/Routing/UriBuilder.php',
        'tx_extbase_object_exception' => $extensionClassesPath . 'Object/Exception.php',
        'tx_extbase_object_manager' => $extensionClassesPath . 'Object/Manager.php',
        'tx_extbase_object_managerinterface' => $extensionClassesPath . 'Object/ManagerInterface.php',