Merge "[BUGFIX] ConfigurationManager use FrontendSimulator only if neccessary"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / Web / RequestBuilderTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 class Tx_Extbase_Tests_Unit_MVC_Web_RequestBuilderTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @var Tx_Extbase_MVC_Web_RequestBuilder
32 */
33 protected $requestBuilder;
34
35 /**
36 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
37 */
38 protected $mockConfigurationManager;
39
40 /**
41 * @var array
42 */
43 protected $configuration;
44
45 /**
46 * @var Tx_Extbase_Object_ObjectManagerInterface
47 */
48 protected $mockObjectManager;
49
50 /**
51 * @var Tx_Extbase_Service_ExtensionService
52 */
53 protected $mockExtensionService;
54
55 /**
56 * @var Tx_Extbase_MVC_Web_Request
57 */
58 protected $mockRequest;
59
60 /**
61 * @var array
62 */
63 protected $getBackup = array();
64
65 /**
66 * @var array
67 */
68 protected $postBackup = array();
69
70 /**
71 * @var array
72 */
73 protected $serverBackup = array();
74
75 public function setUp() {
76 $this->requestBuilder = $this->getAccessibleMock('Tx_Extbase_MVC_Web_RequestBuilder', array('dummy'));
77 $this->configuration = array(
78 'userFunc' => 'Tx_Extbase_Dispatcher->dispatch',
79 'pluginName' => 'Pi1',
80 'extensionName' => 'MyExtension',
81 'controller' => 'TheFirstController',
82 'action' => 'show',
83 'controllerConfiguration' => array(
84 'TheFirstController' => array(
85 'actions' => array('show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test')
86 ),
87 'TheSecondController' => array(
88 'actions' => array('show', 'index')
89 ),
90 'TheThirdController' => array(
91 'actions' => array('delete', 'create', 'onlyInThirdController')
92 )
93 )
94 );
95 $this->mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
96 $this->mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request');
97 $this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
98 $this->mockExtensionService = $this->getMock('Tx_Extbase_Service_ExtensionService');
99
100 $this->getBackup = $_GET;
101 $this->postBackup = $_POST;
102 $this->serverBackup = $_SERVER;
103 }
104
105 /**
106 * @return void
107 */
108 protected function injectDependencies() {
109 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
110 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
111
112 $this->mockObjectManager->expects($this->any())->method('create')->with('Tx_Extbase_MVC_Web_Request')->will($this->returnValue($this->mockRequest));
113 $this->requestBuilder->injectObjectManager($this->mockObjectManager);
114
115 $pluginNamespace = 'tx_' . strtolower($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']);
116 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
117 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
118 }
119
120 public function tearDown() {
121 $_GET = $this->getBackup;
122 $_POST = $this->postBackup;
123 $_SERVER = $this->serverBackup;
124 }
125
126 /**
127 * @test
128 */
129 public function buildReturnsAWebRequestObject() {
130 $this->injectDependencies();
131 $request = $this->requestBuilder->build();
132 $this->assertSame($this->mockRequest, $request);
133 }
134
135 /**
136 * @test
137 */
138 public function buildSetsRequestPluginName() {
139 $this->injectDependencies();
140 $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1');
141 $this->requestBuilder->build();
142 }
143
144 /**
145 * @test
146 */
147 public function buildSetsRequestControllerExtensionName() {
148 $this->injectDependencies();
149 $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension');
150 $this->requestBuilder->build();
151 }
152
153 /**
154 * @test
155 */
156 public function buildSetsRequestControllerName() {
157 $this->injectDependencies();
158 $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
159 $this->requestBuilder->build();
160 }
161
162 /**
163 * @test
164 */
165 public function buildSetsRequestControllerActionName() {
166 $this->injectDependencies();
167 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show');
168 $this->requestBuilder->build();
169 }
170
171 /**
172 * @test
173 */
174 public function buildSetsRequestRequestUri() {
175 $this->injectDependencies();
176 $expectedRequestUri = t3lib_div::getIndpEnv('TYPO3_REQUEST_URL');
177 $this->mockRequest->expects($this->once())->method('setRequestUri')->with($expectedRequestUri);
178 $this->requestBuilder->build();
179 }
180
181 /**
182 * @test
183 */
184 public function buildSetsRequestBaseUri() {
185 $this->injectDependencies();
186 $expectedBaseUri = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
187 $this->mockRequest->expects($this->once())->method('setBaseUri')->with($expectedBaseUri);
188 $this->requestBuilder->build();
189 }
190
191 /**
192 * @test
193 */
194 public function buildSetsRequestMethod() {
195 $this->injectDependencies();
196 $_SERVER['REQUEST_METHOD'] = 'SomeRequestMethod';
197 $expectedMethod = 'SomeRequestMethod';
198 $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
199 $this->requestBuilder->build();
200 }
201
202
203 /**
204 * @test
205 * @expectedException Tx_Extbase_MVC_Exception
206 */
207 public function buildThrowsExceptionIfExtensionNameIsNotConfigured() {
208 unset($this->configuration['extensionName']);
209 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
210 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
211 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager);
212 $this->requestBuilder->build();
213 }
214
215 /**
216 * @test
217 * @expectedException Tx_Extbase_MVC_Exception
218 */
219 public function buildThrowsExceptionIfPluginNameIsNotConfigured() {
220 unset($this->configuration['pluginName']);
221 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
222 $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
223 $this->requestBuilder->injectConfigurationManager($mockConfigurationManager);
224 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
225 $this->requestBuilder->build();
226 }
227
228 /**
229 * @test
230 * @expectedException Tx_Extbase_MVC_Exception
231 */
232 public function buildThrowsExceptionIfControllerConfigurationIsEmptyOrNotSet() {
233 unset($this->configuration['controllerConfiguration']);
234 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
235 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
236 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
237 $this->requestBuilder->build();
238 }
239
240 /**
241 * @test
242 * @expectedException Tx_Extbase_MVC_Exception
243 */
244 public function buildThrowsExceptionIfControllerConfigurationHasNoDefaultActionDefined() {
245 $this->configuration['controllerConfiguration']['TheFirstController'] = array();
246 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
247 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
248 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
249 $this->requestBuilder->build();
250 }
251
252 /**
253 * @test
254 * @expectedException Tx_Extbase_MVC_Exception
255 */
256 public function buildThrowsExceptionIfNoDefaultControllerCanBeResolved() {
257 $this->configuration['controllerConfiguration'] = array(
258 '' => array(
259 'actions' => array('foo')
260 )
261 );
262 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
263 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
264 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
265 $this->requestBuilder->build();
266 }
267
268 /**
269 * @test
270 */
271 public function buildSetsParametersFromGetAndPostVariables() {
272 $this->configuration['extensionName'] = 'SomeExtensionName';
273 $this->configuration['pluginName'] = 'SomePluginName';
274 $this->injectDependencies();
275
276 $_GET = array(
277 'tx_someotherextensionname_somepluginname' => array(
278 'foo' => 'bar'
279 ),
280 'tx_someextensionname_somepluginname' => array(
281 'parameter1' => 'valueGetsOverwritten',
282 'parameter2' => array(
283 'parameter3' => 'value3'
284 )
285 )
286 );
287 $_POST = array(
288 'tx_someextensionname_someotherpluginname' => array(
289 'foo' => 'bar'
290 ),
291 'tx_someextensionname_somepluginname' => array(
292 'parameter1' => 'value1',
293 'parameter2' => array(
294 'parameter4' => 'value4'
295 )
296 )
297 );
298 $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter1', 'value1');
299 $this->mockRequest->expects($this->at(9))->method('setArgument')->with('parameter2', array('parameter3' => 'value3', 'parameter4' => 'value4'));
300
301 $this->requestBuilder->build();
302 }
303
304 /**
305 * @test
306 */
307 public function buildSetsFormatFromGetAndPostVariables() {
308 $this->configuration['extensionName'] = 'SomeExtensionName';
309 $this->configuration['pluginName'] = 'SomePluginName';
310 $this->injectDependencies();
311
312 $_GET = array(
313 'tx_someextensionname_somepluginname' => array(
314 'format' => 'GET',
315 )
316 );
317 $_POST = array(
318 'tx_someextensionname_somepluginname' => array(
319 'format' => 'POST',
320 )
321 );
322 $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST');
323
324 $this->requestBuilder->build();
325 }
326
327 /**
328 * @test
329 */
330 public function buildCorrectlySetsAllowedControllerActions() {
331 $this->injectDependencies();
332 $expectedResult = array(
333 'TheFirstController' => array(
334 'show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test'
335 ),
336 'TheSecondController' => array(
337 'show', 'index'
338 ),
339 'TheThirdController' => array(
340 'delete', 'create', 'onlyInThirdController'
341 )
342 );
343 $this->requestBuilder->build();
344 $actualResult = $this->requestBuilder->_get('allowedControllerActions');
345 $this->assertEquals($expectedResult, $actualResult);
346 }
347
348 /**
349 * @test
350 * @expectedException Tx_Extbase_MVC_Exception
351 */
352 public function buildThrowsExceptionIfDefaultControllerCantBeDetermined() {
353 $this->configuration['controllerConfiguration'] = array();
354
355 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
356 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
357
358 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
359 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
360
361 $this->requestBuilder->build();
362 }
363
364 /**
365 * @test
366 */
367 public function buildSetsDefaultControllerIfNoControllerIsSpecified() {
368 $this->injectDependencies();
369 $_GET = array(
370 'tx_myextension_pi1' => array(
371 'foo' => 'bar',
372 )
373 );
374 $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
375 $this->requestBuilder->build();
376 }
377
378 /**
379 * @test
380 */
381 public function buildCorrectlySetsSpecifiedControllerNameIfItsAllowedForTheCurrentPlugin() {
382 $this->injectDependencies();
383 $_GET = array(
384 'tx_myextension_pi1' => array(
385 'controller' => 'TheSecondController',
386 )
387 );
388 $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheSecondController');
389 $this->requestBuilder->build();
390 }
391
392 /**
393 * @test
394 * @expectedException Tx_Extbase_MVC_Exception_InvalidControllerName
395 */
396 public function buildThrowsInvalidControllerNameExceptionIfSpecifiedControllerIsNotAllowed() {
397 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
398 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
399
400 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
401 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
402
403 $_GET = array(
404 'tx_myextension_pi1' => array(
405 'controller' => 'SomeIllegalController',
406 )
407 );
408 $this->requestBuilder->build();
409 }
410
411 /**
412 * @test
413 * @expectedException t3lib_error_http_PageNotFoundException
414 */
415 public function buildThrowsPageNotFoundExceptionIfEnabledAndSpecifiedControllerIsNotAllowed() {
416 $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
417 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
418 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
419
420 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
421 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
422
423 $_GET = array(
424 'tx_myextension_pi1' => array(
425 'controller' => 'SomeIllegalController',
426 )
427 );
428 $this->requestBuilder->build();
429 }
430
431
432 /**
433 * @test
434 * @expectedException Tx_Extbase_MVC_Exception
435 */
436 public function buildThrowsExceptionIfDefaultActionCantBeDetermined() {
437 $this->configuration['controllerConfiguration'] = array();
438
439 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
440 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
441
442 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
443 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
444
445 $this->requestBuilder->build();
446 }
447
448 /**
449 * @test
450 */
451 public function buildSetsDefaultActionOfTheCurrentControllerIfNoActionIsSpecified() {
452 $this->injectDependencies();
453 $_GET = array(
454 'tx_myextension_pi1' => array(
455 'controller' => 'TheThirdController',
456 )
457 );
458 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
459 $this->requestBuilder->build();
460 }
461
462 /**
463 * @test
464 */
465 public function buildCorrectlySetsSpecifiedActionNameForTheDefaultControllerIfItsAllowedForTheCurrentPlugin() {
466 $this->injectDependencies();
467 $_GET = array(
468 'tx_myextension_pi1' => array(
469 'action' => 'create',
470 )
471 );
472 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('create');
473 $this->requestBuilder->build();
474 }
475
476 /**
477 * @test
478 */
479 public function buildCorrectlySetsSpecifiedActionNameForTheSpecifiedControllerIfItsAllowedForTheCurrentPlugin() {
480 $this->injectDependencies();
481 $_GET = array(
482 'tx_myextension_pi1' => array(
483 'controller' => 'TheThirdController',
484 'action' => 'onlyInThirdController',
485 )
486 );
487 $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('onlyInThirdController');
488 $this->requestBuilder->build();
489 }
490
491 /**
492 * @test
493 * @expectedException Tx_Extbase_MVC_Exception_InvalidActionName
494 */
495 public function buildThrowsInvalidActionNameExceptionIfSpecifiedActionIsNotAllowed() {
496 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
497 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
498
499 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
500 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
501
502 $_GET = array(
503 'tx_myextension_pi1' => array(
504 'action' => 'someIllegalAction',
505 )
506 );
507 $this->requestBuilder->build();
508 }
509
510 /**
511 * @test
512 * @expectedException t3lib_error_http_PageNotFoundException
513 */
514 public function buildThrowsPageNotFoundExceptionIfEnabledAndSpecifiedActionIsNotAllowed() {
515 $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
516 $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
517 $this->requestBuilder->injectConfigurationManager($this->mockConfigurationManager);
518
519 $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
520 $this->requestBuilder->injectExtensionService($this->mockExtensionService);
521
522 $_GET = array(
523 'tx_myextension_pi1' => array(
524 'action' => 'someIllegalAction',
525 )
526 );
527 $this->requestBuilder->build();
528 }
529
530 }
531 ?>