2007-08-01 14:06:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General-purpose test-harness that makes testing functions that require
|
|
|
|
* configuration and context objects easier when those two parameters are
|
|
|
|
* meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the object that will execute the method
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $obj;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the function to be executed
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $func;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the method deals in tokens. If set to true, assertResult()
|
|
|
|
* will transparently convert HTML to and back from tokens.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $to_tokens = false;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to convert tokens back into HTML before performing
|
|
|
|
* equality check, has no effect on bools.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $to_html = false;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of an HTMLPurifier_Lexer implementation.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $lexer;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of HTMLPurifier_Generator
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $generator;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default config to fall back on if no config is available
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $config;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default context to fall back on if no context is available
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $context;
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
function HTMLPurifier_ComplexHarness() {
|
|
|
|
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
|
|
|
|
$this->generator = new HTMLPurifier_Generator();
|
|
|
|
parent::HTMLPurifier_Harness();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts a specific result from a one parameter + config/context function
|
|
|
|
* @param $input Input parameter
|
|
|
|
* @param $expect Expectation
|
|
|
|
* @param $config Configuration array in form of Ns.Directive => Value.
|
|
|
|
* Has no effect if $this->config is set.
|
|
|
|
* @param $context_array Context array in form of Key => Value or an actual
|
|
|
|
* context object.
|
|
|
|
*/
|
2007-08-08 05:38:52 +00:00
|
|
|
function assertResult($input, $expect = true) {
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
if ($this->to_tokens && is_string($input)) {
|
|
|
|
// $func may cause $input to change, so "clone" another copy
|
|
|
|
// to sacrifice
|
2007-08-08 05:38:52 +00:00
|
|
|
$input = $this->tokenize($temp = $input);
|
|
|
|
$input_c = $this->tokenize($temp);
|
2007-08-01 14:06:59 +00:00
|
|
|
} else {
|
|
|
|
$input_c = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the function
|
|
|
|
$func = $this->func;
|
2007-08-08 05:38:52 +00:00
|
|
|
$result = $this->obj->$func($input_c, $this->config, $this->context);
|
2007-08-01 14:06:59 +00:00
|
|
|
|
|
|
|
// test a bool result
|
|
|
|
if (is_bool($result)) {
|
|
|
|
$this->assertIdentical($expect, $result);
|
|
|
|
return;
|
|
|
|
} elseif (is_bool($expect)) {
|
|
|
|
$expect = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->to_html) {
|
2007-08-08 05:38:52 +00:00
|
|
|
$result = $this->generate($result);
|
2007-08-01 14:06:59 +00:00
|
|
|
if (is_array($expect)) {
|
2007-08-08 05:38:52 +00:00
|
|
|
$expect = $this->generate($expect);
|
2007-08-01 14:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertIdentical($expect, $result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-08 05:38:52 +00:00
|
|
|
/**
|
|
|
|
* Tokenize HTML into tokens, uses member variables for common variables
|
|
|
|
*/
|
|
|
|
function tokenize($html) {
|
|
|
|
return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate textual HTML from tokens
|
|
|
|
*/
|
|
|
|
function generate($tokens) {
|
|
|
|
return $this->generator->generateFromTokens($tokens, $this->config, $this->context);
|
|
|
|
}
|
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|