[BUGFIX] Make changed action resolving backwards compatible
authorBastian Waidelich <bastian@typo3.org>
Wed, 2 Nov 2011 09:24:22 +0000 (10:24 +0100)
committerBastian Waidelich <bastian@typo3.org>
Wed, 2 Nov 2011 09:24:22 +0000 (10:24 +0100)
With 1.4 Extbase throws an Exception if a requested controller
and/or action is not configured to be part of the current
plugin. If mvc.throwPageNotFoundExceptionIfActionCantBeResolved
is set, a 404 not-found page is shown (see #6235 and related).

That makes it easier for developers to track down typos in their
plugin configuration.
If, however, two plugins listen to the same "plugin namespace"
(e.g. because they are two instances of the same plugin) one
sometimes prefer the plugins to fall back to their default
controller/action pair, if the requested action is not supported

This change adds a new setting:
Add mvc.callDefaultActionIfActionCantBeResolved = 1 to your
extension/plugin configuration in order to get this behavior.

Note: This will resolve to the default action of the *current*
default controller. So this behavior is not exactly the same as
in Extbase 1.3 (see #7282).

Change-Id: I619690c7540381561ebf0a43446c7d094b534fbe
Resolves: #31322

typo3/sysext/extbase/Classes/MVC/Web/RequestBuilder.php
typo3/sysext/extbase/Tests/Unit/MVC/Web/RequestBuilderTest.php

index 3d8af4b..ad8c36f 100644 (file)
@@ -197,6 +197,8 @@ class Tx_Extbase_MVC_Web_RequestBuilder implements t3lib_Singleton {
                                        'The requested resource was not found',
                                        1313857897
                                );
+                       } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (boolean)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
+                               return $this->defaultControllerName;
                        }
                        throw new Tx_Extbase_MVC_Exception_InvalidControllerName(
                                'The controller "' . $parameters['controller'] . '" is not allowed by this plugin. Please check for Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
@@ -217,8 +219,8 @@ class Tx_Extbase_MVC_Web_RequestBuilder implements t3lib_Singleton {
         * @throws t3lib_error_http_PageNotFoundException|Tx_Extbase_MVC_Exception|Tx_Extbase_MVC_Exception_InvalidActionName if the action could not be resolved
         */
        protected function resolveActionName($controllerName, array $parameters) {
+               $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
                if (!isset($parameters['action']) || strlen($parameters['action']) === 0) {
-                       $defaultActionName = is_array($this->allowedControllerActions[$controllerName]) ? current($this->allowedControllerActions[$controllerName]) : '';
                        if (strlen($defaultActionName) === 0) {
                                throw new Tx_Extbase_MVC_Exception(
                                        'The default action can not be determined for controller "' . $controllerName . '". Please check Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
@@ -236,10 +238,12 @@ class Tx_Extbase_MVC_Web_RequestBuilder implements t3lib_Singleton {
                                        'The requested resource was not found',
                                        1313857897
                                );
+                       } elseif (isset($configuration['mvc']['callDefaultActionIfActionCantBeResolved']) && (boolean)$configuration['mvc']['callDefaultActionIfActionCantBeResolved']) {
+                               return $defaultActionName;
                        }
                        throw new Tx_Extbase_MVC_Exception_InvalidActionName(
                                'The action "' . $actionName . '" (controller "' . $controllerName . '") is not allowed by this plugin. Please check Tx_Extbase_Utility_Extension::configurePlugin() in your ext_localconf.php.',
-                               1313855173
+                               1313855175
                        );
                }
                return filter_var($actionName, FILTER_SANITIZE_STRING);
index ebd68f2..69627e0 100644 (file)
@@ -402,7 +402,7 @@ class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_
 
                $_GET = array(
                        'tx_myextension_pi1' => array(
-                               'controller' => 'SomeIllegalController',
+                               'controller' => 'SomeInvalidController',
                        )
                );
                $this->requestBuilder->build();
@@ -422,12 +422,29 @@ class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_
 
                $_GET = array(
                        'tx_myextension_pi1' => array(
-                               'controller' => 'SomeIllegalController',
+                               'controller' => 'SomeInvalidController',
                        )
                );
                $this->requestBuilder->build();
        }
 
+       /**
+        * @test
+        */
+       public function buildSetsDefaultControllerNameIfSpecifiedControllerIsNotAllowedAndCallDefaultActionIfActionCantBeResolvedIsSet() {
+               $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
+               $this->injectDependencies();
+
+               $this->requestBuilder->injectExtensionService($this->mockExtensionService);
+
+               $_GET = array(
+                       'tx_myextension_pi1' => array(
+                               'controller' => 'SomeInvalidController',
+                       )
+               );
+               $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
+               $this->requestBuilder->build();
+       }
 
        /**
         * @test
@@ -501,7 +518,7 @@ class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_
 
                $_GET = array(
                        'tx_myextension_pi1' => array(
-                               'action' => 'someIllegalAction',
+                               'action' => 'someInvalidAction',
                        )
                );
                $this->requestBuilder->build();
@@ -521,9 +538,29 @@ class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_
 
                $_GET = array(
                        'tx_myextension_pi1' => array(
-                               'action' => 'someIllegalAction',
+                               'action' => 'someInvalidAction',
+                       )
+               );
+               $this->requestBuilder->build();
+       }
+
+       /**
+        * @test
+        */
+       public function buildSetsDefaultActionNameIfSpecifiedActionIsNotAllowedAndCallDefaultActionIfActionCantBeResolvedIsSet() {
+               $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
+               $this->injectDependencies();
+
+               $this->requestBuilder->injectExtensionService($this->mockExtensionService);
+
+               $_GET = array(
+                       'tx_myextension_pi1' => array(
+                               'controller' => 'TheThirdController',
+                               'action' => 'someInvalidAction',
                        )
                );
+               $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheThirdController');
+               $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
                $this->requestBuilder->build();
        }