Revert "[BUGFIX] Fix broken functional tests"
[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 /**
18 * Test case
19 */
20 class PageRendererTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
21
22 /**
23 * @test
24 */
25 public function pageRendererRendersInsertsMainContentStringsInOutput() {
26 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
27 $subject->setCharSet('utf-8');
28 $subject->setLanguage('default');
29
30 $prologueString = $expectedPrologueString = '<?xml version="1.0" encoding="utf-8" ?>';
31 $subject->setXmlPrologAndDocType($prologueString);
32
33 $title = $this->getUniqueId('aTitle-');
34 $subject->setTitle($title);
35 $expectedTitleString = '<title>' . $title . '</title>';
36
37 $charset = 'utf-8';
38 $subject->setCharSet($charset);
39 $expectedCharsetString = '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '" />';
40
41 $favouriteIcon = 'http://google.com/favicon.ico';
42 $subject->setFavIcon($favouriteIcon);
43 $expectedFavouriteIconPartOne = '<link rel="shortcut icon" href="' . $favouriteIcon . '" />';
44 $expectedFavouriteIconPartTwo = '<link rel="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 $expectedLoadPrototypeRegExp = '#<script src="contrib/prototype/prototype\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
83 $subject->loadPrototype();
84
85 $subject->loadScriptaculous('slider,controls');
86 $expectedScriptaculousMain = '<script src="contrib/scriptaculous/scriptaculous.js" type="text/javascript"></script>';
87 $expectedScriptaculousEffects = '<script src="contrib/scriptaculous/effects.js" type="text/javascript"></script>';
88 $expectedScriptaculousControls = '<script src="contrib/scriptaculous/controls.js" type="text/javascript"></script>';
89 $expectedScriptaculousSlider = '<script src="contrib/scriptaculous/slider.js" type="text/javascript"></script>';
90
91 $subject->loadJquery();
92 $expectedJqueryRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
93 $expectedJqueryStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
94
95 $subject->loadExtJS(TRUE, TRUE, 'jquery');
96 $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';
97
98 $expectedBodyContent = $this->getUniqueId('ABCDE-');
99 $subject->setBodyContent($expectedBodyContent);
100
101 $renderedString = $subject->render();
102
103 $this->assertContains($expectedPrologueString, $renderedString);
104 $this->assertContains($expectedTitleString, $renderedString);
105 $this->assertContains($expectedCharsetString, $renderedString);
106 $this->assertContains($expectedFavouriteIconPartOne, $renderedString);
107 $this->assertContains($expectedFavouriteIconPartTwo, $renderedString);
108 $this->assertContains($expectedBaseUrlString, $renderedString);
109 $this->assertContains($expectedMetaTagString, $renderedString);
110 $this->assertContains($expectedInlineCommentString, $renderedString);
111 $this->assertContains($expectedHeaderData, $renderedString);
112 $this->assertRegExp($expectedJsLibraryRegExp, $renderedString);
113 $this->assertRegExp($expectedJsFileRegExp, $renderedString);
114 $this->assertContains($expectedJsInlineCodeString, $renderedString);
115 $this->assertContains($expectedExtOnReadyCodePartOne, $renderedString);
116 $this->assertContains($expectedExtOnReadyCodePartTwo, $renderedString);
117 $this->assertContains($expectedCssFileString, $renderedString);
118 $this->assertContains($expectedCssInlineBlockOnTopString, $renderedString);
119 $this->assertRegExp($expectedLoadPrototypeRegExp, $renderedString);
120 $this->assertContains($expectedScriptaculousMain, $renderedString);
121 $this->assertContains($expectedScriptaculousEffects, $renderedString);
122 $this->assertContains($expectedScriptaculousControls, $renderedString);
123 $this->assertContains($expectedScriptaculousSlider, $renderedString);
124 $this->assertRegExp($expectedJqueryRegExp, $renderedString);
125 $this->assertContains($expectedJqueryStatement, $renderedString);
126 $this->assertRegExp($expectedExtJsRegExp, $renderedString);
127 $this->assertContains($expectedBodyContent, $renderedString);
128 }
129
130 /**
131 * @test
132 */
133 public function pageRendererRendersFooterValues() {
134 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
135 $subject->setCharSet('utf-8');
136 $subject->setLanguage('default');
137
138 $subject->enableMoveJsFromHeaderToFooter();
139
140 $footerData = $expectedFooterData = '<tag method="private" name="test" />';
141 $subject->addFooterData($footerData);
142
143 $expectedJsFooterLibraryRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
144 $subject->addJsFooterLibrary('test', 'fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
145
146 $expectedJsFooterRegExp = '#wrapBefore<script src="fileadmin/test\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>wrapAfter#';
147 $subject->addJsFooterFile('fileadmin/test.js', 'text/javascript', FALSE, FALSE, 'wrapBeforeXwrapAfter', FALSE, 'X');
148
149 $jsFooterInlineCode = $expectedJsFooterInlineCodeString = 'var x = "' . $this->getUniqueId('jsFooterInline-') . '"';
150 $subject->addJsFooterInlineCode($this->getUniqueId(), $jsFooterInlineCode);
151
152 // Bunch of label tests
153 $subject->loadExtJS();
154 $subject->addInlineLanguageLabel('myKey', 'myValue');
155 $subject->addInlineLanguageLabelArray(array(
156 'myKeyArray1' => 'myValueArray1',
157 'myKeyArray2' => 'myValueArray2'
158 ));
159 $subject->addInlineLanguageLabelArray(array(
160 'myKeyArray3' => 'myValueArray3'
161 ));
162 $expectedInlineLabelReturnValue = 'TYPO3.lang = {"myKey":"myValue","myKeyArray1":"myValueArray1","myKeyArray2":"myValueArray2","myKeyArray3":"myValueArray3"';
163
164 $subject->addInlineLanguageLabelFile('EXT:lang/locallang_core.xlf');
165 $expectedLanguageLabel1 = 'labels.beUser';
166 $expectedLanguageLabel2 = 'labels.feUser';
167
168 // Bunch of inline settings test
169 $subject->addInlineSetting('myApp', 'myKey', 'myValue');
170 $subject->addInlineSettingArray('myApp', array(
171 'myKey1' => 'myValue1',
172 'myKey2' => 'myValue2'
173 ));
174 $subject->addInlineSettingArray('myApp', array(
175 'myKey3' => 'myValue3'
176 ));
177 $expectedInlineSettingsReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue","myKey1":"myValue1","myKey2":"myValue2","myKey3":"myValue3"}';
178
179 $renderedString = $subject->render(\TYPO3\CMS\Core\Page\PageRenderer::PART_FOOTER);
180
181 $this->assertContains($expectedFooterData, $renderedString);
182 $this->assertRegExp($expectedJsFooterLibraryRegExp, $renderedString);
183 $this->assertRegExp($expectedJsFooterRegExp, $renderedString);
184 $this->assertContains($expectedJsFooterInlineCodeString, $renderedString);
185 $this->assertContains($expectedInlineLabelReturnValue, $renderedString);
186 $this->assertContains($expectedLanguageLabel1, $renderedString);
187 $this->assertContains($expectedLanguageLabel2, $renderedString);
188 $this->assertContains($expectedInlineSettingsReturnValue, $renderedString);
189 }
190
191 /**
192 * @test
193 */
194 public function isScriptaculousLoadedCompressedIfConfiguredAndClientIsCapable() {
195 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
196
197 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
198 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
199 $subject->loadScriptaculous('slider,controls');
200 $subject->enableCompressJavascript();
201 $out = $subject->render();
202 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
203 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
204 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
205 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js.gzip" type="text/javascript"></script>#', $out);
206 }
207
208 /**
209 * @test
210 */
211 public function isScriptaculousNotLoadedCompressedIfClientCannotHandleCompression() {
212 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
213
214 $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
215 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
216 $subject->loadScriptaculous('slider,controls');
217 $subject->enableCompressJavascript();
218 $out = $subject->render();
219 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
220 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
221 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
222 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
223 }
224
225 /**
226 * @test
227 */
228 public function isScriptaculousNotLoadedCompressedIfCompressionIsNotConfigured() {
229 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
230
231 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
232 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '';
233 $subject->loadScriptaculous('slider,controls');
234 $subject->enableCompressJavascript();
235 $out = $subject->render();
236 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
237 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
238 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
239 $this->assertRegExp('#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#', $out);
240 }
241
242 /**
243 * @test
244 */
245 public function loadJqueryRespectsGivenNamespace() {
246 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
247
248 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
249 $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.MyNameSpace = jQuery.noConflict(true);';
250 $subject->loadJquery(NULL, NULL, 'MyNameSpace');
251 $out = $subject->render();
252 $this->assertRegExp($expectedRegExp, $out);
253 $this->assertContains($expectedStatement, $out);
254 }
255
256 /**
257 * @test
258 */
259 public function loadJqueryWithDefaultNoConflictModeDoesNotSetNamespace() {
260 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
261
262 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
263 $expectedStatement = 'jQuery.noConflict();';
264 $subject->loadJquery(NULL, NULL, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_DEFAULT_NOCONFLICT);
265 $out = $subject->render();
266 $this->assertRegExp($expectedRegExp, $out);
267 $this->assertContains($expectedStatement, $out);
268 $this->assertNotContains('var TYPO3 = TYPO3 || {}; TYPO3.', $out);
269 }
270
271 /**
272 * @test
273 */
274 public function loadJqueryWithNamespaceNoneDoesNotIncludeNoConflictHandling() {
275 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
276
277 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.min\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
278 $subject->loadJquery(NULL, NULL, \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_NAMESPACE_NONE);
279 $out = $subject->render();
280 $this->assertRegExp($expectedRegExp, $out);
281 $this->assertNotContains('jQuery.noConflict', $out);
282 }
283
284 /**
285 * @test
286 */
287 public function loadJqueryLoadsTheLatestJqueryVersionInNoConflictModeUncompressedInDebugMode() {
288 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
289
290 $expectedRegExp = '#<script src="contrib/jquery/jquery-' . \TYPO3\CMS\Core\Page\PageRenderer::JQUERY_VERSION_LATEST . '\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
291 $expectedStatement = 'var TYPO3 = TYPO3 || {}; TYPO3.jQuery = jQuery.noConflict(true);';
292 $subject->loadJquery();
293 $subject->enableDebugMode();
294 $out = $subject->render();
295 $this->assertRegExp($expectedRegExp, $out);
296 $this->assertContains($expectedStatement, $out);
297 }
298
299 /**
300 * @return array
301 */
302 public function loadJqueryFromSourceDataProvider() {
303 return array(
304 'google with version number' => array(
305 '1.6.3',
306 'google',
307 '#<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.js" type="text/javascript"></script>#'
308 ),
309 'msn with version number' => array(
310 '1.6.3',
311 'msn',
312 '#<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.3.js" type="text/javascript"></script>#'
313 ),
314 'jquery with version number' => array(
315 '1.6.3',
316 'jquery',
317 '#<script src="http://code.jquery.com/jquery-1.6.3.js" type="text/javascript"></script>#'
318 ),
319 'jquery with custom URL' => array(
320 '1.6.3',
321 'http://my.cool.cdn/foo/jquery.js',
322 '#<script src="http://my.cool.cdn/foo/jquery.js" type="text/javascript"></script>#'
323 ),
324 );
325 }
326
327 /**
328 * @dataProvider loadJqueryFromSourceDataProvider
329 * @test
330 */
331 public function isJqueryLoadedFromSourceUncompressedIfDebugModeIsEnabled($version, $source, $regex) {
332 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
333
334 $subject->loadJquery($version, $source);
335 $subject->enableDebugMode();
336 $out = $subject->render();
337 $this->assertRegExp($regex, $out);
338 }
339
340 /**
341 * @test
342 */
343 public function isJqueryLoadedMinifiedFromGoogleByDefault() {
344 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
345
346 $expectedRegex = '#<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js" type="text/javascript"></script>#';
347 $subject->loadJquery('1.6.3', 'google');
348 $out = $subject->render();
349 $this->assertRegExp($expectedRegex, $out);
350 }
351
352 /**
353 * @test
354 */
355 public function loadExtJsInDebugLoadsDebugExtJs() {
356 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
357
358 $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
359 $subject->loadExtJS(TRUE, TRUE, 'jquery');
360 $subject->enableExtJsDebug();
361 $out = $subject->render();
362 $this->assertRegExp($expectedRegExp, $out);
363 }
364
365 /**
366 * @test
367 */
368 public function loadExtCoreLoadsExtCore() {
369 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
370
371 $expectedRegExp = '#<script src="contrib/extjs/ext-core\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
372 $subject->loadExtCore();
373 $out = $subject->render();
374 $this->assertRegExp($expectedRegExp, $out);
375 }
376
377 /**
378 * @test
379 */
380 public function loadExtCoreInDebugLoadsDebugExtCore() {
381 $subject = new \TYPO3\CMS\Core\Page\PageRenderer();
382
383 $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\\.(js|\\d+\\.js|js\\?\\d+)" type="text/javascript"></script>#';
384 $subject->loadExtCore();
385 $subject->enableExtCoreDebug();
386 $out = $subject->render();
387 $this->assertRegExp($expectedRegExp, $out);
388 }
389 }