[TASK] Make WidgetRequestBuilderTest notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / Widget / WidgetRequestBuilderTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\Widget;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
19 use TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder;
20 use TYPO3\CMS\Fluid\Core\Widget\WidgetContext;
21 use TYPO3\CMS\Fluid\Core\Widget\WidgetRequest;
22 use TYPO3\CMS\Fluid\Core\Widget\WidgetRequestBuilder;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24
25 /**
26 * Test case
27 */
28 class WidgetRequestBuilderTest extends UnitTestCase
29 {
30 /**
31 * @var WidgetRequestBuilder
32 */
33 protected $widgetRequestBuilder;
34
35 /**
36 * @var ObjectManagerInterface
37 */
38 protected $mockObjectManager;
39
40 /**
41 * @var WidgetRequest
42 */
43 protected $mockWidgetRequest;
44
45 /**
46 * @var AjaxWidgetContextHolder
47 */
48 protected $mockAjaxWidgetContextHolder;
49
50 /**
51 * @var WidgetContext
52 */
53 protected $mockWidgetContext;
54
55 /**
56 *
57 */
58 protected function setUp()
59 {
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($this->once())->method('get')->with(WidgetRequest::class)->will($this->returnValue($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 }
69
70 /**
71 * @test
72 */
73 public function buildThrowsIfNoFluidWidgetIdWasSet()
74 {
75 $_SERVER = [
76 'REMOTE_ADDR' => 'foo',
77 'SSL_SESSION_ID' => 'foo',
78 'REQUEST_URI' => 'foo',
79 'ORIG_SCRIPT_NAME' => 'foo',
80 'REQUEST_METHOD' => 'foo'
81 ];
82 $_GET = [
83 'not-the-fluid-widget-id' => 'foo'
84 ];
85 $this->expectException(\InvalidArgumentException::class);
86 $this->expectExceptionCode(1521190675);
87 $this->widgetRequestBuilder->build();
88 }
89
90 /**
91 * @test
92 */
93 public function buildSetsRequestUri()
94 {
95 $_SERVER = [
96 'REMOTE_ADDR' => 'foo',
97 'SSL_SESSION_ID' => 'foo',
98 'REQUEST_URI' => 'foo',
99 'ORIG_SCRIPT_NAME' => 'foo',
100 'REQUEST_METHOD' => 'foo'
101 ];
102 $_GET = [
103 'fluid-widget-id' => 'foo'
104 ];
105 $requestUri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
106 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
107 $this->mockWidgetRequest->expects($this->once())->method('setRequestURI')->with($requestUri);
108 $this->widgetRequestBuilder->build();
109 }
110
111 /**
112 * @test
113 */
114 public function buildSetsBaseUri()
115 {
116 $_SERVER = [
117 'REMOTE_ADDR' => 'foo',
118 'SSL_SESSION_ID' => 'foo',
119 'REQUEST_URI' => 'foo',
120 'ORIG_SCRIPT_NAME' => 'foo',
121 'REQUEST_METHOD' => 'foo'
122 ];
123 $_GET = [
124 'fluid-widget-id' => 'foo'
125 ];
126 $baseUri = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
127 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
128 $this->mockWidgetRequest->expects($this->once())->method('setBaseURI')->with($baseUri);
129 $this->widgetRequestBuilder->build();
130 }
131
132 /**
133 * @test
134 */
135 public function buildSetsRequestMethod()
136 {
137 $_SERVER = [
138 'REMOTE_ADDR' => 'foo',
139 'SSL_SESSION_ID' => 'foo',
140 'REQUEST_URI' => 'foo',
141 'ORIG_SCRIPT_NAME' => 'foo',
142 'REQUEST_METHOD' => 'POST'
143 ];
144 $_GET = [
145 'fluid-widget-id' => 'foo'
146 ];
147 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
148 $this->mockWidgetRequest->expects($this->once())->method('setMethod')->with('POST');
149 $this->widgetRequestBuilder->build();
150 }
151
152 /**
153 * @test
154 */
155 public function buildSetsPostArgumentsFromRequest()
156 {
157 $_SERVER = [
158 'REMOTE_ADDR' => 'foo',
159 'SSL_SESSION_ID' => 'foo',
160 'REQUEST_URI' => 'foo',
161 'ORIG_SCRIPT_NAME' => 'foo',
162 'REQUEST_METHOD' => 'POST'
163 ];
164 $_GET = [
165 'get' => 'foo',
166 'fluid-widget-id' => 'foo'
167 ];
168 $_POST = [
169 'post' => 'bar'
170 ];
171 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
172 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_POST);
173 $this->widgetRequestBuilder->build();
174 }
175
176 /**
177 * @test
178 */
179 public function buildSetsGetArgumentsFromRequest()
180 {
181 $_SERVER = [
182 'REMOTE_ADDR' => 'foo',
183 'SSL_SESSION_ID' => 'foo',
184 'REQUEST_URI' => 'foo',
185 'ORIG_SCRIPT_NAME' => 'foo',
186 'REQUEST_METHOD' => 'GET'
187 ];
188 $_GET = [
189 'get' => 'foo',
190 'fluid-widget-id' => 'foo'
191 ];
192 $_POST = [
193 'post' => 'bar'
194 ];
195 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
196 $this->mockWidgetRequest->expects($this->once())->method('setArguments')->with($_GET);
197 $this->widgetRequestBuilder->build();
198 }
199
200 /**
201 * @test
202 */
203 public function buildSetsControllerActionNameFromGetArguments()
204 {
205 $_SERVER = [
206 'REMOTE_ADDR' => 'foo',
207 'SSL_SESSION_ID' => 'foo',
208 'REQUEST_URI' => 'foo',
209 'ORIG_SCRIPT_NAME' => 'foo',
210 'REQUEST_METHOD' => 'foo'
211 ];
212 $_GET = [
213 'action' => 'myAction',
214 'fluid-widget-id' => 'foo'
215 ];
216 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
217 $this->mockWidgetRequest->expects($this->once())->method('setControllerActionName')->with('myAction');
218 $this->widgetRequestBuilder->build();
219 }
220
221 /**
222 * @test
223 */
224 public function buildSetsWidgetContext()
225 {
226 $_SERVER = [
227 'REMOTE_ADDR' => 'foo',
228 'SSL_SESSION_ID' => 'foo',
229 'REQUEST_URI' => 'foo',
230 'ORIG_SCRIPT_NAME' => 'foo',
231 'REQUEST_METHOD' => 'foo'
232 ];
233 $_GET = [
234 'fluid-widget-id' => '123'
235 ];
236 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
237 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->with('123')->will($this->returnValue($this->mockWidgetContext));
238 $this->mockWidgetRequest->expects($this->once())->method('setWidgetContext')->with($this->mockWidgetContext);
239 $this->widgetRequestBuilder->build();
240 }
241
242 /**
243 * @test
244 */
245 public function buildReturnsRequest()
246 {
247 $_SERVER = [
248 'REMOTE_ADDR' => 'foo',
249 'SSL_SESSION_ID' => 'foo',
250 'REQUEST_URI' => 'foo',
251 'ORIG_SCRIPT_NAME' => 'foo',
252 'REQUEST_METHOD' => 'foo'
253 ];
254 $_GET = [
255 'fluid-widget-id' => 'foo'
256 ];
257 $this->mockAjaxWidgetContextHolder->expects($this->once())->method('get')->will($this->returnValue($this->mockWidgetContext));
258 $expected = $this->mockWidgetRequest;
259 $actual = $this->widgetRequestBuilder->build();
260 $this->assertSame($expected, $actual);
261 }
262 }