‪TYPO3CMS  10.4
WidgetRequestBuilderTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪WidgetRequestBuilderTest extends UnitTestCase
31 {
35  protected ‪$widgetRequestBuilder;
36 
40  protected ‪$mockObjectManager;
41 
45  protected ‪$mockWidgetRequest;
46 
51 
55  protected ‪$mockWidgetContext;
56 
57  protected function ‪setUp(): void
58  {
59  parent::setUp();
60  $this->widgetRequestBuilder = $this->getAccessibleMock(WidgetRequestBuilder::class, ['setArgumentsFromRawRequestData']);
61  $this->mockWidgetRequest = $this->createMock(WidgetRequest::class);
62  $this->mockObjectManager = $this->createMock(ObjectManagerInterface::class);
63  $this->mockObjectManager->expects(self::any())->method('get')->with(WidgetRequest::class)->willReturn($this->mockWidgetRequest);
64  $this->widgetRequestBuilder->_set('objectManager', $this->mockObjectManager);
65  $this->mockWidgetContext = $this->createMock(WidgetContext::class);
66  $this->mockAjaxWidgetContextHolder = $this->createMock(AjaxWidgetContextHolder::class);
67  $this->widgetRequestBuilder->injectAjaxWidgetContextHolder($this->mockAjaxWidgetContextHolder);
68  $environmentServiceMock = $this->createMock(EnvironmentService::class);
69  $environmentServiceMock->expects(self::any())->method('isEnvironmentInFrontendMode')->willReturn(true);
70  $environmentServiceMock->expects(self::any())->method('isEnvironmentInBackendMode')->willReturn(false);
71  $this->widgetRequestBuilder->injectEnvironmentService($environmentServiceMock);
72  }
73 
78  {
79  $_SERVER = [
80  'REMOTE_ADDR' => 'foo',
81  'SSL_SESSION_ID' => 'foo',
82  'REQUEST_URI' => 'foo',
83  'SCRIPT_NAME' => 'foo',
84  'REQUEST_METHOD' => 'foo'
85  ];
86  $_GET = [
87  'not-the-fluid-widget-id' => 'foo'
88  ];
89  $this->expectException(\InvalidArgumentException::class);
90  $this->expectExceptionCode(1521190675);
91  $this->widgetRequestBuilder->build();
92  }
93 
97  public function ‪buildSetsRequestUri()
98  {
99  $_SERVER = [
100  'REMOTE_ADDR' => 'foo',
101  'SSL_SESSION_ID' => 'foo',
102  'REQUEST_URI' => 'foo',
103  'SCRIPT_NAME' => 'foo',
104  'REQUEST_METHOD' => 'foo'
105  ];
106  $_GET = [
107  'fluid-widget-id' => 'foo'
108  ];
109  $requestUri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
110  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
111  $this->mockWidgetRequest->expects(self::once())->method('setRequestURI')->with($requestUri);
112  $this->widgetRequestBuilder->build();
113  }
114 
118  public function ‪buildSetsBaseUri()
119  {
120  $_SERVER = [
121  'REMOTE_ADDR' => 'foo',
122  'SSL_SESSION_ID' => 'foo',
123  'REQUEST_URI' => 'foo',
124  'SCRIPT_NAME' => 'foo',
125  'REQUEST_METHOD' => 'foo'
126  ];
127  $_GET = [
128  'fluid-widget-id' => 'foo'
129  ];
130  $baseUri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
131  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
132  $this->mockWidgetRequest->expects(self::once())->method('setBaseURI')->with($baseUri);
133  $this->widgetRequestBuilder->build();
134  }
135 
139  public function ‪buildSetsRequestMethod()
140  {
141  $_SERVER = [
142  'REMOTE_ADDR' => 'foo',
143  'SSL_SESSION_ID' => 'foo',
144  'REQUEST_URI' => 'foo',
145  'SCRIPT_NAME' => 'foo',
146  'REQUEST_METHOD' => 'POST'
147  ];
148  $_GET = [
149  'fluid-widget-id' => 'foo'
150  ];
151  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
152  $this->mockWidgetRequest->expects(self::once())->method('setMethod')->with('POST');
153  $this->widgetRequestBuilder->build();
154  }
155 
159  public function ‪buildSetsPostArgumentsFromRequest()
160  {
161  $_SERVER = [
162  'REMOTE_ADDR' => 'foo',
163  'SSL_SESSION_ID' => 'foo',
164  'REQUEST_URI' => 'foo',
165  'SCRIPT_NAME' => 'foo',
166  'REQUEST_METHOD' => 'POST'
167  ];
168  $_GET = [
169  'get' => 'foo',
170  'fluid-widget-id' => 'foo'
171  ];
172  $_POST = [
173  'post' => 'bar'
174  ];
175  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
176  $this->mockWidgetRequest->expects(self::once())->method('setArguments')->with($_POST);
177  $this->widgetRequestBuilder->build();
178  }
179 
183  public function ‪buildSetsGetArgumentsFromRequest()
184  {
185  $_SERVER = [
186  'REMOTE_ADDR' => 'foo',
187  'SSL_SESSION_ID' => 'foo',
188  'REQUEST_URI' => 'foo',
189  'SCRIPT_NAME' => 'foo',
190  'REQUEST_METHOD' => 'GET'
191  ];
192  $_GET = [
193  'get' => 'foo',
194  'fluid-widget-id' => 'foo'
195  ];
196  $_POST = [
197  'post' => 'bar'
198  ];
199  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
200  $this->mockWidgetRequest->expects(self::once())->method('setArguments')->with($_GET);
201  $this->widgetRequestBuilder->build();
202  }
203 
208  {
209  $_SERVER = [
210  'REMOTE_ADDR' => 'foo',
211  'SSL_SESSION_ID' => 'foo',
212  'REQUEST_URI' => 'foo',
213  'SCRIPT_NAME' => 'foo',
214  'REQUEST_METHOD' => 'foo'
215  ];
216  $_GET = [
217  'action' => 'myAction',
218  'fluid-widget-id' => 'foo'
219  ];
220  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
221  $this->mockWidgetRequest->expects(self::once())->method('setControllerActionName')->with('myAction');
222  $this->widgetRequestBuilder->build();
223  }
224 
228  public function ‪buildSetsWidgetContext()
229  {
230  $_SERVER = [
231  'REMOTE_ADDR' => 'foo',
232  'SSL_SESSION_ID' => 'foo',
233  'REQUEST_URI' => 'foo',
234  'SCRIPT_NAME' => 'foo',
235  'REQUEST_METHOD' => 'foo'
236  ];
237  $_GET = [
238  'fluid-widget-id' => '123'
239  ];
240  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
241  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->with('123')->willReturn($this->mockWidgetContext);
242  $this->mockWidgetRequest->expects(self::once())->method('setWidgetContext')->with($this->mockWidgetContext);
243  $this->widgetRequestBuilder->build();
244  }
245 
249  public function ‪buildReturnsRequest()
250  {
251  $_SERVER = [
252  'REMOTE_ADDR' => 'foo',
253  'SSL_SESSION_ID' => 'foo',
254  'REQUEST_URI' => 'foo',
255  'SCRIPT_NAME' => 'foo',
256  'REQUEST_METHOD' => 'foo'
257  ];
258  $_GET = [
259  'fluid-widget-id' => 'foo'
260  ];
261  $this->mockAjaxWidgetContextHolder->expects(self::once())->method('get')->willReturn($this->mockWidgetContext);
262  $expected = ‪$this->mockWidgetRequest;
263  $actual = $this->widgetRequestBuilder->build();
264  self::assertSame($expected, $actual);
265  }
266 }
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequest
Definition: WidgetRequest.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\setUp
‪setUp()
Definition: WidgetRequestBuilderTest.php:52
‪TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder
Definition: WidgetRequestBuilder.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildThrowsIfNoFluidWidgetIdWasSet
‪buildThrowsIfNoFluidWidgetIdWasSet()
Definition: WidgetRequestBuilderTest.php:72
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsGetArgumentsFromRequest
‪buildSetsGetArgumentsFromRequest()
Definition: WidgetRequestBuilderTest.php:178
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\$mockWidgetRequest
‪WidgetRequest $mockWidgetRequest
Definition: WidgetRequestBuilderTest.php:42
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsControllerActionNameFromGetArguments
‪buildSetsControllerActionNameFromGetArguments()
Definition: WidgetRequestBuilderTest.php:202
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsPostArgumentsFromRequest
‪buildSetsPostArgumentsFromRequest()
Definition: WidgetRequestBuilderTest.php:154
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsBaseUri
‪buildSetsBaseUri()
Definition: WidgetRequestBuilderTest.php:113
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\$mockAjaxWidgetContextHolder
‪AjaxWidgetContextHolder $mockAjaxWidgetContextHolder
Definition: WidgetRequestBuilderTest.php:46
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:27
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildReturnsRequest
‪buildReturnsRequest()
Definition: WidgetRequestBuilderTest.php:244
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsRequestMethod
‪buildSetsRequestMethod()
Definition: WidgetRequestBuilderTest.php:134
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget
Definition: AbstractWidgetControllerTest.php:16
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsWidgetContext
‪buildSetsWidgetContext()
Definition: WidgetRequestBuilderTest.php:223
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext
Definition: WidgetContext.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\$mockWidgetContext
‪WidgetContext $mockWidgetContext
Definition: WidgetRequestBuilderTest.php:50
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\$mockObjectManager
‪ObjectManagerInterface $mockObjectManager
Definition: WidgetRequestBuilderTest.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\buildSetsRequestUri
‪buildSetsRequestUri()
Definition: WidgetRequestBuilderTest.php:92
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest\$widgetRequestBuilder
‪WidgetRequestBuilder $widgetRequestBuilder
Definition: WidgetRequestBuilderTest.php:34
‪TYPO3\CMS\Fluid\Tests\Unit\Core\Widget\WidgetRequestBuilderTest
Definition: WidgetRequestBuilderTest.php:31