/vendors/phptal/tests/EscapeHTMLTest.php

https://github.com/GunioRobot/CakePHP-TALTAL · PHP · 200 lines · 156 code · 30 blank · 14 comment · 0 complexity · 8e19201c589ec864e21693660b37a534 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPTAL templating engine
  4. *
  5. * PHP Version 5
  6. *
  7. * @category HTML
  8. * @package PHPTAL
  9. * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
  10. * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
  11. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  12. * @version SVN: $Id: EscapeHTMLTest.php 888 2010-06-08 09:48:33Z kornel $
  13. * @link http://phptal.org/
  14. */
  15. class EscapeHTMLTest extends PHPTAL_TestCase {
  16. private function executeString($str, $params = array())
  17. {
  18. $tpl = $this->newPHPTAL();
  19. foreach ($params as $k => $v) $tpl->set($k, $v);
  20. $tpl->setSource($str);
  21. return $tpl->execute();
  22. }
  23. function testDoesEscapeHTMLContent(){
  24. $tpl = $this->newPHPTAL('input/escape.html');
  25. $exp = normalize_html_file('output/escape.html');
  26. $res = normalize_html($tpl->execute());
  27. $this->assertEquals($exp, $res);
  28. }
  29. function testEntityDecodingPath1()
  30. {
  31. $res = $this->executeString('<div title="&quot;" class=\'&quot;\' tal:content="\'&quot; quote character\'" />');
  32. $this->assertNotContains('&amp;', $res);
  33. }
  34. function testEntityDecodingBeforePHP()
  35. {
  36. /* PHP block in attributes gets raw input (that's not XML style, but PHP style) */
  37. $res = $this->executeString('<div title="${php:strlen(\'&quot;&amp;\')}" class="<?php echo strlen(\'&quot;&amp;\')?>">'.
  38. '<tal:block tal:content="php:strlen(\'&quot;&amp;\')" />,${php:strlen(\'&quot;&amp;\')}</div>');
  39. $this->assertEquals('<div title="2" class="11">2,2</div>', $res);
  40. }
  41. function testEntityEncodingAfterPHP()
  42. {
  43. $res = $this->executeString('<div title="${php:urldecode(\'%26%22%3C\')}"><tal:block tal:content="php:urldecode(\'%26%22%3C\')" />,${php:urldecode(\'%26%22%3C\')}</div>');
  44. $this->assertEquals('<div title="&amp;&quot;&lt;">&amp;&quot;&lt;,&amp;&quot;&lt;</div>', $res);
  45. }
  46. function testNoEntityEncodingAfterStructurePHP()
  47. {
  48. $res = $this->executeString('<div title="${structure php:urldecode(\'%26%20%3E%27\')}" class="<?php echo urldecode(\'%26%20%3E%27\')?>">'.
  49. '<tal:block tal:content="structure php:urldecode(\'%26%20%3E%22\')" />,${structure php:urldecode(\'%26%20%3E%22\')},<?php echo urldecode(\'%26%20%3E%22\')?></div>');
  50. $this->assertEquals('<div title="& >\'" class="& >\'">& >",& >",& >"</div>', $res);
  51. }
  52. function testDecodingBeforeStructure()
  53. {
  54. $res = $this->executeString('<div tal:content="structure php:\'&amp; quote character\'" />');
  55. $this->assertNotContains('&amp;', $res);
  56. }
  57. function testEntityDecodingPHP1()
  58. {
  59. $res = $this->executeString('<div tal:content="php:\'&quot; quote character\'" />');
  60. $this->assertNotContains('&amp;', $res);
  61. }
  62. function testEntityDecodingPath2()
  63. {
  64. $res = $this->executeString('<div tal:attributes="title \'&quot; quote character\'" />');
  65. $this->assertNotContains('&amp;', $res);
  66. }
  67. function testEntityDecodingPHP2()
  68. {
  69. $res = $this->executeString('<div tal:attributes="title php:\'&quot; quote character\'" />');
  70. $this->assertNotContains('&amp;', $res);
  71. }
  72. function testEntityDecodingPath3()
  73. {
  74. $res = $this->executeString('<p>${\'&quot; quote character\'}</p>');
  75. $this->assertNotContains('&amp;', $res);
  76. }
  77. function testEntityDecodingPHP3()
  78. {
  79. $res = $this->executeString('<p>${php:\'&quot; quote character\'}</p>');
  80. $this->assertNotContains('&amp;', $res);
  81. }
  82. function testEntityEncodingPath1()
  83. {
  84. $res = $this->executeString('<div tal:content="\'&amp; ampersand character\'" />');
  85. $this->assertContains('&amp;', $res);
  86. $this->assertNotContains('&amp;amp;', $res);
  87. $this->assertNotContains('&amp;&amp;', $res);
  88. }
  89. function testEntityEncodingPHP1()
  90. {
  91. $res = $this->executeString('<div tal:content="php:\'&amp; ampersand character\'" />');
  92. $this->assertContains('&amp;', $res);
  93. $this->assertNotContains('&amp;amp;', $res);
  94. $this->assertNotContains('&amp;&amp;', $res);
  95. }
  96. function testEntityEncodingPath2()
  97. {
  98. $res = $this->executeString('<div tal:attributes="title \'&amp; ampersand character\'" />');
  99. $this->assertContains('&amp;', $res);
  100. $this->assertNotContains('&amp;amp;', $res);
  101. $this->assertNotContains('&amp;&amp;', $res);
  102. }
  103. function testEntityEncodingVariables()
  104. {
  105. $res = $this->executeString('<div tal:attributes="title variable; class variable">${variable}${php:variable}</div>',
  106. array('variable'=>'& = ampersand, " = quote, \' = apostrophe'));
  107. $this->assertContains('&amp;',$res);
  108. $this->assertNotContains('&amp;amp;',$res);
  109. $this->assertNotContains('&amp;&amp;',$res);
  110. }
  111. function testEntityEncodingAttributesDefault1()
  112. {
  113. $res = $this->executeString('<div tal:attributes="title idontexist | default" title=\'&amp; ampersand character\' />');
  114. $this->assertContains('&amp;', $res);
  115. $this->assertNotContains('&amp;amp;', $res);
  116. $this->assertNotContains('&amp;&amp;', $res);
  117. }
  118. function testEntityEncodingAttributesDefault2()
  119. {
  120. $res = $this->executeString('<div tal:attributes="title idontexist | default" title=\'&quot;&apos;\' />');
  121. $this->assertNotContains('&amp;', $res);
  122. $this->assertContains('&quot;', $res); // or apos...
  123. }
  124. function testEntityEncodingPHP2()
  125. {
  126. $res = $this->executeString('<div tal:attributes="title php:\'&amp; ampersand character\'" />');
  127. $this->assertContains('&amp;', $res);
  128. $this->assertNotContains('&amp;amp;', $res);
  129. $this->assertNotContains('&amp;&amp;', $res);
  130. }
  131. function testEntityEncodingPath3()
  132. {
  133. $res = $this->executeString('<p>${\'&amp; ampersand character\'}</p>');
  134. $this->assertContains('&amp;', $res);
  135. $this->assertNotContains('&amp;amp;', $res);
  136. $this->assertNotContains('&amp;&amp;', $res);
  137. }
  138. function testEntityEncodingPHP3()
  139. {
  140. $res = $this->executeString('<p>&{php:\'&amp; ampersand character\'}</p>');
  141. $this->assertContains('&amp;', $res);
  142. $this->assertNotContains('&amp;amp;', $res);
  143. $this->assertNotContains('&amp;&amp;', $res);
  144. }
  145. function testSimpleXML()
  146. {
  147. $tpl = $this->newPHPTAL();
  148. $tpl->setSource('<p>${x} ${y}</p>');
  149. $simplexml = new SimpleXMLElement('<foo title="bar&amp;&lt;" empty="">foo&amp;&lt;</foo>');
  150. $tpl->x = $simplexml['title'];
  151. $tpl->y = $simplexml['empty'];
  152. $this->assertEquals('<p>bar&amp;&lt; </p>', $tpl->execute());
  153. }
  154. function testStructureSimpleXML()
  155. {
  156. $tpl = $this->newPHPTAL();
  157. $tpl->setSource('<p>${structure x} ${structure y}</p>');
  158. $simplexml = new SimpleXMLElement('<foo title="bar&amp;&lt;" empty="">foo&amp;&lt;</foo>');
  159. $tpl->x = $simplexml['title'];
  160. $tpl->y = $simplexml['empty'];
  161. $this->assertEquals('<p>bar&< </p>', $tpl->execute());
  162. }
  163. function testUnicodeUnescaped()
  164. {
  165. $tpl = $this->newPHPTAL();
  166. $tpl->World = '${World}'; // a quine! ;)
  167. $tpl->setSource($src = '<p>Hello “${World}!”</p>');
  168. $this->assertEquals($src, $tpl->execute());
  169. }
  170. }