Fix standards-compliance issue with YouTube filter with double hyphens.
[htmlpurifier/bfroehle.git] / tests / HTMLPurifier / ComplexHarness.php
blob76d04152f241f33561ccd151c0e80a2a259a4273
1 <?php
3 /**
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.
7 */
8 class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
11 /**
12 * Instance of the object that will execute the method
14 protected $obj;
16 /**
17 * Name of the function to be executed
19 protected $func;
21 /**
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;
27 /**
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;
33 /**
34 * Instance of an HTMLPurifier_Lexer implementation.
36 protected $lexer;
38 public function __construct() {
39 $this->lexer = new HTMLPurifier_Lexer_DirectLex();
40 parent::__construct();
43 /**
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
50 * context object.
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
56 // to sacrifice
57 $input = $this->tokenize($temp = $input);
58 $input_c = $this->tokenize($temp);
59 } else {
60 $input_c = $input;
63 // call the function
64 $func = $this->func;
65 $result = $this->obj->$func($input_c, $this->config, $this->context);
67 // test a bool result
68 if (is_bool($result)) {
69 $this->assertIdentical($expect, $result);
70 return;
71 } elseif (is_bool($expect)) {
72 $expect = $input;
75 if ($this->to_html) {
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>';
89 /**
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);
96 /**
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