4 * General-purpose test-harness that makes testing functions that require
5 * configuration and context objects easier when those two parameters are
6 * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
8 class HTMLPurifier_ComplexHarness
extends HTMLPurifier_Harness
12 * Instance of the object that will execute the method
17 * Name of the function to be executed
22 * Whether or not the method deals in tokens. If set to true, assertResult()
23 * will transparently convert HTML to and back from tokens.
25 protected $to_tokens = false;
28 * Whether or not to convert tokens back into HTML before performing
29 * equality check, has no effect on bools.
31 protected $to_html = false;
34 * Instance of an HTMLPurifier_Lexer implementation.
38 public function __construct() {
39 $this->lexer
= new HTMLPurifier_Lexer_DirectLex();
40 parent
::__construct();
44 * Asserts a specific result from a one parameter + config/context function
45 * @param $input Input parameter
46 * @param $expect Expectation
47 * @param $config Configuration array in form of Ns.Directive => Value.
48 * Has no effect if $this->config is set.
49 * @param $context_array Context array in form of Key => Value or an actual
52 protected function assertResult($input, $expect = true) {
54 if ($this->to_tokens
&& is_string($input)) {
55 // $func may cause $input to change, so "clone" another copy
57 $input = $this->tokenize($temp = $input);
58 $input_c = $this->tokenize($temp);
65 $result = $this->obj
->$func($input_c, $this->config
, $this->context
);
68 if (is_bool($result)) {
69 $this->assertIdentical($expect, $result);
71 } elseif (is_bool($expect)) {
76 $result = $this->generate($result);
77 if (is_array($expect)) {
78 $expect = $this->generate($expect);
81 $this->assertIdentical($expect, $result);
83 if ($expect !== $result) {
84 echo '<pre>' . htmlspecialchars($result) . '</pre>';
90 * Tokenize HTML into tokens, uses member variables for common variables
92 protected function tokenize($html) {
93 return $this->lexer
->tokenizeHTML($html, $this->config
, $this->context
);
97 * Generate textual HTML from tokens
99 protected function generate($tokens) {
100 $generator = new HTMLPurifier_Generator($this->config
, $this->context
);
101 return $generator->generateFromTokens($tokens);
106 // vim: et sw=4 sts=4