a66dbb22521a0af7ca2d56da0b87cd38b07997e3
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Mvc / Controller / ActionControllerTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * All rights reserved
9 *
10 * This class is a backport of the corresponding class of FLOW3.
11 * All credits go to the v5 team.
12 *
13 * This script is part of the TYPO3 project. The TYPO3 project is
14 * free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * The GNU General Public License can be found at
20 * http://www.gnu.org/copyleft/gpl.html.
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 class ActionControllerTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
33 */
34 protected $actionController;
35
36 /**
37 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
38 */
39 protected $mockObjectManager;
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
43 */
44 protected $mockUriBuilder;
45
46 public function setUp() {
47 $this->actionController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController');
48 }
49
50 /**
51 * @test
52 */
53 public function processRequestSticksToSpecifiedSequence() {
54 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request', array(), array(), '', FALSE);
55 $mockRequest->expects($this->once())->method('setDispatched')->with(TRUE);
56 $mockUriBuilder = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder');
57 $mockUriBuilder->expects($this->once())->method('setRequest')->with($mockRequest);
58 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
59 $mockObjectManager->expects($this->once())->method('create')->with('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Routing\\UriBuilder')->will($this->returnValue($mockUriBuilder));
60 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Response', array(), array(), '', FALSE);
61 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
62 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array(
63 'initializeFooAction',
64 'initializeAction',
65 'resolveActionMethodName',
66 'initializeActionMethodArguments',
67 'initializeActionMethodValidators',
68 'mapRequestArgumentsToControllerArguments',
69 'buildControllerContext',
70 'resolveView',
71 'initializeView',
72 'callActionMethod',
73 'checkRequestHash'
74 ), array(), '', FALSE);
75 $mockController->_set('objectManager', $mockObjectManager);
76 $mockController->expects($this->at(0))->method('resolveActionMethodName')->will($this->returnValue('fooAction'));
77 $mockController->expects($this->at(1))->method('initializeActionMethodArguments');
78 $mockController->expects($this->at(2))->method('initializeActionMethodValidators');
79 $mockController->expects($this->at(3))->method('initializeAction');
80 $mockController->expects($this->at(4))->method('initializeFooAction');
81 $mockController->expects($this->at(5))->method('mapRequestArgumentsToControllerArguments');
82 $mockController->expects($this->at(6))->method('checkRequestHash');
83 $mockController->expects($this->at(7))->method('buildControllerContext');
84 $mockController->expects($this->at(8))->method('resolveView');
85 $mockController->processRequest($mockRequest, $mockResponse);
86 $this->assertSame($mockRequest, $mockController->_get('request'));
87 $this->assertSame($mockResponse, $mockController->_get('response'));
88 }
89
90 /**
91 * @test
92 * @author Robert Lemke <robert@typo3.org>
93 */
94 public function callActionMethodAppendsStringsReturnedByActionMethodToTheResponseObject() {
95 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
96 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
97 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
98 $mockArguments = new \ArrayObject();
99 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
100 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
101 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'initializeAction'), array(), '', FALSE);
102 $this->enableDeprecatedPropertyMapperInController($mockController);
103 $mockController->expects($this->once())->method('fooAction')->will($this->returnValue('the returned string'));
104 $mockController->_set('request', $mockRequest);
105 $mockController->_set('response', $mockResponse);
106 $mockController->_set('arguments', $mockArguments);
107 $mockController->_set('actionMethodName', 'fooAction');
108 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
109 $mockController->_call('callActionMethod');
110 }
111
112 /**
113 * @test
114 * @author Robert Lemke <robert@typo3.org>
115 */
116 public function callActionMethodRendersTheViewAutomaticallyIfTheActionReturnedNullAndAViewExists() {
117 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
118 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
119 $mockResponse->expects($this->once())->method('appendContent')->with('the view output');
120 $mockView = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
121 $mockView->expects($this->once())->method('render')->will($this->returnValue('the view output'));
122 $mockArguments = new \ArrayObject();
123 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
124 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
125 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'initializeAction'), array(), '', FALSE);
126 $this->enableDeprecatedPropertyMapperInController($mockController);
127 $mockController->expects($this->once())->method('fooAction');
128 $mockController->_set('request', $mockRequest);
129 $mockController->_set('response', $mockResponse);
130 $mockController->_set('arguments', $mockArguments);
131 $mockController->_set('actionMethodName', 'fooAction');
132 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
133 $mockController->_set('view', $mockView);
134 $mockController->_call('callActionMethod');
135 }
136
137 /**
138 * @test
139 * @author Robert Lemke <robert@typo3.org>
140 */
141 public function callActionMethodCallsTheErrorActionIfTheMappingResultsHaveErrors() {
142 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
143 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
144 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
145 $mockArguments = new \ArrayObject();
146 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
147 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(TRUE));
148 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('barAction', 'initializeAction'), array(), '', FALSE);
149 $this->enableDeprecatedPropertyMapperInController($mockController);
150 $mockController->expects($this->once())->method('barAction')->will($this->returnValue('the returned string'));
151 $mockController->_set('request', $mockRequest);
152 $mockController->_set('response', $mockResponse);
153 $mockController->_set('arguments', $mockArguments);
154 $mockController->_set('actionMethodName', 'fooAction');
155 $mockController->_set('errorMethodName', 'barAction');
156 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
157 $mockController->_call('callActionMethod');
158 }
159
160 /**
161 * @test
162 * @author Sebastian Kurfürst <sebastian@typo3.org>
163 */
164 public function callActionMethodPassesDefaultValuesAsArguments() {
165 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
166 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
167 $arguments = new \ArrayObject();
168 $optionalArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('name1', 'Text');
169 $optionalArgument->setDefaultValue('Default value');
170 $arguments[] = $optionalArgument;
171 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
172 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
173 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'initializeAction'), array(), '', FALSE);
174 $this->enableDeprecatedPropertyMapperInController($mockController);
175 $mockController->expects($this->once())->method('fooAction')->with('Default value');
176 $mockController->_set('request', $mockRequest);
177 $mockController->_set('response', $mockResponse);
178 $mockController->_set('arguments', $arguments);
179 $mockController->_set('actionMethodName', 'fooAction');
180 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
181 $mockController->_call('callActionMethod');
182 }
183
184 /**
185 * @test
186 * @author Karsten Dambekalns <karsten@typo3.org>
187 */
188 public function resolveViewUsesFluidTemplateViewIfTemplateIsAvailable() {
189 $mockSession = $this->getMock('Tx_Extbase_Session_SessionInterface');
190 $mockControllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext', array(), array(), '', FALSE);
191 $mockFluidTemplateView = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
192 $mockFluidTemplateView->expects($this->once())->method('setControllerContext')->with($mockControllerContext);
193 $mockFluidTemplateView->expects($this->once())->method('canRender')->with($mockControllerContext)->will($this->returnValue(TRUE));
194 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
195 $mockObjectManager->expects($this->at(0))->method('create')->with('TYPO3\\CMS\\Fluid\\View\\TemplateView')->will($this->returnValue($mockFluidTemplateView));
196 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('buildControllerContext', 'resolveViewObjectName', 'setViewConfiguration'), array(), '', FALSE);
197 $mockController->expects($this->once())->method('resolveViewObjectName')->will($this->returnValue(FALSE));
198 $mockController->_set('session', $mockSession);
199 $mockController->_set('objectManager', $mockObjectManager);
200 $mockController->_set('controllerContext', $mockControllerContext);
201 $this->assertSame($mockFluidTemplateView, $mockController->_call('resolveView'));
202 }
203
204 /**
205 * @test
206 * @author Bastian Waidelich <bastian@typo3.org>
207 */
208 public function resolveViewObjectNameUsesViewObjectNamePatternToResolveViewObjectName() {
209 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
210 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
211 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
212 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
213 $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
214 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
215 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
216 $mockController->_set('request', $mockRequest);
217 $mockController->_set('objectManager', $mockObjectManager);
218 $mockController->_set('viewObjectNamePattern', 'RandomViewObjectPattern_@package_@controller_@action_@format');
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('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
228 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
229 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
230 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooBarAction'), array(), '', FALSE);
231 $mockController->_set('request', $mockRequest);
232 $this->assertEquals('fooBarAction', $mockController->_call('resolveActionMethodName'));
233 }
234
235 /**
236 * @test
237 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchActionException
238 * @author Robert Lemke <robert@typo3.org>
239 */
240 public function resolveActionMethodNameThrowsAnExceptionIfTheActionDefinedInTheRequestDoesNotExist() {
241 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
242 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
243 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
244 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('otherBarAction'), array(), '', FALSE);
245 $mockController->_set('request', $mockRequest);
246 $mockController->_call('resolveActionMethodName');
247 }
248
249 /**
250 * @test
251 * @author Robert Lemke <robert@typo3.org>
252 */
253 public function initializeActionMethodArgumentsRegistersArgumentsFoundInTheSignatureOfTheCurrentActionMethod() {
254 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
255 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('addNewArgument', 'removeAll'), array(), '', FALSE);
256 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', TRUE);
257 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', TRUE);
258 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'F3_Foo_Bar', TRUE);
259 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
260 $methodParameters = array(
261 'stringArgument' => array(
262 'position' => 0,
263 'byReference' => FALSE,
264 'array' => FALSE,
265 'optional' => FALSE,
266 'allowsNull' => FALSE,
267 'type' => 'string'
268 ),
269 'integerArgument' => array(
270 'position' => 1,
271 'byReference' => FALSE,
272 'array' => FALSE,
273 'optional' => FALSE,
274 'allowsNull' => FALSE,
275 'type' => 'integer'
276 ),
277 'objectArgument' => array(
278 'position' => 2,
279 'byReference' => FALSE,
280 'array' => FALSE,
281 'optional' => FALSE,
282 'allowsNull' => FALSE,
283 'type' => 'F3_Foo_Bar'
284 )
285 );
286 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
287 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
288 $mockController->injectReflectionService($mockReflectionService);
289 $mockController->_set('request', $mockRequest);
290 $mockController->_set('arguments', $mockArguments);
291 $mockController->_set('actionMethodName', 'fooAction');
292 $mockController->_call('initializeActionMethodArguments');
293 }
294
295 /**
296 * @test
297 * @author Robert Lemke <robert@typo3.org>
298 */
299 public function initializeActionMethodArgumentsRegistersOptionalArgumentsAsSuch() {
300 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
301 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array(), array(), '', FALSE);
302 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'string', TRUE);
303 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', FALSE, array(21));
304 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'string', FALSE, 42);
305 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
306 $methodParameters = array(
307 'arg1' => array(
308 'position' => 0,
309 'byReference' => FALSE,
310 'array' => FALSE,
311 'optional' => FALSE,
312 'allowsNull' => FALSE,
313 'type' => 'string'
314 ),
315 'arg2' => array(
316 'position' => 1,
317 'byReference' => FALSE,
318 'array' => TRUE,
319 'optional' => TRUE,
320 'defaultValue' => array(21),
321 'allowsNull' => FALSE
322 ),
323 'arg3' => array(
324 'position' => 2,
325 'byReference' => FALSE,
326 'array' => FALSE,
327 'optional' => TRUE,
328 'defaultValue' => 42,
329 'allowsNull' => FALSE,
330 'type' => 'string'
331 )
332 );
333 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
334 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
335 $mockController->injectReflectionService($mockReflectionService);
336 $mockController->_set('request', $mockRequest);
337 $mockController->_set('arguments', $mockArguments);
338 $mockController->_set('actionMethodName', 'fooAction');
339 $mockController->_call('initializeActionMethodArguments');
340 }
341
342 /**
343 * @test
344 * @author Sebastian Kurfürst <sbastian@typo3.org>
345 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException
346 */
347 public function initializeActionMethodArgumentsThrowsExceptionIfDataTypeWasNotSpecified() {
348 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
349 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array(), array(), '', FALSE);
350 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
351 $methodParameters = array(
352 'arg1' => array(
353 'position' => 0,
354 'byReference' => FALSE,
355 'array' => FALSE,
356 'optional' => FALSE,
357 'allowsNull' => FALSE
358 )
359 );
360 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
361 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
362 $mockController->injectReflectionService($mockReflectionService);
363 $mockController->_set('request', $mockRequest);
364 $mockController->_set('arguments', $mockArguments);
365 $mockController->_set('actionMethodName', 'fooAction');
366 $mockController->_call('initializeActionMethodArguments');
367 }
368
369 /**
370 * @test
371 * @author Sebastian Kurfürst <sbastian@typo3.org>
372 */
373 public function initializeActionMethodValidatorsCorrectlyRegistersValidatorsBasedOnDataType() {
374 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
375 $this->enableDeprecatedPropertyMapperInController($mockController);
376 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
377 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
378 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
379 $arguments->addArgument($argument);
380 $methodTagsValues = array();
381 $methodArgumentsValidatorConjunctions = array();
382 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
383 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
384 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
385 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
386 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
387 $mockController->injectReflectionService($mockReflectionService);
388 $mockController->injectValidatorResolver($mockValidatorResolver);
389 $mockController->_set('arguments', $arguments);
390 $mockController->_set('actionMethodName', 'fooAction');
391 $mockController->_call('initializeActionMethodValidators');
392 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
393 }
394
395 /**
396 * @test
397 * @author Sebastian Kurfürst <sbastian@typo3.org>
398 */
399 public function initializeActionMethodValidatorsRegistersModelBasedValidators() {
400 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
401 $this->enableDeprecatedPropertyMapperInController($mockController);
402 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getDataType'), array(), '', FALSE);
403 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
404 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
405 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
406 $arguments->addArgument($argument);
407 $methodTagsValues = array();
408 $quuxBaseValidatorConjunction = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
409 $methodArgumentsValidatorConjunctions = array();
410 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
411 $methodArgumentsValidatorConjunctions['arg1']->expects($this->once())->method('addValidator')->with($quuxBaseValidatorConjunction);
412 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
413 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
414 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
415 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
416 $mockValidatorResolver->expects($this->once())->method('getBaseValidatorConjunction')->with('F3_Foo_Quux')->will($this->returnValue($quuxBaseValidatorConjunction));
417 $mockController->injectReflectionService($mockReflectionService);
418 $mockController->injectValidatorResolver($mockValidatorResolver);
419 $mockController->_set('arguments', $arguments);
420 $mockController->_set('actionMethodName', 'fooAction');
421 $mockController->_call('initializeActionMethodValidators');
422 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
423 }
424
425 /**
426 * @test
427 * @author Sebastian Kurfürst <sbastian@typo3.org>
428 */
429 public function initializeActionMethodValidatorsDoesNotRegisterModelBasedValidatorsIfDontValidateAnnotationIsSet() {
430 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
431 $this->enableDeprecatedPropertyMapperInController($mockController);
432 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getDataType'), array(), '', FALSE);
433 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
434 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
435 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
436 $arguments->addArgument($argument);
437 $methodTagsValues = array(
438 'dontvalidate' => array(
439 '$arg1'
440 )
441 );
442 $methodArgumentsValidatorConjunctions = array();
443 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
444 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array(), array(), '', FALSE);
445 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
446 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
447 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
448 $mockValidatorResolver->expects($this->any())->method('getBaseValidatorConjunction')->will($this->throwException(new \Exception('This should not be called because the dontvalidate annotation is set.')));
449 $mockController->injectReflectionService($mockReflectionService);
450 $mockController->injectValidatorResolver($mockValidatorResolver);
451 $mockController->_set('arguments', $arguments);
452 $mockController->_set('actionMethodName', 'fooAction');
453 $mockController->_call('initializeActionMethodValidators');
454 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
455 }
456
457 /**
458 * @test
459 * @author Christopher Hlubek <hlubek@networkteam.com>
460 */
461 public function defaultErrorActionSetsArgumentMappingResultsErrorsInRequest() {
462 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
463 $mockFlashMessages = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\FlashMessages', array(), array(), '', FALSE);
464 $mockError = $this->getMock('TYPO3\\CMS\\Extbase\\Error\\Error', array('getMessage'), array(), '', FALSE);
465 $mockArgumentsMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array('getErrors', 'getWarnings'), array(), '', FALSE);
466 $mockArgumentsMappingResults->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array($mockError)));
467 $mockArgumentsMappingResults->expects($this->any())->method('getWarnings')->will($this->returnValue(array()));
468 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('pushFlashMessage', 'clearCacheOnError'), array(), '', FALSE);
469 $this->enableDeprecatedPropertyMapperInController($mockController);
470 $mockController->_set('request', $mockRequest);
471 $mockController->_set('flashMessageContainer', $mockFlashMessages);
472 $mockController->_set('argumentsMappingResults', $mockArgumentsMappingResults);
473 $mockRequest->expects($this->once())->method('setErrors')->with(array($mockError));
474 $mockController->_call('errorAction');
475 }
476
477 /**
478 * @test
479 * @author Christopher Hlubek <hlubek@networkteam.com>
480 */
481 public function defaultErrorActionCallsGetErrorFlashMessageAndPutsFlashMessage() {
482 $this->markTestIncomplete('To be implemented');
483 }
484
485 /**
486 * Data Provider for checkRequestHashDoesNotThrowExceptionInNormalOperations
487 *
488 * @return array
489 */
490 public function checkRequestHashInNormalOperation() {
491 return array(
492 // HMAC is verified
493 array(TRUE),
494 // HMAC not verified, but objects are directly fetched from persistence layer
495 array(FALSE, FALSE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE),
496 // HMAC not verified, objects new and modified, but dontverifyrequesthash-annotation set
497 array(FALSE, TRUE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE_AND_MODIFIED, array('dontverifyrequesthash' => ''))
498 );
499 }
500
501 /**
502 * @test
503 * @author Sebastian Kurfürst <sebastian@typo3.org>
504 * @dataProvider checkRequestHashInNormalOperation
505 * @param boolean $hmacVerified
506 * @param boolean $reflectionServiceNeedsInitialization
507 * @param integer $argument1Origin
508 * @param integer $argument2Origin
509 * @param array $methodTagsValues
510 */
511 public function checkRequestHashDoesNotThrowExceptionInNormalOperations($hmacVerified, $reflectionServiceNeedsInitialization = FALSE, $argument1Origin = 3, $argument2Origin = 3, array $methodTagsValues = array()) {
512 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request', array('isHmacVerified'), array(), '', FALSE);
513 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
514 $argument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
515 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
516 $argument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
517 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
518 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
519 $this->enableDeprecatedPropertyMapperInController($mockController);
520 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array('getMethodTagsValues'), array(), '', FALSE);
521 if ($reflectionServiceNeedsInitialization) {
522 // Somehow this is needed, else I get "Mocked method does not exist."
523 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
524 }
525 $mockController->_set('arguments', array($argument1, $argument2));
526 $mockController->_set('request', $mockRequest);
527 $mockController->_set('actionMethodName', 'fooAction');
528 $mockController->injectReflectionService($mockReflectionService);
529 $mockController->_call('checkRequestHash');
530 }
531
532 /**
533 * @test
534 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidOrNoRequestHashException
535 * @author Sebastian Kurfürst <sebastian@typo3.org>
536 */
537 public function checkRequestHashThrowsExceptionIfNeeded() {
538 $hmacVerified = FALSE;
539 $argument1Origin = \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE_AND_MODIFIED;
540 $argument2Origin = \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE;
541 $methodTagsValues = array();
542 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request', array('isHmacVerified'), array(), '', FALSE);
543 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
544 $argument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
545 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
546 $argument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
547 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
548 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
549 $this->enableDeprecatedPropertyMapperInController($mockController);
550 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\Service', array('getMethodTagsValues'), array(), '', FALSE);
551 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
552 $mockController->_set('arguments', array($argument1, $argument2));
553 $mockController->_set('request', $mockRequest);
554 $mockController->_set('actionMethodName', 'fooAction');
555 $mockController->injectReflectionService($mockReflectionService);
556 $mockController->_call('checkRequestHash');
557 }
558
559 /**
560 * Helper which enables the deprecated property mapper in the ActionController class.
561 *
562 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ActionController $actionController
563 */
564 protected function enableDeprecatedPropertyMapperInController(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController $actionController) {
565 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
566 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(FALSE));
567 $actionController->injectConfigurationManager($mockConfigurationManager);
568 }
569
570 }
571
572
573 ?>