TidyTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. Mock::generatePartial(
  3. 'HTMLPurifier_HTMLModule_Tidy',
  4. 'HTMLPurifier_HTMLModule_Tidy_TestForConstruct',
  5. array('makeFixes', 'makeFixesForLevel', 'populate')
  6. );
  7. class HTMLPurifier_HTMLModule_TidyTest extends HTMLPurifier_Harness
  8. {
  9. function test_getFixesForLevel() {
  10. $module = new HTMLPurifier_HTMLModule_Tidy();
  11. $module->fixesForLevel['light'][] = 'light-fix';
  12. $module->fixesForLevel['medium'][] = 'medium-fix';
  13. $module->fixesForLevel['heavy'][] = 'heavy-fix';
  14. $this->assertIdentical(
  15. array(),
  16. $module->getFixesForLevel('none')
  17. );
  18. $this->assertIdentical(
  19. array('light-fix' => true),
  20. $module->getFixesForLevel('light')
  21. );
  22. $this->assertIdentical(
  23. array('light-fix' => true, 'medium-fix' => true),
  24. $module->getFixesForLevel('medium')
  25. );
  26. $this->assertIdentical(
  27. array('light-fix' => true, 'medium-fix' => true, 'heavy-fix' => true),
  28. $module->getFixesForLevel('heavy')
  29. );
  30. $this->expectError('Tidy level turbo not recognized');
  31. $module->getFixesForLevel('turbo');
  32. }
  33. function test_setup() {
  34. $i = 0; // counter, helps us isolate expectations
  35. // initialize partial mock
  36. $module = new HTMLPurifier_HTMLModule_Tidy_TestForConstruct();
  37. $module->fixesForLevel['light'] = array('light-fix-1', 'light-fix-2');
  38. $module->fixesForLevel['medium'] = array('medium-fix-1', 'medium-fix-2');
  39. $module->fixesForLevel['heavy'] = array('heavy-fix-1', 'heavy-fix-2');
  40. $j = 0;
  41. $fixes = array(
  42. 'light-fix-1' => $lf1 = $j++,
  43. 'light-fix-2' => $lf2 = $j++,
  44. 'medium-fix-1' => $mf1 = $j++,
  45. 'medium-fix-2' => $mf2 = $j++,
  46. 'heavy-fix-1' => $hf1 = $j++,
  47. 'heavy-fix-2' => $hf2 = $j++
  48. );
  49. $module->setReturnValue('makeFixes', $fixes);
  50. $config = HTMLPurifier_Config::create(array(
  51. 'HTML.TidyLevel' => 'none'
  52. ));
  53. $module->expectAt($i++, 'populate', array(array()));
  54. $module->setup($config);
  55. // basic levels
  56. $config = HTMLPurifier_Config::create(array(
  57. 'HTML.TidyLevel' => 'light'
  58. ));
  59. $module->expectAt($i++, 'populate', array(array(
  60. 'light-fix-1' => $lf1,
  61. 'light-fix-2' => $lf2
  62. )));
  63. $module->setup($config);
  64. $config = HTMLPurifier_Config::create(array(
  65. 'HTML.TidyLevel' => 'heavy'
  66. ));
  67. $module->expectAt($i++, 'populate', array(array(
  68. 'light-fix-1' => $lf1,
  69. 'light-fix-2' => $lf2,
  70. 'medium-fix-1' => $mf1,
  71. 'medium-fix-2' => $mf2,
  72. 'heavy-fix-1' => $hf1,
  73. 'heavy-fix-2' => $hf2
  74. )));
  75. $module->setup($config);
  76. // fine grained tuning
  77. $config = HTMLPurifier_Config::create(array(
  78. 'HTML.TidyLevel' => 'none',
  79. 'HTML.TidyAdd' => array('light-fix-1', 'medium-fix-1')
  80. ));
  81. $module->expectAt($i++, 'populate', array(array(
  82. 'light-fix-1' => $lf1,
  83. 'medium-fix-1' => $mf1
  84. )));
  85. $module->setup($config);
  86. $config = HTMLPurifier_Config::create(array(
  87. 'HTML.TidyLevel' => 'medium',
  88. 'HTML.TidyRemove' => array('light-fix-1', 'medium-fix-1')
  89. ));
  90. $module->expectAt($i++, 'populate', array(array(
  91. 'light-fix-2' => $lf2,
  92. 'medium-fix-2' => $mf2
  93. )));
  94. $module->setup($config);
  95. }
  96. function test_makeFixesForLevel() {
  97. $module = new HTMLPurifier_HTMLModule_Tidy();
  98. $module->defaultLevel = 'heavy';
  99. $module->makeFixesForLevel(array(
  100. 'fix-1' => 0,
  101. 'fix-2' => 1,
  102. 'fix-3' => 2
  103. ));
  104. $this->assertIdentical($module->fixesForLevel['heavy'], array('fix-1', 'fix-2', 'fix-3'));
  105. $this->assertIdentical($module->fixesForLevel['medium'], array());
  106. $this->assertIdentical($module->fixesForLevel['light'], array());
  107. }
  108. function test_makeFixesForLevel_undefinedLevel() {
  109. $module = new HTMLPurifier_HTMLModule_Tidy();
  110. $module->defaultLevel = 'bananas';
  111. $this->expectError('Default level bananas does not exist');
  112. $module->makeFixesForLevel(array(
  113. 'fix-1' => 0
  114. ));
  115. }
  116. function test_getFixType() {
  117. // syntax needs documenting
  118. $module = new HTMLPurifier_HTMLModule_Tidy();
  119. $this->assertIdentical(
  120. $module->getFixType('a'),
  121. array('tag_transform', array('element' => 'a'))
  122. );
  123. $this->assertIdentical(
  124. $module->getFixType('a@href'),
  125. $reuse = array('attr_transform_pre', array('element' => 'a', 'attr' => 'href'))
  126. );
  127. $this->assertIdentical(
  128. $module->getFixType('a@href#pre'),
  129. $reuse
  130. );
  131. $this->assertIdentical(
  132. $module->getFixType('a@href#post'),
  133. array('attr_transform_post', array('element' => 'a', 'attr' => 'href'))
  134. );
  135. $this->assertIdentical(
  136. $module->getFixType('xml:foo@xml:bar'),
  137. array('attr_transform_pre', array('element' => 'xml:foo', 'attr' => 'xml:bar'))
  138. );
  139. $this->assertIdentical(
  140. $module->getFixType('blockquote#child'),
  141. array('child', array('element' => 'blockquote'))
  142. );
  143. $this->assertIdentical(
  144. $module->getFixType('@lang'),
  145. array('attr_transform_pre', array('attr' => 'lang'))
  146. );
  147. $this->assertIdentical(
  148. $module->getFixType('@lang#post'),
  149. array('attr_transform_post', array('attr' => 'lang'))
  150. );
  151. }
  152. function test_populate() {
  153. $i = 0;
  154. $module = new HTMLPurifier_HTMLModule_Tidy();
  155. $module->populate(array(
  156. 'element' => $element = $i++,
  157. 'element@attr' => $attr = $i++,
  158. 'element@attr#post' => $attr_post = $i++,
  159. 'element#child' => $child = $i++,
  160. 'element#content_model_type' => $content_model_type = $i++,
  161. '@attr' => $global_attr = $i++,
  162. '@attr#post' => $global_attr_post = $i++
  163. ));
  164. $module2 = new HTMLPurifier_HTMLModule_Tidy();
  165. $e = $module2->addBlankElement('element');
  166. $e->attr_transform_pre['attr'] = $attr;
  167. $e->attr_transform_post['attr'] = $attr_post;
  168. $e->child = $child;
  169. $e->content_model_type = $content_model_type;
  170. $module2->info_tag_transform['element'] = $element;
  171. $module2->info_attr_transform_pre['attr'] = $global_attr;
  172. $module2->info_attr_transform_post['attr'] = $global_attr_post;
  173. $this->assertEqual($module, $module2);
  174. }
  175. }
  176. // vim: et sw=4 sts=4