[!!!][TASK] Remove deprecated GeneralUtility functionality
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / UnitDeprecated / Page / PageGeneratorTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Frontend\Tests\UnitDeprecated\Page;
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
18 use Prophecy\Argument;
19 use TYPO3\CMS\Core\Page\PageRenderer;
20 use TYPO3\CMS\Core\TypoScript\TemplateService;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
24 use TYPO3\CMS\Frontend\Page\PageGenerator;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26
27 /**
28 * Test case
29 */
30 class PageGeneratorTest extends UnitTestCase
31 {
32 /**
33 * Tear down
34 */
35 protected function tearDown()
36 {
37 GeneralUtility::purgeInstances();
38 parent::tearDown();
39 }
40
41 /**
42 * @return array
43 */
44 public function generateMetaTagHtmlGeneratesCorrectTagsDataProvider()
45 {
46 return [
47 'simple meta' => [
48 [
49 'author' => 'Markus Klein',
50 ],
51 '',
52 [
53 'type' => 'name',
54 'name' => 'author',
55 'content' => 'Markus Klein'
56 ]
57 ],
58 'httpEquivalent meta' => [
59 [
60 'X-UA-Compatible' => 'IE=edge,chrome=1',
61 'X-UA-Compatible.' => ['httpEquivalent' => 1]
62 ],
63 'IE=edge,chrome=1',
64 [
65 'type' => 'http-equiv',
66 'name' => 'X-UA-Compatible',
67 'content' => 'IE=edge,chrome=1'
68 ]
69 ],
70 'httpEquivalent meta xhtml new notation' => [
71 [
72 'X-UA-Compatible' => 'IE=edge,chrome=1',
73 'X-UA-Compatible.' => ['attribute' => 'http-equiv']
74 ],
75 'IE=edge,chrome=1',
76 [
77 'type' => 'http-equiv',
78 'name' => 'X-UA-Compatible',
79 'content' => 'IE=edge,chrome=1'
80 ]
81 ],
82 'refresh meta' => [
83 [
84 'refresh' => '10',
85 ],
86 '',
87 [
88 'type' => 'http-equiv',
89 'name' => 'refresh',
90 'content' => '10'
91 ]
92 ],
93 'refresh meta new notation' => [
94 [
95 'refresh' => '10',
96 'refresh.' => ['attribute' => 'http-equiv']
97 ],
98 '10',
99 [
100 'type' => 'http-equiv',
101 'name' => 'refresh',
102 'content' => '10'
103 ]
104 ],
105 'meta with dot' => [
106 [
107 'DC.author' => 'Markus Klein',
108 ],
109 '',
110 [
111 'type' => 'name',
112 'name' => 'DC.author',
113 'content' => 'Markus Klein'
114 ]
115 ],
116 'meta with colon' => [
117 [
118 'OG:title' => 'Magic Tests',
119 ],
120 '',
121 [
122 'type' => 'name',
123 'name' => 'OG:title',
124 'content' => 'Magic Tests'
125 ]
126 ],
127 'different attribute name' => [
128 [
129 'og:site_title' => 'My TYPO3 site',
130 'og:site_title.' => ['attribute' => 'property'],
131 ],
132 'My TYPO3 site',
133 [
134 'type' => 'property',
135 'name' => 'og:site_title',
136 'content' => 'My TYPO3 site'
137 ]
138 ],
139 'meta with 0 value' => [
140 [
141 'custom:key' => '0',
142 ],
143 '',
144 [
145 'type' => 'name',
146 'name' => 'custom:key',
147 'content' => '0'
148 ]
149 ],
150 ];
151 }
152
153 /**
154 * @test
155 */
156 public function generateMetaTagExpectExceptionOnBogusTags()
157 {
158 $stdWrapResult = '10';
159
160 $typoScript = [
161 'refresh' => '10',
162 'refresh.' => ['attribute' => 'http-equiv-new']
163 ];
164
165 $expectedTags = [
166 'type' => 'http-equiv-new',
167 'name' => 'refresh',
168 'content' => '10'
169 ];
170
171 $cObj = $this->prophesize(ContentObjectRenderer::class);
172 $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
173 $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
174 $tmpl = $this->prophesize(TemplateService::class);
175 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
176 $tsfe->generatePageTitle()->willReturn('');
177 $tsfe->INTincScript_loadJSCode()->shouldBeCalled();
178 $tsfe->cObj = $cObj->reveal();
179 $tsfe->tmpl = $tmpl->reveal();
180 $tsfe->page = [
181 'title' => ''
182 ];
183 $tsfe->pSetup = [
184 'meta.' => $typoScript
185 ];
186 $GLOBALS['TSFE'] = $tsfe->reveal();
187
188 $pageRendererProphecy = $this->prophesize(PageRenderer::class);
189 GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRendererProphecy->reveal());
190
191 PageGenerator::renderContentWithHeader('');
192
193 $pageRendererProphecy->setMetaTag($expectedTags['type'], $expectedTags['name'], $expectedTags['content'])->willThrow(\InvalidArgumentException::class);
194 }
195
196 /**
197 * @test
198 * @dataProvider generateMetaTagHtmlGeneratesCorrectTagsDataProvider
199 *
200 * @param array $typoScript
201 * @param string $stdWrapResult
202 * @param array $expectedTags
203 */
204 public function generateMetaTagHtmlGeneratesCorrectTags(array $typoScript, string $stdWrapResult, array $expectedTags)
205 {
206 $cObj = $this->prophesize(ContentObjectRenderer::class);
207 $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
208 $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
209 $tmpl = $this->prophesize(TemplateService::class);
210 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
211 $tsfe->generatePageTitle()->willReturn('');
212 $tsfe->INTincScript_loadJSCode()->shouldBeCalled();
213 $tsfe->cObj = $cObj->reveal();
214 $tsfe->tmpl = $tmpl->reveal();
215 $tsfe->config = [
216 'config' => [],
217 ];
218 $tsfe->page = [
219 'title' => ''
220 ];
221 $tsfe->pSetup = [
222 'meta.' => $typoScript
223 ];
224 $GLOBALS['TSFE'] = $tsfe->reveal();
225
226 $pageRendererProphecy = $this->prophesize(PageRenderer::class);
227 GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRendererProphecy->reveal());
228
229 PageGenerator::renderContentWithHeader('');
230
231 $pageRendererProphecy->setMetaTag($expectedTags['type'], $expectedTags['name'], $expectedTags['content'], [], false)->shouldHaveBeenCalled();
232 }
233
234 /**
235 * @test
236 */
237 public function generateMetaTagHtmlGenerateNoTagWithEmptyContent()
238 {
239 $stdWrapResult = '';
240
241 $typoScript = [
242 'custom:key' => '',
243 ];
244
245 $cObj = $this->prophesize(ContentObjectRenderer::class);
246 $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
247 $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
248 $tmpl = $this->prophesize(TemplateService::class);
249 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
250 $tsfe->generatePageTitle()->willReturn('');
251 $tsfe->INTincScript_loadJSCode()->shouldBeCalled();
252 $tsfe->cObj = $cObj->reveal();
253 $tsfe->tmpl = $tmpl->reveal();
254 $tsfe->config = [
255 'config' => [],
256 ];
257 $tsfe->page = [
258 'title' => ''
259 ];
260 $tsfe->pSetup = [
261 'meta.' => $typoScript
262 ];
263 $GLOBALS['TSFE'] = $tsfe->reveal();
264
265 $pageRendererProphecy = $this->prophesize(PageRenderer::class);
266 GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRendererProphecy->reveal());
267
268 PageGenerator::renderContentWithHeader('');
269
270 $pageRendererProphecy->setMetaTag(null, null, null)->shouldNotBeCalled();
271 }
272
273 public function generateMultipleMetaTagsDataProvider()
274 {
275 return [
276 'multi value attribute name' => [
277 [
278 'og:locale:alternate.' => [
279 'attribute' => 'property',
280 'value' => [
281 10 => 'nl_NL',
282 20 => 'de_DE',
283 ]
284 ],
285 ],
286 '',
287 [
288 [
289 'type' => 'property',
290 'name' => 'og:locale:alternate',
291 'content' => 'nl_NL'
292 ],
293 [
294 'type' => 'property',
295 'name' => 'og:locale:alternate',
296 'content' => 'de_DE'
297 ]
298 ]
299 ],
300 'multi value attribute name (empty values are skipped)' => [
301 [
302 'og:locale:alternate.' => [
303 'attribute' => 'property',
304 'value' => [
305 10 => 'nl_NL',
306 20 => '',
307 30 => 'de_DE',
308 ]
309 ],
310 ],
311 '',
312 [
313 [
314 'type' => 'property',
315 'name' => 'og:locale:alternate',
316 'content' => 'nl_NL'
317 ],
318 [
319 'type' => 'property',
320 'name' => 'og:locale:alternate',
321 'content' => 'de_DE'
322 ]
323 ],
324 ],
325 ];
326 }
327
328 /**
329 * @test
330 * @dataProvider generateMultipleMetaTagsDataProvider
331 *
332 * @param array $typoScript
333 * @param string $stdWrapResult
334 * @param array $expectedTags
335 */
336 public function generateMultipleMetaTags(array $typoScript, string $stdWrapResult, array $expectedTags)
337 {
338 $cObj = $this->prophesize(ContentObjectRenderer::class);
339 $cObj->cObjGet(Argument::cetera())->shouldBeCalled();
340 $cObj->stdWrap(Argument::cetera())->willReturn($stdWrapResult);
341 $tmpl = $this->prophesize(TemplateService::class);
342 $tsfe = $this->prophesize(TypoScriptFrontendController::class);
343 $tsfe->generatePageTitle()->willReturn('');
344 $tsfe->INTincScript_loadJSCode()->shouldBeCalled();
345 $tsfe->cObj = $cObj->reveal();
346 $tsfe->tmpl = $tmpl->reveal();
347 $tsfe->config = [
348 'config' => [],
349 ];
350 $tsfe->page = [
351 'title' => ''
352 ];
353 $tsfe->pSetup = [
354 'meta.' => $typoScript
355 ];
356 $GLOBALS['TSFE'] = $tsfe->reveal();
357
358 $pageRendererProphecy = $this->prophesize(PageRenderer::class);
359 GeneralUtility::setSingletonInstance(PageRenderer::class, $pageRendererProphecy->reveal());
360
361 PageGenerator::renderContentWithHeader('');
362
363 $pageRendererProphecy->setMetaTag($expectedTags[0]['type'], $expectedTags[0]['name'], $expectedTags[0]['content'], [], false)->shouldHaveBeenCalled();
364 $pageRendererProphecy->setMetaTag($expectedTags[1]['type'], $expectedTags[1]['name'], $expectedTags[1]['content'], [], false)->shouldHaveBeenCalled();
365 }
366 }