‪TYPO3CMS  ‪main
RequestBuilderTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use Psr\Http\Message\ServerRequestInterface;
39 use TYPO3\TestingFramework\Core\Functional\Framework\FrameworkState;
40 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
42 
43 final class ‪RequestBuilderTest extends FunctionalTestCase
44 {
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  // Some tests need GeneralUtility::getIndpEnv('SCRIPT_NAME') to return correct value instead of
49  // 'vendor/phpunit/phpunit/phpunit', used eg. in TypoScriptFrontendController. To manipulate/set it
50  // before creating request from globals, the global must be set. Thus the framework state has to be
51  // saved/reset and later restored. Similar requirement is needed when emitting frontend requests with
52  // the testing-framework. This is done globally for the testcase, so reset can be cleanly done even if
53  // a test has failed.
54  // @see FunctionalTestCase::retrieveFrontendSubRequestResult()
55  // @todo: Get rid of getIndpEnv var usage in TSFE
56  FrameworkState::push();
57  FrameworkState::reset();
58  $request = (new ‪ServerRequest())->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
59  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
60  }
61 
62  protected function ‪tearDown(): void
63  {
64  // Restore previously saved framework state.
65  // This is to clean-up manipulated framework state for testing
66  // purposes even on failures, thus done globally for the testcase.
67  // @see FunctionalTestCase::retrieveFrontendSubRequestResult()
68  // @todo: Get rid of getIndpEnv var usage in TSFE
69  FrameworkState::pop();
70  parent::tearDown();
71  }
72 
73  #[Test]
74  public function ‪buildBuildsARequestInterfaceObject(): void
75  {
76  $extensionName = 'blog_example';
77  $pluginName = 'blog';
78 
79  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
80  'packageName' => 'typo3/cms-blog-example',
81  'path' => '/blog-example',
82  'extensionName' => $extensionName,
83  'controllerActions' => [
84  BlogController::class => ['list'],
85  ],
86  ]);
87 
88  $configuration = [];
89  $configuration['extensionName'] = $extensionName;
90  $configuration['pluginName'] = $pluginName;
91 
92  $configurationManager = $this->get(ConfigurationManager::class);
93  $configurationManager->setConfiguration($configuration);
94 
95  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
96  $mainRequest = $mainRequest->withAttribute('module', $module);
97  $request = $this->get(RequestBuilder::class)->build($mainRequest);
98 
99  self::assertInstanceOf(RequestInterface::class, $request);
100  self::assertSame('html', $request->getFormat());
101  }
102 
103  #[Test]
105  {
106  $extensionName = 'blog_example';
107  $pluginName = 'blog';
108 
109  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
110  'packageName' => 'typo3/cms-blog-example',
111  'path' => '/blog-example',
112  'extensionName' => $extensionName,
113  'controllerActions' => [
114  BlogController::class => ['list'],
115  ],
116  ]);
117 
118  $configuration = [];
119  $configuration['extensionName'] = $extensionName;
120  $configuration['pluginName'] = $pluginName;
121  $configuration['format'] = 'json';
122 
123  $configurationManager = $this->get(ConfigurationManager::class);
124  $configurationManager->setConfiguration($configuration);
125 
126  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
127  $mainRequest = $mainRequest->withAttribute('module', $module);
128  $request = $this->get(RequestBuilder::class)->build($mainRequest);
129 
130  self::assertInstanceOf(RequestInterface::class, $request);
131  self::assertSame('json', $request->getFormat());
132  }
133 
134  #[Test]
136  {
137  $extensionName = 'blog_example';
138  $pluginName = 'blog';
139 
140  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
141  'packageName' => 'typo3/cms-blog-example',
142  'path' => '/blog-example',
143  'extensionName' => $extensionName,
144  'controllerActions' => [
145  BlogController::class => ['list'],
146  ],
147  ]);
148 
149  $configuration = [];
150  $configuration['extensionName'] = $extensionName;
151  $configuration['pluginName'] = $pluginName;
152 
153  $configurationManager = $this->get(ConfigurationManager::class);
154  $configurationManager->setConfiguration($configuration);
155 
156  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
157  $mainRequest = $mainRequest->withQueryParams(['format' => 'json']);
158  $mainRequest = $mainRequest->withAttribute('module', $module);
159  $request = $this->get(RequestBuilder::class)->build($mainRequest);
160 
161  self::assertInstanceOf(RequestInterface::class, $request);
162  self::assertSame('json', $request->getFormat());
163  }
164 
165  #[Test]
167  {
168  $this->expectException(Exception::class);
169  $this->expectExceptionCode(1289843275);
170  $this->expectExceptionMessage('"extensionName" is not properly configured. Request can\'t be dispatched!');
171 
172  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
173  $requestBuilder = $this->get(RequestBuilder::class);
174  $requestBuilder->build($mainRequest);
175  }
176 
177  #[Test]
179  {
180  $this->expectException(Exception::class);
181  $this->expectExceptionCode(1289843277);
182  $this->expectExceptionMessage('"pluginName" is not properly configured. Request can\'t be dispatched!');
183 
184  $configurationManager = $this->get(ConfigurationManager::class);
185  $configurationManager->setConfiguration(['extensionName' => 'blog_example']);
186 
187  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
188  $requestBuilder = $this->get(RequestBuilder::class);
189  $requestBuilder->build($mainRequest);
190  }
191 
192  #[Test]
194  {
195  $_FILES['dummy'] = [
196  'name' => 'name.pdf',
197  'type' => 'application/pdf',
198  'tmp_name' => '/tmp/php/php1h4j1o',
199  'error' => UPLOAD_ERR_OK,
200  'size' => 98174,
201  ];
202 
203  $extensionName = 'blog_example';
204  $pluginName = 'blog';
205 
206  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
207  'packageName' => 'typo3/cms-blog-example',
208  'path' => '/blog-example',
209  'extensionName' => $extensionName,
210  'controllerActions' => [
211  BlogController::class => ['list'],
212  ],
213  ]);
214 
215  $configuration = [];
216  $configuration['extensionName'] = $extensionName;
217  $configuration['pluginName'] = $pluginName;
218 
219  $configurationManager = $this->get(ConfigurationManager::class);
220  $configurationManager->setConfiguration($configuration);
221 
222  ‪$_SERVER['REQUEST_METHOD'] = 'POST';
223  ‪$_SERVER['HTTP_HOST'] = 'https://example.com/';
224  ‪$_SERVER['SERVER_NAME'] = 'https://example.com/';
225  // Needed for GeneralUtility::getIndpEnv('SCRIPT_NAME') to return correct value instead of
226  // 'vendor/phpunit/phpunit/phpunit', used eg. in TypoScriptFrontendController absRefPrefix='auto
227  // and other places.
228  // @see FunctionalTestCase::retrieveFrontendSubRequestResult()
229  // @todo: Get rid of getIndpEnv var usage in TSFE
230  ‪$_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
231 
233  $normalizedParams = ‪NormalizedParams::createFromRequest($mainRequest);
234  $mainRequest = $mainRequest->withAttribute('normalizedParams', $normalizedParams)->withAttribute('module', $module);
235  $request = $this->get(RequestBuilder::class)->build($mainRequest);
236 
237  self::assertInstanceOf(RequestInterface::class, $request);
238  $uploadedFiles = $request->getUploadedFiles();
239  $uploadedFile = $uploadedFiles['dummy'];
240  self::assertInstanceOf(UploadedFile::class, $uploadedFile);
241  self::assertSame('name.pdf', $uploadedFile->getClientFilename());
242  self::assertSame('application/pdf', $uploadedFile->getClientMediaType());
243  self::assertSame(UPLOAD_ERR_OK, $uploadedFile->getError());
244  self::assertSame(98174, $uploadedFile->getSize());
245  self::assertSame('/tmp/php/php1h4j1o', $uploadedFile->getTemporaryFileName());
246  }
247 
248  #[Test]
250  {
251  $_FILES['dummy'] = [
252  'error' => [
253  'pdf' => UPLOAD_ERR_OK,
254  'jpg' => UPLOAD_ERR_OK,
255  ],
256  'name' => [
257  'pdf' => 'name.pdf',
258  'jpg' => 'name.jpg',
259  ],
260  'type' => [
261  'pdf' => 'application/pdf',
262  'jpg' => 'image/jpg',
263  ],
264  'tmp_name' => [
265  'pdf' => '/tmp/php/php1h4j1o',
266  'jpg' => '/tmp/php/php6hst32',
267  ],
268  'size' => [
269  'pdf' => 98174,
270  ],
271  ];
272 
273  $extensionName = 'blog_example';
274  $pluginName = 'blog';
275 
276  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
277  'packageName' => 'typo3/cms-blog-example',
278  'path' => '/blog-example',
279  'extensionName' => $extensionName,
280  'controllerActions' => [
281  BlogController::class => ['list'],
282  ],
283  ]);
284 
285  $configuration = [];
286  $configuration['extensionName'] = $extensionName;
287  $configuration['pluginName'] = $pluginName;
288 
289  $configurationManager = $this->get(ConfigurationManager::class);
290  $configurationManager->setConfiguration($configuration);
291 
292  ‪$_SERVER['REQUEST_METHOD'] = 'POST';
293  ‪$_SERVER['HTTP_HOST'] = 'https://example.com/';
294  ‪$_SERVER['SERVER_NAME'] = 'https://example.com/';
295  // Needed for GeneralUtility::getIndpEnv('SCRIPT_NAME') to return correct value instead of
296  // 'vendor/phpunit/phpunit/phpunit', used eg. in TypoScriptFrontendController absRefPrefix='auto
297  // and other places.
298  // @see FunctionalTestCase::retrieveFrontendSubRequestResult()
299  // @todo: Get rid of getIndpEnv var usage in TSFE
300  ‪$_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
301 
303  $normalizedParams = ‪NormalizedParams::createFromRequest($mainRequest);
304  $mainRequest = $mainRequest->withAttribute('normalizedParams', $normalizedParams)->withAttribute('module', $module);
305  $request = $this->get(RequestBuilder::class)->build($mainRequest);
306 
307  self::assertInstanceOf(RequestInterface::class, $request);
308  $uploadedFiles = $request->getUploadedFiles();
309  $uploadedFile1 = $uploadedFiles['dummy']['pdf'];
310  self::assertInstanceOf(UploadedFile::class, $uploadedFile1);
311  self::assertSame('name.pdf', $uploadedFile1->getClientFilename());
312  self::assertSame('application/pdf', $uploadedFile1->getClientMediaType());
313  self::assertSame(UPLOAD_ERR_OK, $uploadedFile1->getError());
314  self::assertSame(98174, $uploadedFile1->getSize());
315  self::assertSame('/tmp/php/php1h4j1o', $uploadedFile1->getTemporaryFileName());
316  $uploadedFile2 = $uploadedFiles['dummy']['jpg'];
317  self::assertInstanceOf(UploadedFile::class, $uploadedFile2);
318  self::assertSame('name.jpg', $uploadedFile2->getClientFilename());
319  self::assertSame('image/jpg', $uploadedFile2->getClientMediaType());
320  self::assertSame(UPLOAD_ERR_OK, $uploadedFile2->getError());
321  self::assertSame(0, $uploadedFile2->getSize());
322  self::assertSame('/tmp/php/php6hst32', $uploadedFile2->getTemporaryFileName());
323  }
324 
325  #[Test]
327  {
328  $this->expectException(InvalidControllerNameException::class);
329  $this->expectExceptionCode(1313855173);
330  $this->expectExceptionMessage('The controller "NonExistentController" is not allowed by plugin "blog". Please check for TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php.');
331 
332  $_GET['tx_blog_example_blog']['controller'] = 'NonExistentController';
333 
334  $extensionName = 'blog_example';
335  $pluginName = 'blog';
336 
337  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
338  'packageName' => 'typo3/cms-blog-example',
339  'path' => '/blog-example',
340  'extensionName' => $extensionName,
341  'controllerActions' => [
342  BlogController::class => ['list'],
343  ],
344  ]);
345 
346  $configuration = [];
347  $configuration['extensionName'] = $extensionName;
348  $configuration['pluginName'] = $pluginName;
349 
350  $configurationManager = $this->get(ConfigurationManager::class);
351  $configurationManager->setConfiguration($configuration);
352 
353  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
354  $mainRequest = $mainRequest->withAttribute('module', $module);
355  $mainRequest = $mainRequest->withQueryParams(['controller' => 'NonExistentController']);
356  $requestBuilder = $this->get(RequestBuilder::class);
357  $requestBuilder->build($mainRequest);
358  }
359 
360  #[Test]
362  {
363  $this->expectException(PageNotFoundException::class);
364  $this->expectExceptionCode(1313857897);
365  $this->expectExceptionMessage('The requested resource was not found');
366 
367  $extensionName = 'blog_example';
368  $pluginName = 'blog';
369 
370  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
371  'packageName' => 'typo3/cms-blog-example',
372  'path' => '/blog-example',
373  'extensionName' => $extensionName,
374  'controllerActions' => [
375  BlogController::class => ['list'],
376  ],
377  ]);
378 
379  $configuration = [];
380  $configuration['extensionName'] = $extensionName;
381  $configuration['pluginName'] = $pluginName;
382  $configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = true;
383 
384  $configurationManager = $this->get(ConfigurationManager::class);
385  $configurationManager->setConfiguration($configuration);
386 
387  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
388  $mainRequest = $mainRequest->withQueryParams(['controller' => 'NonExistentController']);
389  $mainRequest = $mainRequest->withAttribute('module', $module);
390  $requestBuilder = $this->get(RequestBuilder::class);
391  $requestBuilder->build($mainRequest);
392  }
393 
394  #[Test]
396  {
397  $this->expectException(Exception::class);
398  $this->expectExceptionCode(1316104317);
399  $this->expectExceptionMessage('The default controller for extension "blog_example" and plugin "blog" can not be determined. Please check for TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php.');
400 
401  $extensionName = 'blog_example';
402  $pluginName = 'blog';
403 
404  $configuration = [];
405  $configuration['extensionName'] = $extensionName;
406  $configuration['pluginName'] = $pluginName;
407 
408  $configurationManager = $this->get(ConfigurationManager::class);
409  $configurationManager->setConfiguration($configuration);
410 
411  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
412  $requestBuilder = $this->get(RequestBuilder::class);
413  $requestBuilder->build($mainRequest);
414  }
415 
416  #[Test]
418  {
419  $extensionName = 'blog_example';
420  $pluginName = 'blog';
421 
422  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
423  'packageName' => 'typo3/cms-blog-example',
424  'path' => '/blog-example',
425  'extensionName' => $extensionName,
426  'controllerActions' => [
427  BlogController::class => ['list'],
428  ],
429  ]);
430 
431  $configuration = [];
432  $configuration['extensionName'] = $extensionName;
433  $configuration['pluginName'] = $pluginName;
434  $configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = true;
435 
436  $configurationManager = $this->get(ConfigurationManager::class);
437  $configurationManager->setConfiguration($configuration);
438 
439  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
440  $mainRequest = $mainRequest->withAttribute('module', $module);
441  $mainRequest = $mainRequest->withQueryParams(['controller' => 'NonExistentController']);
442  $request = $this->get(RequestBuilder::class)->build($mainRequest);
443 
444  self::assertInstanceOf(RequestInterface::class, $request);
445  self::assertSame('Blog', $request->getControllerName());
446  }
447 
448  #[Test]
450  {
451  $extensionName = 'blog_example';
452  $pluginName = 'blog';
453 
454  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
455  'packageName' => 'typo3/cms-blog-example',
456  'path' => '/blog-example',
457  'extensionName' => $extensionName,
458  'controllerActions' => [
459  BlogController::class => ['list'],
460  'TYPO3Tests\BlogExample\Controller\UserController' => ['list'],
461  ],
462  ]);
463 
464  $configuration = [];
465  $configuration['extensionName'] = $extensionName;
466  $configuration['pluginName'] = $pluginName;
467 
468  $configurationManager = $this->get(ConfigurationManager::class);
469  $configurationManager->setConfiguration($configuration);
470 
471  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
472  $mainRequest = $mainRequest->withAttribute('module', $module);
473  $mainRequest = $mainRequest->withQueryParams(['controller' => 'User']);
474  $request = $this->get(RequestBuilder::class)->build($mainRequest);
475 
476  self::assertInstanceOf(RequestInterface::class, $request);
477  self::assertSame('User', $request->getControllerName());
478  }
479 
480  #[Test]
482  {
483  $this->expectException(InvalidActionNameException::class);
484  $this->expectExceptionCode(1313855175);
485  $this->expectExceptionMessage('The action "NonExistentAction" (controller "TYPO3Tests\BlogExample\Controller\BlogController") is not allowed by this plugin / module. Please check TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php / TYPO3\CMS\Extbase\Utility\ExtensionUtility::configureModule() in your ext_tables.php.');
486 
487  $extensionName = 'blog_example';
488  $pluginName = 'blog';
489 
490  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
491  'packageName' => 'typo3/cms-blog-example',
492  'path' => '/blog-example',
493  'extensionName' => $extensionName,
494  'controllerActions' => [
495  BlogController::class => ['list'],
496  ],
497  ]);
498 
499  $configuration = [];
500  $configuration['extensionName'] = $extensionName;
501  $configuration['pluginName'] = $pluginName;
502 
503  $configurationManager = $this->get(ConfigurationManager::class);
504  $configurationManager->setConfiguration($configuration);
505 
506  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
507  $mainRequest = $mainRequest->withAttribute('module', $module);
508  $mainRequest = $mainRequest->withQueryParams(['action' => 'NonExistentAction']);
509  $requestBuilder = $this->get(RequestBuilder::class);
510  $requestBuilder->build($mainRequest);
511  }
512 
513  #[Test]
515  {
516  $this->expectException(PageNotFoundException::class);
517  $this->expectExceptionCode(1313857898);
518  $this->expectExceptionMessage('The requested resource was not found');
519 
520  $extensionName = 'blog_example';
521  $pluginName = 'blog';
522 
523  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
524  'packageName' => 'typo3/cms-blog-example',
525  'path' => '/blog-example',
526  'extensionName' => $extensionName,
527  'controllerActions' => [
528  BlogController::class => ['list'],
529  ],
530  ]);
531 
532  $configuration = [];
533  $configuration['extensionName'] = $extensionName;
534  $configuration['pluginName'] = $pluginName;
535  $configuration['mvc']['throwPageNotFoundExceptionIfActionCantBeResolved'] = true;
536 
537  $configurationManager = $this->get(ConfigurationManager::class);
538  $configurationManager->setConfiguration($configuration);
539 
540  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
541  $mainRequest = $mainRequest->withAttribute('module', $module);
542  $mainRequest = $mainRequest->withQueryParams(['action' => 'NonExistentAction']);
543  $requestBuilder = $this->get(RequestBuilder::class);
544  $requestBuilder->build($mainRequest);
545  }
546 
547  #[Test]
549  {
550  $extensionName = 'blog_example';
551  $pluginName = 'blog';
552 
553  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
554  'packageName' => 'typo3/cms-blog-example',
555  'path' => '/blog-example',
556  'extensionName' => $extensionName,
557  'controllerActions' => [
558  BlogController::class => ['list'],
559  ],
560  ]);
561 
562  $configuration = [];
563  $configuration['extensionName'] = $extensionName;
564  $configuration['pluginName'] = $pluginName;
565  $configuration['mvc']['callDefaultActionIfActionCantBeResolved'] = true;
566 
567  $configurationManager = $this->get(ConfigurationManager::class);
568  $configurationManager->setConfiguration($configuration);
569 
570  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
571  $mainRequest = $mainRequest->withAttribute('module', $module);
572  $mainRequest = $mainRequest->withQueryParams(['tx_blog_example_blog' => ['action' => 'NonExistentAction']]);
573  $request = $this->get(RequestBuilder::class)->build($mainRequest);
574 
575  self::assertInstanceOf(RequestInterface::class, $request);
576  self::assertSame('list', $request->getControllerActionName());
577  }
578 
579  #[Test]
581  {
582  $extensionName = 'blog_example';
583  $pluginName = 'blog';
584 
585  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
586  'packageName' => 'typo3/cms-blog-example',
587  'path' => '/blog-example',
588  'extensionName' => $extensionName,
589  'controllerActions' => [
590  BlogController::class => ['list', 'show'],
591  ],
592  ]);
593 
594  $configuration = [];
595  $configuration['extensionName'] = $extensionName;
596  $configuration['pluginName'] = $pluginName;
597 
598  $configurationManager = $this->get(ConfigurationManager::class);
599  $configurationManager->setConfiguration($configuration);
600 
601  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
602  $mainRequest = $mainRequest->withAttribute('module', $module);
603  $mainRequest = $mainRequest->withQueryParams(['action' => 'show']);
604  $request = $this->get(RequestBuilder::class)->build($mainRequest);
605 
606  self::assertInstanceOf(RequestInterface::class, $request);
607  self::assertSame('show', $request->getControllerActionName());
608  }
609 
610  #[Test]
612  {
613  $this->expectException(Exception::class);
614  $this->expectExceptionCode(1295479651);
615  $this->expectExceptionMessage('The default action can not be determined for controller "TYPO3Tests\BlogExample\Controller\BlogController". Please check TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin() in your ext_localconf.php.');
616 
617  $extensionName = 'blog_example';
618  $pluginName = 'blog';
619 
620  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
621  'packageName' => 'typo3/cms-blog-example',
622  'path' => '/blog-example',
623  'extensionName' => $extensionName,
624  'controllerActions' => [
625  BlogController::class => '',
626  ],
627  ]);
628 
629  $configuration = [];
630  $configuration['extensionName'] = $extensionName;
631  $configuration['pluginName'] = $pluginName;
632 
633  $configurationManager = $this->get(ConfigurationManager::class);
634  $configurationManager->setConfiguration($configuration);
635 
636  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
637  $mainRequest = $mainRequest->withAttribute('module', $module);
638  $requestBuilder = $this->get(RequestBuilder::class);
639  $requestBuilder->build($mainRequest);
640  }
641 
642  #[Test]
644  {
645  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
646  $mainRequest = $mainRequest
647  ->withQueryParams(['action' => 'show'])
648  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
649 
650  $extensionName = 'blog_example';
651  $pluginName = 'blog';
652 
653  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
654  'packageName' => 'typo3/cms-blog-example',
655  'path' => '/blog-example',
656  'extensionName' => $extensionName,
657  'controllerActions' => [
658  BlogController::class => ['list', 'show'],
659  ],
660  ]);
661  $mainRequest = $mainRequest->withAttribute('module', $module);
662 
663  $configuration = [];
664  $configuration['extensionName'] = $extensionName;
665  $configuration['pluginName'] = $pluginName;
666 
667  $configurationManager = $this->get(ConfigurationManager::class);
668  $configurationManager->setConfiguration($configuration);
669 
670  $request = $this->get(RequestBuilder::class)->build($mainRequest);
671 
672  self::assertInstanceOf(RequestInterface::class, $request);
673  self::assertSame('show', $request->getControllerActionName());
674  }
675 
676  #[Test]
678  {
679  $pageArguments = new ‪PageArguments(1, '0', ['tx_blog_example_blog' => ['action' => 'show']]);
680 
681  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), []);
682  $frontendTypoScript->setSetupArray([]);
683  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
684  $mainRequest = $mainRequest
685  ->withAttribute('routing', $pageArguments)
686  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE)
687  ->withAttribute('frontend.typoscript', $frontendTypoScript);
688 
689  ‪$GLOBALS['TYPO3_REQUEST'] = $mainRequest;
690 
691  $extensionName = 'blog_example';
692  $pluginName = 'blog';
693 
694  $configuration = [];
695  $configuration['extensionName'] = $extensionName;
696  $configuration['pluginName'] = $pluginName;
697  ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['extbase']['extensions'][$extensionName]['plugins'][$pluginName]['controllers'] = [
698  BlogController::class => [
699  'className' => BlogController::class,
700  'alias' => 'blog',
701  'actions' => ['list', 'show'],
702  ],
703  ];
704 
705  $configurationManager = $this->get(ConfigurationManager::class);
706  $configurationManager->setConfiguration($configuration);
707 
708  $request = $this->get(RequestBuilder::class)->build($mainRequest);
709 
710  self::assertInstanceOf(RequestInterface::class, $request);
711  self::assertSame('show', $request->getControllerActionName());
712  }
713 
714  #[Test]
716  {
717  $mainRequest = $this->‪prepareServerRequest('https://example.com/', 'POST');
718  $mainRequest = $mainRequest
719  ->withParsedBody(['action' => 'show'])
720  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
721 
722  $extensionName = 'blog_example';
723  $pluginName = 'blog';
724 
725  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
726  'packageName' => 'typo3/cms-blog-example',
727  'path' => '/blog-example',
728  'extensionName' => $extensionName,
729  'controllerActions' => [
730  BlogController::class => ['list', 'show'],
731  ],
732  ]);
733  $mainRequest = $mainRequest->withAttribute('module', $module);
734 
735  $configuration = [];
736  $configuration['extensionName'] = $extensionName;
737  $configuration['pluginName'] = $pluginName;
738 
739  $configurationManager = $this->get(ConfigurationManager::class);
740  $configurationManager->setConfiguration($configuration);
741 
742  $request = $this->get(RequestBuilder::class)->build($mainRequest);
743 
744  self::assertInstanceOf(RequestInterface::class, $request);
745  self::assertSame('show', $request->getControllerActionName());
746  }
747 
748  #[Test]
750  {
751  $pageArguments = new ‪PageArguments(1, '0', ['tx_blog_example_blog' => 'not_an_array']);
752 
753  $mainRequest = $this->‪prepareServerRequest('https://example.com/');
754  $mainRequest = $mainRequest
755  ->withParsedBody(['tx_blog_example_blog' => ['action' => 'show']])
756  ->withAttribute('routing', $pageArguments)
757  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_FE);
758 
759  $extensionName = 'blog_example';
760  $pluginName = 'blog';
761 
762  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
763  'packageName' => 'typo3/cms-blog-example',
764  'path' => '/blog-example',
765  'extensionName' => $extensionName,
766  'controllerActions' => [
767  BlogController::class => ['list', 'show'],
768  ],
769  ]);
770  $mainRequest = $mainRequest->withAttribute('module', $module);
771 
772  $configuration = [];
773  $configuration['extensionName'] = $extensionName;
774  $configuration['pluginName'] = $pluginName;
775  $configurationManager = $this->get(ConfigurationManager::class);
776  $configurationManager->setConfiguration($configuration);
777 
778  $request = $this->get(RequestBuilder::class)->build($mainRequest);
779 
780  self::assertInstanceOf(RequestInterface::class, $request);
781  self::assertSame('list', $request->getControllerActionName());
782  }
783 
784  #[Test]
786  {
787  $mainRequest = $this->‪prepareServerRequest('https://example.com/typo3/module/blog-example/Blog/show');
788 
789  $pluginName = 'blog';
790  $extensionName = 'blog_example';
791 
792  $module = ‪ExtbaseModule::createFromConfiguration($pluginName, [
793  'packageName' => 'typo3/cms-blog-example',
794  'path' => '/blog-example',
795  'extensionName' => $extensionName,
796  'controllerActions' => [
797  BlogController::class => ['list', 'show'],
798  ],
799  ]);
800 
801  $mainRequest = $mainRequest
802  ->withAttribute('module', $module)
803  ->withAttribute('route', new ‪Route(
804  '/module/blog-example/Blog/show',
805  [
806  'module' => $module,
807  'controller' => 'Blog',
808  'action' => 'show',
809  ]
810  ));
811 
812  $configuration = [];
813  $configuration['extensionName'] = $extensionName;
814  $configuration['pluginName'] = $pluginName;
815 
816  $configurationManager = $this->get(ConfigurationManager::class);
817  $configurationManager->setConfiguration($configuration);
818 
819  $request = $this->get(RequestBuilder::class)->build($mainRequest);
820 
821  self::assertInstanceOf(RequestInterface::class, $request);
822  self::assertSame('show', $request->getControllerActionName());
823  self::assertSame('Blog', $request->getArgument('controller'));
824  self::assertSame('show', $request->getArgument('action'));
825  }
826 
827  private function ‪prepareServerRequest(string ‪$url, string $method = 'GET'): ServerRequestInterface
828  {
829  $request = (new ‪ServerRequest(‪$url, $method))
830  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
831  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
832  return $request->withAttribute('normalizedParams', $normalizedParams);
833  }
834 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3Tests\BlogExample\Controller\BlogController
Definition: BlogController.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\tearDown
‪tearDown()
Definition: RequestBuilderTest.php:62
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameReturnsActionDefinedViaParametersOfServerRequest
‪resolveActionNameReturnsActionDefinedViaParametersOfServerRequest()
Definition: RequestBuilderTest.php:643
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\loadDefaultValuesThrowsExceptionIfExtensionNameIsNotProperlyConfigured
‪loadDefaultValuesThrowsExceptionIfExtensionNameIsNotProperlyConfigured()
Definition: RequestBuilderTest.php:166
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameThrowsInvalidActionNameExceptionIfNonExistentActionIsSetViaGetParameter
‪resolveActionNameThrowsInvalidActionNameExceptionIfNonExistentActionIsSetViaGetParameter()
Definition: RequestBuilderTest.php:481
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveControllerClassNameThrowsInvalidControllerNameExceptionIfNonExistentControllerIsSetViaGetParameter
‪resolveControllerClassNameThrowsInvalidControllerNameExceptionIfNonExistentControllerIsSetViaGetParameter()
Definition: RequestBuilderTest.php:326
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Extbase\Mvc\Exception\InvalidControllerNameException
Definition: InvalidControllerNameException.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveControllerClassNameThrowsPageNotFoundException
‪resolveControllerClassNameThrowsPageNotFoundException()
Definition: RequestBuilderTest.php:361
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\multipleUploadedFileCanBeRetrievedFromRequest
‪multipleUploadedFileCanBeRetrievedFromRequest()
Definition: RequestBuilderTest.php:249
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\buildBuildsARequestInterfaceObject
‪buildBuildsARequestInterfaceObject()
Definition: RequestBuilderTest.php:74
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameThrowsAnExceptionIfTheDefaultActionCannotBeDetermined
‪resolveActionNameThrowsAnExceptionIfTheDefaultActionCannotBeDetermined()
Definition: RequestBuilderTest.php:611
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveControllerClassNameReturnsControllerDefinedViaParametersIfControllerIsConfigured
‪resolveControllerClassNameReturnsControllerDefinedViaParametersIfControllerIsConfigured()
Definition: RequestBuilderTest.php:449
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:32
‪TYPO3\CMS\Backend\Module\ExtbaseModule
Definition: ExtbaseModule.php:30
‪TYPO3\CMS\Core\Error\Http\PageNotFoundException
Definition: PageNotFoundException.php:24
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameReturnsDefaultActionIfCallDefaultActionIfActionCantBeResolvedIsConfigured
‪resolveActionNameReturnsDefaultActionIfCallDefaultActionIfActionCantBeResolvedIsConfigured()
Definition: RequestBuilderTest.php:548
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\silentlyIgnoreInvalidParameterAndUseDefaultAction
‪silentlyIgnoreInvalidParameterAndUseDefaultAction()
Definition: RequestBuilderTest.php:749
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\uploadedFileCanBeRetrievedFromRequest
‪uploadedFileCanBeRetrievedFromRequest()
Definition: RequestBuilderTest.php:193
‪TYPO3\CMS\Extbase\Mvc\Exception
Definition: InfiniteLoopException.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameReturnsActionDefinedViaParametersIfActionIsConfigured
‪resolveActionNameReturnsActionDefinedViaParametersIfActionIsConfigured()
Definition: RequestBuilderTest.php:580
‪TYPO3\CMS\Backend\Module\ExtbaseModule\createFromConfiguration
‪static createFromConfiguration(string $identifier, array $configuration)
Definition: ExtbaseModule.php:109
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\controllerActionParametersAreAddedToRequest
‪controllerActionParametersAreAddedToRequest()
Definition: RequestBuilderTest.php:785
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameReturnsActionDefinedViaParsedBodyOfServerRequest
‪resolveActionNameReturnsActionDefinedViaParsedBodyOfServerRequest()
Definition: RequestBuilderTest.php:715
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameThrowsPageNotFoundException
‪resolveActionNameThrowsPageNotFoundException()
Definition: RequestBuilderTest.php:514
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\prepareServerRequest
‪prepareServerRequest(string $url, string $method='GET')
Definition: RequestBuilderTest.php:827
‪TYPO3\CMS\Extbase\Mvc\Exception\InvalidActionNameException
Definition: InvalidActionNameException.php:25
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveActionNameReturnsActionDefinedViaPageArgumentOfServerRequest
‪resolveActionNameReturnsActionDefinedViaPageArgumentOfServerRequest()
Definition: RequestBuilderTest.php:677
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:34
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web
Definition: RequestBuilderTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\loadDefaultValuesThrowsExceptionIfPluginNameIsNotProperlyConfigured
‪loadDefaultValuesThrowsExceptionIfPluginNameIsNotProperlyConfigured()
Definition: RequestBuilderTest.php:178
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\setUp
‪setUp()
Definition: RequestBuilderTest.php:45
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveControllerClassNameThrowsAnExceptionIfTheDefaultControllerCannotBeDetermined
‪resolveControllerClassNameThrowsAnExceptionIfTheDefaultControllerCannotBeDetermined()
Definition: RequestBuilderTest.php:395
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_FE
‪const REQUESTTYPE_FE
Definition: SystemEnvironmentBuilder.php:43
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\resolveControllerClassNameReturnsDefaultControllerIfCallDefaultActionIfActionCantBeResolvedIsConfigured
‪resolveControllerClassNameReturnsDefaultControllerIfCallDefaultActionIfActionCantBeResolvedIsConfigured()
Definition: RequestBuilderTest.php:417
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\loadDefaultValuesOverridesFormatIfConfigured
‪loadDefaultValuesOverridesFormatIfConfigured()
Definition: RequestBuilderTest.php:104
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest\buildOverridesFormatIfSetInGetParameters
‪buildOverridesFormatIfSetInGetParameters()
Definition: RequestBuilderTest.php:135
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Web\RequestBuilderTest
Definition: RequestBuilderTest.php:44
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Core\Http\ServerRequest\withAttribute
‪static withAttribute(string $name, $value)
Definition: ServerRequest.php:310
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59
‪TYPO3\CMS\Extbase\Mvc\Web\RequestBuilder
Definition: RequestBuilder.php:40