Skip to content

Commit

Permalink
[TASK] Make WidgetRequestBuilderTest notice free
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
janhelke authored and lolli42 committed Mar 16, 2018
1 parent a9d92a0 commit ba6f3b3
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 36 deletions.
24 changes: 17 additions & 7 deletions typo3/sysext/fluid/Classes/Core/Widget/WidgetRequestBuilder.php
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace TYPO3\CMS\Fluid\Core\Widget;

/*
Expand All @@ -15,33 +16,35 @@
*/

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;
}

/**
* 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);
Expand All @@ -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;
Expand Down
171 changes: 142 additions & 29 deletions typo3/sysext/fluid/Tests/Unit/Core/Widget/WidgetRequestBuilderTest.php
Expand Up @@ -14,60 +14,96 @@
* 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();
}

/**
* @test
*/
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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit ba6f3b3

Please sign in to comment.