‪TYPO3CMS  10.4
PageRendererTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\NullLogger;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
29 class ‪PageRendererTest extends FunctionalTestCase
30 {
35  {
36  $subject = new ‪PageRenderer();
37  $subject->setCharSet('utf-8');
38  $subject->setLanguage('default');
39 
40  $prologueString = $expectedPrologueString = '<?xml version="1.0" encoding="utf-8" ?>';
41  $subject->setXmlPrologAndDocType($prologueString);
42 
43  $title = ‪StringUtility::getUniqueId('aTitle-');
44  $subject->setTitle($title);
45  $expectedTitleString = '<title>' . $title . '</title>';
46 
47  $charset = 'utf-8';
48  $subject->setCharSet($charset);
49  $expectedCharsetString = '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
50 
51  $favouriteIcon = 'http://google.com/favicon.ico';
52  $subject->setFavIcon($favouriteIcon);
53  $expectedFavouriteIconPartOne = '<link rel="shortcut icon" href="' . $favouriteIcon . '" />';
54 
55  $baseUrl = 'http://google.com/';
56  $subject->setBaseUrl($baseUrl);
57  $expectedBaseUrlString = '<base href="' . $baseUrl . '" />';
58 
59  $subject->setMetaTag('property', 'og:type', 'foobar');
60  $subject->setMetaTag('name', 'author', 'husel');
61  $subject->setMetaTag('name', 'author', 'foobar');
62  $subject->setMetaTag('http-equiv', 'refresh', '5');
63  $subject->setMetaTag('name', 'DC.Author', '<evil tag>');
64  $subject->setMetaTag('property', 'og:image', '/path/to/image1.jpg', [], false);
65  $subject->setMetaTag('property', 'og:image', '/path/to/image2.jpg', [], false);
66 
67  // Unset meta tag
68  $subject->setMetaTag('NaMe', 'randomTag', 'foobar');
69  $subject->removeMetaTag('name', 'RanDoMtAg');
70 
71  $inlineComment = ‪StringUtility::getUniqueId('comment');
72  $subject->addInlineComment($inlineComment);
73  $expectedInlineCommentString = '<!-- ' . LF . $inlineComment . '-->';
74 
75  $headerData = $expectedHeaderData = '<tag method="private" name="test" />';
76  $subject->addHeaderData($headerData);
77 
78  $subject->addJsLibrary(
79  'test',
80  '/fileadmin/test.js',
81  'text/javascript',
82  false,
83  false,
84  'wrapBeforeXwrapAfter',
85  false,
86  'X'
87  );
88  $expectedJsLibraryRegExp = '#wrapBefore<script src="/fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
89 
90  $subject->addJsFile('/fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
91  $expectedJsFileRegExp = '#wrapBefore<script src="/fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
92 
93  $subject->addJsFile('/fileadmin/test-plain.js', '', false, false, 'wrapBeforeXwrapAfter', false, 'X');
94  $expectedJsFileWithoutTypeRegExp = '#wrapBefore<script src="/fileadmin/test-plain\\.(js|\\d+\\.js|js\\?\\d+)"></script>wrapAfter#';
95 
96  $jsInlineCode = $expectedJsInlineCodeString = 'var x = "' . ‪StringUtility::getUniqueId('jsInline-') . '"';
97  $subject->addJsInlineCode(‪StringUtility::getUniqueId(), $jsInlineCode);
98 
99  $cssFile = ‪StringUtility::getUniqueId('/cssFile-');
100  $expectedCssFileString = 'wrapBefore<link rel="stylesheet" type="text/css" href="' . $cssFile . '" media="print" />wrapAfter';
101  $subject->addCssFile($cssFile, 'stylesheet', 'print', '', true, false, 'wrapBeforeXwrapAfter', false, 'X');
102 
103  $expectedCssInlineBlockOnTopString = '/*general3*/' . LF . 'h1 {margin:20px;}' . LF . '/*general2*/' . LF . 'body {margin:20px;}';
104  $subject->addCssInlineBlock('general2', 'body {margin:20px;}');
105  $subject->addCssInlineBlock('general3', 'h1 {margin:20px;}', null, true);
106 
107  $expectedBodyContent = ‪StringUtility::getUniqueId('ABCDE-');
108  $subject->setBodyContent($expectedBodyContent);
109 
110  $renderedString = $subject->render();
111 
112  self::assertStringContainsString($expectedPrologueString, $renderedString);
113  self::assertStringContainsString($expectedTitleString, $renderedString);
114  self::assertStringContainsString($expectedCharsetString, $renderedString);
115  self::assertStringContainsString($expectedFavouriteIconPartOne, $renderedString);
116  self::assertStringContainsString($expectedBaseUrlString, $renderedString);
117  self::assertStringContainsString($expectedInlineCommentString, $renderedString);
118  self::assertStringContainsString($expectedHeaderData, $renderedString);
119  self::assertRegExp($expectedJsLibraryRegExp, $renderedString);
120  self::assertRegExp($expectedJsFileRegExp, $renderedString);
121  self::assertRegExp($expectedJsFileWithoutTypeRegExp, $renderedString);
122  self::assertStringContainsString($expectedJsInlineCodeString, $renderedString);
123  self::assertStringContainsString($expectedCssFileString, $renderedString);
124  self::assertStringContainsString($expectedCssInlineBlockOnTopString, $renderedString);
125  self::assertStringContainsString($expectedBodyContent, $renderedString);
126  self::assertStringContainsString('<meta property="og:type" content="foobar" />', $renderedString);
127  self::assertStringContainsString('<meta name="author" content="foobar" />', $renderedString);
128  self::assertStringContainsString('<meta http-equiv="refresh" content="5" />', $renderedString);
129  self::assertStringContainsString('<meta name="dc.author" content="&lt;evil tag&gt;" />', $renderedString);
130  self::assertStringNotContainsString('<meta name="randomtag" content="foobar">', $renderedString);
131  self::assertStringNotContainsString('<meta name="randomtag" content="foobar" />', $renderedString);
132  self::assertStringContainsString('<meta name="generator" content="TYPO3 CMS" />', $renderedString);
133  self::assertStringContainsString('<meta property="og:image" content="/path/to/image1.jpg" />', $renderedString);
134  self::assertStringContainsString('<meta property="og:image" content="/path/to/image2.jpg" />', $renderedString);
135  }
136 
141  {
142  $subject = new ‪PageRenderer();
143  $subject->setCharSet('utf-8');
144  $subject->setLanguage('default');
145 
146  $subject->enableMoveJsFromHeaderToFooter();
147 
148  $footerData = $expectedFooterData = '<tag method="private" name="test" />';
149  $subject->addFooterData($footerData);
150 
151  $expectedJsFooterLibraryRegExp = '#wrapBefore<script src="/fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
152  $subject->addJsFooterLibrary(
153  'test',
154  '/fileadmin/test.js',
155  'text/javascript',
156  false,
157  false,
158  'wrapBeforeXwrapAfter',
159  false,
160  'X'
161  );
162 
163  $expectedJsFooterRegExp = '#wrapBefore<script src="/fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
164  $subject->addJsFooterFile(
165  '/fileadmin/test.js',
166  'text/javascript',
167  false,
168  false,
169  'wrapBeforeXwrapAfter',
170  false,
171  'X'
172  );
173 
174  $jsFooterInlineCode = $expectedJsFooterInlineCodeString = 'var x = "' . ‪StringUtility::getUniqueId('jsFooterInline-') . '"';
175  $subject->addJsFooterInlineCode(‪StringUtility::getUniqueId(), $jsFooterInlineCode);
176 
177  // Bunch of label tests
178  $subject->addInlineLanguageLabel('myKey', 'myValue');
179  $subject->addInlineLanguageLabelArray([
180  'myKeyArray1' => 'myValueArray1',
181  'myKeyArray2' => 'myValueArray2'
182  ]);
183  $subject->addInlineLanguageLabelArray([
184  'myKeyArray3' => 'myValueArray3'
185  ]);
186  $expectedInlineLabelReturnValue = 'TYPO3.lang = {"myKey":"myValue","myKeyArray1":"myValueArray1","myKeyArray2":"myValueArray2","myKeyArray3":"myValueArray3"';
187 
188  $subject->addInlineLanguageLabelFile('EXT:core/Resources/Private/Language/locallang_core.xlf');
189  $expectedLanguageLabel1 = 'labels.beUser';
190  $expectedLanguageLabel2 = 'labels.feUser';
191 
192  // Bunch of inline settings test
193  $subject->addInlineSetting('myApp', 'myKey', 'myValue');
194  $subject->addInlineSettingArray('myApp', [
195  'myKey1' => 'myValue1',
196  'myKey2' => 'myValue2'
197  ]);
198  $subject->addInlineSettingArray('myApp', [
199  'myKey3' => 'myValue3'
200  ]);
201  $expectedInlineSettingsReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue","myKey1":"myValue1","myKey2":"myValue2","myKey3":"myValue3"}';
202 
203  $renderedString = $subject->render(‪PageRenderer::PART_FOOTER);
204 
205  self::assertStringContainsString($expectedFooterData, $renderedString);
206  self::assertRegExp($expectedJsFooterLibraryRegExp, $renderedString);
207  self::assertRegExp($expectedJsFooterRegExp, $renderedString);
208  self::assertStringContainsString($expectedJsFooterInlineCodeString, $renderedString);
209  self::assertStringContainsString($expectedInlineLabelReturnValue, $renderedString);
210  self::assertStringContainsString($expectedLanguageLabel1, $renderedString);
211  self::assertStringContainsString($expectedLanguageLabel2, $renderedString);
212  self::assertStringContainsString($expectedInlineSettingsReturnValue, $renderedString);
213  }
214 
219  {
220  $subject = new ‪PageRenderer();
221  $subject->setCharSet('utf-8');
222  $subject->setLanguage('default');
223 
224  $subject->addJsFooterLibrary(
225  'test',
226  '/fileadmin/test.js',
227  'text/javascript',
228  false,
229  false,
230  '',
231  false,
232  '|',
233  false,
234  '',
235  false,
236  '',
237  true
238  );
239  $expectedJsFooterLibrary = '<script src="/fileadmin/test.js" type="text/javascript" nomodule="nomodule"></script>';
240 
241  $subject->addJsLibrary(
242  'test2',
243  '/fileadmin/test2.js',
244  'text/javascript',
245  false,
246  false,
247  '',
248  false,
249  '|',
250  false,
251  '',
252  false,
253  '',
254  true
255  );
256  $expectedJsLibrary = '<script src="/fileadmin/test2.js" type="text/javascript" nomodule="nomodule"></script>';
257 
258  $subject->addJsFile(
259  '/fileadmin/test3.js',
260  'text/javascript',
261  false,
262  false,
263  '',
264  false,
265  '|',
266  false,
267  '',
268  false,
269  '',
270  true
271  );
272  $expectedJsFile = '<script src="/fileadmin/test3.js" type="text/javascript" nomodule="nomodule"></script>';
273 
274  $subject->addJsFooterFile(
275  '/fileadmin/test4.js',
276  'text/javascript',
277  false,
278  false,
279  '',
280  false,
281  '|',
282  false,
283  '',
284  false,
285  '',
286  true
287  );
288  $expectedJsFooter = '<script src="/fileadmin/test4.js" type="text/javascript" nomodule="nomodule"></script>';
289 
290  $renderedString = $subject->render();
291 
292  self::assertStringContainsString($expectedJsFooterLibrary, $renderedString);
293  self::assertStringContainsString($expectedJsLibrary, $renderedString);
294  self::assertStringContainsString($expectedJsFile, $renderedString);
295  self::assertStringContainsString($expectedJsFooter, $renderedString);
296  }
297 
302  {
303  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['session']['BE'] = [
304  'backend' => DatabaseSessionBackend::class,
305  'options' => [
306  'table' => 'be_sessions',
307  ],
308  ];
309  ‪$GLOBALS['BE_USER'] = new ‪BackendUserAuthentication();
310  ‪$GLOBALS['BE_USER']->id = md5('abc');
311  ‪$GLOBALS['BE_USER']->user = ['uid' => 1];
312  ‪$GLOBALS['BE_USER']->setLogger(new NullLogger());
313 
315 
316  $subject = new ‪PageRenderer();
317  $subject->setCharSet('utf-8');
318  $subject->setLanguage('default');
319 
320  $packages = [
321  [
322  'name' => 'foo',
323  'location' => '/typo3conf/ext/foo/Resources/Public/JavaScript/Contrib/foo',
324  'main' => 'lib/foo'
325  ],
326  [
327  'name' => 'bar',
328  'location' => '/typo3conf/ext/bar/Resources/Public/JavaScript/Contrib/bar',
329  'main' => 'lib/bar'
330  ]
331  ];
332 
333  foreach ($packages as $package) {
334  $subject->addRequireJsConfiguration([
335  'packages' => [$package]
336  ]);
337  }
338 
339  $expectedConfiguration = json_encode(['packages' => $packages]);
340  // Remove surrounding brackets as the expectation is a substring of a larger JSON string
341  $expectedConfiguration = trim($expectedConfiguration, '{}');
342 
343  $renderedString = $subject->render();
344  self::assertStringContainsString($expectedConfiguration, $renderedString);
345  }
346 }
‪TYPO3\CMS\Core\Tests\Functional\Page\PageRendererTest\pageRendererRendersNomoduleJavascript
‪pageRendererRendersNomoduleJavascript()
Definition: PageRendererTest.php:218
‪TYPO3\CMS\Core\Tests\Functional\Page\PageRendererTest
Definition: PageRendererTest.php:30
‪TYPO3\CMS\Core\Localization\LanguageService\createFromUserPreferences
‪static createFromUserPreferences(?AbstractUserAuthentication $user)
Definition: LanguageService.php:435
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Core\Page\PageRenderer\PART_FOOTER
‪const PART_FOOTER
Definition: PageRenderer.php:46
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Tests\Functional\Page\PageRendererTest\pageRendererMergesRequireJsPackagesOnConsecutiveCalls
‪pageRendererMergesRequireJsPackagesOnConsecutiveCalls()
Definition: PageRendererTest.php:301
‪TYPO3\CMS\Core\Tests\Functional\Page\PageRendererTest\pageRendererRendersFooterValues
‪pageRendererRendersFooterValues()
Definition: PageRendererTest.php:140
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Session\Backend\DatabaseSessionBackend
Definition: DatabaseSessionBackend.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Functional\Page
Definition: PageRendererTest.php:16
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Functional\Page\PageRendererTest\pageRendererRendersInsertsMainContentStringsInOutput
‪pageRendererRendersInsertsMainContentStringsInOutput()
Definition: PageRendererTest.php:34