Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

225 lines
6.9KB

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