2 /***************************************************************
5 * (c) 2009 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_PageRenderer_testcase
extends tx_phpunit_testcase
{
36 * @var t3lib_PageRenderer
40 public function setUp() {
41 $this->fixture
= new t3lib_PageRenderer();
42 $this->fixture
->setCharSet('utf-8');
45 public function tearDown() {
52 //////////////////////////////////////
53 // Tests for the basic functionality
54 //////////////////////////////////////
59 public function fixtureCanBeCreated() {
61 $this->fixture
instanceof t3lib_PageRenderer
65 //////////////////////
67 //////////////////////
70 * test set xml prolog and doctype
73 public function testSetXmlPrologAndDocType() {
75 $expectedReturnValue = '<?xml version="1.0" encoding="utf-8" ?>';
76 $this->fixture
->setXmlPrologAndDocType('<?xml version="1.0" encoding="utf-8" ?>');
77 $out = $this->fixture
->render();
79 $this->assertContains(
89 public function testSetTitle() {
91 $expectedReturnValue = '<title>This is the title</title>';
92 $this->fixture
->setTitle('This is the title');
93 $out = $this->fixture
->render();
95 $this->assertContains(
105 public function testSetCharset() {
107 $expectedReturnValue = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
108 $this->fixture
->setCharset('utf-8');
109 $out = $this->fixture
->render();
111 $this->assertContains(
112 $expectedReturnValue,
121 public function testSetFavIcon() {
123 $expectedReturnValue1 = '<link rel="shortcut icon" href="http://google.com/favicon.ico" />';
124 $expectedReturnValue2 = '<link rel="icon" href="http://google.com/favicon.ico" />';
125 $this->fixture
->setFavIcon('http://google.com/favicon.ico');
126 $out = $this->fixture
->render();
128 $this->assertContains(
129 $expectedReturnValue1,
132 $this->assertContains(
133 $expectedReturnValue2,
143 public function testSetBaseUrl() {
145 $expectedReturnValue = '<base href="http://ggogle.com/" />';
146 $this->fixture
->setBaseUrl('http://ggogle.com/');
147 $out = $this->fixture
->render();
149 $this->assertContains(
150 $expectedReturnValue,
159 public function testAddMetaTag() {
161 $expectedReturnValue = '<meta name="author" content="Anna Lyse">';
162 $this->fixture
->addMetaTag('<meta name="author" content="Anna Lyse">');
163 $out = $this->fixture
->render();
165 $this->assertContains(
166 $expectedReturnValue,
172 * test add inline comment
175 public function testAddInlineComment() {
177 $expectedReturnValue = 'this is an inline comment written by unit test';
178 $this->fixture
->addInlineComment('this is an inline comment written by unit test');
179 $out = $this->fixture
->render();
181 $this->assertContains(
182 $expectedReturnValue,
188 * test add header data
191 public function testAddHeaderData() {
193 $expectedReturnValue = '<tag method="private" name="test" />';
194 $this->fixture
->addHeaderData('<tag method="private" name="test" />');
195 $out = $this->fixture
->render();
197 $this->assertContains(
198 $expectedReturnValue,
204 * test add footer data
207 public function testAddFooterData() {
209 $expectedReturnValue = '<tag method="private" name="test" />';
210 $this->fixture
->addFooterData('<tag method="private" name="test" />');
211 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
213 $this->assertContains(
214 $expectedReturnValue,
220 * test add JS library file
223 public function testAddJsLibrary() {
225 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
226 $this->fixture
->addJsLibrary('test', 'fileadmin/test.js');
227 $out = $this->fixture
->render();
237 * test add JS footer library file
240 public function testAddJsFooterLibrary() {
242 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
243 $this->fixture
->addJsFooterLibrary('test', 'fileadmin/test.js');
244 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
256 public function testAddJsFile() {
258 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
259 $this->fixture
->addJsFile('fileadmin/test.js');
260 $out = $this->fixture
->render();
269 * test add JS file for footer
272 public function testAddJsFooterFile() {
274 $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
275 $this->fixture
->addJsFooterFile('fileadmin/test.js');
276 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
288 public function testAddJsInlineCode() {
290 $expectedReturnValue = 'var x = "testvar"';
291 $this->fixture
->addJsInlineCode('test', 'var x = "testvar"');
292 $out = $this->fixture
->render();
294 $this->assertContains(
295 $expectedReturnValue,
301 * test add JS inline for footer
304 public function testAddJsFooterInlineCode() {
306 $expectedReturnValue = 'var x = "testvar"';
307 $this->fixture
->addJsFooterInlineCode('test', 'var x = "testvar"');
308 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
310 $this->assertContains(
311 $expectedReturnValue,
317 * test add JS handler
320 public function testAddExtOnReadyCode() {
322 $expectedReturnValue1 = 'Ext.onReady(function() {';
323 $expectedReturnValue2 = 'var x = "testvar";';
324 $this->fixture
->loadExtJS();
325 $this->fixture
->addExtOnReadyCode('var x = "testvar";');
326 $out = $this->fixture
->render();
328 $this->assertContains(
329 $expectedReturnValue1,
333 $this->assertContains(
334 $expectedReturnValue2,
344 public function testAddCssFile() {
346 $expectedReturnValue = '<link rel="stylesheet" type="text/css" href="fileadmin/test.css" media="all" />';
347 $this->fixture
->addCssFile('fileadmin/test.css');
348 $out = $this->fixture
->render();
350 $this->assertContains(
351 $expectedReturnValue,
357 * test add CSS inline
360 public function testAddCssInlineBlock() {
362 $expectedReturnValue = 'body {margin:20px;}';
363 $this->fixture
->addCssInlineBlock('general', 'body {margin:20px;}');
364 $out = $this->fixture
->render();
366 $this->assertContains(
367 $expectedReturnValue,
373 * test add CSS inline and force on top
376 public function testAddCssInlineBlockForceOnTop() {
378 $expectedReturnValue = '/*general1*/' . LF
. 'h1 {margin:20px;}' . LF
. '/*general*/' . LF
. 'body {margin:20px;}';
379 $this->fixture
->addCssInlineBlock('general', 'body {margin:20px;}');
380 $this->fixture
->addCssInlineBlock('general1', 'h1 {margin:20px;}', NULL, TRUE);
381 $out = $this->fixture
->render();
383 $this->assertContains(
384 $expectedReturnValue,
390 * test load prototype
393 public function testLoadPrototype() {
395 $expectedRegExp = '#<script src="contrib/prototype/prototype\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
396 $this->fixture
->loadPrototype();
397 $out = $this->fixture
->render();
406 * test load Scriptaculous
409 public function testLoadScriptaculous() {
411 $expectedRegExp = '#<script src="contrib/scriptaculous/scriptaculous\.js\?load=effects,controls,slider(&\d+)?" type="text/javascript"></script>#';
412 $this->fixture
->loadScriptaculous('slider,controls');
413 $out = $this->fixture
->render();
425 public function testLoadExtJS() {
427 $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';
428 $this->fixture
->loadExtJS(TRUE, TRUE, 'jquery');
429 $out = $this->fixture
->render();
441 public function testLoadExtCore() {
443 $expectedRegExp = '#<script src="contrib/extjs/ext-core\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
444 $this->fixture
->loadExtCore();
445 $out = $this->fixture
->render();
454 * test enable ExtJsDebug
457 public function testEnableExtJsDebug() {
459 $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
460 $this->fixture
->loadExtJS(TRUE, TRUE, 'jquery');
461 $this->fixture
->enableExtJsDebug();
462 $out = $this->fixture
->render();
471 * test enable ExtCoreDebug
474 public function testEnableExtCoreDebug() {
476 $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
477 $this->fixture
->loadExtCore();
478 $this->fixture
->enableExtCoreDebug();
479 $out = $this->fixture
->render();
488 * test inline language label
491 public function testAddInlineLanguageLabel() {
493 $expectedReturnValue = 'TYPO3.lang = {"myKey":"myValue"}';
494 $this->fixture
->loadExtJS();
495 $this->fixture
->addInlineLanguageLabel('myKey', 'myValue');
496 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
497 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
499 $this->assertContains(
500 $expectedReturnValue,
506 * test inline language label as array
509 public function testAddInlineLanguageLabelArray() {
511 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
512 $this->fixture
->loadExtJS();
513 $this->fixture
->addInlineLanguageLabelArray(array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
514 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
515 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
517 $this->assertContains(
518 $expectedReturnValue,
524 * test inline language label as array get merged
527 public function testAddInlineLanguageLabelArrayMerged() {
529 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
530 $this->fixture
->loadExtJS();
531 $this->fixture
->addInlineLanguageLabelArray(array('myKey1' => 'myValue1',));
532 $this->fixture
->addInlineLanguageLabelArray(array('myKey2' => 'myValue2',));
533 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
534 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
536 $this->assertContains(
537 $expectedReturnValue,
543 * test inline setting
546 public function testAddInlineSetting() {
548 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue"}};';
549 $this->fixture
->loadExtJS();
550 $this->fixture
->addInlineSetting('myApp', 'myKey', 'myValue');
551 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
552 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
554 $this->assertContains(
555 $expectedReturnValue,
561 * test inline settings with array
564 public function testAddInlineSettingArray() {
566 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
567 $this->fixture
->loadExtJS();
568 $this->fixture
->addInlineSettingArray('myApp', array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
569 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
570 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
572 $this->assertContains(
573 $expectedReturnValue,
579 * test inline settings with array get merged
582 public function testAddInlineSettingArrayMerged() {
584 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
585 $this->fixture
->loadExtJS();
586 $this->fixture
->addInlineSettingArray('myApp', array('myKey1' => 'myValue1',));
587 $this->fixture
->addInlineSettingArray('myApp', array('myKey2' => 'myValue2',));
588 $out = $this->fixture
->enableMoveJsFromHeaderToFooter();
589 $out = $this->fixture
->render(t3lib_PageRenderer
::PART_FOOTER
);
591 $this->assertContains(
592 $expectedReturnValue,
598 * test add body content
601 public function testAddBodyContent() {
602 $expectedReturnValue = 'ABCDE';
603 $this->fixture
->addBodyContent('A');
604 $this->fixture
->addBodyContent('B');
605 $this->fixture
->addBodyContent('C');
606 $this->fixture
->addBodyContent('D');
607 $this->fixture
->addBodyContent('E');
609 $out = $this->fixture
->getBodyContent();
611 $expectedReturnValue,
618 * test set body content
621 public function testSetBodyContent() {
622 $expectedReturnValue = 'ABCDE';
623 $this->fixture
->setBodyContent('ABCDE');
625 $out = $this->fixture
->getBodyContent();
627 $expectedReturnValue,
631 $out = $this->fixture
->render();
633 $this->assertContains(
634 $expectedReturnValue,
640 * Tests whether labels are delivered in a non-UTF-8 context.
641 * (background: json_encode() requires UTF-8 to work properly)
645 public function isInlineLanguageLabelDeliveredWithNonUTF8() {
646 $testPrefix = uniqid('test');
647 $this->fixture
->loadExtCore();
648 $this->fixture
->setCharSet('iso-8859-1');
649 $this->fixture
->addInlineLanguageLabel($testPrefix, $testPrefix . "_\xd8");
651 $out = $this->fixture
->render();
653 $this->assertContains($testPrefix . '_\\u00d8', $out);