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.
18 * Name of the function to be executed.
24 * Whether or not the method deals in tokens.
25 * If set to true, assertResult()
26 * will transparently convert HTML to and back from tokens.
29 protected $to_tokens = false;
32 * Whether or not the method deals in a node list.
33 * If set to true, assertResult() will transparently convert HTML
34 * to and back from node.
37 protected $to_node_list = false;
40 * Whether or not to convert tokens back into HTML before performing
41 * equality check, has no effect on bools.
44 protected $to_html = false;
47 * Instance of an HTMLPurifier_Lexer implementation.
48 * @type HTMLPurifier_Lexer
52 public function __construct()
54 $this->lexer
= new HTMLPurifier_Lexer_DirectLex();
55 parent
::__construct();
59 * Asserts a specific result from a one parameter + config/context function
60 * @param string $input Input parameter
61 * @param bool|string $expect Expectation
63 protected function assertResult($input, $expect = true)
65 // $func may cause $input to change, so "clone" another copy
67 if ($this->to_node_list
&& is_string($input)) {
68 $input = HTMLPurifier_Arborize
::arborize($this->tokenize($temp = $input), $this->config
, $this->context
)->children
;
69 $input_c = HTMLPurifier_Arborize
::arborize($this->tokenize($temp), $this->config
, $this->context
)->children
;
70 } elseif ($this->to_tokens
&& is_string($input)) {
71 $input = $this->tokenize($temp = $input);
72 $input_c = $this->tokenize($temp);
79 $result = $this->obj
->$func($input_c, $this->config
, $this->context
);
82 if (is_bool($result)) {
83 $this->assertIdentical($expect, $result);
85 } elseif (is_bool($expect)) {
90 if ($this->to_node_list
) {
91 $result = $this->generateTokens($result);
92 if (is_array($expect) && !empty($expect) && $expect[0] instanceof HTMLPurifier_Node
) {
93 $expect = $this->generateTokens($expect);
96 $result = $this->generate($result);
97 if (is_array($expect)) {
98 $expect = $this->generate($expect);
101 $this->assertIdentical($expect, $result);
103 if ($expect !== $result) {
104 echo '<pre>' . var_dump($result) . '</pre>';
110 * Tokenize HTML into tokens, uses member variables for common variables
112 protected function tokenize($html)
114 return $this->lexer
->tokenizeHTML($html, $this->config
, $this->context
);
118 * Generate textual HTML from tokens
120 protected function generate($tokens)
122 $generator = new HTMLPurifier_Generator($this->config
, $this->context
);
123 return $generator->generateFromTokens($tokens);
127 * Generate tokens from node list
129 protected function generateTokens($children)
131 $dummy = new HTMLPurifier_Node_Element("dummy");
132 $dummy->children
= $children;
133 return HTMLPurifier_Arborize
::flatten($dummy, $this->context
, $this->config
);
138 // vim: et sw=4 sts=4