[TASK] Make WidgetRequestBuilderTest notice free 04/56204/3
authorJan Helke <typo3@helke.de>
Fri, 16 Mar 2018 09:11:43 +0000 (10:11 +0100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 16 Mar 2018 12:22:53 +0000 (13:22 +0100)
It also throws now an exception, is no fluid-widget-id was given.
Also this exception is covered by a test.

Releases: master
Resolves: #84329
Change-Id: Ibeb7a404f2ab2ebfa0b233279facee978ded41aa
Reviewed-on: https://review.typo3.org/56204
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php
typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php

index a858bd0..92c0f3c 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types=1);
 namespace TYPO3\CMS\Fluid\Core\Widget;
 
 /*
@@ -15,21 +16,23 @@ namespace TYPO3\CMS\Fluid\Core\Widget;
  */
 
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Mvc\RequestInterface;
+use TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder;
 
 /**
  * Builds the WidgetRequest if an AJAX widget is called.
  */
-class WidgetRequestBuilder extends \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
+class WidgetRequestBuilder extends RequestBuilder
 {
     /**
-     * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
+     * @var AjaxWidgetContextHolder
      */
     private $ajaxWidgetContextHolder;
 
     /**
-     * @param \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder
+     * @param AjaxWidgetContextHolder $ajaxWidgetContextHolder
      */
-    public function injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder $ajaxWidgetContextHolder)
+    public function injectAjaxWidgetContextHolder(AjaxWidgetContextHolder $ajaxWidgetContextHolder)
     {
         $this->ajaxWidgetContextHolder = $ajaxWidgetContextHolder;
     }
@@ -37,11 +40,11 @@ class WidgetRequestBuilder extends \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
     /**
      * Builds a widget request object from the raw HTTP information
      *
-     * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest The widget request as an object
+     * @return RequestInterface The widget request as an object
      */
