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