TYPO3 CMS  TYPO3_6-2
RequestBuilderTest.php
Go to the documentation of this file.
1 <?php
3 
21 
25  protected $requestBuilder;
26 
31 
35  protected $configuration;
36 
40  protected $mockObjectManager;
41 
46 
51 
55  protected $mockRequest;
56 
57  public function setUp() {
58  $this->requestBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\RequestBuilder', array('dummy'));
59  $this->configuration = array(
60  'userFunc' => 'Tx_Extbase_Dispatcher->dispatch',
61  'pluginName' => 'Pi1',
62  'extensionName' => 'MyExtension',
63  'controller' => 'TheFirstController',
64  'action' => 'show',
65  'controllerConfiguration' => array(
66  'TheFirstController' => array(
67  'actions' => array('show', 'index', 'new', 'create', 'delete', 'edit', 'update', 'setup', 'test')
68  ),
69  'TheSecondController' => array(
70  'actions' => array('show', 'index')
71  ),
72  'TheThirdController' => array(
73  'actions' => array('delete', 'create', 'onlyInThirdController')
74  )
75  )
76  );
77  $this->mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
78  $this->mockRequest = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request');
79  $this->mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
80  $this->mockExtensionService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\ExtensionService');
81  $this->mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('getServerRequestMethod'));
82  }
83 
87  protected function injectDependencies() {
88  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
89  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
90  $this->mockObjectManager->expects($this->any())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Web\\Request')->will($this->returnValue($this->mockRequest));
91  $this->requestBuilder->_set('objectManager', $this->mockObjectManager);
92  $pluginNamespace = 'tx_' . strtolower(($this->configuration['extensionName'] . '_' . $this->configuration['pluginName']));
93  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue($pluginNamespace));
94  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
95  $this->mockEnvironmentService->expects($this->any())->method('getServerRequestMethod')->will($this->returnValue('GET'));
96  $this->requestBuilder->_set('environmentService', $this->mockEnvironmentService);
97  }
98 
102  public function buildReturnsAWebRequestObject() {
103  $this->injectDependencies();
104  $request = $this->requestBuilder->build();
105  $this->assertSame($this->mockRequest, $request);
106  }
107 
111  public function buildSetsRequestPluginName() {
112  $this->injectDependencies();
113  $this->mockRequest->expects($this->once())->method('setPluginName')->with('Pi1');
114  $this->requestBuilder->build();
115  }
116 
121  $this->injectDependencies();
122  $this->mockRequest->expects($this->once())->method('setControllerExtensionName')->with('MyExtension');
123  $this->requestBuilder->build();
124  }
125 
129  public function buildSetsRequestControllerName() {
130  $this->injectDependencies();
131  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
132  $this->requestBuilder->build();
133  }
134 
139  $this->injectDependencies();
140  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('show');
141  $this->requestBuilder->build();
142  }
143 
147  public function buildSetsRequestRequestUri() {
148  $this->injectDependencies();
149  $expectedRequestUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
150  $this->mockRequest->expects($this->once())->method('setRequestUri')->with($expectedRequestUri);
151  $this->requestBuilder->build();
152  }
153 
157  public function buildSetsRequestBaseUri() {
158  $this->injectDependencies();
159  $expectedBaseUri = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
160  $this->mockRequest->expects($this->once())->method('setBaseUri')->with($expectedBaseUri);
161  $this->requestBuilder->build();
162  }
163 
167  public function buildSetsRequestMethod() {
168  $this->injectDependencies();
169  $expectedMethod = 'SomeRequestMethod';
170  $mockEnvironmentService = $this->getMock('TYPO3\\CMS\\Extbase\\Service\\EnvironmentService', array('getServerRequestMethod'));
171  $mockEnvironmentService->expects($this->once())->method('getServerRequestMethod')->will($this->returnValue($expectedMethod));
172  $this->requestBuilder->_set('environmentService', $mockEnvironmentService);
173  $this->mockRequest->expects($this->once())->method('setMethod')->with($expectedMethod);
174  $this->requestBuilder->build();
175  }
176 
181  $this->injectDependencies();
182  $expectedVendor = 'Vendor';
183  $this->configuration['vendorName'] = $expectedVendor;
184  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
185  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
186  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
187  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
188  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
189  $this->requestBuilder->build();
190  }
191 
196  $this->injectDependencies();
197  $expectedVendor = 'Vendor';
198  $this->configuration['vendorName'] = $expectedVendor;
199 
200  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
201  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
202  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
203  $this->mockRequest->expects($this->once())->method('setControllerVendorName')->with($expectedVendor);
204 
205  $this->requestBuilder->build();
206 
207  unset($this->configuration['vendorName']);
208  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
209  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
210  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
211 
212  $this->mockRequest->expects($this->never())->method('setControllerVendorName');
213  $this->requestBuilder->build();
214  }
215 
221  unset($this->configuration['extensionName']);
222  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
223  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
224  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
225  $this->requestBuilder->build();
226  }
227 
233  unset($this->configuration['pluginName']);
234  $mockConfigurationManager = $this->getMock('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManagerInterface');
235  $mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
236  $this->requestBuilder->_set('configurationManager', $mockConfigurationManager);
237  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
238  $this->requestBuilder->build();
239  }
240 
246  $this->configuration['controllerConfiguration'] = array();
247  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
248  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
249  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
250  $this->requestBuilder->build();
251  }
252 
258  $this->configuration['controllerConfiguration']['TheFirstController'] = array();
259  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
260  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
261  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
262  $this->requestBuilder->build();
263  }
264 
270  $this->configuration['controllerConfiguration'] = array(
271  '' => array(
272  'actions' => array('foo')
273  )
274  );
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 
285  $this->configuration['extensionName'] = 'SomeExtensionName';
286  $this->configuration['pluginName'] = 'SomePluginName';
287  $this->injectDependencies();
288  $_GET = array(
289  'tx_someotherextensionname_somepluginname' => array(
290  'foo' => 'bar'
291  ),
292  'tx_someextensionname_somepluginname' => array(
293  'parameter1' => 'valueGetsOverwritten',
294  'parameter2' => array(
295  'parameter3' => 'value3'
296  )
297  )
298  );
299  $_POST = array(
300  'tx_someextensionname_someotherpluginname' => array(
301  'foo' => 'bar'
302  ),
303  'tx_someextensionname_somepluginname' => array(
304  'parameter1' => 'value1',
305  'parameter2' => array(
306  'parameter4' => 'value4'
307  )
308  )
309  );
310  $this->mockRequest->expects($this->at(8))->method('setArgument')->with('parameter1', 'value1');
311  $this->mockRequest->expects($this->at(9))->method('setArgument')->with('parameter2', array('parameter3' => 'value3', 'parameter4' => 'value4'));
312  $this->requestBuilder->build();
313  }
314 
319  $this->configuration['extensionName'] = 'SomeExtensionName';
320  $this->configuration['pluginName'] = 'SomePluginName';
321  $this->injectDependencies();
322  $_GET = array(
323  'tx_someextensionname_somepluginname' => array(
324  'format' => 'GET'
325  )
326  );
327  $_POST = array(
328  'tx_someextensionname_somepluginname' => array(
329  'format' => 'POST'
330  )
331  );
332  $this->mockRequest->expects($this->at(7))->method('setFormat')->with('POST');
333  $this->requestBuilder->build();
334  }
335 
340  $this->injectDependencies();
341  $expectedResult = array(
342  'TheFirstController' => array(
343  'show',
344  'index',
345  'new',
346  'create',
347  'delete',
348  'edit',
349  'update',
350  'setup',
351  'test'
352  ),
353  'TheSecondController' => array(
354  'show',
355  'index'
356  ),
357  'TheThirdController' => array(
358  'delete',
359  'create',
360  'onlyInThirdController'
361  )
362  );
363  $this->requestBuilder->build();
364  $actualResult = $this->requestBuilder->_get('allowedControllerActions');
365  $this->assertEquals($expectedResult, $actualResult);
366  }
367 
373  $this->configuration['controllerConfiguration'] = array();
374  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
375  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
376  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
377  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
378  $this->requestBuilder->build();
379  }
380 
385  $this->injectDependencies();
386  $_GET = array(
387  'tx_myextension_pi1' => array(
388  'foo' => 'bar'
389  )
390  );
391  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
392  $this->requestBuilder->build();
393  }
394 
399  $this->injectDependencies();
400  $_GET = array(
401  'tx_myextension_pi1' => array(
402  'controller' => 'TheSecondController'
403  )
404  );
405  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheSecondController');
406  $this->requestBuilder->build();
407  }
408 
414  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
415  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
416  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
417  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
418  $_GET = array(
419  'tx_myextension_pi1' => array(
420  'controller' => 'SomeInvalidController'
421  )
422  );
423  $this->requestBuilder->build();
424  }
425 
431  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
432  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
433  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
434  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
435  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
436  $_GET = array(
437  'tx_myextension_pi1' => array(
438  'controller' => 'SomeInvalidController'
439  )
440  );
441  $this->requestBuilder->build();
442  }
443 
448  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
449  $this->injectDependencies();
450  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
451  $_GET = array(
452  'tx_myextension_pi1' => array(
453  'controller' => 'SomeInvalidController'
454  )
455  );
456  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheFirstController');
457  $this->requestBuilder->build();
458  }
459 
465  $this->configuration['controllerConfiguration'] = array();
466  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
467  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
468  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
469  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
470  $this->requestBuilder->build();
471  }
472 
477  $this->injectDependencies();
478  $_GET = array(
479  'tx_myextension_pi1' => array(
480  'controller' => 'TheThirdController'
481  )
482  );
483  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
484  $this->requestBuilder->build();
485  }
486 
491  $this->injectDependencies();
492  $_GET = array(
493  'tx_myextension_pi1' => array(
494  'action' => 'create'
495  )
496  );
497  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('create');
498  $this->requestBuilder->build();
499  }
500 
505  $this->injectDependencies();
506  $_GET = array(
507  'tx_myextension_pi1' => array(
508  'controller' => 'TheThirdController',
509  'action' => 'onlyInThirdController'
510  )
511  );
512  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('onlyInThirdController');
513  $this->requestBuilder->build();
514  }
515 
521  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
522  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
523  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
524  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
525  $_GET = array(
526  'tx_myextension_pi1' => array(
527  'action' => 'someInvalidAction'
528  )
529  );
530  $this->requestBuilder->build();
531  }
532 
538  $this->configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = 1;
539  $this->mockConfigurationManager->expects($this->any())->method('getConfiguration')->will($this->returnValue($this->configuration));
540  $this->requestBuilder->_set('configurationManager', $this->mockConfigurationManager);
541  $this->mockExtensionService->expects($this->any())->method('getPluginNamespace')->will($this->returnValue('tx_myextension_pi1'));
542  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
543  $_GET = array(
544  'tx_myextension_pi1' => array(
545  'action' => 'someInvalidAction'
546  )
547  );
548  $this->requestBuilder->build();
549  }
550 
555  $this->configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = 1;
556  $this->injectDependencies();
557  $this->requestBuilder->_set('extensionService', $this->mockExtensionService);
558  $_GET = array(
559  'tx_myextension_pi1' => array(
560  'controller' => 'TheThirdController',
561  'action' => 'someInvalidAction'
562  )
563  );
564  $this->mockRequest->expects($this->once())->method('setControllerName')->with('TheThirdController');
565  $this->mockRequest->expects($this->once())->method('setControllerActionName')->with('delete');
566  $this->requestBuilder->build();
567  }
568 
574  $convolutedFiles = array(
575  'a0' => array(
576  'name' => array(
577  'a1' => 'a.txt'
578  ),
579  'type' => array(
580  'a1' => 'text/plain'
581  ),
582  'tmp_name' => array(
583  'a1' => '/private/var/tmp/phpbqXsYt'
584  ),
585  'error' => array(
586  'a1' => 0
587  ),
588  'size' => array(
589  'a1' => 100
590  )
591  ),
592  'b0' => array(
593  'name' => array(
594  'b1' => 'b.txt'
595  ),
596  'type' => array(
597  'b1' => 'text/plain'
598  ),
599  'tmp_name' => array(
600  'b1' => '/private/var/tmp/phpvZ6oUD'
601  ),
602  'error' => array(
603  'b1' => 0
604  ),
605  'size' => array(
606  'b1' => 200
607  )
608  ),
609  'c' => array(
610  'name' => 'c.txt',
611  'type' => 'text/plain',
612  'tmp_name' => '/private/var/tmp/phpS9KMNw',
613  'error' => 0,
614  'size' => 300
615  ),
616  'd0' => array(
617  'name' => array(
618  'd1' => array(
619  'd2' => array(
620  'd3' => 'd.txt'
621  )
622  )
623  ),
624  'type' => array(
625  'd1' => array(
626  'd2' => array(
627  'd3' => 'text/plain'
628  )
629  )
630  ),
631  'tmp_name' => array(
632  'd1' => array(
633  'd2' => array(
634  'd3' => '/private/var/tmp/phprR3fax'
635  )
636  )
637  ),
638  'error' => array(
639  'd1' => array(
640  'd2' => array(
641  'd3' => 0
642  )
643  )
644  ),
645  'size' => array(
646  'd1' => array(
647  'd2' => array(
648  'd3' => 400
649  )
650  )
651  )
652  ),
653  'e0' => array(
654  'name' => array(
655  'e1' => array(
656  'e2' => array(
657  0 => 'e_one.txt',
658  1 => 'e_two.txt'
659  )
660  )
661  ),
662  'type' => array(
663  'e1' => array(
664  'e2' => array(
665  0 => 'text/plain',
666  1 => 'text/plain'
667  )
668  )
669  ),
670  'tmp_name' => array(
671  'e1' => array(
672  'e2' => array(
673  0 => '/private/var/tmp/php01fitB',
674  1 => '/private/var/tmp/phpUUB2cv'
675  )
676  )
677  ),
678  'error' => array(
679  'e1' => array(
680  'e2' => array(
681  0 => 0,
682  1 => 0
683  )
684  )
685  ),
686  'size' => array(
687  'e1' => array(
688  'e2' => array(
689  0 => 510,
690  1 => 520
691  )
692  )
693  )
694  )
695  );
696  $untangledFiles = array(
697  'a0' => array(
698  'a1' => array(
699  'name' => 'a.txt',
700  'type' => 'text/plain',
701  'tmp_name' => '/private/var/tmp/phpbqXsYt',
702  'error' => 0,
703  'size' => 100
704  )
705  ),
706  'b0' => array(
707  'b1' => array(
708  'name' => 'b.txt',
709  'type' => 'text/plain',
710  'tmp_name' => '/private/var/tmp/phpvZ6oUD',
711  'error' => 0,
712  'size' => 200
713  )
714  ),
715  'c' => array(
716  'name' => 'c.txt',
717  'type' => 'text/plain',
718  'tmp_name' => '/private/var/tmp/phpS9KMNw',
719  'error' => 0,
720  'size' => 300
721  ),
722  'd0' => array(
723  'd1' => array(
724  'd2' => array(
725  'd3' => array(
726  'name' => 'd.txt',
727  'type' => 'text/plain',
728  'tmp_name' => '/private/var/tmp/phprR3fax',
729  'error' => 0,
730  'size' => 400
731  )
732  )
733  )
734  ),
735  'e0' => array(
736  'e1' => array(
737  'e2' => array(
738  0 => array(
739  'name' => 'e_one.txt',
740  'type' => 'text/plain',
741  'tmp_name' => '/private/var/tmp/php01fitB',
742  'error' => 0,
743  'size' => 510
744  ),
745  1 => array(
746  'name' => 'e_two.txt',
747  'type' => 'text/plain',
748  'tmp_name' => '/private/var/tmp/phpUUB2cv',
749  'error' => 0,
750  'size' => 520
751  )
752  )
753  )
754  )
755  );
756  $requestBuilder = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Mvc\\Web\\RequestBuilder', array('dummy'), array(), '', FALSE);
757  $result = $requestBuilder->_call('untangleFilesArray', $convolutedFiles);
758  $this->assertSame($untangledFiles, $result);
759  }
760 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.