Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

139 linhas
4.0KB

  1. <?php
  2. /**
  3. * General-purpose test-harness that makes testing functions that require
  4. * configuration and context objects easier when those two parameters are
  5. * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
  6. */
  7. class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
  8. {
  9. /**
  10. * Instance of the object that will execute the method.
  11. * @type object
  12. */
  13. protected $obj;
  14. /**
  15. * Name of the function to be executed.
  16. * @type string
  17. */
  18. protected $func;
  19. /**
  20. * Whether or not the method deals in tokens.
  21. * If set to true, assertResult()
  22. * will transparently convert HTML to and back from tokens.
  23. * @type bool
  24. */
  25. protected $to_tokens = false;
  26. /**
  27. * Whether or not the method deals in a node list.
  28. * If set to true, assertResult() will transparently convert HTML
  29. * to and back from node.
  30. * @type bool
  31. */
  32. protected $to_node_list = false;
  33. /**
  34. * Whether or not to convert tokens back into HTML before performing
  35. * equality check, has no effect on bools.
  36. * @type bool
  37. */
  38. protected $to_html = false;
  39. /**
  40. * Instance of an HTMLPurifier_Lexer implementation.
  41. * @type HTMLPurifier_Lexer
  42. */
  43. protected $lexer;
  44. public function __construct()
  45. {
  46. $this->lexer = new HTMLPurifier_Lexer_DirectLex();
  47. parent::__construct();
  48. }
  49. /**
  50. * Asserts a specific result from a one parameter + config/context function
  51. * @param string $input Input parameter
  52. * @param bool|string $expect Expectation
  53. */
  54. protected function assertResult($input, $expect = true)
  55. {
  56. // $func may cause $input to change, so "clone" another copy
  57. // to sacrifice
  58. if ($this->to_node_list && is_string($input)) {
  59. $input = HTMLPurifier_Arborize::arborize($this->tokenize($temp = $input), $this->config, $this->context)->children;
  60. $input_c = HTMLPurifier_Arborize::arborize($this->tokenize($temp), $this->config, $this->context)->children;
  61. } elseif ($this->to_tokens && is_string($input)) {
  62. $input = $this->tokenize($temp = $input);
  63. $input_c = $this->tokenize($temp);
  64. } else {
  65. $input_c = $input;
  66. }
  67. // call the function
  68. $func = $this->func;
  69. $result = $this->obj->$func($input_c, $this->config, $this->context);
  70. // test a bool result
  71. if (is_bool($result)) {
  72. $this->assertIdentical($expect, $result);
  73. return;
  74. } elseif (is_bool($expect)) {
  75. $expect = $input;
  76. }
  77. if ($this->to_html) {
  78. if ($this->to_node_list) {
  79. $result = $this->generateTokens($result);
  80. if (is_array($expect) && !empty($expect) && $expect[0] instanceof HTMLPurifier_Node) {
  81. $expect = $this->generateTokens($expect);
  82. }
  83. }
  84. $result = $this->generate($result);
  85. if (is_array($expect)) {
  86. $expect = $this->generate($expect);
  87. }
  88. }
  89. $this->assertIdentical($expect, $result);
  90. if ($expect !== $result) {
  91. echo '<pre>' . var_dump($result) . '</pre>';
  92. }
  93. }
  94. /**
  95. * Tokenize HTML into tokens, uses member variables for common variables
  96. */
  97. protected function tokenize($html)
  98. {
  99. return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
  100. }
  101. /**
  102. * Generate textual HTML from tokens
  103. */
  104. protected function generate($tokens)
  105. {
  106. $generator = new HTMLPurifier_Generator($this->config, $this->context);
  107. return $generator->generateFromTokens($tokens);
  108. }
  109. /**
  110. * Generate tokens from node list
  111. */
  112. protected function generateTokens($children)
  113. {
  114. $dummy = new HTMLPurifier_Node_Element("dummy");
  115. $dummy->children = $children;
  116. return HTMLPurifier_Arborize::flatten($dummy, $this->context, $this->config);
  117. }
  118. }
  119. // vim: et sw=4 sts=4