[!!!][TASK] Remove TYPO3.jQuery
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Page / PageRendererTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\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 * Test case
21 */
22 class PageRendererTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
23 {
24 /**
25 * @test
26 */
27 public function pageRendererRendersInsertsMainContentStringsInOutput()
28 {
29 $subject = new PageRenderer();
30 $subject->setCharSet('utf-8');
31 $subject->setLanguage('default');
32
33 $prologueString = $expectedPrologueString = '<?xml version="1.0" encoding="utf-8" ?>';
34 $subject->setXmlPrologAndDocType($prologueString);
35
36 $title = $this->getUniqueId('aTitle-');
37 $subject->setTitle($title);
38 $expectedTitleString = '<title>' . $title . '</title>';
39
40 $charset = 'utf-8';
41 $subject->setCharSet($charset);
42 $expectedCharsetString = '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
43
44 $favouriteIcon = 'http://google.com/favicon.ico';
45 $subject->setFavIcon($favouriteIcon);
46 $expectedFavouriteIconPartOne = '<link rel="shortcut icon" href="' . $favouriteIcon . '" />';
47
48 $baseUrl = 'http://google.com/';
49 $subject->setBaseUrl($baseUrl);
50 $expectedBaseUrlString = '<base href="' . $baseUrl . '" />';
51
52 $metaTag = $expectedMetaTagString = '<meta name="author" content="Anna Lyse">';
53 $subject->addMetaTag($metaTag);
54
55 $inlineComment = $this->getUniqueId('comment');
56 $subject->addInlineComment($inlineComment);
57 $expectedInlineCommentString = '<!-- ' . LF . $inlineComment . '-->';
58
59 $headerData = $expectedHeaderData = '<tag method="private" name="test" />';
60 $subject->addHeaderData($headerData);
61
62 $subject->addJsLibrary('test', 'fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
63 $expectedJsLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
64
65 $subject->addJsFile('fileadmin/test.js', 'text/javascript', false, false, 'wrapBeforeXwrapAfter', false, 'X');
66 $expectedJsFileRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
67
68 $jsInlineCode = $expectedJsInlineCodeString = 'var x = "' . $this->getUniqueId('jsInline-') . '"';
69 $subject->addJsInlineCode($this->getUniqueId(), $jsInlineCode);
70
71 $extOnReadyCode = $expectedExtOnReadyCodePartOne = $this->getUniqueId('extOnReady-');
72 $expectedExtOnReadyCodePartTwo = 'Ext.onReady(function() {';
73 $subject->loadExtJS();
74 $subject->addExtOnReadyCode($extOnReadyCode);
75
76 $cssFile = $this->getUniqueId('cssFile-');
77 $expectedCssFileString = 'wrapBefore<link rel="stylesheet" type="text/css" href="' . $cssFile . '" media="print" />wrapAfter';
78 $subject->addCssFile($cssFile, 'stylesheet', 'print', '', true, false, 'wrapBeforeXwrapAfter', false, 'X');
79
80 $expectedCssInlineBlockOnTopString = '/*general3*/' . LF . 'h1 {margin:20px;}' . LF . '/*general2*/' . LF . 'body {margin:20px;}';
81 $subject->addCssInlineBlock('general2', 'body {margin:20px;}');
82 $subject->addCssInlineBlock('general3', 'h1 {margin:20px;}', null, true);
83
84 $subject->loadJquery();
85 $expectedJqueryRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
86
87 $expectedBodyContent = $this->getUniqueId('ABCDE-');
88 $subject->setBodyContent($expectedBodyContent);
89
90 $renderedString = $subject->render();
91
92 $this->assertContains($expectedPrologueString, $renderedString);
93 $this->assertContains($expectedTitleString, $renderedString);
94 $this->assertContains($expectedCharsetString, $renderedString);
95 $this->assertContains($expectedFavouriteIconPartOne, $renderedString);
96 $this->assertContains($expectedBaseUrlString, $renderedString);
97 $this->assertContains($expectedMetaTagString, $renderedString);
98 $this->assertContains($expectedInlineCommentString, $renderedString);
99 $this->assertContains($expectedHeaderData, $renderedString);
100 $this->assertRegExp($expectedJsLibraryRegExp, $renderedString);
101 $this->assertRegExp($expectedJsFileRegExp, $renderedString);
102 $this->assertContains($expectedJsInlineCodeString, $renderedString);
103 $this->assertContains($expectedExtOnReadyCodePartOne, $renderedString);
104 $this->assertContains($expectedExtOnReadyCodePartTwo, $renderedString);
105 $this->assertContains($expectedCssFileString, $renderedString);
106 $this->assertContains($expectedCssInlineBlockOnTopString, $renderedString);
107 $this->assertRegExp($expectedJqueryRegExp, $renderedString);
108 $this->assertContains($expectedBodyContent, $renderedString);
109 }
110
111 /**
112 * @test
113 */
114 public function pageRendererRendersFooterValues()
115 {
116 $subject = new 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/Resources/Private/Language/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(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
173 /**
174 * @return array
175 */
176 public function jqueryNamespaceDataProvider()
177 {
178 return [
179 'Custom namespace falls back to "no conflict"' => [
180 'namespace' => 'MyNameSpace',
181 'matchNoConflict' => true,
182 ],
183 '"None" namespace is respected' => [
184 'namespace' => PageRenderer::JQUERY_NAMESPACE_NONE,
185 'matchNoConflict' => false,
186 ],
187 ];
188 }
189
190 /**
191 * @param string $namespace
192 * @param bool $matchNoConflict
193 * @test
194 * @dataProvider jqueryNamespaceDataProvider
195 */
196 public function loadJqueryRespectsNamespace($namespace, $matchNoConflict)
197 {
198 $subject = new PageRenderer();
199
200 $expectedRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
201 $noConflictStatement = 'jQuery.noConflict();';
202 $subject->loadJquery(null, null, $namespace);
203
204 //jQuery.noConflict();
205 $out = $subject->render();
206 $this->assertRegExp($expectedRegExp, $out);
207
208 if ($matchNoConflict) {
209 $this->assertContains($noConflictStatement, $out);
210 } else {
211 $this->assertNotContains($noConflictStatement, $out);
212 }
213 }
214
215 /**
216 * @test
217 */
218 public function loadJqueryWithDefaultNoConflictModeDoesNotSetNamespace()
219 {
220 $subject = new PageRenderer();
221
222 $expectedRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
223 $expectedStatement = 'jQuery.noConflict();';
224 $subject->loadJquery(null, null, 'MyNameSpace');
225 $out = $subject->render();
226 $this->assertRegExp($expectedRegExp, $out);
227 $this->assertContains($expectedStatement, $out);
228 $this->assertNotContains('var TYPO3 = TYPO3 || {}; TYPO3.', $out);
229 }
230
231 /**
232 * @test
233 */
234 public function loadJqueryWithNamespaceNoneDoesNotIncludeNoConflictHandling()
235 {
236 $subject = new PageRenderer();
237
238 $expectedRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
239 $subject->loadJquery(null, null, PageRenderer::JQUERY_NAMESPACE_NONE);
240 $out = $subject->render();
241 $this->assertRegExp($expectedRegExp, $out);
242 $this->assertNotContains('jQuery.noConflict', $out);
243 }
244
245 /**
246 * @test
247 */
248 public function loadJqueryLoadsTheLatestJqueryVersionInNoConflictModeUncompressedInDebugMode()
249 {
250 $subject = new PageRenderer();
251
252 $expectedRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/jquery/jquery-' . PageRenderer::JQUERY_VERSION_LATEST . '\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
253 $expectedStatement = 'jQuery.noConflict();';
254 $subject->loadJquery(null, null, 'MyNameSpace');
255 $subject->enableDebugMode();
256 $out = $subject->render();
257 $this->assertRegExp($expectedRegExp, $out);
258 $this->assertContains($expectedStatement, $out);
259 }
260
261 /**
262 * @return array
263 */
264 public function loadJqueryFromSourceDataProvider()
265 {
266 return [
267 'google with version number' => [
268 '1.6.3',
269 'google',
270 '#<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.js" type="text/javascript"></script>#'
271 ],
272 'msn with version number' => [
273 '1.6.3',
274 'msn',
275 '#<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.3.js" type="text/javascript"></script>#'
276 ],
277 'jquery with version number' => [
278 '1.6.3',
279 'jquery',
280 '#<script src="https://code.jquery.com/jquery-1.6.3.js" type="text/javascript"></script>#'
281 ],
282 'jquery with custom URL' => [
283 '1.6.3',
284 'https://my.cool.cdn/foo/jquery.js',
285 '#<script src="https://my.cool.cdn/foo/jquery.js" type="text/javascript"></script>#'
286 ],
287 ];
288 }
289
290 /**
291 * @param string $version
292 * @param string $source
293 * @param string $regex
294 * @dataProvider loadJqueryFromSourceDataProvider
295 * @test
296 */
297 public function isJqueryLoadedFromSourceUncompressedIfDebugModeIsEnabled($version, $source, $regex)
298 {
299 $subject = new PageRenderer();
300
301 $subject->loadJquery($version, $source);
302 $subject->enableDebugMode();
303 $out = $subject->render();
304 $this->assertRegExp($regex, $out);
305 }
306
307 /**
308 * @test
309 */
310 public function isJqueryLoadedMinifiedFromGoogleByDefault()
311 {
312 $subject = new PageRenderer();
313
314 $expectedRegex = '#<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script>#';
315 $subject->loadJquery('1.6.3', 'google');
316 $out = $subject->render();
317 $this->assertRegExp($expectedRegex, $out);
318 }
319
320 /**
321 * @test
322 */
323 public function loadExtJsInDebugLoadsDebugExtJs()
324 {
325 $subject = new PageRenderer();
326
327 $expectedRegExp = '#<script src="typo3/sysext/core/Resources/Public/JavaScript/Contrib/extjs/ext-all-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
328 $subject->loadExtJS(true, true);
329 $subject->enableExtJsDebug();
330 $out = $subject->render();
331 $this->assertRegExp($expectedRegExp, $out);
332 }
333 }