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