-    public function build()
+    public function build(): RequestInterface
     {
-        $request = $this->objectManager->get(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
+        $request = $this->objectManager->get(WidgetRequest::class);
         $request->setRequestUri(GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'));
         $request->setBaseUri(GeneralUtility::getIndpEnv('TYPO3_SITE_URL'));
         $request->setMethod($_SERVER['REQUEST_METHOD'] ?? null);
@@ -54,6 +57,13 @@ class WidgetRequestBuilder extends \TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
         if (isset($rawGetArguments['action'])) {
             $request->setControllerActionName($rawGetArguments['action']);
         }
+        if (!isset($rawGetArguments['fluid-widget-id'])) {
+            // Low level test, WidgetRequestHandler returns false in canHandleRequest () if this is not set
+            throw new \InvalidArgumentException(
+                'No Fluid Widget ID was given.',
+                1521190675
+            );
+        }
         $widgetContext = $this->ajaxWidgetContextHolder->get($rawGetArguments['fluid-widget-id']);
         $request->setWidgetContext($widgetContext);
         return $request;
index 99c861e..bb541cb 100644 (file)
@@ -14,52 +14,77 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\Core\Widget;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
+use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
+use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
+use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest;
+use TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
 /**
  * Test case
  */
-class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+class WidgetRequestBuilderTest extends UnitTestCase
 {
     /**
-     * Subject is not notice free, disable E_NOTICES
-     */
-    protected static $suppressNotices = true;
-
-    /**
-     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder
+     * @var WidgetRequestBuilder
      */
     protected $widgetRequestBuilder;
 
     /**
-     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
+     * @var ObjectManagerInterface
      */
     protected $mockObjectManager;
 
     /**
-     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
+     * @var WidgetRequest
      */
     protected $mockWidgetRequest;
 
     /**
-     * @var \TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
+     * @var AjaxWidgetContextHolder
      */
     protected $mockAjaxWidgetContextHolder;
 
     /**
-     * @var \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
+     * @var WidgetContext
      */
     protected $mockWidgetContext;
 
+    /**
+     *
+     */
     protected function setUp()
     {
-        $this->widgetRequestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder::class, ['setArgumentsFromRawRequestData']);
-        $this->mockWidgetRequest = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class);
-        $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
-        $this->mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Fluid\Core\Widget\WidgetRequest::class)->will($this->returnValue($this->mockWidgetRequest));
+        $this->widgetRequestBuilder = $this->getAccessibleMock(WidgetRequestBuilder::class, ['setArgumentsFromRawRequestData']);
+        $this->mockWidgetRequest = $this->createMock(WidgetRequest::class);
+        $this->mockObjectManager = $this->createMock(ObjectManagerInterface::class);
+        $this->mockObjectManager->expects($this->once())->method('get')->with(WidgetRequest::class)->will($this->returnValue($this->mockWidgetRequest));
         $this->widgetRequestBuilder->_set('objectManager', $this->mockObjectManager);
-        $this->mockWidgetContext = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\WidgetContext::class);
-        $this->mockAjaxWidgetContextHolder = $this->createMock(\TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder::class);
+        $this->mockWidgetContext = $this->createMock(WidgetContext::class);
+        $this->mockAjaxWidgetContextHolder = $this->createMock(AjaxWidgetContextHolder::class);
         $this->widgetRequestBuilder->injectAjaxWidgetContextHolder($this->mockAjaxWidgetContextHolder);
-        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
+    }
+
+    /**
+     * @test
+     */
+    public function buildThrowsIfNoFluidWidgetIdWasSet()
+    {
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'not-the-fluid-widget-id' => 'foo'
+        ];
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1521190675);
+        $this->widgetRequestBuilder->build();
     }
 
     /**
@@ -67,7 +92,18 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsRequestUri()
     {
-        $requestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'fluid-widget-id' => 'foo'
+        ];
+        $requestUri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setRequestURI')->with($requestUri);
         $this->widgetRequestBuilder->build();
     }
@@ -77,7 +113,18 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsBaseUri()
     {
-        $baseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'fluid-widget-id' => 'foo'
+        ];
+        $baseUri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setBaseURI')->with($baseUri);
         $this->widgetRequestBuilder->build();
     }
@@ -87,7 +134,17 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsRequestMethod()
     {
-        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'POST'
+        ];
+        $_GET = [
+            'fluid-widget-id' => 'foo'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setMethod')->with('POST');
         $this->widgetRequestBuilder->build();
     }
@@ -97,9 +154,21 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsPostArgumentsFromRequest()
     {
-        $_SERVER['REQUEST_METHOD'] = 'POST';
-        $_GET = ['get' => 'foo'];
-        $_POST = ['post' => 'bar'];
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'POST'
+        ];
+        $_GET = [
+            'get' => 'foo',
+            'fluid-widget-id' => 'foo'
+        ];
+        $_POST = [
+            'post' => 'bar'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_POST);
         $this->widgetRequestBuilder->build();
     }
@@ -109,9 +178,21 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsGetArgumentsFromRequest()
     {
-        $_SERVER['REQUEST_METHOD'] = 'GET';
-        $_GET = ['get' => 'foo'];
-        $_POST = ['post' => 'bar'];
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'GET'
+        ];
+        $_GET = [
+            'get' => 'foo',
+            'fluid-widget-id' => 'foo'
+        ];
+        $_POST = [
+            'post' => 'bar'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_GET);
         $this->widgetRequestBuilder->build();
     }
@@ -121,7 +202,18 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsControllerActionNameFromGetArguments()
     {
-        $_GET = ['action' => 'myAction'];
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'action' => 'myAction',
+            'fluid-widget-id' => 'foo'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
         $this->widgetRequestBuilder->build();
     }
@@ -131,7 +223,17 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildSetsWidgetContext()
     {
-        $_GET = ['fluid-widget-id' => '123'];
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'fluid-widget-id' => '123'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->with('123')->will($this->returnValue($this->mockWidgetContext));
         $this->mockWidgetRequest->expects($this->once())->method('setWidgetContext')->with($this->mockWidgetContext);
         $this->widgetRequestBuilder->build();
@@ -142,6 +244,17 @@ class WidgetRequestBuilderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTes
      */
     public function buildReturnsRequest()
     {
+        $_SERVER = [
+            'REMOTE_ADDR' => 'foo',
+            'SSL_SESSION_ID' => 'foo',
+            'REQUEST_URI' => 'foo',
+            'ORIG_SCRIPT_NAME' => 'foo',
+            'REQUEST_METHOD' => 'foo'
+        ];
+        $_GET = [
+            'fluid-widget-id' => 'foo'
+        ];
+        $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
         $expected = $this->mockWidgetRequest;
         $actual = $this->widgetRequestBuilder->build();
         $this->assertSame($expected, $actual);