[TASK] Followup to "Cleanup Ajax URL inline settings"
[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 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Test case
29 */
30 class PageRendererTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
31
32 /**
33 * @test
34 */
35 public function pageRendererRendersInsertsMainContentStringsInOutput() {
36 $subject = new \TYPO3\CMS\Core\Page\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 = uniqid('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 $expectedFavouriteIconPartTwo = '<link rel="icon" href="' . $favouriteIcon . '" />';
55
56 $baseUrl = 'http://google.com/';
57 $subject->setBaseUrl($baseUrl);
58 $expectedBaseUrlString = '<base href="' . $baseUrl . '" />';
59
60 $metaTag = $expectedMetaTagString = '<meta name="author" content="Anna Lyse">';
61 $subject->addMetaTag($metaTag);
62
63 $inlineComment = uniqid('comment');
64 $subject->addInlineComment($inlineComment);
65 $expectedInlineCommentString = '<!-- ' . LF . $inlineComment . '-->';
66
67 $headerData = $expectedHeaderData = '<tag method="private" name="test" />';
68 $subject->addHeaderData($headerData);
69
70 $subject->addJsLibrary('test', 'fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
71 $expectedJsLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
72
73 $subject->addJsFile('fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
74 $expectedJsFileRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
75
76 $jsInlineCode = $expectedJsInlineCodeString = 'var x = "' . uniqid('jsInline-') . '"';
77 $subject->addJsInlineCode(uniqid(), $jsInlineCode);
78
79 $extOnReadyCode = $expectedExtOnReadyCodePartOne = uniqid('extOnReady-');
80 $expectedExtOnReadyCodePartTwo = 'Ext.onReady(function() {';
81 $subject->loadExtJS();
82 $subject->addExtOnReadyCode($extOnReadyCode);
83
84 $cssFile = uniqid('cssFile-');
85 $expectedCssFileString = 'wrapBefore<link rel="stylesheet" type="text/css" href="' . $cssFile . '" media="print" />wrapAfter';
86 $subject->addCssFile($cssFile, 'stylesheet', 'print', '', TRUE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
87
88 $expectedCssInlineBlockOnTopString = '/*general3*/' . LF . 'h1 {margin:20px;}' . LF . '/*general2*/' . LF . 'body {margin:20px;}';
89 $subject->addCssInlineBlock('general2', 'body {margin:20px;}');
90 $subject->addCssInlineBlock('general3', 'h1 {margin:20px;}', NULL, TRUE);
91
92 $expectedLoadPrototypeRegExp = '#<script src="contrib/prototype/prototype\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
93 $subject->loadPrototype();
94
95 $subject->loadScriptaculous('slider,controls');
96 $expectedScriptaculousMain = '<script src="contrib/scriptaculous/scriptaculous.js" type="text/javascript"></script>';
97 $expectedScriptaculousEffects = '<script src="contrib/scriptaculous/effects.js" type="text/javascript"></script>';
98 $expectedScriptaculousControls = '<script src="contrib/scriptaculous/controls.js" type="text/javascript"></script>';
99 $expectedScriptaculousSlider = '<script src="contrib/scriptaculous/slider.js" type="text/javascript"></script>';
100
101 $subject->loadJquery();
102 $expectedJqueryRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
103 $expectedJqueryStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
104
105 $subject->loadExtJS(TRUE, TRUE, 'jquery');
106 $expectedExtJsRegExp = '#<script src="contrib/extjs/adapter/jquery/ext-jquery-adapter\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>' . LF . '<script src="contrib/extjs/ext-all\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#m';
107
108 $expectedBodyContent = uniqid('ABCDE-');
109 $subject->setBodyContent($expectedBodyContent);
110
111 $renderedString = $subject->render();
112
113 $this->assertContains($expectedPrologueString, $renderedString);
114 $this->assertContains($expectedTitleString, $renderedString);
115 $this->assertContains($expectedCharsetString, $renderedString);
116 $this->assertContains($expectedFavouriteIconPartOne, $renderedString);
117 $this->assertContains($expectedFavouriteIconPartTwo, $renderedString);
118 $this->assertContains($expectedBaseUrlString, $renderedString);
119 $this->assertContains($expectedMetaTagString, $renderedString);
120 $this->assertContains($expectedInlineCommentString, $renderedString);
121 $this->assertContains($expectedHeaderData, $renderedString);
122 $this->assertRegExp($expectedJsLibraryRegExp, $renderedString);
123 $this->assertRegExp($expectedJsFileRegExp, $renderedString);
124 $this->assertContains($expectedJsInlineCodeString, $renderedString);
125 $this->assertContains($expectedExtOnReadyCodePartOne, $renderedString);
126 $this->assertContains($expectedExtOnReadyCodePartTwo, $renderedString);
127 $this->assertContains($expectedCssFileString, $renderedString);
128 $this->assertContains($expectedCssInlineBlockOnTopString, $renderedString);
129 $this->assertRegExp($expectedLoadPrototypeRegExp, $renderedString);
130 $this->assertContains($expectedScriptaculousMain, $renderedString);
131 $this->assertContains($expectedScriptaculousEffects, $renderedString);
132 $this->assertContains($expectedScriptaculousControls, $renderedString);
133 $this->assertContains($expectedScriptaculousSlider, $renderedString);
134 $this->assertRegExp($expectedJqueryRegExp, $renderedString);
135 $this->assertContains($expectedJqueryStatement, $renderedString);
136 $this->assertRegExp($expectedExtJsRegExp, $renderedString);
137 $this->assertContains($expectedBodyContent, $renderedString);
138 }
139
140 /**
141 * @test
142 */
143 public function pageRendererRendersFooterValues() {
144 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
145 $subject->setCharSet('utf-8');
146 $subject->setLanguage('default');
147
148 $subject->enableMoveJsFromHeaderToFooter();
149
150 $footerData = $expectedFooterData = '<tag method="private" name="test" />';
151 $subject->addFooterData($footerData);
152
153 $expectedJsFooterLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
154 $subject->addJsFooterLibrary('test', 'fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
155
156 $expectedJsFooterRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
157 $subject->addJsFooterFile('fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
158
159 $jsFooterInlineCode = $expectedJsFooterInlineCodeString = 'var x = "' . uniqid('jsFooterInline-') . '"';
160 $subject->addJsFooterInlineCode(uniqid(), $jsFooterInlineCode);
161
162 // Bunch of label tests
163 $subject->loadExtJS();
164 $subject->addInlineLanguageLabel('myKey', 'myValue');
165 $subject->addInlineLanguageLabelArray(array(
166 'myKeyArray1' => 'myValueArray1',
167 'myKeyArray2' => 'myValueArray2'
168 ));
169 $subject->addInlineLanguageLabelArray(array(
170 'myKeyArray3' => 'myValueArray3'
171 ));
172 $expectedInlineLabelReturnValue = 'TYPO3.lang = {"myKey":"myValue","myKeyArray1":"myValueArray1","myKeyArray2":"myValueArray2","myKeyArray3":"myValueArray3"';
173
174 $subject->addInlineLanguageLabelFile('EXT:lang/locallang_core.xlf');
175 $expectedLanguageLabel1 = 'labels.beUser';
176 $expectedLanguageLabel2 = 'labels.feUser';
177
178 // Bunch of inline settings test
179 $subject->addInlineSetting('myApp', 'myKey', 'myValue');
180 $subject->addInlineSettingArray('myApp', array(
181 'myKey1' => 'myValue1',
182 'myKey2' => 'myValue2'
183 ));
184 $subject->addInlineSettingArray('myApp', array(
185 'myKey3' => 'myValue3'
186 ));
187 $expectedInlineSettingsReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue","myKey1":"myValue1","myKey2":"myValue2","myKey3":"myValue3"}';
188
189 $renderedString = $subject->render(\TYPO3\CMS\Core\Page\PageRenderer::PART_FOOTER);
190
191 $this->assertContains($expectedFooterData, $renderedString);
192 $this->assertRegExp($expectedJsFooterLibraryRegExp, $renderedString);
193 $this->assertRegExp($expectedJsFooterRegExp, $renderedString);
194 $this->assertContains($expectedJsFooterInlineCodeString, $renderedString);
195 $this->assertContains($expectedInlineLabelReturnValue, $renderedString);
196 $this->assertContains($expectedLanguageLabel1, $renderedString);
197 $this->assertContains($expectedLanguageLabel2, $renderedString);
198 $this->assertContains($expectedInlineSettingsReturnValue, $renderedString);
199 }
200
201 /**
202 * @test
203 */
204 public function isScriptaculousLoadedCompressedIfConfiguredAndClientIsCapable() {
205 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
206
207 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
208 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
209 $subject->loadScriptaculous('slider,controls');
210 $subject->enableCompressJavascript();
211 $out = $subject->render();
212 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
213 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
214 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
215 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
216 }
217
218 /**
219 * @test
220 */
221 public function isScriptaculousNotLoadedCompressedIfClientCannotHandleCompression() {
222 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
223
224 $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
225 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
226 $subject->loadScriptaculous('slider,controls');
227 $subject->enableCompressJavascript();
228 $out = $subject->render();
229 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
230 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
231 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
232 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
233 }
234
235 /**
236 * @test
237 */
238 public function isScriptaculousNotLoadedCompressedIfCompressionIsNotConfigured() {
239 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
240
241 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
242 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '';
243 $subject->loadScriptaculous('slider,controls');
244 $subject->enableCompressJavascript();
245 $out = $subject->render();
246 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
247 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
248 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
249 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
250 }
251
252 /**
253 * @test
254 */
255 public function loadJqueryRespectsGivenNamespace() {
256 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
257
258 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
259 $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.MyNameSpace = jQuery.noConflict(true);';
260 $subject->loadJquery(NULL, NULL, 'MyNameSpace');
261 $out = $subject->render();
262 $this->assertRegExp($expectedRegExp, $out);
263 $this->assertContains($expectedStatement, $out);
264 }
265
266 /**
267 * @test
268 */
269 public function loadJqueryWithDefaultNoConflictModeDoesNotSetNamespace() {
270 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
271
272 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
273 $expectedStatement = 'jQuery.noConflict();';
274 $subject->loadJquery(NULL, NULL, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_DEFAULT_NOCONFLICT);
275 $out = $subject->render();
276 $this->assertRegExp($expectedRegExp, $out);
277 $this->assertContains($expectedStatement, $out);
278 $this->assertNotContains('var TYPO3 = TYPO3 || {}; TYPO3.', $out);
279 }
280
281 /**
282 * @test
283 */
284 public function loadJqueryWithNamespaceNoneDoesNotIncludeNoConflictHandling() {
285 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
286
287 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
288 $subject->loadJquery(NULL, NULL, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_NONE);
289 $out = $subject->render();
290 $this->assertRegExp($expectedRegExp, $out);
291 $this->assertNotContains('jQuery.noConflict', $out);
292 }
293
294 /**
295 * @test
296 */
297 public function loadJqueryLoadsTheLatestJqueryVersionInNoConflictModeUncompressedInDebugMode() {
298 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
299
300 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
301 $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
302 $subject->loadJquery();
303 $subject->enableDebugMode();
304 $out = $subject->render();
305 $this->assertRegExp($expectedRegExp, $out);
306 $this->assertContains($expectedStatement, $out);
307 }
308
309 /**
310 * @return array
311 */
312 public function loadJqueryFromSourceDataProvider() {
313 return array(
314 'google with version number' => array(
315 '1.6.3',
316 'google',
317 '#<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.js" type="text/javascript"></script>#'
318 ),
319 'msn with version number' => array(
320 '1.6.3',
321 'msn',
322 '#<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.3.js" type="text/javascript"></script>#'
323 ),
324 'jquery with version number' => array(
325 '1.6.3',
326 'jquery',
327 '#<script src="http://code.jquery.com/jquery-1.6.3.js" type="text/javascript"></script>#'
328 ),
329 'jquery with custom URL' => array(
330 '1.6.3',
331 'http://my.cool.cdn/foo/jquery.js',
332 '#<script src="http://my.cool.cdn/foo/jquery.js" type="text/javascript"></script>#'
333 ),
334 );
335 }
336
337 /**
338 * @dataProvider loadJqueryFromSourceDataProvider
339 * @test
340 */
341 public function isJqueryLoadedFromSourceUncompressedIfDebugModeIsEnabled($version, $source, $regex) {
342 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
343
344 $subject->loadJquery($version, $source);
345 $subject->enableDebugMode();
346 $out = $subject->render();
347 $this->assertRegExp($regex, $out);
348 }
349
350 /**
351 * @test
352 */
353 public function isJqueryLoadedMinifiedFromGoogleByDefault() {
354 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
355
356 $expectedRegex = '#<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script>#';
357 $subject->loadJquery('1.6.3', 'google');
358 $out = $subject->render();
359 $this->assertRegExp($expectedRegex, $out);
360 }
361
362 /**
363 * @test
364 */
365 public function loadExtJsInDebugLoadsDebugExtJs() {
366 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
367
368 $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
369 $subject->loadExtJS(TRUE, TRUE, 'jquery');
370 $subject->enableExtJsDebug();
371 $out = $subject->render();
372 $this->assertRegExp($expectedRegExp, $out);
373 }
374
375 /**
376 * @test
377 */
378 public function loadExtCoreLoadsExtCore() {
379 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
380
381 $expectedRegExp = '#<script src="contrib/extjs/ext-core\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
382 $subject->loadExtCore();
383 $out = $subject->render();
384 $this->assertRegExp($expectedRegExp, $out);
385 }
386
387 /**
388 * @test
389 */
390 public function loadExtCoreInDebugLoadsDebugExtCore() {
391 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
392
393 $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
394 $subject->loadExtCore();
395 $subject->enableExtCoreDebug();
396 $out = $subject->render();
397 $this->assertRegExp($expectedRegExp, $out);
398 }
399 }