Revert "[FEATURE] Introduce beforeCallActionMethod signal"
[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 $this->enableDeprecatedPropertyMapperInController($mockController);
104 $mockController->expects($this->once())->method('fooAction')->will($this->returnValue('the returned string'));
105 $mockController->_set('request', $mockRequest);
106 $mockController->_set('response', $mockResponse);
107 $mockController->_set('arguments', $mockArguments);
108 $mockController->_set('actionMethodName', 'fooAction');
109 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
110 $mockController->_call('callActionMethod');
111 }
112
113 /**
114 * @test
115 * @author Robert Lemke <robert@typo3.org>
116 */
117 public function callActionMethodRendersTheViewAutomaticallyIfTheActionReturnedNullAndAViewExists() {
118 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
119 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
120 $mockResponse->expects($this->once())->method('appendContent')->with('the view output');
121 $mockView = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
122 $mockView->expects($this->once())->method('render')->will($this->returnValue('the view output'));
123 $mockArguments = new \ArrayObject();
124 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
125 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
126 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'initializeAction'), array(), '', FALSE);
127 $this->enableDeprecatedPropertyMapperInController($mockController);
128 $mockController->expects($this->once())->method('fooAction');
129 $mockController->_set('request', $mockRequest);
130 $mockController->_set('response', $mockResponse);
131 $mockController->_set('arguments', $mockArguments);
132 $mockController->_set('actionMethodName', 'fooAction');
133 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
134 $mockController->_set('view', $mockView);
135 $mockController->_call('callActionMethod');
136 }
137
138 /**
139 * @test
140 * @author Robert Lemke <robert@typo3.org>
141 */
142 public function callActionMethodCallsTheErrorActionIfTheMappingResultsHaveErrors() {
143 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
144 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
145 $mockResponse->expects($this->once())->method('appendContent')->with('the returned string');
146 $mockArguments = new \ArrayObject();
147 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
148 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(TRUE));
149 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('barAction', 'initializeAction'), array(), '', FALSE);
150 $this->enableDeprecatedPropertyMapperInController($mockController);
151 $mockController->expects($this->once())->method('barAction')->will($this->returnValue('the returned string'));
152 $mockController->_set('request', $mockRequest);
153 $mockController->_set('response', $mockResponse);
154 $mockController->_set('arguments', $mockArguments);
155 $mockController->_set('actionMethodName', 'fooAction');
156 $mockController->_set('errorMethodName', 'barAction');
157 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
158 $mockController->_call('callActionMethod');
159 }
160
161 /**
162 * @test
163 * @author Sebastian Kurfürst <sebastian@typo3.org>
164 */
165 public function callActionMethodPassesDefaultValuesAsArguments() {
166 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
167 $mockResponse = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\ResponseInterface', array(), array(), '', FALSE);
168 $arguments = new \ArrayObject();
169 $optionalArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('name1', 'Text');
170 $optionalArgument->setDefaultValue('Default value');
171 $arguments[] = $optionalArgument;
172 $mockArgumentMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array(), array(), '', FALSE);
173 $mockArgumentMappingResults->expects($this->once())->method('hasErrors')->will($this->returnValue(FALSE));
174 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'initializeAction'), array(), '', FALSE);
175 $this->enableDeprecatedPropertyMapperInController($mockController);
176 $mockController->expects($this->once())->method('fooAction')->with('Default value');
177 $mockController->_set('request', $mockRequest);
178 $mockController->_set('response', $mockResponse);
179 $mockController->_set('arguments', $arguments);
180 $mockController->_set('actionMethodName', 'fooAction');
181 $mockController->_set('argumentsMappingResults', $mockArgumentMappingResults);
182 $mockController->_call('callActionMethod');
183 }
184
185 /**
186 * @test
187 * @author Karsten Dambekalns <karsten@typo3.org>
188 */
189 public function resolveViewUsesFluidTemplateViewIfTemplateIsAvailable() {
190 $mockSession = $this->getMock('Tx_Extbase_Session_SessionInterface');
191 $mockControllerContext = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ControllerContext', array(), array(), '', FALSE);
192 $mockFluidTemplateView = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
193 $mockFluidTemplateView->expects($this->once())->method('setControllerContext')->with($mockControllerContext);
194 $mockFluidTemplateView->expects($this->once())->method('canRender')->with($mockControllerContext)->will($this->returnValue(TRUE));
195 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
196 $mockObjectManager->expects($this->at(0))->method('create')->with('TYPO3\\CMS\\Fluid\\View\\TemplateView')->will($this->returnValue($mockFluidTemplateView));
197 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('buildControllerContext', 'resolveViewObjectName', 'setViewConfiguration'), array(), '', FALSE);
198 $mockController->expects($this->once())->method('resolveViewObjectName')->will($this->returnValue(FALSE));
199 $mockController->_set('session', $mockSession);
200 $mockController->_set('objectManager', $mockObjectManager);
201 $mockController->_set('controllerContext', $mockControllerContext);
202 $this->assertSame($mockFluidTemplateView, $mockController->_call('resolveView'));
203 }
204
205 /**
206 * @test
207 * @author Bastian Waidelich <bastian@typo3.org>
208 */
209 public function resolveViewObjectNameUsesViewObjectNamePatternToResolveViewObjectName() {
210 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
211 $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
212 $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
213 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
214 $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
215 $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
216 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
217 $mockController->_set('request', $mockRequest);
218 $mockController->_set('objectManager', $mockObjectManager);
219 $mockController->_set('viewObjectNamePattern', 'RandomViewObjectPattern_@package_@controller_@action_@format');
220 $mockController->_call('resolveViewObjectName');
221 }
222
223 /**
224 * @test
225 * @author Robert Lemke <robert@typo3.org>
226 */
227 public function resolveActionMethodNameReturnsTheCurrentActionMethodNameFromTheRequest() {
228 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
229 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
230 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
231 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooBarAction'), array(), '', FALSE);
232 $mockController->_set('request', $mockRequest);
233 $this->assertEquals('fooBarAction', $mockController->_call('resolveActionMethodName'));
234 }
235
236 /**
237 * @test
238 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchActionException
239 * @author Robert Lemke <robert@typo3.org>
240 */
241 public function resolveActionMethodNameThrowsAnExceptionIfTheActionDefinedInTheRequestDoesNotExist() {
242 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
243 $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
244 /** @var \TYPO3\CMS\Extbase\Mvc\Controller\ActionController|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface */
245 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('otherBarAction'), array(), '', FALSE);
246 $mockController->_set('request', $mockRequest);
247 $mockController->_call('resolveActionMethodName');
248 }
249
250 /**
251 * @test
252 * @author Robert Lemke <robert@typo3.org>
253 */
254 public function initializeActionMethodArgumentsRegistersArgumentsFoundInTheSignatureOfTheCurrentActionMethod() {
255 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
256 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('addNewArgument', 'removeAll'), array(), '', FALSE);
257 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', TRUE);
258 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', TRUE);
259 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'F3_Foo_Bar', TRUE);
260 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
261 $methodParameters = array(
262 'stringArgument' => array(
263 'position' => 0,
264 'byReference' => FALSE,
265 'array' => FALSE,
266 'optional' => FALSE,
267 'allowsNull' => FALSE,
268 'type' => 'string'
269 ),
270 'integerArgument' => array(
271 'position' => 1,
272 'byReference' => FALSE,
273 'array' => FALSE,
274 'optional' => FALSE,
275 'allowsNull' => FALSE,
276 'type' => 'integer'
277 ),
278 'objectArgument' => array(
279 'position' => 2,
280 'byReference' => FALSE,
281 'array' => FALSE,
282 'optional' => FALSE,
283 'allowsNull' => FALSE,
284 'type' => 'F3_Foo_Bar'
285 )
286 );
287 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
288 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
289 $mockController->injectReflectionService($mockReflectionService);
290 $mockController->_set('request', $mockRequest);
291 $mockController->_set('arguments', $mockArguments);
292 $mockController->_set('actionMethodName', 'fooAction');
293 $mockController->_call('initializeActionMethodArguments');
294 }
295
296 /**
297 * @test
298 * @author Robert Lemke <robert@typo3.org>
299 */
300 public function initializeActionMethodArgumentsRegistersOptionalArgumentsAsSuch() {
301 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
302 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array(), array(), '', FALSE);
303 $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'string', TRUE);
304 $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', FALSE, array(21));
305 $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'string', FALSE, 42);
306 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction', 'evaluateDontValidateAnnotations'), array(), '', FALSE);
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 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
335 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
336 $mockController->injectReflectionService($mockReflectionService);
337 $mockController->_set('request', $mockRequest);
338 $mockController->_set('arguments', $mockArguments);
339 $mockController->_set('actionMethodName', 'fooAction');
340 $mockController->_call('initializeActionMethodArguments');
341 }
342
343 /**
344 * @test
345 * @author Sebastian Kurfürst <sbastian@typo3.org>
346 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentTypeException
347 */
348 public function initializeActionMethodArgumentsThrowsExceptionIfDataTypeWasNotSpecified() {
349 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
350 $mockArguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array(), array(), '', FALSE);
351 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
352 $methodParameters = array(
353 'arg1' => array(
354 'position' => 0,
355 'byReference' => FALSE,
356 'array' => FALSE,
357 'optional' => FALSE,
358 'allowsNull' => FALSE
359 )
360 );
361 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
362 $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
363 $mockController->injectReflectionService($mockReflectionService);
364 $mockController->_set('request', $mockRequest);
365 $mockController->_set('arguments', $mockArguments);
366 $mockController->_set('actionMethodName', 'fooAction');
367 $mockController->_call('initializeActionMethodArguments');
368 }
369
370 /**
371 * @test
372 * @author Sebastian Kurfürst <sbastian@typo3.org>
373 */
374 public function initializeActionMethodValidatorsCorrectlyRegistersValidatorsBasedOnDataType() {
375 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
376 $this->enableDeprecatedPropertyMapperInController($mockController);
377 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
378 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
379 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
380 $arguments->addArgument($argument);
381 $methodTagsValues = array();
382 $methodArgumentsValidatorConjunctions = array();
383 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
384 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
385 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
386 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
387 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
388 $mockController->injectReflectionService($mockReflectionService);
389 $mockController->injectValidatorResolver($mockValidatorResolver);
390 $mockController->_set('arguments', $arguments);
391 $mockController->_set('actionMethodName', 'fooAction');
392 $mockController->_call('initializeActionMethodValidators');
393 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
394 }
395
396 /**
397 * @test
398 * @author Sebastian Kurfürst <sbastian@typo3.org>
399 */
400 public function initializeActionMethodValidatorsRegistersModelBasedValidators() {
401 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
402 $this->enableDeprecatedPropertyMapperInController($mockController);
403 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getDataType'), array(), '', FALSE);
404 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
405 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
406 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
407 $arguments->addArgument($argument);
408 $methodTagsValues = array();
409 $quuxBaseValidatorConjunction = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
410 $methodArgumentsValidatorConjunctions = array();
411 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
412 $methodArgumentsValidatorConjunctions['arg1']->expects($this->once())->method('addValidator')->with($quuxBaseValidatorConjunction);
413 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
414 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
415 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
416 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
417 $mockValidatorResolver->expects($this->once())->method('getBaseValidatorConjunction')->with('F3_Foo_Quux')->will($this->returnValue($quuxBaseValidatorConjunction));
418 $mockController->injectReflectionService($mockReflectionService);
419 $mockController->injectValidatorResolver($mockValidatorResolver);
420 $mockController->_set('arguments', $arguments);
421 $mockController->_set('actionMethodName', 'fooAction');
422 $mockController->_call('initializeActionMethodValidators');
423 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
424 }
425
426 /**
427 * @test
428 * @author Sebastian Kurfürst <sbastian@typo3.org>
429 */
430 public function initializeActionMethodValidatorsDoesNotRegisterModelBasedValidatorsIfDontValidateAnnotationIsSet() {
431 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('fooAction'), array(), '', FALSE);
432 $this->enableDeprecatedPropertyMapperInController($mockController);
433 $argument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getDataType'), array(), '', FALSE);
434 $argument->expects($this->any())->method('getName')->will($this->returnValue('arg1'));
435 $argument->expects($this->any())->method('getDataType')->will($this->returnValue('F3_Foo_Quux'));
436 $arguments = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments', array('dummy'), array(), '', FALSE);
437 $arguments->addArgument($argument);
438 $methodTagsValues = array(
439 'dontvalidate' => array(
440 '$arg1'
441 )
442 );
443 $methodArgumentsValidatorConjunctions = array();
444 $methodArgumentsValidatorConjunctions['arg1'] = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
445 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array(), array(), '', FALSE);
446 $mockReflectionService->expects($this->once())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
447 $mockValidatorResolver = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array(), array(), '', FALSE);
448 $mockValidatorResolver->expects($this->once())->method('buildMethodArgumentsValidatorConjunctions')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodArgumentsValidatorConjunctions));
449 $mockValidatorResolver->expects($this->any())->method('getBaseValidatorConjunction')->will($this->throwException(new \Exception('This should not be called because the dontvalidate annotation is set.')));
450 $mockController->injectReflectionService($mockReflectionService);
451 $mockController->injectValidatorResolver($mockValidatorResolver);
452 $mockController->_set('arguments', $arguments);
453 $mockController->_set('actionMethodName', 'fooAction');
454 $mockController->_call('initializeActionMethodValidators');
455 $this->assertEquals($methodArgumentsValidatorConjunctions['arg1'], $arguments['arg1']->getValidator());
456 }
457
458 /**
459 * @test
460 * @author Christopher Hlubek <hlubek@networkteam.com>
461 */
462 public function defaultErrorActionSetsArgumentMappingResultsErrorsInRequest() {
463 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Request', array(), array(), '', FALSE);
464 $mockFlashMessageContainer = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\FlashMessageContainer', array(), array(), '', FALSE);
465 $mockError = $this->getMock('TYPO3\\CMS\\Extbase\\Error\\Error', array('getMessage'), array(), '', FALSE);
466 $mockArgumentsMappingResults = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\MappingResults', array('getErrors', 'getWarnings'), array(), '', FALSE);
467 $mockArgumentsMappingResults->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array($mockError)));
468 $mockArgumentsMappingResults->expects($this->any())->method('getWarnings')->will($this->returnValue(array()));
469 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('pushFlashMessage', 'clearCacheOnError'), array(), '', FALSE);
470 $this->enableDeprecatedPropertyMapperInController($mockController);
471 $mockController->_set('request', $mockRequest);
472 $mockController->_set('flashMessageContainer', $mockFlashMessageContainer);
473 $mockController->_set('argumentsMappingResults', $mockArgumentsMappingResults);
474 $mockRequest->expects($this->once())->method('setErrors')->with(array($mockError));
475 $mockController->_call('errorAction');
476 }
477
478 /**
479 * @test
480 * @author Christopher Hlubek <hlubek@networkteam.com>
481 */
482 public function defaultErrorActionCallsGetErrorFlashMessageAndPutsFlashMessage() {
483 $this->markTestIncomplete('To be implemented');
484 }
485
486 /**
487 * Data Provider for checkRequestHashDoesNotThrowExceptionInNormalOperations
488 *
489 * @return array
490 */
491 public function checkRequestHashInNormalOperation() {
492 return array(
493 // HMAC is verified
494 array(TRUE),
495 // HMAC not verified, but objects are directly fetched from persistence layer
496 array(FALSE, FALSE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE),
497 // HMAC not verified, objects new and modified, but dontverifyrequesthash-annotation set
498 array(FALSE, TRUE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE, \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE_AND_MODIFIED, array('dontverifyrequesthash' => ''))
499 );
500 }
501
502 /**
503 * @test
504 * @author Sebastian Kurfürst <sebastian@typo3.org>
505 * @dataProvider checkRequestHashInNormalOperation
506 * @param boolean $hmacVerified
507 * @param boolean $reflectionServiceNeedsInitialization
508 * @param integer $argument1Origin
509 * @param integer $argument2Origin
510 * @param array $methodTagsValues
511 */
512 public function checkRequestHashDoesNotThrowExceptionInNormalOperations($hmacVerified, $reflectionServiceNeedsInitialization = FALSE, $argument1Origin = 3, $argument2Origin = 3, array $methodTagsValues = array()) {
513 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request', array('isHmacVerified'), array(), '', FALSE);
514 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
515 $argument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
516 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
517 $argument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
518 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
519 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
520 $this->enableDeprecatedPropertyMapperInController($mockController);
521 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array('getMethodTagsValues'), array(), '', FALSE);
522 if ($reflectionServiceNeedsInitialization) {
523 // Somehow this is needed, else I get "Mocked method does not exist."
524 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
525 }
526 $mockController->_set('arguments', array($argument1, $argument2));
527 $mockController->_set('request', $mockRequest);
528 $mockController->_set('actionMethodName', 'fooAction');
529 $mockController->injectReflectionService($mockReflectionService);
530 $mockController->_call('checkRequestHash');
531 }
532
533 /**
534 * @test
535 * @expectedException \TYPO3\CMS\Extbase\Mvc\Exception\InvalidOrNoRequestHashException
536 * @author Sebastian Kurfürst <sebastian@typo3.org>
537 */
538 public function checkRequestHashThrowsExceptionIfNeeded() {
539 $hmacVerified = FALSE;
540 $argument1Origin = \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE_AND_MODIFIED;
541 $argument2Origin = \TYPO3\CMS\Extbase\Mvc\Controller\Argument::ORIGIN_PERSISTENCE;
542 $methodTagsValues = array();
543 $mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request', array('isHmacVerified'), array(), '', FALSE);
544 $mockRequest->expects($this->once())->method('isHmacVerified')->will($this->returnValue($hmacVerified));
545 $argument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
546 $argument1->expects($this->any())->method('getOrigin')->will($this->returnValue($argument1Origin));
547 $argument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getOrigin'), array(), '', FALSE);
548 $argument2->expects($this->any())->method('getOrigin')->will($this->returnValue($argument2Origin));
549 $mockController = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\ActionController', array('dummy'), array(), '', FALSE);
550 $this->enableDeprecatedPropertyMapperInController($mockController);
551 $mockReflectionService = $this->getMock('TYPO3\\CMS\\Extbase\\Reflection\\ReflectionService', array('getMethodTagsValues'), array(), '', FALSE);
552 $mockReflectionService->expects($this->any())->method('getMethodTagsValues')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodTagsValues));
553 $mockController->_set('arguments', array($argument1, $argument2));
554 $mockController->_set('request', $mockRequest);
555 $mockController->_set('actionMethodName', 'fooAction');
556 $mockController->injectReflectionService($mockReflectionService);
557 $mockController->_call('checkRequestHash');
558 }
559
560 /**
561 * Helper which enables the deprecated property mapper in the ActionController class.
562 *
563 * @param \TYPO3\CMS\Extbase\Mvc\Controller\ActionController $actionController
564 */
565 protected function enableDeprecatedPropertyMapperInController(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController $actionController) {
566 $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
567 $mockConfigurationManager->expects($this->any())->method('isFeatureEnabled')->with('rewrittenPropertyMapper')->will($this->returnValue(FALSE));
568 $actionController->injectConfigurationManager($mockConfigurationManager);
569 }
570 }
571
572 ?>