2006-10-01 20:47:07 +00:00
|
|
|
<?php
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
/**
|
|
|
|
* 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_Harness extends UnitTestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of the object that will execute the method
|
|
|
|
*/
|
|
|
|
var $obj;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the function to be executed
|
|
|
|
*/
|
|
|
|
var $func;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the method deals in tokens. If set to true, assertResult()
|
|
|
|
* will transparently convert HTML to and back from tokens.
|
|
|
|
*/
|
|
|
|
var $to_tokens = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to convert tokens back into HTML before performing
|
|
|
|
* equality check, has no effect on bools.
|
|
|
|
*/
|
|
|
|
var $to_html = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of an HTMLPurifier_Lexer implementation.
|
|
|
|
*/
|
|
|
|
var $lexer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of HTMLPurifier_Generator
|
|
|
|
*/
|
|
|
|
var $generator;
|
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
/**
|
|
|
|
* Default config to fall back on if no config is available
|
|
|
|
*/
|
|
|
|
var $config;
|
|
|
|
|
2007-06-25 01:56:00 +00:00
|
|
|
/**
|
|
|
|
* Default context to fall back on if no context is available
|
|
|
|
*/
|
|
|
|
var $context;
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function HTMLPurifier_Harness() {
|
|
|
|
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
|
|
|
|
$this->generator = new HTMLPurifier_Generator();
|
|
|
|
parent::UnitTestCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts a specific result from a one parameter + config/context function
|
|
|
|
* @param $input Input parameter
|
|
|
|
* @param $expect Expectation
|
2007-05-20 22:29:31 +00:00
|
|
|
* @param $config Configuration array in form of Ns.Directive => Value.
|
|
|
|
* Has no effect if $this->config is set.
|
2006-10-01 20:47:07 +00:00
|
|
|
* @param $context_array Context array in form of Key => Value or an actual
|
|
|
|
* context object.
|
|
|
|
*/
|
|
|
|
function assertResult($input, $expect = true,
|
2007-05-21 01:36:15 +00:00
|
|
|
$config_array = array(), $context_array = array()
|
2006-10-01 20:47:07 +00:00
|
|
|
) {
|
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
// setup config
|
|
|
|
if ($this->config) {
|
|
|
|
$config = HTMLPurifier_Config::create($this->config);
|
2007-05-21 01:36:15 +00:00
|
|
|
$config->loadArray($config_array);
|
2007-05-20 22:29:31 +00:00
|
|
|
} else {
|
|
|
|
$config = HTMLPurifier_Config::create($config_array);
|
|
|
|
}
|
2006-10-01 20:47:07 +00:00
|
|
|
|
2006-10-21 18:18:36 +00:00
|
|
|
// setup context object. Note that we are operating on a copy of it!
|
2007-05-20 22:29:31 +00:00
|
|
|
// When necessary, extend the test harness to allow post-tests
|
2006-10-21 18:18:36 +00:00
|
|
|
// on the context object
|
2007-06-25 01:56:00 +00:00
|
|
|
if (empty($this->context)) {
|
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$context->loadArray($context_array);
|
|
|
|
} else {
|
|
|
|
$context =& $this->context;
|
|
|
|
}
|
2006-10-01 20:47:07 +00:00
|
|
|
|
|
|
|
if ($this->to_tokens && is_string($input)) {
|
2007-05-13 03:42:09 +00:00
|
|
|
// $func may cause $input to change, so "clone" another copy
|
|
|
|
// to sacrifice
|
|
|
|
$input = $this->lexer->tokenizeHTML($s = $input, $config, $context);
|
|
|
|
$input_c = $this->lexer->tokenizeHTML($s, $config, $context);
|
|
|
|
} else {
|
|
|
|
$input_c = $input;
|
2006-10-01 20:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// call the function
|
|
|
|
$func = $this->func;
|
2007-05-13 03:42:09 +00:00
|
|
|
$result = $this->obj->$func($input_c, $config, $context);
|
2006-10-01 20:47:07 +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) {
|
|
|
|
$result = $this->generator->
|
|
|
|
generateFromTokens($result, $config, $context);
|
|
|
|
if (is_array($expect)) {
|
|
|
|
$expect = $this->generator->
|
|
|
|
generateFromTokens($expect, $config, $context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-05 20:17:04 +00:00
|
|
|
$this->assertIdentical($expect, $result);
|
2006-10-01 20:47:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|