[TASK] Clean up MetaTagManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Page / PageRendererTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Page;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Page\PageRenderer;
18
19 /**
20 * Unit test case
21 *
22 * @see According functional test case
23 */
24 class PageRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @test
28 */
29 public function renderMethodCallsResetInAnyCase()
30 {
31 $pageRenderer = $this->getMockBuilder(\TYPO3\CMS\Core\Page\PageRenderer::class)
32 ->setMethods(['reset', 'prepareRendering', 'renderJavaScriptAndCss', 'getPreparedMarkerArray', 'getTemplateForPart'])
33 ->getMock();
34 $pageRenderer->expects($this->exactly(3))->method('reset');
35
36 $pageRenderer->render(PageRenderer::PART_COMPLETE);
37 $pageRenderer->render(PageRenderer::PART_HEADER);
38 $pageRenderer->render(PageRenderer::PART_FOOTER);
39 }
40
41 /**
42 * @test
43 */
44 public function includingNotAvailableLocalJqueryVersionThrowsException()
45 {
46 $this->expectException(\UnexpectedValueException::class);
47 $this->expectExceptionCode(1341505305);
48
49 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
50 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
51 $subject->_set('availableLocalJqueryVersions', ['1.1.1']);
52 $subject->loadJquery('2.2.2');
53 }
54
55 /**
56 * @test
57 */
58 public function includingJqueryWithNonAlphnumericNamespaceThrowsException()
59 {
60 $this->expectException(\UnexpectedValueException::class);
61 $this->expectExceptionCode(1341571604);
62
63 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
64 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
65 $subject->loadJquery(null, null, '12sd.12fsd');
66 $subject->render();
67 }
68
69 /**
70 * @test
71 */
72 public function addBodyContentAddsContent()
73 {
74 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
75 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
76 $expectedReturnValue = 'ABCDE';
77 $subject->addBodyContent('A');
78 $subject->addBodyContent('B');
79 $subject->addBodyContent('C');
80 $subject->addBodyContent('D');
81 $subject->addBodyContent('E');
82 $out = $subject->getBodyContent();
83 $this->assertEquals($expectedReturnValue, $out);
84 }
85
86 /**
87 * @test
88 */
89 public function addInlineLanguageLabelFileSetsInlineLanguageLabelFiles()
90 {
91 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
92 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
93 $fileReference = $this->getUniqueId('file_');
94 $selectionPrefix = $this->getUniqueId('prefix_');
95 $stripFromSelectionName = $this->getUniqueId('strip_');
96
97 $expectedInlineLanguageLabelFile = [
98 'fileRef' => $fileReference,
99 'selectionPrefix' => $selectionPrefix,
100 'stripFromSelectionName' => $stripFromSelectionName
101 ];
102
103 $subject->addInlineLanguageLabelFile($fileReference, $selectionPrefix, $stripFromSelectionName);
104 $actualResult = $subject->getInlineLanguageLabelFiles();
105
106 $this->assertSame($expectedInlineLanguageLabelFile, array_pop($actualResult));
107 }
108
109 /**
110 * @test
111 */
112 public function addInlineLanguageLabelFileSetsTwoDifferentInlineLanguageLabelFiles()
113 {
114 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
115 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
116 $fileReference1 = $this->getUniqueId('file1_');
117 $selectionPrefix1 = $this->getUniqueId('prefix1_');
118 $stripFromSelectionName1 = $this->getUniqueId('strip1_');
119 $expectedInlineLanguageLabelFile1 = [
120 'fileRef' => $fileReference1,
121 'selectionPrefix' => $selectionPrefix1,
122 'stripFromSelectionName' => $stripFromSelectionName1
123 ];
124 $fileReference2 = $this->getUniqueId('file2_');
125 $selectionPrefix2 = $this->getUniqueId('prefix2_');
126 $stripFromSelectionName2 = $this->getUniqueId('strip2_');
127 $expectedInlineLanguageLabelFile2 = [
128 'fileRef' => $fileReference2,
129 'selectionPrefix' => $selectionPrefix2,
130 'stripFromSelectionName' => $stripFromSelectionName2
131 ];
132
133 $subject->addInlineLanguageLabelFile($fileReference1, $selectionPrefix1, $stripFromSelectionName1);
134 $subject->addInlineLanguageLabelFile($fileReference2, $selectionPrefix2, $stripFromSelectionName2);
135 $actualResult = $subject->getInlineLanguageLabelFiles();
136
137 $this->assertSame($expectedInlineLanguageLabelFile2, array_pop($actualResult));
138 $this->assertSame($expectedInlineLanguageLabelFile1, array_pop($actualResult));
139 }
140
141 /**
142 * @test
143 */
144 public function addInlineLanguageLabelFileDoesNotSetSameLanguageFileTwice()
145 {
146 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
147 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
148 $fileReference = $this->getUniqueId('file2_');
149 $selectionPrefix = $this->getUniqueId('prefix2_');
150 $stripFromSelectionName = $this->getUniqueId('strip2_');
151
152 $subject->addInlineLanguageLabelFile($fileReference, $selectionPrefix, $stripFromSelectionName);
153 $subject->addInlineLanguageLabelFile($fileReference, $selectionPrefix, $stripFromSelectionName);
154 $this->assertSame(1, count($subject->getInlineLanguageLabelFiles()));
155 }
156
157 /**
158 * @test
159 */
160 public function includeLanguageFileForInlineThrowsExceptionIfLangIsNotSet()
161 {
162 $this->expectException(\RuntimeException::class);
163 $this->expectExceptionCode(1284906026);
164
165 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
166 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
167 $subject->_set('charSet', 'utf-8');
168 $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
169 }
170
171 /**
172 * @test
173 */
174 public function includeLanguageFileForInlineThrowsExceptionIfCharSetIsNotSet()
175 {
176 $this->expectException(\RuntimeException::class);
177 $this->expectExceptionCode(1284906026);
178
179 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
180 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
181 $subject->_set('lang', 'default');
182 $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
183 }
184
185 /**
186 * @test
187 */
188 public function includeLanguageFileForInlineDoesNotAddToInlineLanguageLabelsIfFileCouldNotBeRead()
189 {
190 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
191 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['readLLfile'], [], '', false);
192 $subject->_set('lang', 'default');
193 $subject->_set('charSet', 'utf-8');
194 $subject->_set('inlineLanguageLabels', []);
195 $subject->method('readLLfile')->willReturn(false);
196 $subject->_call('includeLanguageFileForInline', 'someLLFile.xml');
197 $this->assertEquals([], $subject->_get('inlineLanguageLabels'));
198 }
199
200 /**
201 * @return array
202 */
203 public function includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabelsProvider()
204 {
205 $llFileContent = [
206 'default' => [
207 'inline_label_first_Key' => 'first',
208 'inline_label_second_Key' => 'second',
209 'thirdKey' => 'third'
210 ]
211 ];
212 return [
213 'No processing' => [
214 $llFileContent,
215 '',
216 '',
217 $llFileContent['default']
218 ],
219 'Respect $selectionPrefix' => [
220 $llFileContent,
221 'inline_',
222 '',
223 [
224 'inline_label_first_Key' => 'first',
225 'inline_label_second_Key' => 'second'
226 ]
227 ],
228 'Respect $stripFromSelectionName' => [
229 $llFileContent,
230 '',
231 'inline_',
232 [
233 'label_first_Key' => 'first',
234 'label_second_Key' => 'second',
235 'thirdKey' => 'third'
236 ]
237 ],
238 'Respect $selectionPrefix and $stripFromSelectionName' => [
239 $llFileContent,
240 'inline_',
241 'inline_label_',
242 [
243 'first_Key' => 'first',
244 'second_Key' => 'second'
245 ]
246 ],
247 ];
248 }
249
250 /**
251 * @dataProvider includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabelsProvider
252 * @test
253 */
254 public function includeLanguageFileForInlineAddsProcessesLabelsToInlineLanguageLabels($llFileContent, $selectionPrefix, $stripFromSelectionName, $expectation)
255 {
256 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
257 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['readLLfile'], [], '', false);
258 $subject->_set('lang', 'default');
259 $subject->_set('charSet', 'utf-8');
260 $subject->_set('inlineLanguageLabels', []);
261 $subject->method('readLLfile')->willReturn($llFileContent);
262 $subject->_call('includeLanguageFileForInline', 'someLLFile.xml', $selectionPrefix, $stripFromSelectionName);
263 $this->assertEquals($expectation, $subject->_get('inlineLanguageLabels'));
264 }
265
266 /**
267 * @test
268 */
269 public function getAddedMetaTag()
270 {
271 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
272 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['whatDoesThisDo']);
273 $subject->setMetaTag('nAme', 'Author', 'foobar');
274 $actualResult = $subject->getMetaTag('naMe', 'AUTHOR');
275 $expectedResult = [
276 'type' => 'name',
277 'name' => 'author',
278 'content' => 'foobar'
279 ];
280 $this->assertSame($expectedResult, $actualResult);
281 }
282
283 /**
284 * @test
285 */
286 public function overrideMetaTag()
287 {
288 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
289 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['whatDoesThisDo']);
290 $subject->setMetaTag('nAme', 'Author', 'Axel Foley');
291 $subject->setMetaTag('nAme', 'Author', 'foobar');
292 $actualResult = $subject->getMetaTag('naMe', 'AUTHOR');
293 $expectedResult = [
294 'type' => 'name',
295 'name' => 'author',
296 'content' => 'foobar'
297 ];
298 $this->assertSame($expectedResult, $actualResult);
299 }
300
301 /**
302 * @test
303 */
304 public function unsetAddedMetaTag()
305 {
306 /** @var \TYPO3\CMS\Core\Page\PageRenderer|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
307 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['whatDoesThisDo']);
308 $subject->setMetaTag('nAme', 'Author', 'foobar');
309 $subject->removeMetaTag('naMe', 'AUTHOR');
310 $actualResult = $subject->getMetaTag('naMe', 'AUTHOR');
311 $expectedResult = [];
312 $this->assertSame($expectedResult, $actualResult);
313 }
314
315 /**
316 * @test
317 */
318 public function parseLanguageLabelsForJavaScriptReturnsEmptyStringIfEmpty()
319 {
320 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
321 $inlineLanguageLabels = [];
322 $subject->_set('inlineLanguageLabels', $inlineLanguageLabels);
323 $actual = $subject->_call('parseLanguageLabelsForJavaScript');
324 $this->assertEmpty($actual);
325 }
326
327 /**
328 * @test
329 */
330 public function parseLanguageLabelsForJavaScriptReturnsFlatArray()
331 {
332 $subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Page\PageRenderer::class, ['dummy'], [], '', false);
333 $inlineLanguageLabels = [
334 'key' => 'label',
335 'foo' => 'bar',
336 'husel' => [
337 [
338 'source' => 'pusel',
339 ]
340 ],
341 'hello' => [
342 [
343 'source' => 'world',
344 'target' => 'welt',
345 ]
346 ],
347 ];
348 $subject->_set('inlineLanguageLabels', $inlineLanguageLabels);
349 $expected = [
350 'key' => 'label',
351 'foo' => 'bar',
352 'husel' => 'pusel',
353 'hello' => 'welt',
354 ];
355 $actual = $subject->_call('parseLanguageLabelsForJavaScript');
356 $this->assertSame($expected, $actual);
357 }
358 }