TYPO3 CMS  TYPO3_7-6
PageRendererTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
26  {
27  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
28  $subject->setCharSet('utf-8');
29  $subject->setLanguage('default');
30 
31  $prologueString = $expectedPrologueString = '<?xml version="1.0" encoding="utf-8" ?>';
32  $subject->setXmlPrologAndDocType($prologueString);
33 
34  $title = $this->getUniqueId('aTitle-');
35  $subject->setTitle($title);
36  $expectedTitleString = '<title>' . $title . '</title>';
37 
38  $charset = 'utf-8';
39  $subject->setCharSet($charset);
40  $expectedCharsetString = '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
41 
42  $favouriteIcon = 'http://google.com/favicon.ico';
43  $subject->setFavIcon($favouriteIcon);
44  $expectedFavouriteIconPartOne = '<link rel="shortcut icon" href="' . $favouriteIcon . '" />';
45 
46  $baseUrl = 'http://google.com/';
47  $subject->setBaseUrl($baseUrl);
48  $expectedBaseUrlString = '<base href="' . $baseUrl . '" />';
49 
50  $metaTag = $expectedMetaTagString = '<meta name="author" content="Anna Lyse">';
51  $subject->addMetaTag($metaTag);
52 
53  $inlineComment = $this->getUniqueId('comment');
54  $subject->addInlineComment($inlineComment);
55  $expectedInlineCommentString = '<!-- ' . LF . $inlineComment . '-->';
56 
57  $headerData = $expectedHeaderData = '<tag method="private" name="test" />';
58  $subject->addHeaderData($headerData);
59 
60  $subject->addJsLibrary('test', 'fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
61  $expectedJsLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
62 
63  $subject->addJsFile('fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
64  $expectedJsFileRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
65 
66  $jsInlineCode = $expectedJsInlineCodeString = 'var x = "' . $this->getUniqueId('jsInline-') . '"';
67  $subject->addJsInlineCode($this->getUniqueId(), $jsInlineCode);
68 
69  $extOnReadyCode = $expectedExtOnReadyCodePartOne = $this->getUniqueId('extOnReady-');
70  $expectedExtOnReadyCodePartTwo = 'Ext.onReady(function() {';
71  $subject->loadExtJS();
72  $subject->addExtOnReadyCode($extOnReadyCode);
73 
74  $cssFile = $this->getUniqueId('cssFile-');
75  $expectedCssFileString = 'wrapBefore<link rel="stylesheet" type="text/css" href="' . $cssFile . '" media="print" />wrapAfter';
76  $subject->addCssFile($cssFile, 'stylesheet', 'print', '', true, false, 'wrapBeforeXwrapAfter', false, 'X');
77 
78  $expectedCssInlineBlockOnTopString = '/*general3*/' . LF . 'h1 {margin:20px;}' . LF . '/*general2*/' . LF . 'body {margin:20px;}';
79  $subject->addCssInlineBlock('general2', 'body {margin:20px;}');
80  $subject->addCssInlineBlock('general3', 'h1 {margin:20px;}', null, true);
81 
82  $subject->loadJquery();
83  $expectedJqueryRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
84  $expectedJqueryStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
85 
86  $expectedBodyContent = $this->getUniqueId('ABCDE-');
87  $subject->setBodyContent($expectedBodyContent);
88 
89  $renderedString = $subject->render();
90 
91  $this->assertContains($expectedPrologueString, $renderedString);
92  $this->assertContains($expectedTitleString, $renderedString);
93  $this->assertContains($expectedCharsetString, $renderedString);
94  $this->assertContains($expectedFavouriteIconPartOne, $renderedString);
95  $this->assertContains($expectedBaseUrlString, $renderedString);
96  $this->assertContains($expectedMetaTagString, $renderedString);
97  $this->assertContains($expectedInlineCommentString, $renderedString);
98  $this->assertContains($expectedHeaderData, $renderedString);
99  $this->assertRegExp($expectedJsLibraryRegExp, $renderedString);
100  $this->assertRegExp($expectedJsFileRegExp, $renderedString);
101  $this->assertContains($expectedJsInlineCodeString, $renderedString);
102  $this->assertContains($expectedExtOnReadyCodePartOne, $renderedString);
103  $this->assertContains($expectedExtOnReadyCodePartTwo, $renderedString);
104  $this->assertContains($expectedCssFileString, $renderedString);
105  $this->assertContains($expectedCssInlineBlockOnTopString, $renderedString);
106  $this->assertRegExp($expectedJqueryRegExp, $renderedString);
107  $this->assertContains($expectedJqueryStatement, $renderedString);
108  $this->assertContains($expectedBodyContent, $renderedString);
109  }
110 
115  {
116  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
117  $subject->setCharSet('utf-8');
118  $subject->setLanguage('default');
119 
120  $subject->enableMoveJsFromHeaderToFooter();
121 
122  $footerData = $expectedFooterData = '<tag method="private" name="test" />';
123  $subject->addFooterData($footerData);
124 
125  $expectedJsFooterLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
126  $subject->addJsFooterLibrary('test', 'fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
127 
128  $expectedJsFooterRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
129  $subject->addJsFooterFile('fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
130 
131  $jsFooterInlineCode = $expectedJsFooterInlineCodeString = 'var x = "' . $this->getUniqueId('jsFooterInline-') . '"';
132  $subject->addJsFooterInlineCode($this->getUniqueId(), $jsFooterInlineCode);
133 
134  // Bunch of label tests
135  $subject->loadExtJS();
136  $subject->addInlineLanguageLabel('myKey', 'myValue');
137  $subject->addInlineLanguageLabelArray([
138  'myKeyArray1' => 'myValueArray1',
139  'myKeyArray2' => 'myValueArray2'
140  ]);
141  $subject->addInlineLanguageLabelArray([
142  'myKeyArray3' => 'myValueArray3'
143  ]);
144  $expectedInlineLabelReturnValue = 'TYPO3.lang = {"myKey":"myValue","myKeyArray1":"myValueArray1","myKeyArray2":"myValueArray2","myKeyArray3":"myValueArray3"';
145 
146  $subject->addInlineLanguageLabelFile('EXT:lang/locallang_core.xlf');
147  $expectedLanguageLabel1 = 'labels.beUser';
148  $expectedLanguageLabel2 = 'labels.feUser';
149 
150  // Bunch of inline settings test
151  $subject->addInlineSetting('myApp', 'myKey', 'myValue');
152  $subject->addInlineSettingArray('myApp', [
153  'myKey1' => 'myValue1',
154  'myKey2' => 'myValue2'
155  ]);
156  $subject->addInlineSettingArray('myApp', [
157  'myKey3' => 'myValue3'
158  ]);
159  $expectedInlineSettingsReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue","myKey1":"myValue1","myKey2":"myValue2","myKey3":"myValue3"}';
160 
161  $renderedString = $subject->render(\TYPO3\CMS\Core\Page\PageRenderer::PART_FOOTER);
162 
163  $this->assertContains($expectedFooterData, $renderedString);
164  $this->assertRegExp($expectedJsFooterLibraryRegExp, $renderedString);
165  $this->assertRegExp($expectedJsFooterRegExp, $renderedString);
166  $this->assertContains($expectedJsFooterInlineCodeString, $renderedString);
167  $this->assertContains($expectedInlineLabelReturnValue, $renderedString);
168  $this->assertContains($expectedLanguageLabel1, $renderedString);
169  $this->assertContains($expectedLanguageLabel2, $renderedString);
170  $this->assertContains($expectedInlineSettingsReturnValue, $renderedString);
171  }
172 
177  {
178  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
179 
180  $expectedRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
181  $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.MyNameSpace = jQuery.noConflict(true);';
182  $subject->loadJquery(null, null, 'MyNameSpace');
183  $out = $subject->render();
184  $this->assertRegExp($expectedRegExp, $out);
185  $this->assertContains($expectedStatement, $out);
186  }
187 
192  {
193  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
194 
195  $expectedRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
196  $expectedStatement = 'jQuery.noConflict();';
197  $subject->loadJquery(null, null, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_DEFAULT_NOCONFLICT);
198  $out = $subject->render();
199  $this->assertRegExp($expectedRegExp, $out);
200  $this->assertContains($expectedStatement, $out);
201  $this->assertNotContains('var TYPO3 = TYPO3 || {}; TYPO3.', $out);
202  }
203 
208  {
209  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
210 
211  $expectedRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
212  $subject->loadJquery(null, null, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_NONE);
213  $out = $subject->render();
214  $this->assertRegExp($expectedRegExp, $out);
215  $this->assertNotContains('jQuery.noConflict', $out);
216  }
217 
222  {
223  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
224 
225  $expectedRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
226  $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
227  $subject->loadJquery();
228  $subject->enableDebugMode();
229  $out = $subject->render();
230  $this->assertRegExp($expectedRegExp, $out);
231  $this->assertContains($expectedStatement, $out);
232  }
233 
238  {
239  return [
240  'google with version number' => [
241  '1.6.3',
242  'google',
243  '#<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.js" type="text/javascript"></script>#'
244  ],
245  'msn with version number' => [
246  '1.6.3',
247  'msn',
248  '#<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.3.js" type="text/javascript"></script>#'
249  ],
250  'jquery with version number' => [
251  '1.6.3',
252  'jquery',
253  '#<script src="https://code.jquery.com/jquery-1.6.3.js" type="text/javascript"></script>#'
254  ],
255  'jquery with custom URL' => [
256  '1.6.3',
257  'https://my.cool.cdn/foo/jquery.js',
258  '#<script src="https://my.cool.cdn/foo/jquery.js" type="text/javascript"></script>#'
259  ],
260  ];
261  }
262 
267  public function isJqueryLoadedFromSourceUncompressedIfDebugModeIsEnabled($version, $source, $regex)
268  {
269  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
270 
271  $subject->loadJquery($version, $source);
272  $subject->enableDebugMode();
273  $out = $subject->render();
274  $this->assertRegExp($regex, $out);
275  }
276 
281  {
282  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
283 
284  $expectedRegex = '#<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script>#';
285  $subject->loadJquery('1.6.3', 'google');
286  $out = $subject->render();
287  $this->assertRegExp($expectedRegex, $out);
288  }
289 
294  {
295  $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
296 
297  $expectedRegExp = '#<script src="sysext/core/Resources/Public/JavaScript/Contrib/extjs/ext-all-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
298  $subject->loadExtJS(true, true);
299  $subject->enableExtJsDebug();
300  $out = $subject->render();
301  $this->assertRegExp($expectedRegExp, $out);
302  }
303 }
isJqueryLoadedFromSourceUncompressedIfDebugModeIsEnabled($version, $source, $regex)