From ba6f3b3e57ac1dfc75801dfaad194a1a2f3504c4 Mon Sep 17 00:00:00 2001 From: Jan Helke Date: Fri, 16 Mar 2018 10:11:43 +0100 Subject: [PATCH] [TASK] Make WidgetRequestBuilderTest notice free 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 Reviewed-by: Anja Leichsenring Tested-by: Anja Leichsenring Reviewed-by: Christian Kuhn Tested-by: Christian Kuhn --- .../Core/Widget/WidgetRequestBuilder.php | 24 ++- .../Core/Widget/WidgetRequestBuilderTest.php | 171 +++++++++++++++--- 2 files changed, 159 insertions(+), 36 deletions(-) diff --git a/typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php b/typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php index a858bd0e58d1..92c0f3c5e81c 100644 --- a/typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php +++ b/typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php @@ -1,4 +1,5 @@ ajaxWidgetContextHolder = $ajaxWidgetContextHolder; } @@ -37,11 +40,11 @@ public function injectAjaxWidgetContextHolder(\TYPO3\CMS\Fluid\Core\Widget\AjaxW /** * 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 @@ public function build() 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; diff --git a/typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php b/typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php index 99c861efa574..bb541cb2a9fc 100644 --- a/typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php +++ b/typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php @@ -14,52 +14,77 @@ * 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 @@ protected function setUp() */ 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 @@ public function buildSetsRequestUri() */ 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 @@ public function buildSetsBaseUri() */ 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 @@ public function buildSetsRequestMethod() */ 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 @@ public function buildSetsPostArgumentsFromRequest() */ 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 @@ public function buildSetsGetArgumentsFromRequest() */ 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 @@ public function buildSetsControllerActionNameFromGetArguments() */ 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 @@ public function buildSetsWidgetContext() */ 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);