3 require_once 'HTMLPurifier/Lexer/DirectLex.php';
6 * General-purpose test-harness that makes testing functions that require
7 * configuration and context objects easier when those two parameters are
8 * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
10 class HTMLPurifier_Harness
extends UnitTestCase
14 * Instance of the object that will execute the method
19 * Name of the function to be executed
24 * Whether or not the method deals in tokens. If set to true, assertResult()
25 * will transparently convert HTML to and back from tokens.
27 var $to_tokens = false;
30 * Whether or not to convert tokens back into HTML before performing
31 * equality check, has no effect on bools.
36 * Instance of an HTMLPurifier_Lexer implementation.
41 * Instance of HTMLPurifier_Generator
46 * Default config to fall back on if no config is available
51 * Default context to fall back on if no context is available
55 function HTMLPurifier_Harness() {
56 $this->lexer
= new HTMLPurifier_Lexer_DirectLex();
57 $this->generator
= new HTMLPurifier_Generator();
58 parent
::UnitTestCase();
62 * Asserts a specific result from a one parameter + config/context function
63 * @param $input Input parameter
64 * @param $expect Expectation
65 * @param $config Configuration array in form of Ns.Directive => Value.
66 * Has no effect if $this->config is set.
67 * @param $context_array Context array in form of Key => Value or an actual
70 function assertResult($input, $expect = true,
71 $config_array = array(), $context_array = array()
76 $config = HTMLPurifier_Config
::create($this->config
);
77 $config->loadArray($config_array);
79 $config = HTMLPurifier_Config
::create($config_array);
82 // setup context object. Note that we are operating on a copy of it!
83 // When necessary, extend the test harness to allow post-tests
84 // on the context object
85 if (empty($this->context
)) {
86 $context = new HTMLPurifier_Context();
87 $context->loadArray($context_array);
89 $context =& $this->context
;
92 if ($this->to_tokens
&& is_string($input)) {
93 // $func may cause $input to change, so "clone" another copy
95 $input = $this->lexer
->tokenizeHTML($s = $input, $config, $context);
96 $input_c = $this->lexer
->tokenizeHTML($s, $config, $context);
103 $result = $this->obj
->$func($input_c, $config, $context);
105 // test a bool result
106 if (is_bool($result)) {
107 $this->assertIdentical($expect, $result);
109 } elseif (is_bool($expect)) {
113 if ($this->to_html
) {
114 $result = $this->generator
->
115 generateFromTokens($result, $config, $context);
116 if (is_array($expect)) {
117 $expect = $this->generator
->
118 generateFromTokens($expect, $config, $context);
122 $this->assertIdentical($expect, $result);