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