TYPO3 CMS  TYPO3_7-6
ActionControllerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
28  protected $actionController;
29 
33  protected $mockObjectManager;
34 
38  protected $mockUriBuilder;
39 
44 
45  protected function setUp()
46  {
47  $this->actionController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class);
48  }
49 
53  public function processRequestSticksToSpecifiedSequence()
54  {
55  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Request::class, [], [], '', false);
56  $mockRequest->expects($this->once())->method('setDispatched')->with(true);
57  $mockUriBuilder = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class);
58  $mockUriBuilder->expects($this->once())->method('setRequest')->with($mockRequest);
59  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
60  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder::class)->will($this->returnValue($mockUriBuilder));
61  $mockResponse = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Response::class, [], [], '', false);
62  $configurationService = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class);
64  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, [
65  'initializeFooAction',
66  'initializeAction',
67  'resolveActionMethodName',
68  'initializeActionMethodArguments',
69  'initializeActionMethodValidators',
70  'mapRequestArgumentsToControllerArguments',
71  'buildControllerContext',
72  'resolveView',
73  'initializeView',
74  'callActionMethod',
75  'checkRequestHash'
76  ], [], '', false);
77  $mockController->_set('objectManager', $mockObjectManager);
78 
79  $mockController->expects($this->at(0))->method('resolveActionMethodName')->will($this->returnValue('fooAction'));
80  $mockController->expects($this->at(1))->method('initializeActionMethodArguments');
81  $mockController->expects($this->at(2))->method('initializeActionMethodValidators');
82  $mockController->expects($this->at(3))->method('initializeAction');
83  $mockController->expects($this->at(4))->method('initializeFooAction');
84  $mockController->expects($this->at(5))->method('mapRequestArgumentsToControllerArguments');
85  $mockController->expects($this->at(6))->method('checkRequestHash');
86  $mockController->expects($this->at(7))->method('buildControllerContext');
87  $mockController->expects($this->at(8))->method('resolveView');
88 
89  $mockController->_set('mvcPropertyMappingConfigurationService', $configurationService);
90  $mockController->_set('arguments', new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments());
91 
92  $mockController->processRequest($mockRequest, $mockResponse);
93  $this->assertSame($mockRequest, $mockController->_get('request'));
94  $this->assertSame($mockResponse, $mockController->_get('response'));
95  }
96 
101  {
102  $mockSession = $this->getMock('Tx_Extbase_Session_SessionInterface');
103  $mockControllerContext = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext::class, [], [], '', false);
104  $mockFluidTemplateView = $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class);
105  $mockFluidTemplateView->expects($this->once())->method('setControllerContext')->with($mockControllerContext);
106  $mockFluidTemplateView->expects($this->once())->method('canRender')->with($mockControllerContext)->will($this->returnValue(true));
107  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class, [], [], '', false);
108  $mockObjectManager->expects($this->at(0))->method('get')->with(\TYPO3\CMS\Fluid\View\TemplateView::class)->will($this->returnValue($mockFluidTemplateView));
109  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['buildControllerContext', 'resolveViewObjectName', 'setViewConfiguration'], [], '', false);
110  $mockController->expects($this->once())->method('resolveViewObjectName')->will($this->returnValue(false));
111  $mockController->_set('session', $mockSession);
112  $mockController->_set('objectManager', $mockObjectManager);
113  $mockController->_set('controllerContext', $mockControllerContext);
114  $this->assertSame($mockFluidTemplateView, $mockController->_call('resolveView'));
115  }
116 
121  {
122  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
123  $mockRequest->expects($this->once())->method('getControllerVendorName')->will($this->returnValue('MyVendor'));
124  $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
125  $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
126  $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
127  $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
128  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class, [], [], '', false);
129  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['dummy'], [], '', false);
130  $mockController->_set('request', $mockRequest);
131  $mockController->_set('objectManager', $mockObjectManager);
132  $mockController->_set('namespacesViewObjectNamePattern', 'RandomViewObject@vendor\@extension\View\@controller\@action@format');
133  $mockController->_call('resolveViewObjectName');
134  }
135 
140  {
141  eval('namespace MyVendor\MyPackage\View\MyController; class MyActionMyFormat {}');
142 
143  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
144  $mockRequest->expects($this->once())->method('getControllerExtensionName')->will($this->returnValue('MyPackage'));
145  $mockRequest->expects($this->once())->method('getControllerName')->will($this->returnValue('MyController'));
146  $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('MyAction'));
147  $mockRequest->expects($this->once())->method('getControllerVendorName')->will($this->returnValue('MyVendor'));
148  $mockRequest->expects($this->atLeastOnce())->method('getFormat')->will($this->returnValue('MyFormat'));
149  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class, [], [], '', false);
150  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['dummy'], [], '', false);
151  $mockController->_set('request', $mockRequest);
152  $mockController->_set('objectManager', $mockObjectManager);
153 
154  $this->assertEquals(
155  'MyVendor\MyPackage\View\MyController\MyActionMyFormat',
156  $mockController->_call('resolveViewObjectName')
157  );
158  }
159 
163  public function resolveActionMethodNameReturnsTheCurrentActionMethodNameFromTheRequest()
164  {
165  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
166  $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
168  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['fooBarAction'], [], '', false);
169  $mockController->_set('request', $mockRequest);
170  $this->assertEquals('fooBarAction', $mockController->_call('resolveActionMethodName'));
171  }
172 
177  public function resolveActionMethodNameThrowsAnExceptionIfTheActionDefinedInTheRequestDoesNotExist()
178  {
179  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
180  $mockRequest->expects($this->once())->method('getControllerActionName')->will($this->returnValue('fooBar'));
182  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['otherBarAction'], [], '', false);
183  $mockController->_set('request', $mockRequest);
184  $mockController->_call('resolveActionMethodName');
185  }
186 
191  {
192  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
193  $mockArguments = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['addNewArgument', 'removeAll']);
194  $mockArguments->expects($this->at(0))->method('addNewArgument')->with('stringArgument', 'string', true);
195  $mockArguments->expects($this->at(1))->method('addNewArgument')->with('integerArgument', 'integer', true);
196  $mockArguments->expects($this->at(2))->method('addNewArgument')->with('objectArgument', 'F3_Foo_Bar', true);
197  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['fooAction', 'evaluateDontValidateAnnotations'], [], '', false);
198  $methodParameters = [
199  'stringArgument' => [
200  'position' => 0,
201  'byReference' => false,
202  'array' => false,
203  'optional' => false,
204  'allowsNull' => false,
205  'type' => 'string'
206  ],
207  'integerArgument' => [
208  'position' => 1,
209  'byReference' => false,
210  'array' => false,
211  'optional' => false,
212  'allowsNull' => false,
213  'type' => 'integer'
214  ],
215  'objectArgument' => [
216  'position' => 2,
217  'byReference' => false,
218  'array' => false,
219  'optional' => false,
220  'allowsNull' => false,
221  'type' => 'F3_Foo_Bar'
222  ]
223  ];
224  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, [], [], '', false);
225  $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
226  $mockController->_set('reflectionService', $mockReflectionService);
227  $mockController->_set('request', $mockRequest);
228  $mockController->_set('arguments', $mockArguments);
229  $mockController->_set('actionMethodName', 'fooAction');
230  $mockController->_call('initializeActionMethodArguments');
231  }
232 
237  {
238  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
239  $mockArguments = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, [], [], '', false);
240  $mockArguments->expects($this->at(0))->method('addNewArgument')->with('arg1', 'string', true);
241  $mockArguments->expects($this->at(1))->method('addNewArgument')->with('arg2', 'array', false, [21]);
242  $mockArguments->expects($this->at(2))->method('addNewArgument')->with('arg3', 'string', false, 42);
243  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['fooAction', 'evaluateDontValidateAnnotations'], [], '', false);
244  $methodParameters = [
245  'arg1' => [
246  'position' => 0,
247  'byReference' => false,
248  'array' => false,
249  'optional' => false,
250  'allowsNull' => false,
251  'type' => 'string'
252  ],
253  'arg2' => [
254  'position' => 1,
255  'byReference' => false,
256  'array' => true,
257  'optional' => true,
258  'defaultValue' => [21],
259  'allowsNull' => false
260  ],
261  'arg3' => [
262  'position' => 2,
263  'byReference' => false,
264  'array' => false,
265  'optional' => true,
266  'defaultValue' => 42,
267  'allowsNull' => false,
268  'type' => 'string'
269  ]
270  ];
271  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, [], [], '', false);
272  $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
273  $mockController->_set('reflectionService', $mockReflectionService);
274  $mockController->_set('request', $mockRequest);
275  $mockController->_set('arguments', $mockArguments);
276  $mockController->_set('actionMethodName', 'fooAction');
277  $mockController->_call('initializeActionMethodArguments');
278  }
279 
285  {
286  $mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class, [], [], '', false);
287  $mockArguments = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, [], [], '', false);
288  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['fooAction'], [], '', false);
289  $methodParameters = [
290  'arg1' => [
291  'position' => 0,
292  'byReference' => false,
293  'array' => false,
294  'optional' => false,
295  'allowsNull' => false
296  ]
297  ];
298  $mockReflectionService = $this->getMock(\TYPO3\CMS\Extbase\Reflection\ReflectionService::class, [], [], '', false);
299  $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue($methodParameters));
300  $mockController->_set('reflectionService', $mockReflectionService);
301  $mockController->_set('request', $mockRequest);
302  $mockController->_set('arguments', $mockArguments);
303  $mockController->_set('actionMethodName', 'fooAction');
304  $mockController->_call('initializeActionMethodArguments');
305  }
306 
313  public function setViewConfigurationResolvesTemplateRootPathsForTemplateRootPath($configuration, $expected)
314  {
316  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['dummy'], [], '', false);
318  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
319  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
320  $mockController->injectConfigurationManager($mockConfigurationManager);
321  $mockController->_set('request', $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class), ['getControllerExtensionKey']);
322  $view = $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class, ['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setTemplateRootPaths']);
323  $view->expects($this->once())->method('setTemplateRootPaths')->with($expected);
324  $mockController->_call('setViewConfiguration', $view);
325  }
326 
331  {
332  return [
333  'text keys' => [
334  [
335  'view' => [
336  'templateRootPaths' => [
337  'default' => 'some path',
338  'extended' => 'some other path'
339  ]
340  ]
341  ],
342  [
343  'extended' => 'some other path',
344  'default' => 'some path'
345  ]
346  ],
347  'numerical keys' => [
348  [
349  'view' => [
350  'templateRootPaths' => [
351  '10' => 'some path',
352  '20' => 'some other path',
353  '15' => 'intermediate specific path'
354  ]
355  ]
356  ],
357  [
358  '20' => 'some other path',
359  '15' => 'intermediate specific path',
360  '10' => 'some path'
361  ]
362  ],
363  'mixed keys' => [
364  [
365  'view' => [
366  'templateRootPaths' => [
367  '10' => 'some path',
368  'very_specific' => 'some other path',
369  '15' => 'intermediate specific path'
370  ]
371  ]
372  ],
373  [
374  '15' => 'intermediate specific path',
375  'very_specific' => 'some other path',
376  '10' => 'some path'
377  ]
378  ],
379  ];
380  }
381 
389  public function setViewConfigurationResolvesLayoutRootPathsForLayoutRootPath($configuration, $expected)
390  {
392  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['dummy'], [], '', false);
394  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
395  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
396  $mockController->injectConfigurationManager($mockConfigurationManager);
397  $mockController->_set('request', $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class), ['getControllerExtensionKey']);
398  $view = $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class, ['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setlayoutRootPaths']);
399  $view->expects($this->once())->method('setlayoutRootPaths')->with($expected);
400  $mockController->_call('setViewConfiguration', $view);
401  }
402 
406  public function layoutRootPathDataProvider()
407  {
408  return [
409  'text keys' => [
410  [
411  'view' => [
412  'layoutRootPaths' => [
413  'default' => 'some path',
414  'extended' => 'some other path'
415  ]
416  ]
417  ],
418  [
419  'extended' => 'some other path',
420  'default' => 'some path'
421  ]
422  ],
423  'numerical keys' => [
424  [
425  'view' => [
426  'layoutRootPaths' => [
427  '10' => 'some path',
428  '20' => 'some other path',
429  '15' => 'intermediate specific path'
430  ]
431  ]
432  ],
433  [
434  '20' => 'some other path',
435  '15' => 'intermediate specific path',
436  '10' => 'some path'
437  ]
438  ],
439  'mixed keys' => [
440  [
441  'view' => [
442  'layoutRootPaths' => [
443  '10' => 'some path',
444  'very_specific' => 'some other path',
445  '15' => 'intermediate specific path'
446  ]
447  ]
448  ],
449  [
450  '15' => 'intermediate specific path',
451  'very_specific' => 'some other path',
452  '10' => 'some path'
453  ]
454  ],
455  ];
456  }
457 
465  public function setViewConfigurationResolvesPartialRootPathsForPartialRootPath($configuration, $expected)
466  {
468  $mockController = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\ActionController::class, ['dummy'], [], '', false);
470  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
471  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
472  $mockController->injectConfigurationManager($mockConfigurationManager);
473  $mockController->_set('request', $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class), ['getControllerExtensionKey']);
474  $view = $this->getMock(\TYPO3\CMS\Extbase\Mvc\View\ViewInterface::class, ['setControllerContext', 'assign', 'assignMultiple', 'canRender', 'render', 'initializeView', 'setpartialRootPaths']);
475  $view->expects($this->once())->method('setpartialRootPaths')->with($expected);
476  $mockController->_call('setViewConfiguration', $view);
477  }
478 
482  public function partialRootPathDataProvider()
483  {
484  return [
485  'text keys' => [
486  [
487  'view' => [
488  'partialRootPaths' => [
489  'default' => 'some path',
490  'extended' => 'some other path'
491  ]
492  ]
493  ],
494  [
495  'extended' => 'some other path',
496  'default' => 'some path'
497  ]
498  ],
499  'numerical keys' => [
500  [
501  'view' => [
502  'partialRootPaths' => [
503  '10' => 'some path',
504  '20' => 'some other path',
505  '15' => 'intermediate specific path'
506  ]
507  ]
508  ],
509  [
510  '20' => 'some other path',
511  '15' => 'intermediate specific path',
512  '10' => 'some path'
513  ]
514  ],
515  'mixed keys' => [
516  [
517  'view' => [
518  'partialRootPaths' => [
519  '10' => 'some path',
520  'very_specific' => 'some other path',
521  '15' => 'intermediate specific path'
522  ]
523  ]
524  ],
525  [
526  '15' => 'intermediate specific path',
527  'very_specific' => 'some other path',
528  '10' => 'some path'
529  ]
530  ],
531  ];
532  }
533 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)