TYPO3 CMS  TYPO3_7-6
RequestBuilderTest.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 
21 {
25  protected $requestBuilder;
26 
31 
35  protected $configuration;
36 
40  protected $mockObjectManager;
41 
46 
51 
55  protected $mockRequest;
56 
57  protected function setUp()
58  {
59  $this->requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::class, ['dummy']);
60  $this->configuration = [
61  'userFunc' => 'Tx_Extbase_Dispatcher->dispatch',
62  'pluginName' => 'Pi1',
63  'extensionName' => 'MyExtension',
64  'controller' => 'TheFirstController',
65  'action' => 'show',
66  'controllerConfiguration' => [
67  'TheFirstController' => [
68  'actions' => ['show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test']
69  ],
70  'TheSecondController' => [
71  'actions' => ['show', 'index']
72  ],
73  'TheThirdController' => [
74  'actions' => ['delete', 'create', 'onlyInThirdController']
75  ]
76  ]
77  ];
78  $this->mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
79  $this->mockRequest = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
80  $this->mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
81  $this->mockExtensionService = $this->getMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
82  $this->mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, ['getServerRequestMethod']);
83  }
84 
88  protected function injectDependencies()
89  {
90  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
91  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
92  $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Web\Request::class)->will($this->returnValue($this->mockRequest));
93  $this->requestBuilder->_set('objectManager', $this->mockObjectManager);
94  $pluginNamespace = 'tx_' . strtolower(($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']));
95  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
96  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
97  $this->mockEnvironmentService->expects($this->any())->method('getServerRequestMethod')->will($this->returnValue('GET'));
98  $this->requestBuilder->_set('environmentService', $this->mockEnvironmentService);
99  }
100 
105  {
106  $this->injectDependencies();
107  $request = $this->requestBuilder->build();
108  $this->assertSame($this->mockRequest, $request);
109  }
110 
114  public function buildSetsRequestPluginName()
115  {
116  $this->injectDependencies();
117  $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1');
118  $this->requestBuilder->build();
119  }
120 
125  {
126  $this->injectDependencies();
127  $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension');
128  $this->requestBuilder->build();
129  }
130 
135  {
136  $this->injectDependencies();
137  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
138  $this->requestBuilder->build();
139  }
140 
145  {
146  $this->injectDependencies();
147  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show');
148  $this->requestBuilder->build();
149  }
150 
154  public function buildSetsRequestRequestUri()
155  {
156  $this->injectDependencies();
157  $expectedRequestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
158  $this->mockRequest->expects($this->once())->method('setRequestUri')->with($expectedRequestUri);
159  $this->requestBuilder->build();
160  }
161 
165  public function buildSetsRequestBaseUri()
166  {
167  $this->injectDependencies();
168  $expectedBaseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
169  $this->mockRequest->expects($this->once())->method('setBaseUri')->with($expectedBaseUri);
170  $this->requestBuilder->build();
171  }
172 
176  public function buildSetsRequestMethod()
177  {
178  $this->injectDependencies();
179  $expectedMethod = 'SomeRequestMethod';
180  $mockEnvironmentService = $this->getMock(\TYPO3\CMS\Extbase\Service\EnvironmentService::class, ['getServerRequestMethod']);
181  $mockEnvironmentService->expects($this->once())->method('getServerRequestMethod')->will($this->returnValue($expectedMethod));
182  $this->requestBuilder->_set('environmentService', $mockEnvironmentService);
183  $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
184  $this->requestBuilder->build();
185  }
186 
191  {
192  $this->injectDependencies();
193  $expectedVendor = 'Vendor';
194  $this->configuration['vendorName'] = $expectedVendor;
195  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
196  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
197  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
198  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
199  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
200  $this->requestBuilder->build();
201  }
202 
207  {
208  $this->injectDependencies();
209  $expectedVendor = 'Vendor';
210  $this->configuration['vendorName'] = $expectedVendor;
211 
212  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
213  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
214  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
215  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
216 
217  $this->requestBuilder->build();
218 
219  unset($this->configuration['vendorName']);
220  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
221  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
222  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
223 
224  $this->mockRequest->expects($this->never())->method('setControllerVendorName');
225  $this->requestBuilder->build();
226  }
227 
233  {
234  unset($this->configuration['extensionName']);
235  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
236  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
237  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
238  $this->requestBuilder->build();
239  }
240 
246  {
247  unset($this->configuration['pluginName']);
248  $mockConfigurationManager = $this->getMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
249  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
250  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
251  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
252  $this->requestBuilder->build();
253  }
254 
260  {
261  $this->configuration['controllerConfiguration'] = [];
262  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
263  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
264  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
265  $this->requestBuilder->build();
266  }
267 
273  {
274  $this->configuration['controllerConfiguration']['TheFirstController'] = [];
275  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
276  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
277  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
278  $this->requestBuilder->build();
279  }
280 
286  {
287  $this->configuration['controllerConfiguration'] = [
288  '' => [
289  'actions' => ['foo']
290  ]
291  ];
292  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
293  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
294  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
295  $this->requestBuilder->build();
296  }
297 
302  {
303  $this->configuration['extensionName'] = 'SomeExtensionName';
304  $this->configuration['pluginName'] = 'SomePluginName';
305  $this->injectDependencies();
306  $_GET = [
307  'tx_someotherextensionname_somepluginname' => [
308  'foo' => 'bar'
309  ],
310  'tx_someextensionname_somepluginname' => [
311  'parameter1' => 'valueGetsOverwritten',
312  'parameter2' => [
313  'parameter3' => 'value3'
314  ]
315  ]
316  ];
317  $_POST = [
318  'tx_someextensionname_someotherpluginname' => [
319  'foo' => 'bar'
320  ],
321  'tx_someextensionname_somepluginname' => [
322  'parameter1' => 'value1',
323  'parameter2' => [
324  'parameter4' => 'value4'
325  ]
326  ]
327  ];
328  $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter1', 'value1');
329  $this->mockRequest->expects($this->at(9))->method('setArgument')->with('parameter2', ['parameter3' => 'value3', 'parameter4' => 'value4']);
330  $this->requestBuilder->build();
331  }
332 
337  {
338  $this->configuration['extensionName'] = 'SomeExtensionName';
339  $this->configuration['pluginName'] = 'SomePluginName';
340  $this->injectDependencies();
341  $_GET = [
342  'tx_someextensionname_somepluginname' => [
343  'format' => 'GET'
344  ]
345  ];
346  $_POST = [
347  'tx_someextensionname_somepluginname' => [
348  'format' => 'POST'
349  ]
350  ];
351  $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST');
352  $this->requestBuilder->build();
353  }
354 
359  {
360  $this->injectDependencies();
361  $expectedResult = [
362  'TheFirstController' => [
363  'show',
364  'index',
365  'new',
366  'create',
367  'delete',
368  'edit',
369  'update',
370  'setup',
371  'test'
372  ],
373  'TheSecondController' => [
374  'show',
375  'index'
376  ],
377  'TheThirdController' => [
378  'delete',
379  'create',
380  'onlyInThirdController'
381  ]
382  ];
383  $this->requestBuilder->build();
384  $actualResult = $this->requestBuilder->_get('allowedControllerActions');
385  $this->assertEquals($expectedResult, $actualResult);
386  }
387 
393  {
394  $this->configuration['controllerConfiguration'] = [];
395  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
396  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
397  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
398  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
399  $this->requestBuilder->build();
400  }
401 
406  {
407  $this->injectDependencies();
408  $_GET = [
409  'tx_myextension_pi1' => [
410  'foo' => 'bar'
411  ]
412  ];
413  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
414  $this->requestBuilder->build();
415  }
416 
421  {
422  $this->injectDependencies();
423  $_GET = [
424  'tx_myextension_pi1' => [
425  'controller' => 'TheSecondController'
426  ]
427  ];
428  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheSecondController');
429  $this->requestBuilder->build();
430  }
431 
437  {
438  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
439  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
440  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
441  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
442  $_GET = [
443  'tx_myextension_pi1' => [
444  'controller' => 'SomeInvalidController'
445  ]
446  ];
447  $this->requestBuilder->build();
448  }
449 
455  {
456  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
457  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
458  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
459  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
460  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
461  $_GET = [
462  'tx_myextension_pi1' => [
463  'controller' => 'SomeInvalidController'
464  ]
465  ];
466  $this->requestBuilder->build();
467  }
468 
473  {
474  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
475  $this->injectDependencies();
476  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
477  $_GET = [
478  'tx_myextension_pi1' => [
479  'controller' => 'SomeInvalidController'
480  ]
481  ];
482  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
483  $this->requestBuilder->build();
484  }
485 
491  {
492  $this->configuration['controllerConfiguration'] = [];
493  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
494  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
495  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
496  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
497  $this->requestBuilder->build();
498  }
499 
504  {
505  $this->injectDependencies();
506  $_GET = [
507  'tx_myextension_pi1' => [
508  'controller' => 'TheThirdController'
509  ]
510  ];
511  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
512  $this->requestBuilder->build();
513  }
514 
519  {
520  $this->injectDependencies();
521  $_GET = [
522  'tx_myextension_pi1' => [
523  'action' => 'create'
524  ]
525  ];
526  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('create');
527  $this->requestBuilder->build();
528  }
529 
534  {
535  $this->injectDependencies();
536  $_GET = [
537  'tx_myextension_pi1' => [
538  'controller' => 'TheThirdController',
539  'action' => 'onlyInThirdController'
540  ]
541  ];
542  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('onlyInThirdController');
543  $this->requestBuilder->build();
544  }
545 
551  {
552  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
553  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
554  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
555  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
556  $_GET = [
557  'tx_myextension_pi1' => [
558  'action' => 'someInvalidAction'
559  ]
560  ];
561  $this->requestBuilder->build();
562  }
563 
569  {
570  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
571  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
572  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
573  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
574  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
575  $_GET = [
576  'tx_myextension_pi1' => [
577  'action' => 'someInvalidAction'
578  ]
579  ];
580  $this->requestBuilder->build();
581  }
582 
587  {
588  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
589  $this->injectDependencies();
590  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
591  $_GET = [
592  'tx_myextension_pi1' => [
593  'controller' => 'TheThirdController',
594  'action' => 'someInvalidAction'
595  ]
596  ];
597  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheThirdController');
598  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
599  $this->requestBuilder->build();
600  }
601 
607  {
608  $convolutedFiles = [
609  'a0' => [
610  'name' => [
611  'a1' => 'a.txt'
612  ],
613  'type' => [
614  'a1' => 'text/plain'
615  ],
616  'tmp_name' => [
617  'a1' => '/private/var/tmp/phpbqXsYt'
618  ],
619  'error' => [
620  'a1' => 0
621  ],
622  'size' => [
623  'a1' => 100
624  ]
625  ],
626  'b0' => [
627  'name' => [
628  'b1' => 'b.txt'
629  ],
630  'type' => [
631  'b1' => 'text/plain'
632  ],
633  'tmp_name' => [
634  'b1' => '/private/var/tmp/phpvZ6oUD'
635  ],
636  'error' => [
637  'b1' => 0
638  ],
639  'size' => [
640  'b1' => 200
641  ]
642  ],
643  'c' => [
644  'name' => 'c.txt',
645  'type' => 'text/plain',
646  'tmp_name' => '/private/var/tmp/phpS9KMNw',
647  'error' => 0,
648  'size' => 300
649  ],
650  'd0' => [
651  'name' => [
652  'd1' => [
653  0 => 'd12.txt',
654  'd2' => [
655  'd3' => 'd.txt'
656  ]
657  ]
658  ],
659  'type' => [
660  'd1' => [
661  0 => 'text/plain',
662  'd2' => [
663  'd3' => 'text/plain'
664  ]
665  ]
666  ],
667  'tmp_name' => [
668  'd1' => [
669  0 => '/private/var/tmp/phpMf9Qx9',
670  'd2' => [
671  'd3' => '/private/var/tmp/phprR3fax'
672  ]
673  ]
674  ],
675  'error' => [
676  'd1' => [
677  0 => 0,
678  'd2' => [
679  'd3' => 0
680  ]
681  ]
682  ],
683  'size' => [
684  'd1' => [
685  0 => 200,
686  'd2' => [
687  'd3' => 400
688  ]
689  ]
690  ]
691  ],
692  'e0' => [
693  'name' => [
694  'e1' => [
695  'e2' => [
696  0 => 'e_one.txt',
697  1 => 'e_two.txt'
698  ]
699  ]
700  ],
701  'type' => [
702  'e1' => [
703  'e2' => [
704  0 => 'text/plain',
705  1 => 'text/plain'
706  ]
707  ]
708  ],
709  'tmp_name' => [
710  'e1' => [
711  'e2' => [
712  0 => '/private/var/tmp/php01fitB',
713  1 => '/private/var/tmp/phpUUB2cv'
714  ]
715  ]
716  ],
717  'error' => [
718  'e1' => [
719  'e2' => [
720  0 => 0,
721  1 => 0
722  ]
723  ]
724  ],
725  'size' => [
726  'e1' => [
727  'e2' => [
728  0 => 510,
729  1 => 520
730  ]
731  ]
732  ]
733  ],
734  'error' => [
735  'name' => 'error_file.txt',
736  'type' => 'text/plain',
737  'tmp_name' => '/private/var/tmp/phpADDu87fE',
738  'error' => 0,
739  'size' => 120
740  ]
741  ];
742  $untangledFiles = [
743  'a0' => [
744  'a1' => [
745  'name' => 'a.txt',
746  'type' => 'text/plain',
747  'tmp_name' => '/private/var/tmp/phpbqXsYt',
748  'error' => 0,
749  'size' => 100
750  ]
751  ],
752  'b0' => [
753  'b1' => [
754  'name' => 'b.txt',
755  'type' => 'text/plain',
756  'tmp_name' => '/private/var/tmp/phpvZ6oUD',
757  'error' => 0,
758  'size' => 200
759  ]
760  ],
761  'c' => [
762  'name' => 'c.txt',
763  'type' => 'text/plain',
764  'tmp_name' => '/private/var/tmp/phpS9KMNw',
765  'error' => 0,
766  'size' => 300
767  ],
768  'd0' => [
769  'd1' => [
770  0 => [
771  'name' => 'd12.txt',
772  'type' => 'text/plain',
773  'tmp_name' => '/private/var/tmp/phpMf9Qx9',
774  'error' => 0,
775  'size' => 200
776  ],
777  'd2' => [
778  'd3' => [
779  'name' => 'd.txt',
780  'type' => 'text/plain',
781  'tmp_name' => '/private/var/tmp/phprR3fax',
782  'error' => 0,
783  'size' => 400
784  ]
785  ]
786  ]
787  ],
788  'e0' => [
789  'e1' => [
790  'e2' => [
791  0 => [
792  'name' => 'e_one.txt',
793  'type' => 'text/plain',
794  'tmp_name' => '/private/var/tmp/php01fitB',
795  'error' => 0,
796  'size' => 510
797  ],
798  1 => [
799  'name' => 'e_two.txt',
800  'type' => 'text/plain',
801  'tmp_name' => '/private/var/tmp/phpUUB2cv',
802  'error' => 0,
803  'size' => 520
804  ]
805  ]
806  ]
807  ] ,
808  'error' => [
809  'name' => 'error_file.txt',
810  'type' => 'text/plain',
811  'tmp_name' => '/private/var/tmp/phpADDu87fE',
812  'error' => 0,
813  'size' => 120
814  ]
815  ];
816  $requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::class, ['dummy'], [], '', false);
817  $result = $requestBuilder->_call('untangleFilesArray', $convolutedFiles);
818  $this->assertSame($untangledFiles, $result);
819  }
820 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)