[BUGFIX] RequestBuilder must not call $request->setMethod with NULL
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Sat, 13 Apr 2013 09:05:19 +0000 (11:05 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Sat, 13 Apr 2013 09:29:29 +0000 (11:29 +0200)
If running a cli script the variable $_SERVER['REQUEST_METHOD'] is
not set at all. So when dispatching a sub webrequest the variable is
still not set and the RequestBuilder sets the method of Request to
NULL which is not allowed.

Therefore the variable will now be fetched via the Enrivonment-
Service which detects if the variable is empty and falls back to
"GET".

Releases: 6.1, 6.0, 4.7, 1.3
Resolves: #13845
Change-Id: I5e33aba48605895f9eae1e3b710364091e7e84f0
Reviewed-on: https://review.typo3.org/19887
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
typo3/sysext/extbase/Classes/Mvc/Web/RequestBuilder.php
typo3/sysext/extbase/Classes/Service/EnvironmentService.php
typo3/sysext/extbase/Tests/Unit/Mvc/Web/RequestBuilderTest.php

index 75105b2..00e6caf 100644 (file)
@@ -90,6 +90,11 @@ class RequestBuilder implements \TYPO3\CMS\Core\SingletonInterface {
        protected $extensionService;
 
        /**
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService
+        */
+       protected $environmentService;
+
+       /**
         * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
         */
        public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
@@ -115,6 +120,14 @@ class RequestBuilder implements \TYPO3\CMS\Core\SingletonInterface {
        }
 
        /**
+        * @param \TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService
+        * @return void
+        */
+       public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService) {
+               $this->environmentService = $environmentService;
+       }
+
+       /**
         * @throws \TYPO3\CMS\Extbase\Mvc\Exception
         * @return void
         */
@@ -169,7 +182,7 @@ class RequestBuilder implements \TYPO3\CMS\Core\SingletonInterface {
                $request->setControllerActionName($actionName);
                $request->setRequestUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
                $request->setBaseUri(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
-               $request->setMethod(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : NULL);
+               $request->setMethod($this->environmentService->getServerRequestMethod());
                if (is_string($parameters['format']) && strlen($parameters['format'])) {
                        $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
                } else {
@@ -217,7 +230,7 @@ class RequestBuilder implements \TYPO3\CMS\Core\SingletonInterface {
         * If no action is specified, the defaultActionName will be returned.
         * If that's not available or the specified action is not defined in the current plugin, an exception is thrown.
         *
-        * @param $controllerName
+        * @param string $controllerName
         * @param array $parameters
         * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException
         * @throws \TYPO3\CMS\Extbase\Mvc\Exception
index 4eaa612..60bc9ef 100644 (file)
@@ -49,6 +49,13 @@ class EnvironmentService {
        public function isEnvironmentInBackendMode() {
                return (defined('TYPO3_MODE') && TYPO3_MODE === 'BE') ?: FALSE;
        }
+
+       /**
+        * @return string
+        */
+       public function getServerRequestMethod() {
+               return isset($_SERVER['REQUEST_METHOD']) && is_string($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
+       }
 }
 
 ?>
\ No newline at end of file
index 232503d..e2ae629 100644 (file)
@@ -35,7 +35,7 @@ class RequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        protected $requestBuilder;
 
        /**
-        * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
+        * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface|\PHPUnit_Framework_MockObject_MockObject
         */
        protected $mockConfigurationManager;
 
@@ -45,17 +45,22 @@ class RequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        protected $configuration;
 
        /**
-        * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+        * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
         */
        protected $mockObjectManager;
 
        /**
-        * @var \TYPO3\CMS\Extbase\Service\ExtensionService
+        * @var \TYPO3\CMS\Extbase\Service\ExtensionService|\PHPUnit_Framework_MockObject_MockObject
         */
        protected $mockExtensionService;
 
        /**
-        * @var \TYPO3\CMS\Extbase\Mvc\Web\Request
+        * @var \TYPO3\CMS\Extbase\Service\EnvironmentService|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $mockEnvironmentService;
+
+       /**
+        * @var \TYPO3\CMS\Extbase\Mvc\Web\Request|\PHPUnit_Framework_MockObject_MockObject
         */
        protected $mockRequest;
 
@@ -83,6 +88,7 @@ class RequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $this->mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
                $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
                $this->mockExtensionService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\ExtensionService');
+               $this->mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('getServerRequestMethod'));
        }
 
        /**
@@ -96,6 +102,8 @@ class RequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                $pluginNamespace = 'tx_' . strtolower(($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']));
                $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
                $this->requestBuilder->injectExtensionService($this->mockExtensionService);
+               $this->mockEnvironmentService->expects($this->any())->method('getServerRequestMethod')->will($this->returnValue('GET'));
+               $this->requestBuilder->injectEnvironmentService($this->mockEnvironmentService);
        }
 
        /**
@@ -168,8 +176,10 @@ class RequestBuilderTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
         */
        public function buildSetsRequestMethod() {
                $this->injectDependencies();
-               $_SERVER['REQUEST_METHOD'] = 'SomeRequestMethod';
                $expectedMethod = 'SomeRequestMethod';
+               $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('getServerRequestMethod'));
+               $mockEnvironmentService->expects($this->once())->method('getServerRequestMethod')->will($this->returnValue($expectedMethod));
+               $this->requestBuilder->injectEnvironmentService($mockEnvironmentService);
                $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
                $this->requestBuilder->build();
        }