PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/tests/HTMLPurifier/HTMLDefinitionTest.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 347 lines | 265 code | 74 blank | 8 comment | 4 complexity | f91b51d53e3cab3bb104df5b741c10c7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. class HTMLPurifier_HTMLDefinitionTest extends HTMLPurifier_Harness
  3. {
  4. function expectError($error = false, $message = '%s')
  5. {
  6. // Because we're testing a definition, it's vital that the cache
  7. // is turned off for tests that expect errors.
  8. $this->config->set('Cache.DefinitionImpl', null);
  9. parent :: expectError($error);
  10. }
  11. function test_parseTinyMCEAllowedList()
  12. {
  13. $def = new HTMLPurifier_HTMLDefinition();
  14. // note: this is case-sensitive, but its config schema
  15. // counterpart is not. This is generally a good thing for users,
  16. // but it's a slight internal inconsistency
  17. $this->assertEqual($def->parseTinyMCEAllowedList(''), array(array(), array()));
  18. $this->assertEqual($def->parseTinyMCEAllowedList('a,b,c'), array(array('a' => true, 'b' => true, 'c' => true),
  19. array()));
  20. $this->assertEqual($def->parseTinyMCEAllowedList('a[x|y|z]'), array(array('a' => true),
  21. array('a.x' => true, 'a.y' => true, 'a.z' => true)));
  22. $this->assertEqual($def->parseTinyMCEAllowedList('*[id]'), array(array(), array('*.id' => true)));
  23. $this->assertEqual($def->parseTinyMCEAllowedList('a[*]'), array(array('a' => true), array('a.*' => true)));
  24. $this->assertEqual($def->parseTinyMCEAllowedList('span[style],strong,a[href|title]'), array(
  25. array('span' => true, 'strong' => true, 'a' => true),
  26. array('span.style' => true, 'a.href' => true, 'a.title' => true)));
  27. $this->assertEqual(// alternate form:
  28. $def->parseTinyMCEAllowedList('span[style]
  29. strong
  30. a[href|title]
  31. '), $val = array(array('span' => true, 'strong' => true, 'a' => true),
  32. array('span.style' => true, 'a.href' => true, 'a.title' => true)));
  33. $this->assertEqual($def->parseTinyMCEAllowedList(' span [ style ], strong' . "\n\t" . 'a[href | title]'), $val);
  34. }
  35. function test_Allowed()
  36. {
  37. $config1 = HTMLPurifier_Config :: create(array('HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
  38. 'HTML.AllowedAttributes' => array('a@href', '*@id')));
  39. $config2 = HTMLPurifier_Config :: create(array('HTML.Allowed' => 'b,i,p,a[href],*[id]'));
  40. $this->assertEqual($config1->getHTMLDefinition(), $config2->getHTMLDefinition());
  41. }
  42. function assertPurification_AllowedElements_p()
  43. {
  44. $this->assertPurification('<p><b>Jelly</b></p>', '<p>Jelly</p>');
  45. }
  46. function test_AllowedElements()
  47. {
  48. $this->config->set('HTML.AllowedElements', 'p');
  49. $this->assertPurification_AllowedElements_p();
  50. }
  51. function test_AllowedElements_multiple()
  52. {
  53. $this->config->set('HTML.AllowedElements', 'p,div');
  54. $this->assertPurification('<div><p><b>Jelly</b></p></div>', '<div><p>Jelly</p></div>');
  55. }
  56. function test_AllowedElements_invalidElement()
  57. {
  58. $this->config->set('HTML.AllowedElements', 'obviously_invalid,p');
  59. $this->expectError(new PatternExpectation("/Element 'obviously_invalid' is not supported/"));
  60. $this->assertPurification_AllowedElements_p();
  61. }
  62. function test_AllowedElements_invalidElement_xssAttempt()
  63. {
  64. $this->config->set('HTML.AllowedElements', '<script>,p');
  65. $this->expectError(new PatternExpectation("/Element '&lt;script&gt;' is not supported/"));
  66. $this->assertPurification_AllowedElements_p();
  67. }
  68. function test_AllowedElements_multipleInvalidElements()
  69. {
  70. $this->config->set('HTML.AllowedElements', 'dr-wiggles,dr-pepper,p');
  71. $this->expectError(new PatternExpectation("/Element 'dr-wiggles' is not supported/"));
  72. $this->expectError(new PatternExpectation("/Element 'dr-pepper' is not supported/"));
  73. $this->assertPurification_AllowedElements_p();
  74. }
  75. function assertPurification_AllowedAttributes_global_style()
  76. {
  77. $this->assertPurification('<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />', '<p style="font-weight:bold;">Jelly</p><br style="clear:both;" />');
  78. }
  79. function test_AllowedAttributes_global_preferredSyntax()
  80. {
  81. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  82. $this->config->set('HTML.AllowedAttributes', 'style');
  83. $this->assertPurification_AllowedAttributes_global_style();
  84. }
  85. function test_AllowedAttributes_global_verboseSyntax()
  86. {
  87. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  88. $this->config->set('HTML.AllowedAttributes', '*@style');
  89. $this->assertPurification_AllowedAttributes_global_style();
  90. }
  91. function test_AllowedAttributes_global_discouragedSyntax()
  92. {
  93. // Emit errors eventually
  94. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  95. $this->config->set('HTML.AllowedAttributes', '*.style');
  96. $this->assertPurification_AllowedAttributes_global_style();
  97. }
  98. function assertPurification_AllowedAttributes_local_p_style()
  99. {
  100. $this->assertPurification('<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />', '<p style="font-weight:bold;">Jelly</p><br />');
  101. }
  102. function test_AllowedAttributes_local_preferredSyntax()
  103. {
  104. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  105. $this->config->set('HTML.AllowedAttributes', 'p@style');
  106. $this->assertPurification_AllowedAttributes_local_p_style();
  107. }
  108. function test_AllowedAttributes_local_discouragedSyntax()
  109. {
  110. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  111. $this->config->set('HTML.AllowedAttributes', 'p.style');
  112. $this->assertPurification_AllowedAttributes_local_p_style();
  113. }
  114. function test_AllowedAttributes_multiple()
  115. {
  116. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  117. $this->config->set('HTML.AllowedAttributes', 'p@style,br@class,title');
  118. $this->assertPurification('<p style="font-weight:bold;" class="foo" title="foo">Jelly</p><br style="clear:both;" class="foo" title="foo" />', '<p style="font-weight:bold;" title="foo">Jelly</p><br class="foo" title="foo" />');
  119. }
  120. function test_AllowedAttributes_local_invalidAttribute()
  121. {
  122. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  123. $this->config->set('HTML.AllowedAttributes', array('p@style', 'p@<foo>'));
  124. $this->expectError(new PatternExpectation("/Attribute '&lt;foo&gt;' in element 'p' not supported/"));
  125. $this->assertPurification_AllowedAttributes_local_p_style();
  126. }
  127. function test_AllowedAttributes_global_invalidAttribute()
  128. {
  129. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  130. $this->config->set('HTML.AllowedAttributes', array('style', '<foo>'));
  131. $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
  132. $this->assertPurification_AllowedAttributes_global_style();
  133. }
  134. function test_AllowedAttributes_local_invalidAttributeDueToMissingElement()
  135. {
  136. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  137. $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style');
  138. $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
  139. $this->assertPurification_AllowedAttributes_local_p_style();
  140. }
  141. function test_AllowedAttributes_duplicate()
  142. {
  143. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  144. $this->config->set('HTML.AllowedAttributes', 'p.style,p@style');
  145. $this->assertPurification_AllowedAttributes_local_p_style();
  146. }
  147. function test_AllowedAttributes_multipleErrors()
  148. {
  149. $this->config->set('HTML.AllowedElements', array('p', 'br'));
  150. $this->config->set('HTML.AllowedAttributes', 'p.style,foo.style,<foo>');
  151. $this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
  152. $this->expectError(new PatternExpectation("/Global attribute '&lt;foo&gt;' is not supported in any elements/"));
  153. $this->assertPurification_AllowedAttributes_local_p_style();
  154. }
  155. function test_ForbiddenElements()
  156. {
  157. $this->config->set('HTML.ForbiddenElements', 'b');
  158. $this->assertPurification('<b>b</b><i>i</i>', 'b<i>i</i>');
  159. }
  160. function test_ForbiddenElements_invalidElement()
  161. {
  162. $this->config->set('HTML.ForbiddenElements', 'obviously_incorrect');
  163. // no error!
  164. $this->assertPurification('<i>i</i>');
  165. }
  166. function assertPurification_ForbiddenAttributes_b_style()
  167. {
  168. $this->assertPurification('<b style="float:left;">b</b><i style="float:left;">i</i>', '<b>b</b><i style="float:left;">i</i>');
  169. }
  170. function test_ForbiddenAttributes()
  171. {
  172. $this->config->set('HTML.ForbiddenAttributes', 'b@style');
  173. $this->assertPurification_ForbiddenAttributes_b_style();
  174. }
  175. function test_ForbiddenAttributes_incorrectSyntax()
  176. {
  177. $this->config->set('HTML.ForbiddenAttributes', 'b.style');
  178. $this->expectError("Error with b.style: tag.attr syntax not supported for HTML.ForbiddenAttributes; use tag@attr instead");
  179. $this->assertPurification('<b style="float:left;">Test</b>');
  180. }
  181. function test_ForbiddenAttributes_incorrectGlobalSyntax()
  182. {
  183. $this->config->set('HTML.ForbiddenAttributes', '*.style');
  184. $this->expectError("Error with *.style: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead");
  185. $this->assertPurification('<b style="float:left;">Test</b>');
  186. }
  187. function assertPurification_ForbiddenAttributes_style()
  188. {
  189. $this->assertPurification('<b class="foo" style="float:left;">b</b><i style="float:left;">i</i>', '<b class="foo">b</b><i>i</i>');
  190. }
  191. function test_ForbiddenAttributes_global()
  192. {
  193. $this->config->set('HTML.ForbiddenAttributes', 'style');
  194. $this->assertPurification_ForbiddenAttributes_style();
  195. }
  196. function test_ForbiddenAttributes_globalVerboseFormat()
  197. {
  198. $this->config->set('HTML.ForbiddenAttributes', '*@style');
  199. $this->assertPurification_ForbiddenAttributes_style();
  200. }
  201. function test_addAttribute()
  202. {
  203. $config = HTMLPurifier_Config :: createDefault();
  204. $def = $config->getHTMLDefinition(true);
  205. $def->addAttribute('span', 'custom', 'Enum#attribute');
  206. $purifier = new HTMLPurifier($config);
  207. $input = '<span custom="attribute">Custom!</span>';
  208. $output = $purifier->purify($input);
  209. $this->assertIdentical($input, $output);
  210. }
  211. function test_addAttribute_multiple()
  212. {
  213. $config = HTMLPurifier_Config :: createDefault();
  214. $def = $config->getHTMLDefinition(true);
  215. $def->addAttribute('span', 'custom', 'Enum#attribute');
  216. $def->addAttribute('span', 'foo', 'Text');
  217. $purifier = new HTMLPurifier($config);
  218. $input = '<span custom="attribute" foo="asdf">Custom!</span>';
  219. $output = $purifier->purify($input);
  220. $this->assertIdentical($input, $output);
  221. }
  222. function test_addElement()
  223. {
  224. $config = HTMLPurifier_Config :: createDefault();
  225. $def = $config->getHTMLDefinition(true);
  226. $def->addElement('marquee', 'Inline', 'Inline', 'Common', array('width' => 'Length'));
  227. $purifier = new HTMLPurifier($config);
  228. $input = '<span><marquee width="50">Foobar</marquee></span>';
  229. $output = $purifier->purify($input);
  230. $this->assertIdentical($input, $output);
  231. }
  232. function test_injector()
  233. {
  234. generate_mock_once('HTMLPurifier_Injector');
  235. $injector = new HTMLPurifier_InjectorMock();
  236. $injector->name = 'MyInjector';
  237. $injector->setReturnValue('checkNeeded', false);
  238. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  239. $module->info_injector[] = $injector;
  240. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector, array('MyInjector' => $injector));
  241. }
  242. function test_injectorMissingNeeded()
  243. {
  244. generate_mock_once('HTMLPurifier_Injector');
  245. $injector = new HTMLPurifier_InjectorMock();
  246. $injector->name = 'MyInjector';
  247. $injector->setReturnValue('checkNeeded', 'a');
  248. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  249. $module->info_injector[] = $injector;
  250. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector, array());
  251. }
  252. function test_injectorIntegration()
  253. {
  254. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  255. $module->info_injector[] = 'Linkify';
  256. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector, array(
  257. 'Linkify' => new HTMLPurifier_Injector_Linkify()));
  258. }
  259. function test_injectorIntegrationFail()
  260. {
  261. $this->config->set('HTML.Allowed', 'p');
  262. $module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
  263. $module->info_injector[] = 'Linkify';
  264. $this->assertIdentical($this->config->getHTMLDefinition()->info_injector, array());
  265. }
  266. function test_notAllowedRequiredAttributeError()
  267. {
  268. $this->expectError("Required attribute 'src' in element 'img' was not allowed, which means 'img' will not be allowed either");
  269. $this->config->set('HTML.Allowed', 'img[alt]');
  270. $this->config->getHTMLDefinition();
  271. }
  272. }
  273. // vim: et sw=4 sts=4