Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / Harness.php
blob84cea5eb2ee3b690f3bf068a0131d3220af265bf
1 <?php
3 require_once 'HTMLPurifier/Lexer/DirectLex.php';
5 /**
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.
9 */
10 class HTMLPurifier_Harness extends UnitTestCase
13 /**
14 * Instance of the object that will execute the method
16 var $obj;
18 /**
19 * Name of the function to be executed
21 var $func;
23 /**
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;
29 /**
30 * Whether or not to convert tokens back into HTML before performing
31 * equality check, has no effect on bools.
33 var $to_html = false;
35 /**
36 * Instance of an HTMLPurifier_Lexer implementation.
38 var $lexer;
40 /**
41 * Instance of HTMLPurifier_Generator
43 var $generator;
45 /**
46 * Default config to fall back on if no config is available
48 var $config;
50 /**
51 * Default context to fall back on if no context is available
53 var $context;
55 function HTMLPurifier_Harness() {
56 $this->lexer = new HTMLPurifier_Lexer_DirectLex();
57 $this->generator = new HTMLPurifier_Generator();
58 parent::UnitTestCase();
61 /**
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
68 * context object.
70 function assertResult($input, $expect = true,
71 $config_array = array(), $context_array = array()
72 ) {
74 // setup config
75 if ($this->config) {
76 $config = HTMLPurifier_Config::create($this->config);
77 $config->loadArray($config_array);
78 } else {
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);
88 } else {
89 $context =& $this->context;
92 if ($this->to_tokens && is_string($input)) {
93 // $func may cause $input to change, so "clone" another copy
94 // to sacrifice
95 $input = $this->lexer->tokenizeHTML($s = $input, $config, $context);
96 $input_c = $this->lexer->tokenizeHTML($s, $config, $context);
97 } else {
98 $input_c = $input;
101 // call the function
102 $func = $this->func;
103 $result = $this->obj->$func($input_c, $config, $context);
105 // test a bool result
106 if (is_bool($result)) {
107 $this->assertIdentical($expect, $result);
108 return;
109 } elseif (is_bool($expect)) {
110 $expect = $input;
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);