‪TYPO3CMS  11.5
RequestHandlerTest.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 Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\EventDispatcher\EventDispatcherInterface;
23 use Psr\Http\Message\ServerRequestInterface;
26 use TYPO3\CMS\Core\Page\PageRenderer;
31 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
40 class ‪RequestHandlerTest extends UnitTestCase
41 {
42  use ProphecyTrait;
43 
44  protected ‪$resetSingletonInstances = true;
45 
47  {
48  return [
49  'no original values' => [
50  [],
51  [],
52  '<html>',
53  ],
54  'no additional values' => [
55  ['dir' => 'left'],
56  [],
57  '<html dir="left">',
58  ],
59  'no additional values #2' => [
60  ['dir' => 'left', 'xmlns:dir' => 'left'],
61  [],
62  '<html dir="left" xmlns:dir="left">',
63  ],
64  'disable all attributes' => [
65  ['dir' => 'left', 'xmlns:dir' => 'left'],
66  ['htmlTag_setParams' => 'none'],
67  '<html>',
68  ],
69  'only add setParams' => [
70  ['dir' => 'left', 'xmlns:dir' => 'left'],
71  ['htmlTag_setParams' => 'amp'],
72  '<html amp>',
73  ],
74  'attributes property trumps htmlTag_setParams' => [
75  ['dir' => 'left', 'xmlns:dir' => 'left'],
76  ['htmlTag.' => ['attributes.' => ['amp' => '']], 'htmlTag_setParams' => 'none'],
77  '<html dir="left" xmlns:dir="left" amp>',
78  ],
79  'attributes property with mixed values' => [
80  ['dir' => 'left', 'xmlns:dir' => 'left'],
81  ['htmlTag.' => ['attributes.' => ['amp' => '', 'no-js' => 'true', 'additional-enabled' => 0]]],
82  '<html dir="left" xmlns:dir="left" amp no-js="true" additional-enabled="0">',
83  ],
84  'attributes property overrides default settings' => [
85  ['dir' => 'left'],
86  ['htmlTag.' => ['attributes.' => ['amp' => '', 'dir' => 'right']]],
87  '<html amp dir="right">',
88  ],
89  ];
90  }
91 
101  public function ‪generateHtmlTagIncludesAllPossibilities(array $htmlTagAttributes, array $configuration, string $expectedResult): void
102  {
103  $subject = $this->getAccessibleMock(RequestHandler::class, ['dummy'], [], '', false);
104  $cObj = $this->prophesize(ContentObjectRenderer::class);
105  $cObj->stdWrap(Argument::cetera())->shouldNotBeCalled();
106  $result = $subject->_call('generateHtmlTag', $htmlTagAttributes, $configuration, $cObj->reveal());
107 
108  self::assertEquals($expectedResult, $result);
109  }
110 
115  {
116  return [
117  'simple meta' => [
118  [
119  'author' => 'Markus Klein',
120  ],
121  '',
122  [
123  'type' => 'name',
124  'name' => 'author',
125  'content' => 'Markus Klein',
126  ],
127  ],
128  'httpEquivalent meta' => [
129  [
130  'X-UA-Compatible' => 'IE=edge,chrome=1',
131  'X-UA-Compatible.' => ['httpEquivalent' => 1],
132  ],
133  'IE=edge,chrome=1',
134  [
135  'type' => 'http-equiv',
136  'name' => 'X-UA-Compatible',
137  'content' => 'IE=edge,chrome=1',
138  ],
139  ],
140  'httpEquivalent meta xhtml new notation' => [
141  [
142  'X-UA-Compatible' => 'IE=edge,chrome=1',
143  'X-UA-Compatible.' => ['attribute' => 'http-equiv'],
144  ],
145  'IE=edge,chrome=1',
146  [
147  'type' => 'http-equiv',
148  'name' => 'X-UA-Compatible',
149  'content' => 'IE=edge,chrome=1',
150  ],
151  ],
152  'refresh meta' => [
153  [
154  'refresh' => '10',
155  ],
156  '',
157  [
158  'type' => 'http-equiv',
159  'name' => 'refresh',
160  'content' => '10',
161  ],
162  ],
163  'refresh meta new notation' => [
164  [
165  'refresh' => '10',
166  'refresh.' => ['attribute' => 'http-equiv'],
167  ],
168  '10',
169  [
170  'type' => 'http-equiv',
171  'name' => 'refresh',
172  'content' => '10',
173  ],
174  ],
175  'meta with dot' => [
176  [
177  'DC.author' => 'Markus Klein',
178  ],
179  '',
180  [
181  'type' => 'name',
182  'name' => 'DC.author',
183  'content' => 'Markus Klein',
184  ],
185  ],
186  'meta with colon' => [
187  [
188  'OG:title' => 'Magic Tests',
189  ],
190  '',
191  [
192  'type' => 'name',
193  'name' => 'OG:title',
194  'content' => 'Magic Tests',
195  ],
196  ],
197  'different attribute name' => [
198  [
199  'og:site_title' => 'My TYPO3 site',
200  'og:site_title.' => ['attribute' => 'property'],
201  ],
202  'My TYPO3 site',
203  [
204  'type' => 'property',
205  'name' => 'og:site_title',
206  'content' => 'My TYPO3 site',
207  ],
208  ],
209  'meta with 0 value' => [
210  [
211  'custom:key' => '0',
212  ],
213  '',
214  [
215  'type' => 'name',
216  'name' => 'custom:key',
217  'content' => '0',
218  ],
219  ],
220  ];
221  }
222 
226  public function ‪generateMetaTagExpectExceptionOnBogusTags(): void
227  {
228  $stdWrapResult = '10';
229 
230  $typoScript = [
231  'refresh' => '10',
232  'refresh.' => ['attribute' => 'http-equiv-new'],
233  ];
234 
235  $expectedTags = [
236  'type' => 'http-equiv-new',
237  'name' => 'refresh',
238  'content' => '10',
239  ];
240 
241  $siteLanguage = $this->‪createSiteWithLanguage()->getLanguageById(3);
242  $cObj = $this->prophesize(ContentObjectRenderer::class);
243  $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
244  $cObj->cObjGetSeparated(Argument::cetera())->willReturn([]);
245  $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
246  $tmpl = $this->prophesize(TemplateService::class);
247  $frontendControllerProphecy = $this->prophesize(TypoScriptFrontendController::class);
248  $frontendControllerProphecy->generatePageTitle()->willReturn('');
249  $frontendControllerProphecy->INTincScript_loadJSCode()->shouldBeCalled();
250  $frontendController = $frontendControllerProphecy->reveal();
251  $frontendController->cObj = $cObj->reveal();
252  $frontendController->tmpl = $tmpl->reveal();
253  $frontendController->page = [
254  'title' => '',
255  ];
256  $frontendController->pSetup = [
257  'meta.' => $typoScript,
258  ];
259  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
260  $typo3InformationProphecy->getInlineHeaderComment()->willReturn('dummy');
261  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
262 
263  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
264  $subject = $this->getAccessibleMock(RequestHandler::class, ['getPageRenderer'], [], '', false);
265  $requestProphecy = $this->prophesize(ServerRequestInterface::class)->reveal();
266  $modifyHrefLangTagsEvent = new ModifyHrefLangTagsEvent($requestProphecy);
267  $dispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
268  $dispatcherProphecy->dispatch($modifyHrefLangTagsEvent)->willReturn($modifyHrefLangTagsEvent);
269  $subject->_set('eventDispatcher', $dispatcherProphecy->reveal());
270  $subject->method('getPageRenderer')->willReturn($pageRendererProphecy->reveal());
271  $subject->_call('processHtmlBasedRenderingSettings', $frontendController, $siteLanguage, $requestProphecy);
272  $pageRendererProphecy->setMetaTag($expectedTags['type'], $expectedTags['name'], $expectedTags['content'])->willThrow(\InvalidArgumentException::class);
273  }
274 
283  public function ‪generateMetaTagHtmlGeneratesCorrectTags(array $typoScript, string $stdWrapResult, array $expectedTags): void
284  {
285  $siteLanguage = $this->‪createSiteWithLanguage()->getLanguageById(3);
286  $cObj = $this->prophesize(ContentObjectRenderer::class);
287  $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
288  $cObj->cObjGetSeparated(Argument::cetera())->willReturn([]);
289  $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
290  $tmpl = $this->prophesize(TemplateService::class);
291  $frontendControllerProphecy = $this->prophesize(TypoScriptFrontendController::class);
292  $frontendControllerProphecy->generatePageTitle()->willReturn('');
293  $frontendControllerProphecy->INTincScript_loadJSCode()->shouldBeCalled();
294  $frontendController = $frontendControllerProphecy->reveal();
295  $frontendController->cObj = $cObj->reveal();
296  $frontendController->tmpl = $tmpl->reveal();
297  $frontendController->config = [
298  'config' => [],
299  ];
300  $frontendController->page = [
301  'title' => '',
302  ];
303  $frontendController->pSetup = [
304  'meta.' => $typoScript,
305  ];
306  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
307  $typo3InformationProphecy->getInlineHeaderComment()->willReturn('dummy');
308  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
309 
310  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
311  $subject = $this->getAccessibleMock(RequestHandler::class, ['getPageRenderer'], [], '', false);
312  $requestProphecy = $this->prophesize(ServerRequestInterface::class)->reveal();
313  $modifyHrefLangTagsEvent = new ModifyHrefLangTagsEvent($requestProphecy);
314  $dispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
315  $dispatcherProphecy->dispatch($modifyHrefLangTagsEvent)->willReturn($modifyHrefLangTagsEvent);
316  $subject->_set('eventDispatcher', $dispatcherProphecy->reveal());
317  $subject->method('getPageRenderer')->willReturn($pageRendererProphecy->reveal());
318  $subject->_call('processHtmlBasedRenderingSettings', $frontendController, $siteLanguage, $requestProphecy);
319 
320  $pageRendererProphecy->setMetaTag($expectedTags['type'], $expectedTags['name'], $expectedTags['content'], [], false)->shouldHaveBeenCalled();
321  }
322 
327  {
328  $stdWrapResult = '';
329 
330  $typoScript = [
331  'custom:key' => '',
332  ];
333 
334  $siteLanguage = $this->‪createSiteWithLanguage()->getLanguageById(3);
335  $cObj = $this->prophesize(ContentObjectRenderer::class);
336  $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
337  $cObj->cObjGetSeparated(Argument::cetera())->willReturn([]);
338  $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
339  $tmpl = $this->prophesize(TemplateService::class);
340  $frontendControllerProphecy = $this->prophesize(TypoScriptFrontendController::class);
341  $frontendControllerProphecy->generatePageTitle()->willReturn('');
342  $frontendControllerProphecy->INTincScript_loadJSCode()->shouldBeCalled();
343  $frontendController = $frontendControllerProphecy->reveal();
344  $frontendController->cObj = $cObj->reveal();
345  $frontendController->tmpl = $tmpl->reveal();
346  $frontendController->config = [
347  'config' => [],
348  ];
349  $frontendController->page = [
350  'title' => '',
351  ];
352  $frontendController->pSetup = [
353  'meta.' => $typoScript,
354  ];
355  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
356  $typo3InformationProphecy->getInlineHeaderComment()->willReturn('dummy');
357  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
358 
359  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
360  $subject = $this->getAccessibleMock(RequestHandler::class, ['getPageRenderer'], [], '', false);
361  $subject->method('getPageRenderer')->willReturn($pageRendererProphecy->reveal());
362  $subject->_set('timeTracker', new TimeTracker(false));
363  $requestProphecy = $this->prophesize(ServerRequestInterface::class)->reveal();
364  $modifyHrefLangTagsEvent = new ModifyHrefLangTagsEvent($requestProphecy);
365  $dispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
366  $dispatcherProphecy->dispatch($modifyHrefLangTagsEvent)->willReturn($modifyHrefLangTagsEvent);
367  $subject->_set('eventDispatcher', $dispatcherProphecy->reveal());
368  $subject->_call('processHtmlBasedRenderingSettings', $frontendController, $siteLanguage, $requestProphecy);
369 
370  $pageRendererProphecy->setMetaTag(null, null, null)->shouldNotBeCalled();
371  }
372 
373  public function ‪generateMultipleMetaTagsDataProvider(): array
374  {
375  return [
376  'multi value attribute name' => [
377  [
378  'og:locale:alternate.' => [
379  'attribute' => 'property',
380  'value' => [
381  10 => 'nl_NL',
382  20 => 'de_DE',
383  ],
384  ],
385  ],
386  '',
387  [
388  [
389  'type' => 'property',
390  'name' => 'og:locale:alternate',
391  'content' => 'nl_NL',
392  ],
393  [
394  'type' => 'property',
395  'name' => 'og:locale:alternate',
396  'content' => 'de_DE',
397  ],
398  ],
399  ],
400  'multi value attribute name (empty values are skipped)' => [
401  [
402  'og:locale:alternate.' => [
403  'attribute' => 'property',
404  'value' => [
405  10 => 'nl_NL',
406  20 => '',
407  30 => 'de_DE',
408  ],
409  ],
410  ],
411  '',
412  [
413  [
414  'type' => 'property',
415  'name' => 'og:locale:alternate',
416  'content' => 'nl_NL',
417  ],
418  [
419  'type' => 'property',
420  'name' => 'og:locale:alternate',
421  'content' => 'de_DE',
422  ],
423  ],
424  ],
425  ];
426  }
427 
436  public function ‪generateMultipleMetaTags(array $typoScript, string $stdWrapResult, array $expectedTags): void
437  {
438  $siteLanguage = $this->‪createSiteWithLanguage()->getLanguageById(3);
439  $cObj = $this->prophesize(ContentObjectRenderer::class);
440  $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
441  $cObj->cObjGetSeparated(Argument::cetera())->willReturn([]);
442  $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
443  $tmpl = $this->prophesize(TemplateService::class);
444  $frontendControllerProphecy = $this->prophesize(TypoScriptFrontendController::class);
445  $frontendControllerProphecy->generatePageTitle()->willReturn('');
446  $frontendControllerProphecy->INTincScript_loadJSCode()->shouldBeCalled();
447  $frontendController = $frontendControllerProphecy->reveal();
448  $frontendController->cObj = $cObj->reveal();
449  $frontendController->tmpl = $tmpl->reveal();
450  $frontendController->config = [
451  'config' => [],
452  ];
453  $frontendController->page = [
454  'title' => '',
455  ];
456  $frontendController->pSetup = [
457  'meta.' => $typoScript,
458  ];
459  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
460  $typo3InformationProphecy->getInlineHeaderComment()->willReturn('This website is...');
461  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
462 
463  $pageRendererProphecy = $this->prophesize(PageRenderer::class);
464  $subject = $this->getAccessibleMock(RequestHandler::class, ['getPageRenderer'], [], '', false);
465  $subject->method('getPageRenderer')->willReturn($pageRendererProphecy->reveal());
466  $subject->_set('timeTracker', new ‪TimeTracker(false));
467  $requestProphecy = $this->prophesize(ServerRequestInterface::class)->reveal();
468  $modifyHrefLangTagsEvent = new ‪ModifyHrefLangTagsEvent($requestProphecy);
469  $dispatcherProphecy = $this->prophesize(EventDispatcherInterface::class);
470  $dispatcherProphecy->dispatch($modifyHrefLangTagsEvent)->willReturn($modifyHrefLangTagsEvent);
471  $subject->_set('eventDispatcher', $dispatcherProphecy->reveal());
472  $subject->_call('processHtmlBasedRenderingSettings', $frontendController, $siteLanguage, $requestProphecy);
473 
474  $pageRendererProphecy->setMetaTag($expectedTags[0]['type'], $expectedTags[0]['name'], $expectedTags[0]['content'], [], false)->shouldHaveBeenCalled();
475  $pageRendererProphecy->setMetaTag($expectedTags[1]['type'], $expectedTags[1]['name'], $expectedTags[1]['content'], [], false)->shouldHaveBeenCalled();
476  }
477 
484  {
485  $getVars = ['outside' => '1'];
486  $postVars = ['world' => 'yo'];
487  GeneralUtility::flushInternalRuntimeCaches();
488  $_SERVER['REQUEST_METHOD'] = 'POST';
489  $_SERVER['HTTP_HOST'] = 'https://www.example.com/my/path/';
490  $_GET = $getVars;
491  $_POST = $postVars;
493 
494  $subject = $this->getAccessibleMock(RequestHandler::class, ['dummy'], [], '', false);
495  $subject->_call('resetGlobalsToCurrentRequest', $request);
496  self::assertEquals($_GET, $getVars);
497  self::assertEquals($_POST, $postVars);
498  }
499 
506  {
507  $getVars = ['typical' => '1'];
508  $postVars = ['mixtape' => 'wheels'];
509  $modifiedGetVars = ['typical' => 1, 'dont-stop' => 'the-music'];
510  $modifiedPostVars = ['mixtape' => 'wheels', 'tx_blogexample_pi1' => ['uid' => 13]];
511  GeneralUtility::flushInternalRuntimeCaches();
512  $_SERVER['REQUEST_METHOD'] = 'POST';
513  $_SERVER['HTTP_HOST'] = 'https://www.example.com/my/path/';
514  $_GET = $getVars;
515  $_POST = $postVars;
517  $request = $request->withQueryParams($modifiedGetVars);
518  $request = $request->withParsedBody($modifiedPostVars);
519 
520  $subject = $this->getAccessibleMock(RequestHandler::class, ['dummy'], [], '', false);
521  $subject->_call('resetGlobalsToCurrentRequest', $request);
522  self::assertEquals($_GET, $modifiedGetVars);
523  self::assertEquals($_POST, $modifiedPostVars);
524  }
525 
526  private function ‪createSiteWithLanguage(): ‪Site
527  {
528  return new ‪Site('test', 1, [
529  'identifier' => 'test',
530  'rootPageId' => 1,
531  'base' => '/',
532  'languages' => [
533  [
534  'base' => '/',
535  'languageId' => 3,
536  'locale' => 'fr_FR',
537  ],
538  ],
539  ]);
540  }
541 }
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateHtmlTagIncludesAllPossibilitiesDataProvider
‪generateHtmlTagIncludesAllPossibilitiesDataProvider()
Definition: RequestHandlerTest.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMultipleMetaTagsDataProvider
‪generateMultipleMetaTagsDataProvider()
Definition: RequestHandlerTest.php:372
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\createSiteWithLanguage
‪createSiteWithLanguage()
Definition: RequestHandlerTest.php:525
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMetaTagHtmlGeneratesCorrectTags
‪generateMetaTagHtmlGeneratesCorrectTags(array $typoScript, string $stdWrapResult, array $expectedTags)
Definition: RequestHandlerTest.php:282
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest
Definition: RequestHandlerTest.php:41
‪TYPO3\CMS\Frontend\Tests\Unit\Http
Definition: RequestHandlerTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\resetGlobalsToCurrentRequestWithModifiedRequestOverridesGlobals
‪resetGlobalsToCurrentRequestWithModifiedRequestOverridesGlobals()
Definition: RequestHandlerTest.php:504
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMetaTagHtmlGenerateNoTagWithEmptyContent
‪generateMetaTagHtmlGenerateNoTagWithEmptyContent()
Definition: RequestHandlerTest.php:325
‪TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent
Definition: ModifyHrefLangTagsEvent.php:27
‪TYPO3\CMS\Frontend\Http\RequestHandler
Definition: RequestHandler.php:68
‪TYPO3\CMS\Core\TypoScript\TemplateService
Definition: TemplateService.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\resetGlobalsToCurrentRequestDoesNotModifyAnything
‪resetGlobalsToCurrentRequestDoesNotModifyAnything()
Definition: RequestHandlerTest.php:482
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMetaTagExpectExceptionOnBogusTags
‪generateMetaTagExpectExceptionOnBogusTags()
Definition: RequestHandlerTest.php:225
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateHtmlTagIncludesAllPossibilities
‪generateHtmlTagIncludesAllPossibilities(array $htmlTagAttributes, array $configuration, string $expectedResult)
Definition: RequestHandlerTest.php:100
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: RequestHandlerTest.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMetaTagHtmlGeneratesCorrectTagsDataProvider
‪array generateMetaTagHtmlGeneratesCorrectTagsDataProvider()
Definition: RequestHandlerTest.php:113
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\Http\RequestHandlerTest\generateMultipleMetaTags
‪generateMultipleMetaTags(array $typoScript, string $stdWrapResult, array $expectedTags)
Definition: RequestHandlerTest.php:435
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59