0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 05:11:52 +00:00

[2.1.0] All unit tests inherit from HTMLPurifier_Harness, not UnitTestCase. prepareCommon() refactored to global test-case.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1332 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-08-01 14:06:59 +00:00
parent 9d98b45dea
commit 2a002857ce
44 changed files with 178 additions and 162 deletions

1
NEWS
View File

@ -48,6 +48,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
run the tests via command line run the tests via command line
. URI scheme is munged off if there is no authority and the scheme is the . URI scheme is munged off if there is no authority and the scheme is the
default one default one
. All unit tests inherit from HTMLPurifier_Harness, not UnitTestCase
2.0.1, released 2007-06-27 2.0.1, released 2007-06-27
! Tag auto-closing now based on a ChildDef heuristic rather than a ! Tag auto-closing now based on a ChildDef heuristic rather than a

View File

@ -8,7 +8,7 @@ Mock::generatePartial(
array('performInclusions', 'expandIdentifiers') array('performInclusions', 'expandIdentifiers')
); );
class HTMLPurifier_AttrCollectionsTest extends UnitTestCase class HTMLPurifier_AttrCollectionsTest extends HTMLPurifier_Harness
{ {
function testConstruction() { function testConstruction() {

View File

@ -27,11 +27,6 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry); HTMLPurifier_URISchemeRegistry::instance($this->oldRegistry);
} }
function prepareCommon(&$config, &$context) {
$config = HTMLPurifier_Config::create($config);
if (!$context) $context = new HTMLPurifier_Context();
}
function &generateSchemeMock($scheme_names = array('http', 'mailto')) { function &generateSchemeMock($scheme_names = array('http', 'mailto')) {
generate_mock_once('HTMLPurifier_URIScheme'); generate_mock_once('HTMLPurifier_URIScheme');
generate_mock_once('HTMLPurifier_URISchemeRegistry'); generate_mock_once('HTMLPurifier_URISchemeRegistry');

View File

@ -1,6 +1,6 @@
<?php <?php
class HTMLPurifier_AttrDefHarness extends UnitTestCase class HTMLPurifier_AttrDefHarness extends HTMLPurifier_Harness
{ {
var $def; var $def;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/AttrDef.php'; require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDefTest extends UnitTestCase class HTMLPurifier_AttrDefTest extends HTMLPurifier_Harness
{ {
function test_parseCDATA() { function test_parseCDATA() {

View File

@ -1,8 +1,8 @@
<?php <?php
require_once 'HTMLPurifier/Harness.php'; require_once 'HTMLPurifier/ComplexHarness.php';
class HTMLPurifier_AttrTransformHarness extends HTMLPurifier_Harness class HTMLPurifier_AttrTransformHarness extends HTMLPurifier_ComplexHarness
{ {
function setUp() { function setUp() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/AttrTransform.php'; require_once 'HTMLPurifier/AttrTransform.php';
class HTMLPurifier_AttrTransformTest extends UnitTestCase class HTMLPurifier_AttrTransformTest extends HTMLPurifier_Harness
{ {
function test_prependCSS() { function test_prependCSS() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/AttrTypes.php'; require_once 'HTMLPurifier/AttrTypes.php';
class HTMLPurifier_AttrTypesTest extends UnitTestCase class HTMLPurifier_AttrTypesTest extends HTMLPurifier_Harness
{ {
function test_get() { function test_get() {

View File

@ -1,9 +1,9 @@
<?php <?php
require_once 'HTMLPurifier/Harness.php'; require_once 'HTMLPurifier/ComplexHarness.php';
require_once 'HTMLPurifier/ChildDef.php'; require_once 'HTMLPurifier/ChildDef.php';
class HTMLPurifier_ChildDefHarness extends HTMLPurifier_Harness class HTMLPurifier_ChildDefHarness extends HTMLPurifier_ComplexHarness
{ {
function setUp() { function setUp() {

View File

@ -0,0 +1,128 @@
<?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
*/
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;
/**
* Default config to fall back on if no config is available
*/
var $config;
/**
* Default context to fall back on if no context is available
*/
var $context;
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.
*/
function assertResult($input, $expect = true,
$config_array = array(), $context_array = array()
) {
// setup config
if ($this->config) {
$config = HTMLPurifier_Config::create($this->config);
$config->loadArray($config_array);
} else {
$config = HTMLPurifier_Config::create($config_array);
}
// setup context object. Note that we are operating on a copy of it!
// When necessary, extend the test harness to allow post-tests
// on the context object
if (empty($this->context)) {
$context = new HTMLPurifier_Context();
$context->loadArray($context_array);
} else {
$context =& $this->context;
}
if ($this->to_tokens && is_string($input)) {
// $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;
}
// call the function
$func = $this->func;
$result = $this->obj->$func($input_c, $config, $context);
// 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);
}
}
$this->assertIdentical($expect, $result);
}
}

View File

@ -6,7 +6,7 @@ if (!class_exists('CS')) {
class CS extends HTMLPurifier_ConfigSchema {} class CS extends HTMLPurifier_ConfigSchema {}
} }
class HTMLPurifier_ConfigSchemaTest extends UnitTestCase class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
{ {
/** /**

View File

@ -6,7 +6,7 @@ if (!class_exists('CS')) {
class CS extends HTMLPurifier_ConfigSchema {} class CS extends HTMLPurifier_ConfigSchema {}
} }
class HTMLPurifier_ConfigTest extends UnitTestCase class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness
{ {
var $our_copy, $old_copy; var $our_copy, $old_copy;

View File

@ -5,7 +5,7 @@ require_once 'HTMLPurifier/Context.php';
// mocks // mocks
require_once 'HTMLPurifier/IDAccumulator.php'; require_once 'HTMLPurifier/IDAccumulator.php';
class HTMLPurifier_ContextTest extends UnitTestCase class HTMLPurifier_ContextTest extends HTMLPurifier_Harness
{ {
var $context; var $context;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/DefinitionCacheFactory.php'; require_once 'HTMLPurifier/DefinitionCacheFactory.php';
class HTMLPurifier_DefinitionCacheFactoryTest extends UnitTestCase class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness
{ {
var $newFactory; var $newFactory;

View File

@ -1,6 +1,6 @@
<?php <?php
class HTMLPurifier_DefinitionCacheHarness extends UnitTestCase class HTMLPurifier_DefinitionCacheHarness extends HTMLPurifier_Harness
{ {
/** /**

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/DefinitionCache.php'; require_once 'HTMLPurifier/DefinitionCache.php';
class HTMLPurifier_DefinitionCacheTest extends UnitTestCase class HTMLPurifier_DefinitionCacheTest extends HTMLPurifier_Harness
{ {
function test_isOld() { function test_isOld() {

View File

@ -7,7 +7,7 @@ Mock::generatePartial(
'HTMLPurifier_Definition_Testable', 'HTMLPurifier_Definition_Testable',
array('doSetup')); array('doSetup'));
class HTMLPurifier_DefinitionTest extends UnitTestCase class HTMLPurifier_DefinitionTest extends HTMLPurifier_Harness
{ {
function test_setup() { function test_setup() {
$def = new HTMLPurifier_Definition_Testable(); $def = new HTMLPurifier_Definition_Testable();

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/DoctypeRegistry.php'; require_once 'HTMLPurifier/DoctypeRegistry.php';
class HTMLPurifier_DoctypeRegistryTest extends UnitTestCase class HTMLPurifier_DoctypeRegistryTest extends HTMLPurifier_Harness
{ {
function test_register() { function test_register() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/ElementDef.php'; require_once 'HTMLPurifier/ElementDef.php';
class HTMLPurifier_ElementDefTest extends UnitTestCase class HTMLPurifier_ElementDefTest extends HTMLPurifier_Harness
{ {
function test_mergeIn() { function test_mergeIn() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/Encoder.php'; require_once 'HTMLPurifier/Encoder.php';
class HTMLPurifier_EncoderTest extends UnitTestCase class HTMLPurifier_EncoderTest extends HTMLPurifier_Harness
{ {
var $_entity_lookup; var $_entity_lookup;

View File

@ -4,7 +4,7 @@
require_once 'HTMLPurifier/EntityLookup.php'; require_once 'HTMLPurifier/EntityLookup.php';
class HTMLPurifier_EntityLookupTest extends UnitTestCase class HTMLPurifier_EntityLookupTest extends HTMLPurifier_Harness
{ {
function test() { function test() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/EntityParser.php'; require_once 'HTMLPurifier/EntityParser.php';
class HTMLPurifier_EntityParserTest extends UnitTestCase class HTMLPurifier_EntityParserTest extends HTMLPurifier_Harness
{ {
var $EntityParser; var $EntityParser;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/ErrorCollector.php'; require_once 'HTMLPurifier/ErrorCollector.php';
class HTMLPurifier_ErrorCollectorTest extends UnitTestCase class HTMLPurifier_ErrorCollectorTest extends HTMLPurifier_Harness
{ {
function setup() { function setup() {

View File

@ -3,7 +3,7 @@
require_once 'HTMLPurifier/ErrorCollectorEMock.php'; require_once 'HTMLPurifier/ErrorCollectorEMock.php';
require_once 'HTMLPurifier/Lexer/DirectLex.php'; require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_ErrorsHarness extends UnitTestCase class HTMLPurifier_ErrorsHarness extends HTMLPurifier_Harness
{ {
var $config, $context; var $config, $context;

View File

@ -3,16 +3,16 @@
require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/EntityLookup.php'; require_once 'HTMLPurifier/EntityLookup.php';
require_once 'HTMLPurifier/Harness.php'; require_once 'HTMLPurifier/ComplexHarness.php';
class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness class HTMLPurifier_GeneratorTest extends HTMLPurifier_ComplexHarness
{ {
var $gen; var $gen;
var $_entity_lookup; var $_entity_lookup;
function HTMLPurifier_GeneratorTest() { function HTMLPurifier_GeneratorTest() {
$this->UnitTestCase(); $this->HTMLPurifier_Harness();
$this->gen = new HTMLPurifier_Generator(); $this->gen = new HTMLPurifier_Generator();
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance(); $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
} }

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/HTMLDefinition.php'; require_once 'HTMLPurifier/HTMLDefinition.php';
class HTMLPurifier_HTMLDefinitionTest extends UnitTestCase class HTMLPurifier_HTMLDefinitionTest extends HTMLPurifier_Harness
{ {
function test_parseTinyMCEAllowedList() { function test_parseTinyMCEAllowedList() {

View File

@ -8,7 +8,7 @@ Mock::generatePartial(
array('makeFixes', 'makeFixesForLevel', 'populate') array('makeFixes', 'makeFixesForLevel', 'populate')
); );
class HTMLPurifier_HTMLModule_TidyTest extends UnitTestCase class HTMLPurifier_HTMLModule_TidyTest extends HTMLPurifier_Harness
{ {
function test_getFixesForLevel() { function test_getFixesForLevel() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/HTMLModuleManager.php'; require_once 'HTMLPurifier/HTMLModuleManager.php';
class HTMLPurifier_HTMLModuleManagerTest extends UnitTestCase class HTMLPurifier_HTMLModuleManagerTest extends HTMLPurifier_Harness
{ {
function test_addModule() { function test_addModule() {

View File

@ -3,7 +3,7 @@
require_once 'HTMLPurifier/HTMLModule.php'; require_once 'HTMLPurifier/HTMLModule.php';
require_once 'HTMLPurifier/AttrDef.php'; require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_HTMLModuleTest extends UnitTestCase class HTMLPurifier_HTMLModuleTest extends HTMLPurifier_Harness
{ {
function test_addElementToContentSet() { function test_addElementToContentSet() {

View File

@ -1,128 +1,19 @@
<?php <?php
require_once 'HTMLPurifier/Lexer/DirectLex.php';
/** /**
* General-purpose test-harness that makes testing functions that require * All-use harness, use this rather than SimpleTest's
* 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 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;
/**
* Default config to fall back on if no config is available
*/
var $config;
/**
* Default context to fall back on if no context is available
*/
var $context;
function HTMLPurifier_Harness() { function HTMLPurifier_Harness() {
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
$this->generator = new HTMLPurifier_Generator();
parent::UnitTestCase(); parent::UnitTestCase();
} }
/** function prepareCommon(&$config, &$context) {
* Asserts a specific result from a one parameter + config/context function $config = HTMLPurifier_Config::create($config);
* @param $input Input parameter if (!$context) $context = new HTMLPurifier_Context();
* @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.
*/
function assertResult($input, $expect = true,
$config_array = array(), $context_array = array()
) {
// setup config
if ($this->config) {
$config = HTMLPurifier_Config::create($this->config);
$config->loadArray($config_array);
} else {
$config = HTMLPurifier_Config::create($config_array);
}
// setup context object. Note that we are operating on a copy of it!
// When necessary, extend the test harness to allow post-tests
// on the context object
if (empty($this->context)) {
$context = new HTMLPurifier_Context();
$context->loadArray($context_array);
} else {
$context =& $this->context;
}
if ($this->to_tokens && is_string($input)) {
// $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;
}
// call the function
$func = $this->func;
$result = $this->obj->$func($input_c, $config, $context);
// 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);
}
}
$this->assertIdentical($expect, $result);
} }
} }

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/IDAccumulator.php'; require_once 'HTMLPurifier/IDAccumulator.php';
class HTMLPurifier_IDAccumulatorTest extends UnitTestCase class HTMLPurifier_IDAccumulatorTest extends HTMLPurifier_Harness
{ {
function test() { function test() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/LanguageFactory.php'; require_once 'HTMLPurifier/LanguageFactory.php';
class HTMLPurifier_LanguageFactoryTest extends UnitTestCase class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness
{ {
function test() { function test() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/Language.php'; require_once 'HTMLPurifier/Language.php';
class HTMLPurifier_LanguageTest extends UnitTestCase class HTMLPurifier_LanguageTest extends HTMLPurifier_Harness
{ {
var $lang; var $lang;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/Lexer/DirectLex.php'; require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_Lexer_DirectLexTest extends UnitTestCase class HTMLPurifier_Lexer_DirectLexTest extends HTMLPurifier_Harness
{ {
var $DirectLex; var $DirectLex;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/Lexer/DirectLex.php'; require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_LexerTest extends UnitTestCase class HTMLPurifier_LexerTest extends HTMLPurifier_Harness
{ {
var $Lexer; var $Lexer;

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/PercentEncoder.php'; require_once 'HTMLPurifier/PercentEncoder.php';
class HTMLPurifier_PercentEncoderTest extends UnitTestCase class HTMLPurifier_PercentEncoderTest extends HTMLPurifier_Harness
{ {
var $PercentEncoder; var $PercentEncoder;

View File

@ -15,7 +15,7 @@ class HTMLPurifier_Strategy_Composite_Test
} }
// doesn't use Strategy harness // doesn't use Strategy harness
class HTMLPurifier_Strategy_CompositeTest extends UnitTestCase class HTMLPurifier_Strategy_CompositeTest extends HTMLPurifier_Harness
{ {
function test() { function test() {

View File

@ -1,8 +1,8 @@
<?php <?php
require_once 'HTMLPurifier/Harness.php'; require_once 'HTMLPurifier/ComplexHarness.php';
class HTMLPurifier_StrategyHarness extends HTMLPurifier_Harness class HTMLPurifier_StrategyHarness extends HTMLPurifier_ComplexHarness
{ {
function setUp() { function setUp() {

View File

@ -6,7 +6,7 @@ require_once 'HTMLPurifier/TagTransform.php';
require_once 'HTMLPurifier/TagTransform/Font.php'; require_once 'HTMLPurifier/TagTransform/Font.php';
require_once 'HTMLPurifier/TagTransform/Simple.php'; require_once 'HTMLPurifier/TagTransform/Simple.php';
class HTMLPurifier_TagTransformTest extends UnitTestCase class HTMLPurifier_TagTransformTest extends HTMLPurifier_Harness
{ {
/** /**

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/TokenFactory.php'; require_once 'HTMLPurifier/TokenFactory.php';
class HTMLPurifier_TokenFactoryTest extends UnitTestCase class HTMLPurifier_TokenFactoryTest extends HTMLPurifier_Harness
{ {
public function test() { public function test() {

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/Token.php'; require_once 'HTMLPurifier/Token.php';
class HTMLPurifier_TokenTest extends UnitTestCase class HTMLPurifier_TokenTest extends HTMLPurifier_Harness
{ {
function assertTokenConstruction($name, $attr, function assertTokenConstruction($name, $attr,

View File

@ -2,7 +2,7 @@
require_once 'HTMLPurifier/URISchemeRegistry.php'; require_once 'HTMLPurifier/URISchemeRegistry.php';
class HTMLPurifier_URISchemeRegistryTest extends UnitTestCase class HTMLPurifier_URISchemeRegistryTest extends HTMLPurifier_Harness
{ {
function test() { function test() {

View File

@ -12,7 +12,7 @@ require_once 'HTMLPurifier/URIScheme/nntp.php';
// WARNING: All the URI schemes are far to relaxed, we need to tighten // WARNING: All the URI schemes are far to relaxed, we need to tighten
// the checks. // the checks.
class HTMLPurifier_URISchemeTest extends UnitTestCase class HTMLPurifier_URISchemeTest extends HTMLPurifier_Harness
{ {
function test_http() { function test_http() {

View File

@ -21,6 +21,7 @@ require_once $simpletest_location . 'unit_tester.php';
require_once $simpletest_location . 'reporter.php'; require_once $simpletest_location . 'reporter.php';
require_once $simpletest_location . 'mock_objects.php'; require_once $simpletest_location . 'mock_objects.php';
require_once 'HTMLPurifier/SimpleTest/Reporter.php'; require_once 'HTMLPurifier/SimpleTest/Reporter.php';
require_once 'HTMLPurifier/Harness.php';
// load Debugger // load Debugger
require_once 'Debugger.php'; require_once 'Debugger.php';