2007-08-01 14:06:59 +00:00
|
|
|
<?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
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Instance of the object that will execute the method.
|
|
|
|
* @type object
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $obj;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Name of the function to be executed.
|
|
|
|
* @type string
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $func;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
/**
|
2013-07-16 11:56:14 +00:00
|
|
|
* Whether or not the method deals in tokens.
|
|
|
|
* If set to true, assertResult()
|
2007-08-01 14:06:59 +00:00
|
|
|
* will transparently convert HTML to and back from tokens.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type bool
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $to_tokens = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the method deals in a node list.
|
|
|
|
* If set to true, assertResult() will transparently convert HTML
|
|
|
|
* to and back from node.
|
|
|
|
* @type bool
|
|
|
|
*/
|
|
|
|
protected $to_node_list = 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.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type bool
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $to_html = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
/**
|
|
|
|
* Instance of an HTMLPurifier_Lexer implementation.
|
2013-07-16 11:56:14 +00:00
|
|
|
* @type HTMLPurifier_Lexer
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $lexer;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2007-08-01 14:06:59 +00:00
|
|
|
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
|
2008-04-21 15:24:18 +00:00
|
|
|
parent::__construct();
|
2007-08-01 14:06:59 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
/**
|
|
|
|
* Asserts a specific result from a one parameter + config/context function
|
2013-07-16 11:56:14 +00:00
|
|
|
* @param string $input Input parameter
|
|
|
|
* @param bool|string $expect Expectation
|
2007-08-01 14:06:59 +00:00
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
protected function assertResult($input, $expect = true)
|
|
|
|
{
|
2013-10-21 05:18:59 +00:00
|
|
|
// $func may cause $input to change, so "clone" another copy
|
|
|
|
// to sacrifice
|
|
|
|
if ($this->to_node_list && is_string($input)) {
|
|
|
|
$input = HTMLPurifier_Arborize::arborize($this->tokenize($temp = $input), $this->config, $this->context)->children;
|
|
|
|
$input_c = HTMLPurifier_Arborize::arborize($this->tokenize($temp), $this->config, $this->context)->children;
|
|
|
|
} elseif ($this->to_tokens && is_string($input)) {
|
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;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
// call the function
|
|
|
|
$func = $this->func;
|
2007-08-08 05:38:52 +00:00
|
|
|
$result = $this->obj->$func($input_c, $this->config, $this->context);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
if ($this->to_html) {
|
2013-10-21 05:18:59 +00:00
|
|
|
if ($this->to_node_list) {
|
|
|
|
$result = $this->generateTokens($result);
|
|
|
|
if (is_array($expect) && !empty($expect) && $expect[0] instanceof HTMLPurifier_Node) {
|
|
|
|
$expect = $this->generateTokens($expect);
|
|
|
|
}
|
|
|
|
}
|
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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-08-15 22:57:44 +00:00
|
|
|
if ($expect !== $result) {
|
2009-02-21 07:58:30 +00:00
|
|
|
echo '<pre>' . var_dump($result) . '</pre>';
|
2008-08-15 22:57:44 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-08 05:38:52 +00:00
|
|
|
/**
|
|
|
|
* Tokenize HTML into tokens, uses member variables for common variables
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
protected function tokenize($html)
|
|
|
|
{
|
2007-08-08 05:38:52 +00:00
|
|
|
return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-08 05:38:52 +00:00
|
|
|
/**
|
|
|
|
* Generate textual HTML from tokens
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
protected function generate($tokens)
|
|
|
|
{
|
2008-04-28 01:35:07 +00:00
|
|
|
$generator = new HTMLPurifier_Generator($this->config, $this->context);
|
|
|
|
return $generator->generateFromTokens($tokens);
|
2007-08-08 05:38:52 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
/**
|
|
|
|
* Generate tokens from node list
|
|
|
|
*/
|
|
|
|
protected function generateTokens($children)
|
|
|
|
{
|
|
|
|
$dummy = new HTMLPurifier_Node_Element("dummy");
|
|
|
|
$dummy->children = $children;
|
|
|
|
return HTMLPurifier_Arborize::flatten($dummy, $this->context, $this->config);
|
|
|
|
}
|
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|