TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Web/RequestBuilderTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Mvc\Web;
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  */
20 
25 {
29  protected $requestBuilder;
30 
35 
39  protected $configuration;
40 
44  protected $mockObjectManager;
45 
50 
55 
59  protected $mockRequest;
60 
61  protected function setUp()
62  {
63  $this->requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::class, ['dummy']);
64  $this->configuration = [
65  'userFunc' => 'Tx_Extbase_Dispatcher->dispatch',
66  'pluginName' => 'Pi1',
67  'extensionName' => 'MyExtension',
68  'controller' => 'TheFirstController',
69  'action' => 'show',
70  'controllerConfiguration' => [
71  'TheFirstController' => [
72  'actions' => ['show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test']
73  ],
74  'TheSecondController' => [
75  'actions' => ['show', 'index']
76  ],
77  'TheThirdController' => [
78  'actions' => ['delete', 'create', 'onlyInThirdController']
79  ]
80  ]
81  ];
82  $this->mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
83  $this->mockRequest = $this->createMock(\TYPO3\CMS\Extbase\Mvc\Web\Request::class);
84  $this->mockObjectManager = $this->createMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
85  $this->mockExtensionService = $this->createMock(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
86  $this->mockEnvironmentService = $this->getMockBuilder(\TYPO3\CMS\Extbase\Service\EnvironmentService::class)
87  ->setMethods(['getServerRequestMethod'])
88  ->getMock();
89  }
90 
94  protected function injectDependencies()
95  {
96  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
97  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
98  $this->mockObjectManager->expects($this->any())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Web\Request::class)->will($this->returnValue($this->mockRequest));
99  $this->requestBuilder->_set('objectManager', $this->mockObjectManager);
100  $pluginNamespace = 'tx_' . strtolower(($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']));
101  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
102  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
103  $this->mockEnvironmentService->expects($this->any())->method('getServerRequestMethod')->will($this->returnValue('GET'));
104  $this->requestBuilder->_set('environmentService', $this->mockEnvironmentService);
105  }
106 
111  {
112  $this->injectDependencies();
113  $request = $this->requestBuilder->build();
114  $this->assertSame($this->mockRequest, $request);
115  }
116 
120  public function buildSetsRequestPluginName()
121  {
122  $this->injectDependencies();
123  $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1');
124  $this->requestBuilder->build();
125  }
126 
131  {
132  $this->injectDependencies();
133  $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension');
134  $this->requestBuilder->build();
135  }
136 
141  {
142  $this->injectDependencies();
143  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
144  $this->requestBuilder->build();
145  }
146 
151  {
152  $this->injectDependencies();
153  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show');
154  $this->requestBuilder->build();
155  }
156 
160  public function buildSetsRequestRequestUri()
161  {
162  $this->injectDependencies();
163  $expectedRequestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
164  $this->mockRequest->expects($this->once())->method('setRequestUri')->with($expectedRequestUri);
165  $this->requestBuilder->build();
166  }
167 
171  public function buildSetsRequestBaseUri()
172  {
173  $this->injectDependencies();
174  $expectedBaseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
175  $this->mockRequest->expects($this->once())->method('setBaseUri')->with($expectedBaseUri);
176  $this->requestBuilder->build();
177  }
178 
182  public function buildSetsRequestMethod()
183  {
184  $this->injectDependencies();
185  $expectedMethod = 'SomeRequestMethod';
186  $mockEnvironmentService = $this->getMockBuilder(\TYPO3\CMS\Extbase\Service\EnvironmentService::class)
187  ->setMethods(['getServerRequestMethod'])
188  ->getMock();
189  $mockEnvironmentService->expects($this->once())->method('getServerRequestMethod')->will($this->returnValue($expectedMethod));
190  $this->requestBuilder->_set('environmentService', $mockEnvironmentService);
191  $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
192  $this->requestBuilder->build();
193  }
194 
199  {
200  $this->injectDependencies();
201  $expectedVendor = 'Vendor';
202  $this->configuration['vendorName'] = $expectedVendor;
203  $mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
204  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
205  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
206  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
207  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
208  $this->requestBuilder->build();
209  }
210 
215  {
216  $this->injectDependencies();
217  $expectedVendor = 'Vendor';
218  $this->configuration['vendorName'] = $expectedVendor;
219 
220  $mockConfigurationManager = $this->createMock(\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  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
224 
225  $this->requestBuilder->build();
226 
227  unset($this->configuration['vendorName']);
228  $mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
229  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
230  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
231 
232  $this->mockRequest->expects($this->never())->method('setControllerVendorName');
233  $this->requestBuilder->build();
234  }
235 
240  {
241  $this->expectException(Exception::class);
242  $this->expectExceptionCode(1289843275);
243  unset($this->configuration['extensionName']);
244  $mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
245  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
246  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
247  $this->requestBuilder->build();
248  }
249 
254  {
255  $this->expectException(Exception::class);
256  $this->expectExceptionCode(1289843277);
257  unset($this->configuration['pluginName']);
258  $mockConfigurationManager = $this->createMock(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::class);
259  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
260  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
261  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
262  $this->requestBuilder->build();
263  }
264 
269  {
270  $this->expectException(Exception::class);
271  $this->expectExceptionCode(1316104317);
272  $this->configuration['controllerConfiguration'] = [];
273  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
274  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
275  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
276  $this->requestBuilder->build();
277  }
278 
283  {
284  $this->expectException(Exception::class);
285  $this->expectExceptionCode(1295479651);
286  $this->configuration['controllerConfiguration']['TheFirstController'] = [];
287  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
288  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
289  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
290  $this->requestBuilder->build();
291  }
292 
297  {
298  $this->expectException(Exception::class);
299  $this->expectExceptionCode(1316104317);
300  $this->configuration['controllerConfiguration'] = [
301  '' => [
302  'actions' => ['foo']
303  ]
304  ];
305  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
306  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
307  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
308  $this->requestBuilder->build();
309  }
310 
315  {
316  $this->configuration['extensionName'] = 'SomeExtensionName';
317  $this->configuration['pluginName'] = 'SomePluginName';
318  $this->injectDependencies();
319  $_GET = [
320  'tx_someotherextensionname_somepluginname' => [
321  'foo' => 'bar'
322  ],
323  'tx_someextensionname_somepluginname' => [
324  'parameter1' => 'valueGetsOverwritten',
325  'parameter2' => [
326  'parameter3' => 'value3'
327  ]
328  ]
329  ];
330  $_POST = [
331  'tx_someextensionname_someotherpluginname' => [
332  'foo' => 'bar'
333  ],
334  'tx_someextensionname_somepluginname' => [
335  'parameter1' => 'value1',
336  'parameter2' => [
337  'parameter4' => 'value4'
338  ]
339  ]
340  ];
341  $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter1', 'value1');
342  $this->mockRequest->expects($this->at(9))->method('setArgument')->with('parameter2', ['parameter3' => 'value3', 'parameter4' => 'value4']);
343  $this->requestBuilder->build();
344  }
345 
350  {
351  $this->configuration['extensionName'] = 'SomeExtensionName';
352  $this->configuration['pluginName'] = 'SomePluginName';
353  $this->injectDependencies();
354  $_GET = [
355  'tx_someextensionname_somepluginname' => [
356  'format' => 'GET'
357  ]
358  ];
359  $_POST = [
360  'tx_someextensionname_somepluginname' => [
361  'format' => 'POST'
362  ]
363  ];
364  $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST');
365  $this->requestBuilder->build();
366  }
367 
372  {
373  $this->injectDependencies();
374  $expectedResult = [
375  'TheFirstController' => [
376  'show',
377  'index',
378  'new',
379  'create',
380  'delete',
381  'edit',
382  'update',
383  'setup',
384  'test'
385  ],
386  'TheSecondController' => [
387  'show',
388  'index'
389  ],
390  'TheThirdController' => [
391  'delete',
392  'create',
393  'onlyInThirdController'
394  ]
395  ];
396  $this->requestBuilder->build();
397  $actualResult = $this->requestBuilder->_get('allowedControllerActions');
398  $this->assertEquals($expectedResult, $actualResult);
399  }
400 
405  {
406  $this->expectException(Exception::class);
407  $this->expectExceptionCode(1316104317);
408  $this->configuration['controllerConfiguration'] = [];
409  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
410  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
411  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
412  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
413  $this->requestBuilder->build();
414  }
415 
420  {
421  $this->injectDependencies();
422  $_GET = [
423  'tx_myextension_pi1' => [
424  'foo' => 'bar'
425  ]
426  ];
427  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
428  $this->requestBuilder->build();
429  }
430 
435  {
436  $this->injectDependencies();
437  $_GET = [
438  'tx_myextension_pi1' => [
439  'controller' => 'TheSecondController'
440  ]
441  ];
442  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheSecondController');
443  $this->requestBuilder->build();
444  }
445 
450  {
451  $this->expectException(InvalidControllerNameException::class);
452  $this->expectExceptionCode(1313855173);
453  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
454  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
455  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
456  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
457  $_GET = [
458  'tx_myextension_pi1' => [
459  'controller' => 'SomeInvalidController'
460  ]
461  ];
462  $this->requestBuilder->build();
463  }
464 
469  {
470  $this->expectException(PageNotFoundException::class);
471  $this->expectExceptionCode(1313857897);
472  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
473  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
474  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
475  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
476  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
477  $_GET = [
478  'tx_myextension_pi1' => [
479  'controller' => 'SomeInvalidController'
480  ]
481  ];
482  $this->requestBuilder->build();
483  }
484 
489  {
490  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
491  $this->injectDependencies();
492  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
493  $_GET = [
494  'tx_myextension_pi1' => [
495  'controller' => 'SomeInvalidController'
496  ]
497  ];
498  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
499  $this->requestBuilder->build();
500  }
501 
506  {
507  $this->expectException(Exception::class);
508  $this->expectExceptionCode(1316104317);
509  $this->configuration['controllerConfiguration'] = [];
510  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
511  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
512  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
513  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
514  $this->requestBuilder->build();
515  }
516 
521  {
522  $this->injectDependencies();
523  $_GET = [
524  'tx_myextension_pi1' => [
525  'controller' => 'TheThirdController'
526  ]
527  ];
528  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
529  $this->requestBuilder->build();
530  }
531 
536  {
537  $this->injectDependencies();
538  $_GET = [
539  'tx_myextension_pi1' => [
540  'action' => 'create'
541  ]
542  ];
543  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('create');
544  $this->requestBuilder->build();
545  }
546 
551  {
552  $this->injectDependencies();
553  $_GET = [
554  'tx_myextension_pi1' => [
555  'controller' => 'TheThirdController',
556  'action' => 'onlyInThirdController'
557  ]
558  ];
559  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('onlyInThirdController');
560  $this->requestBuilder->build();
561  }
562 
567  {
568  $this->expectException(InvalidActionNameException::class);
569  $this->expectExceptionCode(1313855175);
570  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
571  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
572  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
573  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
574  $_GET = [
575  'tx_myextension_pi1' => [
576  'action' => 'someInvalidAction'
577  ]
578  ];
579  $this->requestBuilder->build();
580  }
581 
586  {
587  $this->expectException(PageNotFoundException::class);
588  $this->expectExceptionCode(1313857898);
589  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
590  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
591  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
592  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
593  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
594  $_GET = [
595  'tx_myextension_pi1' => [
596  'action' => 'someInvalidAction'
597  ]
598  ];
599  $this->requestBuilder->build();
600  }
601 
606  {
607  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
608  $this->injectDependencies();
609  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
610  $_GET = [
611  'tx_myextension_pi1' => [
612  'controller' => 'TheThirdController',
613  'action' => 'someInvalidAction'
614  ]
615  ];
616  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheThirdController');
617  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
618  $this->requestBuilder->build();
619  }
620 
626  {
627  $convolutedFiles = [
628  'a0' => [
629  'name' => [
630  'a1' => 'a.txt'
631  ],
632  'type' => [
633  'a1' => 'text/plain'
634  ],
635  'tmp_name' => [
636  'a1' => '/private/var/tmp/phpbqXsYt'
637  ],
638  'error' => [
639  'a1' => 0
640  ],
641  'size' => [
642  'a1' => 100
643  ]
644  ],
645  'b0' => [
646  'name' => [
647  'b1' => 'b.txt'
648  ],
649  'type' => [
650  'b1' => 'text/plain'
651  ],
652  'tmp_name' => [
653  'b1' => '/private/var/tmp/phpvZ6oUD'
654  ],
655  'error' => [
656  'b1' => 0
657  ],
658  'size' => [
659  'b1' => 200
660  ]
661  ],
662  'c' => [
663  'name' => 'c.txt',
664  'type' => 'text/plain',
665  'tmp_name' => '/private/var/tmp/phpS9KMNw',
666  'error' => 0,
667  'size' => 300
668  ],
669  'd0' => [
670  'name' => [
671  'd1' => [
672  'd2' => [
673  'd3' => 'd.txt'
674  ]
675  ]
676  ],
677  'type' => [
678  'd1' => [
679  'd2' => [
680  'd3' => 'text/plain'
681  ]
682  ]
683  ],
684  'tmp_name' => [
685  'd1' => [
686  'd2' => [
687  'd3' => '/private/var/tmp/phprR3fax'
688  ]
689  ]
690  ],
691  'error' => [
692  'd1' => [
693  'd2' => [
694  'd3' => 0
695  ]
696  ]
697  ],
698  'size' => [
699  'd1' => [
700  'd2' => [
701  'd3' => 400
702  ]
703  ]
704  ]
705  ],
706  'e0' => [
707  'name' => [
708  'e1' => [
709  'e2' => [
710  0 => 'e_one.txt',
711  1 => 'e_two.txt'
712  ]
713  ]
714  ],
715  'type' => [
716  'e1' => [
717  'e2' => [
718  0 => 'text/plain',
719  1 => 'text/plain'
720  ]
721  ]
722  ],
723  'tmp_name' => [
724  'e1' => [
725  'e2' => [
726  0 => '/private/var/tmp/php01fitB',
727  1 => '/private/var/tmp/phpUUB2cv'
728  ]
729  ]
730  ],
731  'error' => [
732  'e1' => [
733  'e2' => [
734  0 => 0,
735  1 => 0
736  ]
737  ]
738  ],
739  'size' => [
740  'e1' => [
741  'e2' => [
742  0 => 510,
743  1 => 520
744  ]
745  ]
746  ]
747  ]
748  ];
749  $untangledFiles = [
750  'a0' => [
751  'a1' => [
752  'name' => 'a.txt',
753  'type' => 'text/plain',
754  'tmp_name' => '/private/var/tmp/phpbqXsYt',
755  'error' => 0,
756  'size' => 100
757  ]
758  ],
759  'b0' => [
760  'b1' => [
761  'name' => 'b.txt',
762  'type' => 'text/plain',
763  'tmp_name' => '/private/var/tmp/phpvZ6oUD',
764  'error' => 0,
765  'size' => 200
766  ]
767  ],
768  'c' => [
769  'name' => 'c.txt',
770  'type' => 'text/plain',
771  'tmp_name' => '/private/var/tmp/phpS9KMNw',
772  'error' => 0,
773  'size' => 300
774  ],
775  'd0' => [
776  'd1' => [
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  ];
809  $requestBuilder = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder::class, ['dummy'], [], '', false);
810  $result = $requestBuilder->_call('untangleFilesArray', $convolutedFiles);
811  $this->assertSame($untangledFiles, $result);
812  }
813 }
getAccessibleMock($originalClassName, $methods=[], array $arguments=[], $mockClassName= '', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)