[+FEATURE] Backport CommandController Implementation
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / MVC / Controller / ActionControllerTest.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_Controller_ActionControllerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
29
30 /**
31 * @var Tx_Extbase_MVC_Controller_ActionController
32 */
33 protected $actionController;
34
35 /**
36 * @var Tx_Extbase_Object_ObjectManagerInterface
37 */
38 protected $mockObjectManager;
39
40 /**
41 * @var Tx_Extbase_MVC_Web_Routing_UriBuilder
42 */
43 protected $mockUriBuilder;
44
45 public function setUp() {
46 $this->actionController = $this->getAccessibleMock('Tx_Extbase_MVC_Controller_ActionController');
47 //$this->mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
48 //$this->mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
49 //$this->mockObjectManager->expects($this->any())->method('create')->with('Tx_Extbase_MVC_Web_Routing_UriBuilder')->will($this->returnValue($this->mockUriBuilder));
50 //$this->actionController->_set('objectManager', $this->mockObjectManager);
51 }
52
53 /**
54 * @test
55 */
56 public function processRequestSticksToSpecifiedSequence() {
57 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array(), array(), '', FALSE);
58 $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
59
60 $mockUriBuilder = $this->getMock('Tx_Extbase_MVC_Web_Routing_UriBuilder');
61 $mockUriBuilder->expects($this->once())->method('setRequest')->with($mockRequest);
62
63 $mockControllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array(), array(), '', FALSE);
64
65 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
66 $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_MVC_Web_Routing_UriBuilder')->will($this->returnValue($mockUriBuilder));
67
68 $mockResponse = $this->getMock('Tx_Extbase_MVC_Web_Response', array(), array(), '', FALSE);
69
70 $mockView = $this->getMock('Tx_Extbase_MVC_View_ViewInterface');
71
72 $mockController = $this->getAccessibleMock('Tx_Extbase_MVC_Controller_ActionController', array(
73 'initializeFooAction', 'initializeAction', 'resolveActionMethodName', 'initializeActionMethodArguments',
74 'initializeActionMethodValidators', 'mapRequestArgumentsToControllerArguments', 'buildControllerContext',
75 'resolveView', 'initializeView', 'callActionMethod'),
76 array(), '', FALSE);
77
78 $this->enableDeprecatedPropertyMapperInController($mockController);
79
80 $mockController->_set('objectManager', $mockObjectManager);
81 $mockController->expects($this->at(0))->method('resolveActionMethodName')->will($this->returnValue('fooAction'));
82 $mockController->expects($this->at(1))->method('initializeActionMethodArguments');
83 $mockController->expects($this->at(2))->method('initializeActionMethodValidators');
84 $mockController->expects($this->at(3))->method('initializeAction');
85 $mockController->expects($this->at(4))->method('initializeFooAction');
86 $mockController->expects($this->at(5))->method('mapRequestArgumentsToControllerArguments');
87 $mockController->expects($this->at(6))->method('checkRequestHash');
88 $mockController->expects($this->at(7))->method('buildControllerContext');
89 $mockController->expects($this->at(8))->method('resolveView');
90
91 $mockController->processRequest($mockRequest, $mockResponse);
92 $this->assertSame($mockRequest, $mockController->_get('request'));
93 $this->assertSame($mockResponse, $mockController->_get('response'));
94 }
95
96 /**
97 * @test
98 * @author Robert Lemke <robert@typo3.org>
99 */
100 public function callActionMethodAppendsStringsReturnedByActionMethodToTheResponseObject() {
101 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
102
103 $mockResponse = $this->getMock('Tx_Extbase_MVC_ResponseInterface', array(), array(), '', FALSE);
104 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
105
106 $mockArguments = new ArrayObject;
107
108 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
109 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
110
111 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
112 $this->enableDeprecatedPropertyMapperInController($mockController);
113 $mockController->expects($this->once())->method('fooAction')->will($this->returnValue('the returned string'));
114 $mockController->_set('request', $mockRequest);
115 $mockController->_set('response', $mockResponse);
116 $mockController->_set('arguments', $mockArguments);
117 $mockController->_set('actionMethodName', 'fooAction');
118 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
119 $mockController->_call('callActionMethod');
120 }
121
122 /**
123 * @test
124 * @author Robert Lemke <robert@typo3.org>
125 */
126 public function callActionMethodRendersTheViewAutomaticallyIfTheActionReturnedNullAndAViewExists() {
127 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
128
129 $mockResponse = $this->getMock('Tx_Extbase_MVC_ResponseInterface', array(), array(), '', FALSE);
130 $mockResponse->expects($this->once())->method('appendContent')->with('the view output');
131
132 $mockView = $this->getMock('Tx_Extbase_MVC_View_ViewInterface');
133 $mockView->expects($this->once())->method('render')->will($this->returnValue('the view output'));
134
135 $mockArguments = new ArrayObject;
136
137 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
138 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
139
140 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
141 $this->enableDeprecatedPropertyMapperInController($mockController);
142 $mockController->expects($this->once())->method('fooAction');
143 $mockController->_set('request', $mockRequest);
144 $mockController->_set('response', $mockResponse);
145 $mockController->_set('arguments', $mockArguments);
146 $mockController->_set('actionMethodName', 'fooAction');
147 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
148 $mockController->_set('view', $mockView);
149 $mockController->_call('callActionMethod');
150 }
151
152 /**
153 * @test
154 * @author Robert Lemke <robert@typo3.org>
155 */
156 public function callActionMethodCallsTheErrorActionIfTheMappingResultsHaveErrors() {
157 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
158
159 $mockResponse = $this->getMock('Tx_Extbase_MVC_ResponseInterface', array(), array(), '', FALSE);
160 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
161
162 $mockArguments = new ArrayObject;
163
164 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
165 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(TRUE));
166
167 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('barAction', 'initializeAction'), array(), '', FALSE);
168 $this->enableDeprecatedPropertyMapperInController($mockController);
169 $mockController->expects($this->once())->method('barAction')->will($this->returnValue('the returned string'));
170 $mockController->_set('request', $mockRequest);
171 $mockController->_set('response', $mockResponse);
172 $mockController->_set('arguments', $mockArguments);
173 $mockController->_set('actionMethodName', 'fooAction');
174 $mockController->_set('errorMethodName', 'barAction');
175 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
176 $mockController->_call('callActionMethod');
177 }
178
179 /**
180 * @test
181 * @author Sebastian Kurfürst <sebastian@typo3.org>
182 */
183 public function callActionMethodPassesDefaultValuesAsArguments() {
184 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
185
186 $mockResponse = $this->getMock('Tx_Extbase_MVC_ResponseInterface', array(), array(), '', FALSE);
187
188 $arguments = new ArrayObject();
189 $optionalArgument = new Tx_Extbase_MVC_Controller_Argument('name1', 'Text');
190 $optionalArgument->setDefaultValue('Default value');
191 $arguments[] = $optionalArgument;
192
193 $mockArgumentMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array(), array(), '', FALSE);
194 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
195
196 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'initializeAction'), array(), '', FALSE);
197 $this->enableDeprecatedPropertyMapperInController($mockController);
198 $mockController->expects($this->once())->method('fooAction')->with('Default value');
199 $mockController->_set('request', $mockRequest);
200 $mockController->_set('response', $mockResponse);
201 $mockController->_set('arguments', $arguments);
202 $mockController->_set('actionMethodName', 'fooAction');
203 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
204 $mockController->_call('callActionMethod');
205 }
206
207 /**
208 * @test
209 * @author Karsten Dambekalns <karsten@typo3.org>
210 */
211 public function resolveViewUsesFluidTemplateViewIfTemplateIsAvailable() {
212 $mockSession = $this->getMock('Tx_Extbase_Session_SessionInterface');
213 $mockControllerContext = $this->getMock('Tx_Extbase_MVC_Controller_ControllerContext', array(), array(), '', FALSE);
214
215 $mockFluidTemplateView = $this->getMock('Tx_Extbase_MVC_View_ViewInterface');
216 $mockFluidTemplateView->expects($this->once())->method('setControllerContext')->with($mockControllerContext);
217 $mockFluidTemplateView->expects($this->once())->method('canRender')->with($mockControllerContext)->will($this->returnValue(TRUE));
218
219 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface', array(), array(), '', FALSE);
220 $mockObjectManager->expects($this->at(0))->method('create')->with('Tx_Fluid_View_TemplateView')->will($this->returnValue($mockFluidTemplateView));
221
222 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('buildControllerContext', 'resolveViewObjectName', 'setViewConfiguration'), array(), '', FALSE);
223 $mockController->expects($this->once())->method('resolveViewObjectName')->will($this->returnValue(FALSE));
224 $mockController->_set('session', $mockSession);
225 $mockController->_set('objectManager', $mockObjectManager);
226 $mockController->_set('controllerContext', $mockControllerContext);
227
228 $this->assertSame($mockFluidTemplateView, $mockController->_call('resolveView'));
229 }
230
231 /**
232 * @test
233 * @author Bastian Waidelich <bastian@typo3.org>
234 */
235 public function resolveViewObjectNameUsesViewObjectNamePatternToResolveViewObjectName() {
236 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
237 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
238 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
239 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
240 $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
241
242 $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface', array(), array(), '', FALSE);
243
244 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
245 $mockController->_set('request', $mockRequest);
246 $mockController->_set('objectManager', $mockObjectManager);
247 $mockController->_set('viewObjectNamePattern', 'RandomViewObjectPattern_@package_@controller_@action_@format');
248
249 $mockController->_call('resolveViewObjectName');
250 }
251
252 /**
253 * @test
254 * @author Robert Lemke <robert@typo3.org>
255 */
256 public function resolveActionMethodNameReturnsTheCurrentActionMethodNameFromTheRequest() {
257 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
258 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
259
260 $mockController = $this->getAccessibleMock('Tx_Extbase_MVC_Controller_ActionController', array('fooBarAction'), array(), '', FALSE);
261 $mockController->_set('request', $mockRequest);
262
263 $this->assertEquals('fooBarAction', $mockController->_call('resolveActionMethodName'));
264 }
265
266 /**
267 * @test
268 * @expectedException Tx_Extbase_MVC_Exception_NoSuchAction
269 * @author Robert Lemke <robert@typo3.org>
270 */
271 public function resolveActionMethodNameThrowsAnExceptionIfTheActionDefinedInTheRequestDoesNotExist() {
272 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
273 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
274
275 $mockController = $this->getAccessibleMock('Tx_Extbase_MVC_Controller_ActionController', array('otherBarAction'), array(), '', FALSE);
276 $mockController->_set('request', $mockRequest);
277
278 $mockController->_call('resolveActionMethodName');
279 }
280
281 /**
282 * @test
283 * @author Robert Lemke <robert@typo3.org>
284 */
285 public function initializeActionMethodArgumentsRegistersArgumentsFoundInTheSignatureOfTheCurrentActionMethod() {
286 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
287
288 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array('addNewArgument', 'removeAll'), array(), '', FALSE);
289 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', TRUE);
290 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', TRUE);
291 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'F3_Foo_Bar', TRUE);
292
293 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
294
295 $methodParameters = array(
296 'stringArgument' => array(
297 'position' => 0,
298 'byReference' => FALSE,
299 'array' => FALSE,
300 'optional' => FALSE,
301 'allowsNull' => FALSE,
302 'type' => 'string'
303 ),
304 'integerArgument' => array(
305 'position' => 1,
306 'byReference' => FALSE,
307 'array' => FALSE,
308 'optional' => FALSE,
309 'allowsNull' => FALSE,
310 'type' => 'integer'
311 ),
312 'objectArgument' => array(
313 'position' => 2,
314 'byReference' => FALSE,
315 'array' => FALSE,
316 'optional' => FALSE,
317 'allowsNull' => FALSE,
318 'type' => 'F3_Foo_Bar'
319 )
320 );
321
322 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
323 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
324
325 $mockController->injectReflectionService($mockReflectionService);
326 $mockController->_set('request', $mockRequest);
327 $mockController->_set('arguments', $mockArguments);
328 $mockController->_set('actionMethodName', 'fooAction');
329 $mockController->_call('initializeActionMethodArguments');
330 }
331
332 /**
333 * @test
334 * @author Robert Lemke <robert@typo3.org>
335 */
336 public function initializeActionMethodArgumentsRegistersOptionalArgumentsAsSuch() {
337 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
338
339 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array(), array(), '', FALSE);
340 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'string', TRUE);
341 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', FALSE, array(21));
342 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'string', FALSE, 42);
343
344 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
345
346 $methodParameters = array(
347 'arg1' => array(
348 'position' => 0,
349 'byReference' => FALSE,
350 'array' => FALSE,
351 'optional' => FALSE,
352 'allowsNull' => FALSE,
353 'type' => 'string'
354 ),
355 'arg2' => array(
356 'position' => 1,
357 'byReference' => FALSE,
358 'array' => TRUE,
359 'optional' => TRUE,
360 'defaultValue' => array(21),
361 'allowsNull' => FALSE
362 ),
363 'arg3' => array(
364 'position' => 2,
365 'byReference' => FALSE,
366 'array' => FALSE,
367 'optional' => TRUE,
368 'defaultValue' => 42,
369 'allowsNull' => FALSE,
370 'type' => 'string'
371 )
372 );
373
374 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
375 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
376
377 $mockController->injectReflectionService($mockReflectionService);
378 $mockController->_set('request', $mockRequest);
379 $mockController->_set('arguments', $mockArguments);
380 $mockController->_set('actionMethodName', 'fooAction');
381 $mockController->_call('initializeActionMethodArguments');
382 }
383
384 /**
385 * @test
386 * @author Sebastian Kurfürst <sbastian@typo3.org>
387 * @expectedException Tx_Extbase_MVC_Exception_InvalidArgumentType
388 */
389 public function initializeActionMethodArgumentsThrowsExceptionIfDataTypeWasNotSpecified() {
390 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
391
392 $mockArguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array(), array(), '', FALSE);
393
394 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
395
396 $methodParameters = array(
397 'arg1' => array(
398 'position' => 0,
399 'byReference' => FALSE,
400 'array' => FALSE,
401 'optional' => FALSE,
402 'allowsNull' => FALSE,
403 )
404 );
405
406 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
407 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
408
409 $mockController->injectReflectionService($mockReflectionService);
410 $mockController->_set('request', $mockRequest);
411 $mockController->_set('arguments', $mockArguments);
412 $mockController->_set('actionMethodName', 'fooAction');
413 $mockController->_call('initializeActionMethodArguments');
414 }
415
416 /**
417 * @test
418 * @author Sebastian Kurfürst <sbastian@typo3.org>
419 */
420 public function initializeActionMethodValidatorsCorrectlyRegistersValidatorsBasedOnDataType() {
421 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
422 $this->enableDeprecatedPropertyMapperInController($mockController);
423
424 $argument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName'), array(), '', FALSE);
425 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
426
427 $arguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array('dummy'), array(), '', FALSE);
428 $arguments->addArgument($argument);
429
430 $methodTagsValues = array(
431
432 );
433
434 $methodArgumentsValidatorConjunctions = array();
435 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
436
437 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
438 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
439
440 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
441 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
442
443 $mockController->injectReflectionService($mockReflectionService);
444 $mockController->injectValidatorResolver($mockValidatorResolver);
445 $mockController->_set('arguments', $arguments);
446 $mockController->_set('actionMethodName', 'fooAction');
447 $mockController->_call('initializeActionMethodValidators');
448
449 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
450 }
451
452 /**
453 * @test
454 * @author Sebastian Kurfürst <sbastian@typo3.org>
455 */
456 public function initializeActionMethodValidatorsRegistersModelBasedValidators() {
457 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
458 $this->enableDeprecatedPropertyMapperInController($mockController);
459
460 $argument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getDataType'), array(), '', FALSE);
461 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
462 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
463
464 $arguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array('dummy'), array(), '', FALSE);
465 $arguments->addArgument($argument);
466
467 $methodTagsValues = array(
468
469 );
470
471 $quuxBaseValidatorConjunction = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
472
473 $methodArgumentsValidatorConjunctions = array();
474 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
475 $methodArgumentsValidatorConjunctions['arg1']->expects($this->once())->method('addValidator')->with($quuxBaseValidatorConjunction);
476
477 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
478 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
479
480 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
481 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
482 $mockValidatorResolver->expects($this->once())->method('getBaseValidatorConjunction')->with('F3_Foo_Quux')->will($this->returnValue($quuxBaseValidatorConjunction));
483
484 $mockController->injectReflectionService($mockReflectionService);
485 $mockController->injectValidatorResolver($mockValidatorResolver);
486 $mockController->_set('arguments', $arguments);
487 $mockController->_set('actionMethodName', 'fooAction');
488 $mockController->_call('initializeActionMethodValidators');
489
490 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
491 }
492
493 /**
494 * @test
495 * @author Sebastian Kurfürst <sbastian@typo3.org>
496 */
497 public function initializeActionMethodValidatorsDoesNotRegisterModelBasedValidatorsIfDontValidateAnnotationIsSet() {
498 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('fooAction'), array(), '', FALSE);
499 $this->enableDeprecatedPropertyMapperInController($mockController);
500
501 $argument = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getName', 'getDataType'), array(), '', FALSE);
502 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
503 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
504
505 $arguments = $this->getMock('Tx_Extbase_MVC_Controller_Arguments', array('dummy'), array(), '', FALSE);
506 $arguments->addArgument($argument);
507
508 $methodTagsValues = array(
509 'dontvalidate' => array(
510 '$arg1'
511 )
512 );
513
514 $methodArgumentsValidatorConjunctions = array();
515 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator', array(), array(), '', FALSE);
516
517 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array(), array(), '', FALSE);
518 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
519
520 $mockValidatorResolver = $this->getMock('Tx_Extbase_Validation_ValidatorResolver', array(), array(), '', FALSE);
521 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
522 $mockValidatorResolver->expects($this->any())->method('getBaseValidatorConjunction')->will($this->throwException(new Exception("This should not be called because the dontvalidate annotation is set.")));
523
524 $mockController->injectReflectionService($mockReflectionService);
525 $mockController->injectValidatorResolver($mockValidatorResolver);
526 $mockController->_set('arguments', $arguments);
527 $mockController->_set('actionMethodName', 'fooAction');
528 $mockController->_call('initializeActionMethodValidators');
529
530 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
531 }
532
533 /**
534 * @test
535 * @author Christopher Hlubek <hlubek@networkteam.com>
536 */
537 public function defaultErrorActionSetsArgumentMappingResultsErrorsInRequest() {
538 $mockRequest = $this->getMock('Tx_Extbase_MVC_Request', array(), array(), '', FALSE);
539 $mockFlashMessages = $this->getMock('Tx_Extbase_MVC_Controller_FlashMessages', array(), array(), '', FALSE);
540
541 $mockError = $this->getMock('Tx_Extbase_Error_Error', array('getMessage'), array(), '', FALSE);
542 $mockArgumentsMappingResults = $this->getMock('Tx_Extbase_Property_MappingResults', array('getErrors', 'getWarnings'), array(), '', FALSE);
543 $mockArgumentsMappingResults->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array($mockError)));
544 $mockArgumentsMappingResults->expects($this->any())->method('getWarnings')->will($this->returnValue(array()));
545
546 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('pushFlashMessage', 'clearCacheOnError'), array(), '', FALSE);
547 $this->enableDeprecatedPropertyMapperInController($mockController);
548
549 $mockController->_set('request', $mockRequest);
550 $mockController->_set('flashMessageContainer', $mockFlashMessages);
551 $mockController->_set('argumentsMappingResults', $mockArgumentsMappingResults);
552
553 $mockRequest->expects($this->once())->method('setErrors')->with(array($mockError));
554
555 $mockController->_call('errorAction');
556 }
557
558 /**
559 * @test
560 * @author Christopher Hlubek <hlubek@networkteam.com>
561 */
562 public function defaultErrorActionCallsGetErrorFlashMessageAndPutsFlashMessage() {
563 $this->markTestIncomplete('To be implemented');
564 }
565
566 /**
567 * Data Provider for checkRequestHashDoesNotThrowExceptionInNormalOperations
568 */
569 public function checkRequestHashInNormalOperation() {
570 return array(
571 // HMAC is verified
572 array(TRUE),
573 // HMAC not verified, but objects are directly fetched from persistence layer
574 array(FALSE, FALSE, Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE, Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE),
575 // HMAC not verified, objects new and modified, but dontverifyrequesthash-annotation set
576 array(FALSE, TRUE, Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE, Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE_AND_MODIFIED, array('dontverifyrequesthash' => ''))
577 );
578 }
579
580 /**
581 * @test
582 * @author Sebastian Kurfürst <sebastian@typo3.org>
583 * @dataProvider checkRequestHashInNormalOperation
584 */
585 public function checkRequestHashDoesNotThrowExceptionInNormalOperations($hmacVerified, $reflectionServiceNeedsInitialization = FALSE, $argument1Origin = 3, $argument2Origin = 3, $methodTagsValues = array()) {
586 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array('isHmacVerified'), array(), '', FALSE);
587 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
588
589 $argument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getOrigin'), array(), '', FALSE);
590 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
591 $argument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getOrigin'), array(), '', FALSE);
592 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
593
594 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
595 $this->enableDeprecatedPropertyMapperInController($mockController);
596
597 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array('getMethodTagsValues'), array(), '', FALSE);
598 if ($reflectionServiceNeedsInitialization) {
599 // Somehow this is needed, else I get "Mocked method does not exist."
600 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
601 }
602 $mockController->_set('arguments', array($argument1, $argument2));
603 $mockController->_set('request', $mockRequest);
604 $mockController->_set('actionMethodName', 'fooAction');
605 $mockController->injectReflectionService($mockReflectionService);
606
607 $mockController->_call('checkRequestHash');
608 }
609
610 /**
611 * @test
612 * @expectedException Tx_Extbase_MVC_Exception_InvalidOrNoRequestHash
613 * @author Sebastian Kurfürst <sebastian@typo3.org>
614 */
615 public function checkRequestHashThrowsExceptionIfNeeded() {
616 $hmacVerified = FALSE;
617 $argument1Origin = Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE_AND_MODIFIED;
618 $argument2Origin = Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE;
619 $methodTagsValues = array();
620
621 $mockRequest = $this->getMock('Tx_Extbase_MVC_Web_Request', array('isHmacVerified'), array(), '', FALSE);
622 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
623
624 $argument1 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getOrigin'), array(), '', FALSE);
625 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
626 $argument2 = $this->getMock('Tx_Extbase_MVC_Controller_Argument', array('getOrigin'), array(), '', FALSE);
627 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
628
629 $mockController = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_ActionController'), array('dummy'), array(), '', FALSE);
630 $this->enableDeprecatedPropertyMapperInController($mockController);
631
632 $mockReflectionService = $this->getMock('Tx_Extbase_Reflection_Service', array('getMethodTagsValues'), array(), '', FALSE);
633 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
634
635 $mockController->_set('arguments', array($argument1, $argument2));
636 $mockController->_set('request', $mockRequest);
637 $mockController->_set('actionMethodName', 'fooAction');
638 $mockController->injectReflectionService($mockReflectionService);
639
640 $mockController->_call('checkRequestHash');
641 }
642
643 /**
644 * Helper which enables the deprecated property mapper in the ActionController class.
645 *
646 * @param Tx_Extbase_MVC_Controller_ActionController $actionController
647 */
648 protected function enableDeprecatedPropertyMapperInController(Tx_Extbase_MVC_Controller_ActionController $actionController) {
649 $mockConfigurationManager = $this->getMock('Tx_Extbase_Configuration_ConfigurationManagerInterface');
650 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(FALSE));
651 $actionController->injectConfigurationManager($mockConfigurationManager);
652 }
653 }
654 ?>