91284dffd838c185070b75361dcc0ccb4776c752
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_pagerendererTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 Steffen Kamper (info@sk-typo3.de)
6 * All rights reserved
7 *
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.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
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.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for the t3lib_PageRenderer class in the TYPO3 core.
28 *
29 * @package TYPO3
30 * @subpackage t3lib
31 *
32 * @author Steffen Kamper (info@sk-typo3.de)
33 */
34 class t3lib_PageRendererTest extends tx_phpunit_testcase {
35 /**
36 * @var t3lib_PageRenderer
37 */
38 private $fixture;
39
40 public function setUp() {
41 $this->fixture = new t3lib_PageRenderer();
42 $this->fixture->setCharSet('utf-8');
43 }
44
45 public function tearDown() {
46 unset(
47 $this->fixture
48 );
49 }
50
51
52 //////////////////////////////////////
53 // Tests for the basic functionality
54 //////////////////////////////////////
55
56 /**
57 * @test
58 */
59 public function fixtureCanBeCreated() {
60 $this->assertTrue(
61 $this->fixture instanceof t3lib_PageRenderer
62 );
63 }
64
65 //////////////////////
66 // test functions
67 //////////////////////
68
69 /**
70 * test set xml prolog and doctype
71 *
72 */
73 public function testSetXmlPrologAndDocType() {
74
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();
78
79 $this->assertContains(
80 $expectedReturnValue,
81 $out
82 );
83 }
84
85 /**
86 * test set title
87 *
88 */
89 public function testSetTitle() {
90
91 $expectedReturnValue = '<title>This is the title</title>';
92 $this->fixture->setTitle('This is the title');
93 $out = $this->fixture->render();
94
95 $this->assertContains(
96 $expectedReturnValue,
97 $out
98 );
99 }
100
101 /**
102 * test set charset
103 *
104 */
105 public function testSetCharset() {
106
107 $expectedReturnValue = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
108 $this->fixture->setCharset('utf-8');
109 $out = $this->fixture->render();
110
111 $this->assertContains(
112 $expectedReturnValue,
113 $out
114 );
115 }
116
117 /**
118 * test set favicon
119 *
120 */
121 public function testSetFavIcon() {
122
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();
127
128 $this->assertContains(
129 $expectedReturnValue1,
130 $out
131 );
132 $this->assertContains(
133 $expectedReturnValue2,
134 $out
135 );
136
137 }
138
139 /**
140 * test set baseUrl
141 *
142 */
143 public function testSetBaseUrl() {
144
145 $expectedReturnValue = '<base href="http://ggogle.com/" />';
146 $this->fixture->setBaseUrl('http://ggogle.com/');
147 $out = $this->fixture->render();
148
149 $this->assertContains(
150 $expectedReturnValue,
151 $out
152 );
153 }
154
155 /**
156 * test add meta tag
157 *
158 */
159 public function testAddMetaTag() {
160
161 $expectedReturnValue = '<meta name="author" content="Anna Lyse">';
162 $this->fixture->addMetaTag('<meta name="author" content="Anna Lyse">');
163 $out = $this->fixture->render();
164
165 $this->assertContains(
166 $expectedReturnValue,
167 $out
168 );
169 }
170
171 /**
172 * test add inline comment
173 *
174 */
175 public function testAddInlineComment() {
176
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();
180
181 $this->assertContains(
182 $expectedReturnValue,
183 $out
184 );
185 }
186
187 /**
188 * test add header data
189 *
190 */
191 public function testAddHeaderData() {
192
193 $expectedReturnValue = '<tag method="private" name="test" />';
194 $this->fixture->addHeaderData('<tag method="private" name="test" />');
195 $out = $this->fixture->render();
196
197 $this->assertContains(
198 $expectedReturnValue,
199 $out
200 );
201 }
202
203 /**
204 * test add footer data
205 *
206 */
207 public function testAddFooterData() {
208
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);
212
213 $this->assertContains(
214 $expectedReturnValue,
215 $out
216 );
217 }
218
219 /**
220 * test add JS library file
221 *
222 */
223 public function testAddJsLibrary() {
224
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();
228
229 $this->assertRegExp(
230 $expectedRegExp,
231 $out
232 );
233 }
234
235
236 /**
237 * test add JS footer library file
238 *
239 */
240 public function testAddJsFooterLibrary() {
241
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);
245
246 $this->assertRegExp(
247 $expectedRegExp,
248 $out
249 );
250 }
251
252 /**
253 * test add JS file
254 *
255 */
256 public function testAddJsFile() {
257
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();
261
262 $this->assertRegExp(
263 $expectedRegExp,
264 $out
265 );
266 }
267
268 /**
269 * test add JS file for footer
270 *
271 */
272 public function testAddJsFooterFile() {
273
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);
277
278 $this->assertRegExp(
279 $expectedRegExp,
280 $out
281 );
282 }
283
284 /**
285 * test add JS inline
286 *
287 */
288 public function testAddJsInlineCode() {
289
290 $expectedReturnValue = 'var x = "testvar"';
291 $this->fixture->addJsInlineCode('test', 'var x = "testvar"');
292 $out = $this->fixture->render();
293
294 $this->assertContains(
295 $expectedReturnValue,
296 $out
297 );
298 }
299
300 /**
301 * test add JS inline for footer
302 *
303 */
304 public function testAddJsFooterInlineCode() {
305
306 $expectedReturnValue = 'var x = "testvar"';
307 $this->fixture->addJsFooterInlineCode('test', 'var x = "testvar"');
308 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
309
310 $this->assertContains(
311 $expectedReturnValue,
312 $out
313 );
314 }
315
316 /**
317 * test add JS handler
318 *
319 */
320 public function testAddExtOnReadyCode() {
321
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();
327
328 $this->assertContains(
329 $expectedReturnValue1,
330 $out
331 );
332
333 $this->assertContains(
334 $expectedReturnValue2,
335 $out
336 );
337
338 }
339
340 /**
341 * test add CSS file
342 *
343 */
344 public function testAddCssFile() {
345
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();
349
350 $this->assertContains(
351 $expectedReturnValue,
352 $out
353 );
354 }
355
356 /**
357 * test add CSS inline
358 *
359 */
360 public function testAddCssInlineBlock() {
361
362 $expectedReturnValue = 'body {margin:20px;}';
363 $this->fixture->addCssInlineBlock('general', 'body {margin:20px;}');
364 $out = $this->fixture->render();
365
366 $this->assertContains(
367 $expectedReturnValue,
368 $out
369 );
370 }
371
372 /**
373 * test add CSS inline and force on top
374 *
375 */
376 public function testAddCssInlineBlockForceOnTop() {
377
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();
382
383 $this->assertContains(
384 $expectedReturnValue,
385 $out
386 );
387 }
388
389 /**
390 * test load prototype
391 *
392 */
393 public function testLoadPrototype() {
394
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();
398
399 $this->assertRegExp(
400 $expectedRegExp,
401 $out
402 );
403 }
404
405 /**
406 * test load Scriptaculous
407 *
408 */
409 public function testLoadScriptaculous() {
410 $this->fixture->loadScriptaculous('slider,controls');
411 $out = $this->fixture->render();
412
413 $this->assertContains(
414 '<script src="contrib/scriptaculous/scriptaculous.js" type="text/javascript"></script>',
415 $out
416 );
417 $this->assertContains(
418 '<script src="contrib/scriptaculous/effects.js" type="text/javascript"></script>',
419 $out
420 );
421 $this->assertContains(
422 '<script src="contrib/scriptaculous/controls.js" type="text/javascript"></script>',
423 $out
424 );
425 $this->assertContains(
426 '<script src="contrib/scriptaculous/slider.js" type="text/javascript"></script>',
427 $out
428 );
429 }
430
431 /**
432 * Tests whether scriptaculous is loaded correctly when compression is enabled.
433 *
434 * @test
435 */
436 public function isScriptaculousLoadedCompressed() {
437 $this->fixture->loadScriptaculous('slider,controls');
438 $this->fixture->enableCompressJavascript();
439 $out = $this->fixture->render();
440
441 $this->assertRegExp(
442 '#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#',
443 $out
444 );
445 $this->assertRegExp(
446 '#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#',
447 $out
448 );
449 $this->assertRegExp(
450 '#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#',
451 $out
452 );
453 $this->assertRegExp(
454 '#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#',
455 $out
456 );
457 }
458
459 /**
460 * test load ExtJS
461 *
462 */
463 public function testLoadExtJS() {
464
465 $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';
466 $this->fixture->loadExtJS(TRUE, TRUE, 'jquery');
467 $out = $this->fixture->render();
468
469 $this->assertRegExp(
470 $expectedRegExp,
471 $out
472 );
473 }
474
475 /**
476 * test load ExtCore
477 *
478 */
479 public function testLoadExtCore() {
480
481 $expectedRegExp = '#<script src="contrib/extjs/ext-core\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
482 $this->fixture->loadExtCore();
483 $out = $this->fixture->render();
484
485 $this->assertRegExp(
486 $expectedRegExp,
487 $out
488 );
489 }
490
491 /**
492 * test enable ExtJsDebug
493 *
494 */
495 public function testEnableExtJsDebug() {
496
497 $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
498 $this->fixture->loadExtJS(TRUE, TRUE, 'jquery');
499 $this->fixture->enableExtJsDebug();
500 $out = $this->fixture->render();
501
502 $this->assertRegExp(
503 $expectedRegExp,
504 $out
505 );
506 }
507
508 /**
509 * test enable ExtCoreDebug
510 *
511 */
512 public function testEnableExtCoreDebug() {
513
514 $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
515 $this->fixture->loadExtCore();
516 $this->fixture->enableExtCoreDebug();
517 $out = $this->fixture->render();
518
519 $this->assertRegExp(
520 $expectedRegExp,
521 $out
522 );
523 }
524
525 /**
526 * test inline language label
527 *
528 */
529 public function testAddInlineLanguageLabel() {
530
531 $expectedReturnValue = 'TYPO3.lang = {"myKey":"myValue"}';
532 $this->fixture->loadExtJS();
533 $this->fixture->addInlineLanguageLabel('myKey', 'myValue');
534 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
535 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
536
537 $this->assertContains(
538 $expectedReturnValue,
539 $out
540 );
541 }
542
543 /**
544 * test inline language label as array
545 *
546 */
547 public function testAddInlineLanguageLabelArray() {
548
549 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
550 $this->fixture->loadExtJS();
551 $this->fixture->addInlineLanguageLabelArray(array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
552 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
553 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
554
555 $this->assertContains(
556 $expectedReturnValue,
557 $out
558 );
559 }
560
561 /**
562 * test inline language label as array get merged
563 *
564 */
565 public function testAddInlineLanguageLabelArrayMerged() {
566
567 $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
568 $this->fixture->loadExtJS();
569 $this->fixture->addInlineLanguageLabelArray(array('myKey1' => 'myValue1',));
570 $this->fixture->addInlineLanguageLabelArray(array('myKey2' => 'myValue2',));
571 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
572 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
573
574 $this->assertContains(
575 $expectedReturnValue,
576 $out
577 );
578 }
579
580 /**
581 * test inline setting
582 *
583 */
584 public function testAddInlineSetting() {
585
586 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue"}};';
587 $this->fixture->loadExtJS();
588 $this->fixture->addInlineSetting('myApp', 'myKey', 'myValue');
589 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
590 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
591
592 $this->assertContains(
593 $expectedReturnValue,
594 $out
595 );
596 }
597
598 /**
599 * test inline settings with array
600 *
601 */
602 public function testAddInlineSettingArray() {
603
604 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
605 $this->fixture->loadExtJS();
606 $this->fixture->addInlineSettingArray('myApp', array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
607 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
608 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
609
610 $this->assertContains(
611 $expectedReturnValue,
612 $out
613 );
614 }
615
616 /**
617 * test inline settings with array get merged
618 *
619 */
620 public function testAddInlineSettingArrayMerged() {
621
622 $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
623 $this->fixture->loadExtJS();
624 $this->fixture->addInlineSettingArray('myApp', array('myKey1' => 'myValue1',));
625 $this->fixture->addInlineSettingArray('myApp', array('myKey2' => 'myValue2',));
626 $out = $this->fixture->enableMoveJsFromHeaderToFooter();
627 $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
628
629 $this->assertContains(
630 $expectedReturnValue,
631 $out
632 );
633 }
634
635 /**
636 * test add body content
637 *
638 */
639 public function testAddBodyContent() {
640 $expectedReturnValue = 'ABCDE';
641 $this->fixture->addBodyContent('A');
642 $this->fixture->addBodyContent('B');
643 $this->fixture->addBodyContent('C');
644 $this->fixture->addBodyContent('D');
645 $this->fixture->addBodyContent('E');
646
647 $out = $this->fixture->getBodyContent();
648 $this->assertEquals(
649 $expectedReturnValue,
650 $out
651 );
652 }
653
654
655 /**
656 * test set body content
657 *
658 */
659 public function testSetBodyContent() {
660 $expectedReturnValue = 'ABCDE';
661 $this->fixture->setBodyContent('ABCDE');
662
663 $out = $this->fixture->getBodyContent();
664 $this->assertEquals(
665 $expectedReturnValue,
666 $out
667 );
668
669 $out = $this->fixture->render();
670
671 $this->assertContains(
672 $expectedReturnValue,
673 $out
674 );
675 }
676
677 /**
678 * Tests whether labels are delivered in a non-UTF-8 context.
679 * (background: json_encode() requires UTF-8 to work properly)
680 *
681 * @test
682 */
683 public function isInlineLanguageLabelDeliveredWithNonUTF8() {
684 $testPrefix = uniqid('test');
685 $this->fixture->loadExtCore();
686 $this->fixture->setCharSet('iso-8859-1');
687 $this->fixture->addInlineLanguageLabel($testPrefix, $testPrefix . "_\xd8");
688
689 $out = $this->fixture->render();
690
691 $this->assertContains($testPrefix . '_\\u00d8', $out);
692 }
693 }
694 ?>