2 /***************************************************************
5 * (c) 2009-2011 Steffen Kamper (info@sk-typo3.de)
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License or
12 * (at your option) any later version.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
17 * This script is distributed in the hope that it will be useful
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
27 * Testcase for the t3lib_PageRenderer class in the TYPO3 core.
32 * @author Steffen Kamper (info@sk-typo3.de)
34 class t3lib_PageRendererTest
extends tx_phpunit_testcase
{
37 * Enable backup of global and system variables
41 protected $backupGlobals = TRUE;
44 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
45 * because resource types cannot be handled during serializing
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
52 * @var t3lib_PageRenderer
56 public function setUp() {
57 $this->fixture
= new t3lib_PageRenderer();
58 $this->fixture
->setCharSet($GLOBALS['LANG']->charSet
);
61 public function tearDown() {
68 //////////////////////////////////////
69 // Tests for the basic functionality
70 //////////////////////////////////////
75 public function fixtureCanBeCreated() {
77 $this->fixture
instanceof t3lib_PageRenderer
81 //////////////////////
83 //////////////////////
86 * test set xml prolog and doctype
89 public function testSetXmlPrologAndDocType() {
91 $expectedReturnValue = '<?xml version="1.0" encoding="utf-8" ?>';
92 $this->fixture
->setXmlPrologAndDocType('<?xml version="1.0" encoding="utf-8" ?>');
93 $out = $this->fixture
->render();
95 $this->assertContains(
105 public function testSetTitle() {
107 $expectedReturnValue = '<title>This is the title</title>';
108 $this->fixture
->setTitle('This is the title');
109 $out = $this->fixture
->render();
111 $this->assertContains(
112 $expectedReturnValue,
121 public function testSetCharset() {
123 $expectedReturnValue = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
124 $this->fixture
->setCharset('utf-8');
125 $out = $this->fixture
->render();
127 $this->assertContains(
128 $expectedReturnValue,
137 public function testSetFavIcon() {
139 $expectedReturnValue1 = '<link rel="shortcut icon" href="http://google.com/favicon.ico" />';
140 $expectedReturnValue2 = '<link rel="icon" href="http://google.com/favicon.ico" />';
141 $this->fixture
->setFavIcon('http://google.com/favicon.ico');
142 $out = $this->fixture
->render();
144 $this->assertContains(
145 $expectedReturnValue1,
148 $this->assertContains(
149 $expectedReturnValue2,
159 public function testSetBaseUrl() {
161 $expectedReturnValue = '<base href="http://ggogle.com/" />';
162 $this->fixture
->setBaseUrl('http://ggogle.com/');
163 $out = $this->fixture
->render();
165 $this->assertContains(
166 $expectedReturnValue,
175 public function testAddMetaTag() {
177 $expectedReturnValue = '<meta name="author" content="Anna Lyse">';
178 $this->fixture
->addMetaTag('<meta name="author" content="Anna Lyse">');
179 $out = $this->fixture
->render();
181 $this->assertContains(
182 $expectedReturnValue,
188 * test add inline comment
191 public function testAddInlineComment() {
193 $expectedReturnValue = 'this is an inline comment written by unit test';
194 $this->fixture
->addInlineComment('this is an inline comment written by unit test');
195 $out = $this->fixture
->render();
197 $this->assertContains(
198 $expectedReturnValue,
204 * test add header data
207 public function testAddHeaderData() {
209 $expectedReturnValue = '<tag method="private" name="test" />';
210 $this->fixture
->addHeaderData('<tag method="private" name="test" />');
211 $out = $this->fixture
->render();
213 $this->assertContains(
214 $expectedReturnValue,
220 * test add footer data
223 public function testAddFooterData() {
225 $expectedReturnValue = '<tag method="private" name="test" />';
226 $this->fixture
->addFooterData('<tag method="private" name="test" />');
227 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
229 $this->assertContains(
230 $expectedReturnValue,
236 * test add JS library file
239 public function testAddJsLibrary() {
241 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
242 $this->fixture
->addJsLibrary('test', 'fileadmin/test.js');
243 $out = $this->fixture
->render();
253 * test add JS footer library file
256 public function testAddJsFooterLibrary() {
258 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
259 $this->fixture
->addJsFooterLibrary('test', 'fileadmin/test.js');
260 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
272 public function testAddJsFile() {
274 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
275 $this->fixture
->addJsFile('fileadmin/test.js');
276 $out = $this->fixture
->render();
285 * test add JS file for footer
288 public function testAddJsFooterFile() {
290 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
291 $this->fixture
->addJsFooterFile('fileadmin/test.js');
292 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
304 public function testAddJsInlineCode() {
306 $expectedReturnValue = 'var x = "testvar"';
307 $this->fixture
->addJsInlineCode('test', 'var x = "testvar"');
308 $out = $this->fixture
->render();
310 $this->assertContains(
311 $expectedReturnValue,
317 * test add JS inline for footer
320 public function testAddJsFooterInlineCode() {
322 $expectedReturnValue = 'var x = "testvar"';
323 $this->fixture
->addJsFooterInlineCode('test', 'var x = "testvar"');
324 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
326 $this->assertContains(
327 $expectedReturnValue,
333 * test add JS handler
336 public function testAddExtOnReadyCode() {
338 $expectedReturnValue1 = 'Ext.onReady(function() {';
339 $expectedReturnValue2 = 'var x = "testvar";';
340 $this->fixture
->loadExtJS();
341 $this->fixture
->addExtOnReadyCode('var x = "testvar";');
342 $out = $this->fixture
->render();
344 $this->assertContains(
345 $expectedReturnValue1,
349 $this->assertContains(
350 $expectedReturnValue2,
360 public function testAddCssFile() {
362 $expectedReturnValue = '<link rel="stylesheet" type="text/css" href="fileadmin/test.css" media="all" />';
363 $this->fixture
->addCssFile('fileadmin/test.css');
364 $out = $this->fixture
->render();
366 $this->assertContains(
367 $expectedReturnValue,
373 * test add CSS inline
376 public function testAddCssInlineBlock() {
378 $expectedReturnValue = 'body {margin:20px;}';
379 $this->fixture
->addCssInlineBlock('general', 'body {margin:20px;}');
380 $out = $this->fixture
->render();
382 $this->assertContains(
383 $expectedReturnValue,
389 * test add CSS inline and force on top
392 public function testAddCssInlineBlockForceOnTop() {
394 $expectedReturnValue = '/*general1*/' . LF
. 'h1 {margin:20px;}' . LF
. '/*general*/' . LF
. 'body {margin:20px;}';
395 $this->fixture
->addCssInlineBlock('general', 'body {margin:20px;}');
396 $this->fixture
->addCssInlineBlock('general1', 'h1 {margin:20px;}', NULL, TRUE);
397 $out = $this->fixture
->render();
399 $this->assertContains(
400 $expectedReturnValue,
406 * test load prototype
409 public function testLoadPrototype() {
411 $expectedRegExp = '#<script src="contrib/prototype/prototype\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
412 $this->fixture
->loadPrototype();
413 $out = $this->fixture
->render();
422 * test load Scriptaculous
425 public function testLoadScriptaculous() {
426 $this->fixture
->loadScriptaculous('slider,controls');
427 $out = $this->fixture
->render();
429 $this->assertContains(
430 '<script src="contrib/scriptaculous/scriptaculous.js" type="text/javascript"></script>',
433 $this->assertContains(
434 '<script src="contrib/scriptaculous/effects.js" type="text/javascript"></script>',
437 $this->assertContains(
438 '<script src="contrib/scriptaculous/controls.js" type="text/javascript"></script>',
441 $this->assertContains(
442 '<script src="contrib/scriptaculous/slider.js" type="text/javascript"></script>',
448 * Tests whether scriptaculous is loaded correctly when compression is enabled.
452 public function isScriptaculousLoadedCompressedIfConfiguredAndClientIsCapable() {
453 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
454 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
456 $this->fixture
->loadScriptaculous('slider,controls');
457 $this->fixture
->enableCompressJavascript();
458 $out = $this->fixture
->render();
461 '#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js.gzip" type="text/javascript"></script>#',
465 '#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js.gzip" type="text/javascript"></script>#',
469 '#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js.gzip" type="text/javascript"></script>#',
473 '#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js.gzip" type="text/javascript"></script>#',
479 * Tests whether scriptaculous is correctly loaded, but without compression
480 * if the browser did not send the appropriate headers.
484 public function isScriptaculousNotLoadedCompressedIfClientCannotHandleCompression() {
485 $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
486 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '5';
488 $this->fixture
->loadScriptaculous('slider,controls');
489 $this->fixture
->enableCompressJavascript();
490 $out = $this->fixture
->render();
493 '#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#',
497 '#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#',
501 '#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#',
505 '#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#',
511 * Tests whether scriptaculous is correctly loaded, but without compression
512 * if no compression is configured.
516 public function isScriptaculousNotLoadedCompressedIfCompressionIsNotConfigured() {
517 $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate';
518 $GLOBALS['TYPO3_CONF_VARS']['BE']['compressionLevel'] = '';
520 $this->fixture
->loadScriptaculous('slider,controls');
521 $this->fixture
->enableCompressJavascript();
522 $out = $this->fixture
->render();
525 '#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#',
529 '#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#',
533 '#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#',
537 '#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#',
546 public function testLoadExtJS() {
548 $expectedRegExp = '#<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';
549 $this->fixture
->loadExtJS(TRUE, TRUE, 'jquery');
550 $out = $this->fixture
->render();
562 public function testLoadExtCore() {
564 $expectedRegExp = '#<script src="contrib/extjs/ext-core\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
565 $this->fixture
->loadExtCore();
566 $out = $this->fixture
->render();
575 * test enable ExtJsDebug
578 public function testEnableExtJsDebug() {
580 $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
581 $this->fixture
->loadExtJS(TRUE, TRUE, 'jquery');
582 $this->fixture
->enableExtJsDebug();
583 $out = $this->fixture
->render();
592 * test enable ExtCoreDebug
595 public function testEnableExtCoreDebug() {
597 $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
598 $this->fixture
->loadExtCore();
599 $this->fixture
->enableExtCoreDebug();
600 $out = $this->fixture
->render();
609 * test inline language label
612 public function testAddInlineLanguageLabel() {
614 $expectedReturnValue = 'TYPO3.lang = {"myKey":"myValue"}';
615 $this->fixture
->loadExtJS();
616 $this->fixture
->addInlineLanguageLabel('myKey', 'myValue');
617 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
618 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
620 $this->assertContains(
621 $expectedReturnValue,
627 * test inline language label as array
630 public function testAddInlineLanguageLabelArray() {
632 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
633 $this->fixture
->loadExtJS();
634 $this->fixture
->addInlineLanguageLabelArray(array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
635 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
636 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
638 $this->assertContains(
639 $expectedReturnValue,
645 * test inline language label as array get merged
648 public function testAddInlineLanguageLabelArrayMerged() {
650 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
651 $this->fixture
->loadExtJS();
652 $this->fixture
->addInlineLanguageLabelArray(array('myKey1' => 'myValue1',));
653 $this->fixture
->addInlineLanguageLabelArray(array('myKey2' => 'myValue2',));
654 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
655 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
657 $this->assertContains(
658 $expectedReturnValue,
664 * test inline setting
667 public function testAddInlineSetting() {
669 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue"}};';
670 $this->fixture
->loadExtJS();
671 $this->fixture
->addInlineSetting('myApp', 'myKey', 'myValue');
672 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
673 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
675 $this->assertContains(
676 $expectedReturnValue,
682 * test inline settings with array
685 public function testAddInlineSettingArray() {
687 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
688 $this->fixture
->loadExtJS();
689 $this->fixture
->addInlineSettingArray('myApp', array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
690 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
691 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
693 $this->assertContains(
694 $expectedReturnValue,
700 * test inline settings with array get merged
703 public function testAddInlineSettingArrayMerged() {
705 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
706 $this->fixture
->loadExtJS();
707 $this->fixture
->addInlineSettingArray('myApp', array('myKey1' => 'myValue1',));
708 $this->fixture
->addInlineSettingArray('myApp', array('myKey2' => 'myValue2',));
709 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
710 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
712 $this->assertContains(
713 $expectedReturnValue,
719 * test add body content
722 public function testAddBodyContent() {
723 $expectedReturnValue = 'ABCDE';
724 $this->fixture
->addBodyContent('A');
725 $this->fixture
->addBodyContent('B');
726 $this->fixture
->addBodyContent('C');
727 $this->fixture
->addBodyContent('D');
728 $this->fixture
->addBodyContent('E');
730 $out = $this->fixture
->getBodyContent();
732 $expectedReturnValue,
739 * test set body content
742 public function testSetBodyContent() {
743 $expectedReturnValue = 'ABCDE';
744 $this->fixture
->setBodyContent('ABCDE');
746 $out = $this->fixture
->getBodyContent();
748 $expectedReturnValue,
752 $out = $this->fixture
->render();
754 $this->assertContains(
755 $expectedReturnValue,
761 * Tests whether labels are delivered in a non-UTF-8 context.
762 * (background: json_encode() requires UTF-8 to work properly)
766 public function isInlineLanguageLabelDeliveredWithNonUTF8() {
767 $testPrefix = uniqid('test');
768 $this->fixture
->loadExtCore();
769 $this->fixture
->setCharSet('iso-8859-1');
770 $this->fixture
->addInlineLanguageLabel($testPrefix, $testPrefix . "_\xd8");
772 $out = $this->fixture
->render();
774 $this->assertContains($testPrefix . '_\\u00d8', $out);
778 * Tests the addInlineLanguageLabelFile() method.
781 * @expectedException RuntimeException
783 public function areInlineLanguageLabelsNotProcessable() {
784 $this->fixture
->setLanguage(NULL);
785 $this->fixture
->addInlineLanguageLabelFile(
786 'EXT:lang/locallang_core.xml'
788 $out = $this->fixture
->render();
792 * Tests the addInlineLanguageLabelFile() method.
796 public function areInlineLanguageLabelsPassed() {
797 $this->fixture
->setLanguage($GLOBALS['LANG']->lang
);
798 $this->fixture
->addInlineLanguageLabelFile(
799 'EXT:lang/locallang_core.xml'
802 $out = $this->fixture
->render();
804 $this->assertContains('labels.beUser', $out);
805 $this->assertContains('labels.feUser', $out);
809 * Tests the addInlineLanguageLabelFile() method.
813 public function areInlineLanguageLabelsEmptyOnNonExistingFile() {
814 $this->fixture
->addInlineLanguageLabelFile(
818 $inlineLanguageLabelFiles = $this->fixture
->getInlineLanguageLabelFiles();
819 $this->assertEquals(array(), $inlineLanguageLabelFiles);
823 * Tests the addInlineLanguageLabelFile() method.
827 public function areInlineLanguageLabelsSelected() {
828 $this->fixture
->setLanguage($GLOBALS['LANG']->lang
);
829 $this->fixture
->addInlineLanguageLabelFile(
830 'EXT:lang/locallang_core.xml',
834 $out = $this->fixture
->render();
835 $this->assertContains('labels.beUser', $out);
836 $this->assertContains('labels.feUser', $out);
840 * Tests the addInlineLanguageLabelFile() method.
844 public function areInlineLanguageLabelsSelectedAndStripped() {
845 $this->fixture
->setLanguage($GLOBALS['LANG']->lang
);
846 $this->fixture
->addInlineLanguageLabelFile(
847 'EXT:lang/locallang_core.xml',
852 $out = $this->fixture
->render();
854 $this->assertContains('edRecord', $out);
855 $this->assertContains('edRecord_content', $out);
856 $this->assertContains('edRecordUser', $out);