PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/t3lib/t3lib_pagerendererTest.php

https://github.com/foxsoft/typo3v4core
PHP | 694 lines | 393 code | 116 blank | 185 comment | 0 complexity | fb6086120999cca8cd0c33b04dba59a7 MD5 | raw file
Possible License(s): Apache-2.0
  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. * Testcase for the t3lib_PageRenderer class in the TYPO3 core.
  26. *
  27. * @package TYPO3
  28. * @subpackage t3lib
  29. *
  30. * @author Steffen Kamper (info@sk-typo3.de)
  31. */
  32. class t3lib_PageRendererTest extends tx_phpunit_testcase {
  33. /**
  34. * @var t3lib_PageRenderer
  35. */
  36. private $fixture;
  37. public function setUp() {
  38. $this->fixture = new t3lib_PageRenderer();
  39. $this->fixture->setCharSet('utf-8');
  40. }
  41. public function tearDown() {
  42. unset(
  43. $this->fixture
  44. );
  45. }
  46. //////////////////////////////////////
  47. // Tests for the basic functionality
  48. //////////////////////////////////////
  49. /**
  50. * @test
  51. */
  52. public function fixtureCanBeCreated() {
  53. $this->assertTrue(
  54. $this->fixture instanceof t3lib_PageRenderer
  55. );
  56. }
  57. //////////////////////
  58. // test functions
  59. //////////////////////
  60. /**
  61. * test set xml prolog and doctype
  62. *
  63. */
  64. public function testSetXmlPrologAndDocType() {
  65. $expectedReturnValue = '<?xml version="1.0" encoding="utf-8" ?>';
  66. $this->fixture->setXmlPrologAndDocType('<?xml version="1.0" encoding="utf-8" ?>');
  67. $out = $this->fixture->render();
  68. $this->assertContains(
  69. $expectedReturnValue,
  70. $out
  71. );
  72. }
  73. /**
  74. * test set title
  75. *
  76. */
  77. public function testSetTitle() {
  78. $expectedReturnValue = '<title>This is the title</title>';
  79. $this->fixture->setTitle('This is the title');
  80. $out = $this->fixture->render();
  81. $this->assertContains(
  82. $expectedReturnValue,
  83. $out
  84. );
  85. }
  86. /**
  87. * test set charset
  88. *
  89. */
  90. public function testSetCharset() {
  91. $expectedReturnValue = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  92. $this->fixture->setCharset('utf-8');
  93. $out = $this->fixture->render();
  94. $this->assertContains(
  95. $expectedReturnValue,
  96. $out
  97. );
  98. }
  99. /**
  100. * test set favicon
  101. *
  102. */
  103. public function testSetFavIcon() {
  104. $expectedReturnValue1 = '<link rel="shortcut icon" href="http://google.com/favicon.ico" />';
  105. $expectedReturnValue2 = '<link rel="icon" href="http://google.com/favicon.ico" />';
  106. $this->fixture->setFavIcon('http://google.com/favicon.ico');
  107. $out = $this->fixture->render();
  108. $this->assertContains(
  109. $expectedReturnValue1,
  110. $out
  111. );
  112. $this->assertContains(
  113. $expectedReturnValue2,
  114. $out
  115. );
  116. }
  117. /**
  118. * test set baseUrl
  119. *
  120. */
  121. public function testSetBaseUrl() {
  122. $expectedReturnValue = '<base href="http://ggogle.com/" />';
  123. $this->fixture->setBaseUrl('http://ggogle.com/');
  124. $out = $this->fixture->render();
  125. $this->assertContains(
  126. $expectedReturnValue,
  127. $out
  128. );
  129. }
  130. /**
  131. * test add meta tag
  132. *
  133. */
  134. public function testAddMetaTag() {
  135. $expectedReturnValue = '<meta name="author" content="Anna Lyse">';
  136. $this->fixture->addMetaTag('<meta name="author" content="Anna Lyse">');
  137. $out = $this->fixture->render();
  138. $this->assertContains(
  139. $expectedReturnValue,
  140. $out
  141. );
  142. }
  143. /**
  144. * test add inline comment
  145. *
  146. */
  147. public function testAddInlineComment() {
  148. $expectedReturnValue = 'this is an inline comment written by unit test';
  149. $this->fixture->addInlineComment('this is an inline comment written by unit test');
  150. $out = $this->fixture->render();
  151. $this->assertContains(
  152. $expectedReturnValue,
  153. $out
  154. );
  155. }
  156. /**
  157. * test add header data
  158. *
  159. */
  160. public function testAddHeaderData() {
  161. $expectedReturnValue = '<tag method="private" name="test" />';
  162. $this->fixture->addHeaderData('<tag method="private" name="test" />');
  163. $out = $this->fixture->render();
  164. $this->assertContains(
  165. $expectedReturnValue,
  166. $out
  167. );
  168. }
  169. /**
  170. * test add footer data
  171. *
  172. */
  173. public function testAddFooterData() {
  174. $expectedReturnValue = '<tag method="private" name="test" />';
  175. $this->fixture->addFooterData('<tag method="private" name="test" />');
  176. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  177. $this->assertContains(
  178. $expectedReturnValue,
  179. $out
  180. );
  181. }
  182. /**
  183. * test add JS library file
  184. *
  185. */
  186. public function testAddJsLibrary() {
  187. $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  188. $this->fixture->addJsLibrary('test', 'fileadmin/test.js');
  189. $out = $this->fixture->render();
  190. $this->assertRegExp(
  191. $expectedRegExp,
  192. $out
  193. );
  194. }
  195. /**
  196. * test add JS footer library file
  197. *
  198. */
  199. public function testAddJsFooterLibrary() {
  200. $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  201. $this->fixture->addJsFooterLibrary('test', 'fileadmin/test.js');
  202. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  203. $this->assertRegExp(
  204. $expectedRegExp,
  205. $out
  206. );
  207. }
  208. /**
  209. * test add JS file
  210. *
  211. */
  212. public function testAddJsFile() {
  213. $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  214. $this->fixture->addJsFile('fileadmin/test.js');
  215. $out = $this->fixture->render();
  216. $this->assertRegExp(
  217. $expectedRegExp,
  218. $out
  219. );
  220. }
  221. /**
  222. * test add JS file for footer
  223. *
  224. */
  225. public function testAddJsFooterFile() {
  226. $expectedRegExp = '#<script src="fileadmin/test\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  227. $this->fixture->addJsFooterFile('fileadmin/test.js');
  228. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  229. $this->assertRegExp(
  230. $expectedRegExp,
  231. $out
  232. );
  233. }
  234. /**
  235. * test add JS inline
  236. *
  237. */
  238. public function testAddJsInlineCode() {
  239. $expectedReturnValue = 'var x = "testvar"';
  240. $this->fixture->addJsInlineCode('test', 'var x = "testvar"');
  241. $out = $this->fixture->render();
  242. $this->assertContains(
  243. $expectedReturnValue,
  244. $out
  245. );
  246. }
  247. /**
  248. * test add JS inline for footer
  249. *
  250. */
  251. public function testAddJsFooterInlineCode() {
  252. $expectedReturnValue = 'var x = "testvar"';
  253. $this->fixture->addJsFooterInlineCode('test', 'var x = "testvar"');
  254. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  255. $this->assertContains(
  256. $expectedReturnValue,
  257. $out
  258. );
  259. }
  260. /**
  261. * test add JS handler
  262. *
  263. */
  264. public function testAddExtOnReadyCode() {
  265. $expectedReturnValue1 = 'Ext.onReady(function() {';
  266. $expectedReturnValue2 = 'var x = "testvar";';
  267. $this->fixture->loadExtJS();
  268. $this->fixture->addExtOnReadyCode('var x = "testvar";');
  269. $out = $this->fixture->render();
  270. $this->assertContains(
  271. $expectedReturnValue1,
  272. $out
  273. );
  274. $this->assertContains(
  275. $expectedReturnValue2,
  276. $out
  277. );
  278. }
  279. /**
  280. * test add CSS file
  281. *
  282. */
  283. public function testAddCssFile() {
  284. $expectedReturnValue = '<link rel="stylesheet" type="text/css" href="fileadmin/test.css" media="all" />';
  285. $this->fixture->addCssFile('fileadmin/test.css');
  286. $out = $this->fixture->render();
  287. $this->assertContains(
  288. $expectedReturnValue,
  289. $out
  290. );
  291. }
  292. /**
  293. * test add CSS inline
  294. *
  295. */
  296. public function testAddCssInlineBlock() {
  297. $expectedReturnValue = 'body {margin:20px;}';
  298. $this->fixture->addCssInlineBlock('general', 'body {margin:20px;}');
  299. $out = $this->fixture->render();
  300. $this->assertContains(
  301. $expectedReturnValue,
  302. $out
  303. );
  304. }
  305. /**
  306. * test add CSS inline and force on top
  307. *
  308. */
  309. public function testAddCssInlineBlockForceOnTop() {
  310. $expectedReturnValue = '/*general1*/' . LF . 'h1 {margin:20px;}' . LF . '/*general*/' . LF . 'body {margin:20px;}';
  311. $this->fixture->addCssInlineBlock('general', 'body {margin:20px;}');
  312. $this->fixture->addCssInlineBlock('general1', 'h1 {margin:20px;}', NULL, TRUE);
  313. $out = $this->fixture->render();
  314. $this->assertContains(
  315. $expectedReturnValue,
  316. $out
  317. );
  318. }
  319. /**
  320. * test load prototype
  321. *
  322. */
  323. public function testLoadPrototype() {
  324. $expectedRegExp = '#<script src="contrib/prototype/prototype\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  325. $this->fixture->loadPrototype();
  326. $out = $this->fixture->render();
  327. $this->assertRegExp(
  328. $expectedRegExp,
  329. $out
  330. );
  331. }
  332. /**
  333. * test load Scriptaculous
  334. *
  335. */
  336. public function testLoadScriptaculous() {
  337. $this->fixture->loadScriptaculous('slider,controls');
  338. $out = $this->fixture->render();
  339. $this->assertContains(
  340. '<script src="contrib/scriptaculous/scriptaculous.js" type="text/javascript"></script>',
  341. $out
  342. );
  343. $this->assertContains(
  344. '<script src="contrib/scriptaculous/effects.js" type="text/javascript"></script>',
  345. $out
  346. );
  347. $this->assertContains(
  348. '<script src="contrib/scriptaculous/controls.js" type="text/javascript"></script>',
  349. $out
  350. );
  351. $this->assertContains(
  352. '<script src="contrib/scriptaculous/slider.js" type="text/javascript"></script>',
  353. $out
  354. );
  355. }
  356. /**
  357. * Tests whether scriptaculous is loaded correctly when compression is enabled.
  358. *
  359. * @test
  360. */
  361. public function isScriptaculousLoadedCompressed() {
  362. $this->fixture->loadScriptaculous('slider,controls');
  363. $this->fixture->enableCompressJavascript();
  364. $out = $this->fixture->render();
  365. $this->assertRegExp(
  366. '#<script src="[^"]*/typo3temp/compressor/scriptaculous-[a-f0-9]+.js" type="text/javascript"></script>#',
  367. $out
  368. );
  369. $this->assertRegExp(
  370. '#<script src="[^"]*/typo3temp/compressor/effects-[a-f0-9]+.js" type="text/javascript"></script>#',
  371. $out
  372. );
  373. $this->assertRegExp(
  374. '#<script src="[^"]*/typo3temp/compressor/controls-[a-f0-9]+.js" type="text/javascript"></script>#',
  375. $out
  376. );
  377. $this->assertRegExp(
  378. '#<script src="[^"]*/typo3temp/compressor/slider-[a-f0-9]+.js" type="text/javascript"></script>#',
  379. $out
  380. );
  381. }
  382. /**
  383. * test load ExtJS
  384. *
  385. */
  386. public function testLoadExtJS() {
  387. $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';
  388. $this->fixture->loadExtJS(TRUE, TRUE, 'jquery');
  389. $out = $this->fixture->render();
  390. $this->assertRegExp(
  391. $expectedRegExp,
  392. $out
  393. );
  394. }
  395. /**
  396. * test load ExtCore
  397. *
  398. */
  399. public function testLoadExtCore() {
  400. $expectedRegExp = '#<script src="contrib/extjs/ext-core\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  401. $this->fixture->loadExtCore();
  402. $out = $this->fixture->render();
  403. $this->assertRegExp(
  404. $expectedRegExp,
  405. $out
  406. );
  407. }
  408. /**
  409. * test enable ExtJsDebug
  410. *
  411. */
  412. public function testEnableExtJsDebug() {
  413. $expectedRegExp = '#<script src="contrib/extjs/ext-all-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  414. $this->fixture->loadExtJS(TRUE, TRUE, 'jquery');
  415. $this->fixture->enableExtJsDebug();
  416. $out = $this->fixture->render();
  417. $this->assertRegExp(
  418. $expectedRegExp,
  419. $out
  420. );
  421. }
  422. /**
  423. * test enable ExtCoreDebug
  424. *
  425. */
  426. public function testEnableExtCoreDebug() {
  427. $expectedRegExp = '#<script src="contrib/extjs/ext-core-debug\.(js|\d+\.js|js\?\d+)" type="text/javascript"></script>#';
  428. $this->fixture->loadExtCore();
  429. $this->fixture->enableExtCoreDebug();
  430. $out = $this->fixture->render();
  431. $this->assertRegExp(
  432. $expectedRegExp,
  433. $out
  434. );
  435. }
  436. /**
  437. * test inline language label
  438. *
  439. */
  440. public function testAddInlineLanguageLabel() {
  441. $expectedReturnValue = 'TYPO3.lang = {"myKey":"myValue"}';
  442. $this->fixture->loadExtJS();
  443. $this->fixture->addInlineLanguageLabel('myKey', 'myValue');
  444. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  445. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  446. $this->assertContains(
  447. $expectedReturnValue,
  448. $out
  449. );
  450. }
  451. /**
  452. * test inline language label as array
  453. *
  454. */
  455. public function testAddInlineLanguageLabelArray() {
  456. $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
  457. $this->fixture->loadExtJS();
  458. $this->fixture->addInlineLanguageLabelArray(array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
  459. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  460. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  461. $this->assertContains(
  462. $expectedReturnValue,
  463. $out
  464. );
  465. }
  466. /**
  467. * test inline language label as array get merged
  468. *
  469. */
  470. public function testAddInlineLanguageLabelArrayMerged() {
  471. $expectedReturnValue = 'TYPO3.lang = {"myKey1":"myValue1","myKey2":"myValue2"}';
  472. $this->fixture->loadExtJS();
  473. $this->fixture->addInlineLanguageLabelArray(array('myKey1' => 'myValue1',));
  474. $this->fixture->addInlineLanguageLabelArray(array('myKey2' => 'myValue2',));
  475. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  476. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  477. $this->assertContains(
  478. $expectedReturnValue,
  479. $out
  480. );
  481. }
  482. /**
  483. * test inline setting
  484. *
  485. */
  486. public function testAddInlineSetting() {
  487. $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey":"myValue"}};';
  488. $this->fixture->loadExtJS();
  489. $this->fixture->addInlineSetting('myApp', 'myKey', 'myValue');
  490. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  491. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  492. $this->assertContains(
  493. $expectedReturnValue,
  494. $out
  495. );
  496. }
  497. /**
  498. * test inline settings with array
  499. *
  500. */
  501. public function testAddInlineSettingArray() {
  502. $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
  503. $this->fixture->loadExtJS();
  504. $this->fixture->addInlineSettingArray('myApp', array('myKey1' => 'myValue1', 'myKey2' => 'myValue2',));
  505. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  506. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  507. $this->assertContains(
  508. $expectedReturnValue,
  509. $out
  510. );
  511. }
  512. /**
  513. * test inline settings with array get merged
  514. *
  515. */
  516. public function testAddInlineSettingArrayMerged() {
  517. $expectedReturnValue = 'TYPO3.settings = {"myApp":{"myKey1":"myValue1","myKey2":"myValue2"}};';
  518. $this->fixture->loadExtJS();
  519. $this->fixture->addInlineSettingArray('myApp', array('myKey1' => 'myValue1',));
  520. $this->fixture->addInlineSettingArray('myApp', array('myKey2' => 'myValue2',));
  521. $out = $this->fixture->enableMoveJsFromHeaderToFooter();
  522. $out = $this->fixture->render(t3lib_PageRenderer::PART_FOOTER);
  523. $this->assertContains(
  524. $expectedReturnValue,
  525. $out
  526. );
  527. }
  528. /**
  529. * test add body content
  530. *
  531. */
  532. public function testAddBodyContent() {
  533. $expectedReturnValue = 'ABCDE';
  534. $this->fixture->addBodyContent('A');
  535. $this->fixture->addBodyContent('B');
  536. $this->fixture->addBodyContent('C');
  537. $this->fixture->addBodyContent('D');
  538. $this->fixture->addBodyContent('E');
  539. $out = $this->fixture->getBodyContent();
  540. $this->assertEquals(
  541. $expectedReturnValue,
  542. $out
  543. );
  544. }
  545. /**
  546. * test set body content
  547. *
  548. */
  549. public function testSetBodyContent() {
  550. $expectedReturnValue = 'ABCDE';
  551. $this->fixture->setBodyContent('ABCDE');
  552. $out = $this->fixture->getBodyContent();
  553. $this->assertEquals(
  554. $expectedReturnValue,
  555. $out
  556. );
  557. $out = $this->fixture->render();
  558. $this->assertContains(
  559. $expectedReturnValue,
  560. $out
  561. );
  562. }
  563. /**
  564. * Tests whether labels are delivered in a non-UTF-8 context.
  565. * (background: json_encode() requires UTF-8 to work properly)
  566. *
  567. * @test
  568. */
  569. public function isInlineLanguageLabelDeliveredWithNonUTF8() {
  570. $testPrefix = uniqid('test');
  571. $this->fixture->loadExtCore();
  572. $this->fixture->setCharSet('iso-8859-1');
  573. $this->fixture->addInlineLanguageLabel($testPrefix, $testPrefix . "_\xd8");
  574. $out = $this->fixture->render();
  575. $this->assertContains($testPrefix . '_\\u00d8', $out);
  576. }
  577. }
  578. ?>