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