0732d0d3401df0392c538e1f07cd8a1fdf1d9507
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_pagerenderer_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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_PageRenderer_testcase 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
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();
414
415 $this->assertRegExp(
416 $expectedRegExp,
417 $out
418 );
419 }
420
421 /**
422 * test load ExtJS
423 *
424 */
425 public function testLoadExtJS() {
426
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();
430
431 $this->assertRegExp(
432 $expectedRegExp,
433 $out
434 );
435 }
436
437 /**
438 * test load ExtCore
439 *
440 */
441 public function testLoadExtCore() {
442
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();
446
447 $this->assertRegExp(
448 $expectedRegExp,
449 $out
450 );
451 }
452
453 /**
454 * test enable ExtJsDebug
455 *
456 */
457 public function testEnableExtJsDebug() {
458
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();
463
464 $this->assertRegExp(
465 $expectedRegExp,
466 $out
467 );
468 }
469
470 /**
471 * test enable ExtCoreDebug
472 *
473 */
474 public function testEnableExtCoreDebug() {
475
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();
480
481 $this->assertRegExp(
482 $expectedRegExp,
483 $out
484 );
485 }
486
487 /**
488 * test inline language label
489 *
490 */
491 public function testAddInlineLanguageLabel() {
492
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);
498
499 $this->assertContains(
500 $expectedReturnValue,
501 $out
502 );
503 }
504
505 /**
506 * test inline language label as array
507 *
508 */
509 public function testAddInlineLanguageLabelArray() {
510
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);
516
517 $this->assertContains(
518 $expectedReturnValue,
519 $out
520 );
521 }
522
523 /**
524 * test inline language label as array get merged
525 *
526 */
527 public function testAddInlineLanguageLabelArrayMerged() {
528
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);
535
536 $this->assertContains(
537 $expectedReturnValue,
538 $out
539 );
540 }
541
542 /**
543 * test inline setting
544 *
545 */
546 public function testAddInlineSetting() {
547
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);
553
554 $this->assertContains(
555 $expectedReturnValue,
556 $out
557 );
558 }
559
560 /**
561 * test inline settings with array
562 *
563 */
564 public function testAddInlineSettingArray() {
565
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);
571
572 $this->assertContains(
573 $expectedReturnValue,
574 $out
575 );
576 }
577
578 /**
579 * test inline settings with array get merged
580 *
581 */
582 public function testAddInlineSettingArrayMerged() {
583
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);
590
591 $this->assertContains(
592 $expectedReturnValue,
593 $out
594 );
595 }
596
597 /**
598 * test add body content
599 *
600 */
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');
608
609 $out = $this->fixture->getBodyContent();
610 $this->assertEquals(
611 $expectedReturnValue,
612 $out
613 );
614 }
615
616
617 /**
618 * test set body content
619 *
620 */
621 public function testSetBodyContent() {
622 $expectedReturnValue = 'ABCDE';
623 $this->fixture->setBodyContent('ABCDE');
624
625 $out = $this->fixture->getBodyContent();
626 $this->assertEquals(
627 $expectedReturnValue,
628 $out
629 );
630
631 $out = $this->fixture->render();
632
633 $this->assertContains(
634 $expectedReturnValue,
635 $out
636 );
637 }
638
639 /**
640 * Tests whether labels are delivered in a non-UTF-8 context.
641 * (background: json_encode() requires UTF-8 to work properly)
642 *
643 * @test
644 */
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");
650
651 $out = $this->fixture->render();
652
653 $this->assertContains($testPrefix . '_\\u00d8', $out);
654 }
655 }
656 ?>