ComplexHarness.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. */
  12. protected $obj;
  13. /**
  14. * Name of the function to be executed
  15. */
  16. protected $func;
  17. /**
  18. * Whether or not the method deals in tokens. If set to true, assertResult()
  19. * will transparently convert HTML to and back from tokens.
  20. */
  21. protected $to_tokens = false;
  22. /**
  23. * Whether or not to convert tokens back into HTML before performing
  24. * equality check, has no effect on bools.
  25. */
  26. protected $to_html = false;
  27. /**
  28. * Instance of an HTMLPurifier_Lexer implementation.
  29. */
  30. protected $lexer;
  31. public function __construct() {
  32. $this->lexer = new HTMLPurifier_Lexer_DirectLex();
  33. parent::__construct();
  34. }
  35. /**
  36. * Asserts a specific result from a one parameter + config/context function
  37. * @param $input Input parameter
  38. * @param $expect Expectation
  39. * @param $config Configuration array in form of Ns.Directive => Value.
  40. * Has no effect if $this->config is set.
  41. * @param $context_array Context array in form of Key => Value or an actual
  42. * context object.
  43. */
  44. protected function assertResult($input, $expect = true) {
  45. if ($this->to_tokens && is_string($input)) {
  46. // $func may cause $input to change, so "clone" another copy
  47. // to sacrifice
  48. $input = $this->tokenize($temp = $input);
  49. $input_c = $this->tokenize($temp);
  50. } else {
  51. $input_c = $input;
  52. }
  53. // call the function
  54. $func = $this->func;
  55. $result = $this->obj->$func($input_c, $this->config, $this->context);
  56. // test a bool result
  57. if (is_bool($result)) {
  58. $this->assertIdentical($expect, $result);
  59. return;
  60. } elseif (is_bool($expect)) {
  61. $expect = $input;
  62. }
  63. if ($this->to_html) {
  64. $result = $this->generate($result);
  65. if (is_array($expect)) {
  66. $expect = $this->generate($expect);
  67. }
  68. }
  69. $this->assertIdentical($expect, $result);
  70. if ($expect !== $result) {
  71. echo '<pre>' . var_dump($result) . '</pre>';
  72. }
  73. }
  74. /**
  75. * Tokenize HTML into tokens, uses member variables for common variables
  76. */
  77. protected function tokenize($html) {
  78. return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
  79. }
  80. /**
  81. * Generate textual HTML from tokens
  82. */
  83. protected function generate($tokens) {
  84. $generator = new HTMLPurifier_Generator($this->config, $this->context);
  85. return $generator->generateFromTokens($tokens);
  86. }
  87. }
  88. // vim: et sw=4 sts=4