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