0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

Refactor unit tests so that abstract test cases are now called Harnesses and AttrDef tests use their harness's assertDef() function, which enforces type much better. Also fixed a few bugs.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@161 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-05 00:30:31 +00:00
parent 6232221c08
commit 1945ddca5c
16 changed files with 100 additions and 70 deletions

View File

@ -6,11 +6,11 @@ require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
{
function validate($raw_string, $config = null) {
function validate($string, $config = null) {
if (!$config) $config = HTMLPurifier_Config::createDefault();
$string = trim($raw_string);
$string = trim($string);
// early abort: '' and '0' (strings that convert to false) are invalid
if (!$string) return false;
@ -23,18 +23,20 @@ class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
// and plus it would complicate optimization efforts (you never
// see that anyway).
$matches = array();
$pattern = '/(?:\s|\A)'.
'((?:-?[A-Za-z_]|--)[A-Za-z_\-0-9]*)'.
'(?:\s|\z)/';
$pattern = '/(?:(?<=\s)|\A)'.
'((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)'.
'(?:(?=\s)|\z)/';
preg_match_all($pattern, $string, $matches);
if (empty($matches[1])) return false;
$new_string = '';
foreach ($matches[1] as $class_names) {
$new_string .= $class_names . ' ';
}
$new_string = rtrim($new_string);
return ($new_string == $raw_string) ? true : $new_string ? $new_string : false;
return $new_string;
}

View File

@ -14,16 +14,14 @@ class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
$this->case_sensitive = $case_sensitive;
}
function validate($raw_string) {
$string = trim($raw_string);
function validate($string) {
$string = trim($string);
if (!$this->case_sensitive) {
$string = ctype_lower($string) ? $string : strtolower($string);
}
$result = isset($this->valid_values[$string]);
// if strings equal, return result, otherwise, return
// the new string on a good result and false on a bad one
return ($string == $raw_string) ? $result : $result ? $string : false;
return $result ? $string : false;
}
}

View File

@ -12,9 +12,9 @@ require_once 'HTMLPurifier/IDAccumulator.php';
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
{
function validate($old_id, $config, &$accumulator) {
function validate($id, $config, &$accumulator) {
$id = trim($old_id); // trim it first
$id = trim($id); // trim it first
if ($id === '') return false;
if (isset($accumulator->ids[$id])) return false;
@ -37,7 +37,7 @@ class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
// if no change was made to the ID, return the result
// else, return the new id if stripping whitespace made it
// valid, or return false.
return ($id == $old_id) ? $result : ($result ? $id : false);
return $result ? $id : false;
}

View File

@ -6,8 +6,7 @@ class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
{
function validate($string) {
$new_string = $this->parseCDATA($string);
return ($string == $new_string) ? true : $new_string;
return $this->parseCDATA($string);
}
}

View File

@ -26,6 +26,15 @@ class HTMLPurifier_Config
var $attr_class_blacklist = array();
var $attr_class_whitelist = array();
// designate whether or not to allow numerals in language code subtags
// RFC 1766, the current standard referenced by XML, does not permit
// numbers, but,
// RFC 3066, the superseding best practice standard since January 2001,
// permits them.
// we allow numbers by default, although you generally never see them
// at all.
var $attr_lang_alpha = false;
function createDefault() {
$config = new HTMLPurifier_Config();
return $config;

View File

@ -1,29 +1,30 @@
<?php
require_once 'HTMLPurifier/AttrDefHarness.php';
require_once 'HTMLPurifier/AttrDef/Class.php';
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_ClassTest extends UnitTestCase
class HTMLPurifier_AttrDef_ClassTest extends HTMLPurifier_AttrDefHarness
{
function testDefault() {
$def = new HTMLPurifier_AttrDef_Class();
$this->def = new HTMLPurifier_AttrDef_Class();
$this->assertTrue($def->validate('valid'));
$this->assertTrue($def->validate('a0-_'));
$this->assertTrue($def->validate('-valid'));
$this->assertTrue($def->validate('_valid'));
$this->assertTrue($def->validate('double valid'));
$this->assertDef('valid');
$this->assertDef('a0-_');
$this->assertDef('-valid');
$this->assertDef('_valid');
$this->assertDef('double valid');
$this->assertFalse($def->validate('0invalid'));
$this->assertFalse($def->validate('-0'));
$this->assertDef('0invalid', false);
$this->assertDef('-0', false);
// test conditional replacement
$this->assertEqual('validassoc', $def->validate('validassoc 0invalid'));
$this->assertDef('validassoc 0invalid', 'validassoc');
// test whitespace leniency
$this->assertTrue('double valid', $def->validate(" double\nvalid\r"));
$this->assertDef(" double\nvalid\r", 'double valid');
}

View File

@ -1,33 +1,34 @@
<?php
require_once 'HTMLPurifier/AttrDefHarness.php';
require_once 'HTMLPurifier/AttrDef/Enum.php';
class HTMLPurifier_AttrDef_EnumTest extends UnitTestCase
class HTMLPurifier_AttrDef_EnumTest extends HTMLPurifier_AttrDefHarness
{
function testCaseInsensitive() {
$def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'));
$this->def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'));
$this->assertTrue($def->validate('one'));
$this->assertTrue($def->validate('ONE'));
$this->assertDef('one');
$this->assertDef('ONE', 'one');
}
function testCaseSensitive() {
$def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'), true);
$this->def = new HTMLPurifier_AttrDef_Enum(array('one', 'two'), true);
$this->assertTrue($def->validate('one'));
$this->assertFalse($def->validate('ONE'));
$this->assertDef('one');
$this->assertDef('ONE', false);
}
function testFixing() {
$def = new HTMLPurifier_AttrDef_Enum(array('one'));
$this->def = new HTMLPurifier_AttrDef_Enum(array('one'));
$this->assertEqual('one', $def->validate(' one '));
$this->assertDef(' one ', 'one');
}

View File

@ -1,37 +1,33 @@
<?php
require_once 'HTMLPurifier/AttrDefHarness.php';
require_once 'HTMLPurifier/AttrDef/ID.php';
require_once 'HTMLPurifier/IDAccumulator.php';
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_IDTest extends UnitTestCase
class HTMLPurifier_AttrDef_IDTest extends HTMLPurifier_AttrDefHarness
{
function test() {
$acc = new HTMLPurifier_IDAccumulator();
$def = new HTMLPurifier_AttrDef_ID();
generate_mock_once('HTMLPurifier_Config');
$config = new HTMLPurifier_ConfigMock();
$this->id_accumulator = new HTMLPurifier_IDAccumulator();
$this->def = new HTMLPurifier_AttrDef_ID();
// valid ID names
$this->assertTrue($def->validate('alpha', $config, $acc));
$this->assertTrue($def->validate('al_ha', $config, $acc));
$this->assertTrue($def->validate('a0-:.', $config, $acc));
$this->assertTrue($def->validate('a' , $config, $acc));
$this->assertDef('alpha');
$this->assertDef('al_ha');
$this->assertDef('a0-:.');
$this->assertDef('a');
// invalid ID names
$this->assertFalse($def->validate('<asa', $config, $acc));
$this->assertFalse($def->validate('0123', $config, $acc));
$this->assertFalse($def->validate('.asa', $config, $acc));
$this->assertDef('<asa', false);
$this->assertDef('0123', false);
$this->assertDef('.asa', false);
// test duplicate detection
$this->assertFalse($def->validate('a' , $config, $acc));
$this->assertDef('a', false);
// valid once whitespace stripped, but needs to be amended
$this->assertEqual('whee', $def->validate(' whee ', $config, $acc));
$this->assertDef(' whee ', 'whee');
}

View File

@ -1,17 +1,17 @@
<?php
require_once 'HTMLPurifier/AttrDefHarness.php';
require_once 'HTMLPurifier/AttrDef/Text.php';
class HTMLPurifier_AttrDef_TextTest extends UnitTestCase
class HTMLPurifier_AttrDef_TextTest extends HTMLPurifier_AttrDefHarness
{
function test() {
$def = new HTMLPurifier_AttrDef_Text();
$this->def = new HTMLPurifier_AttrDef_Text();
$this->assertTrue($def->validate('This is spiffy text!'));
$this->assertEqual('Casual CDATA parsecheck.',
$def->validate(" Casual\tCDATA parse\ncheck. "));
$this->assertDef('This is spiffy text!');
$this->assertDef(" Casual\tCDATA parse\ncheck. ", 'Casual CDATA parsecheck.');
}

View File

@ -0,0 +1,24 @@
<?php
class HTMLPurifier_AttrDefHarness extends UnitTestCase
{
var $def;
var $id_accumulator;
var $config;
// cannot be used for accumulator
function assertDef($string, $expect = true) {
// $expect can be a string or bool
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
$result = $this->def->validate($string, $this->config, $this->id_accumulator);
if ($expect === true) {
$this->assertIdentical($string, $result);
} else {
$this->assertIdentical($expect, $result);
}
}
}
?>

View File

@ -1,10 +1,10 @@
<?php
require_once 'HTMLPurifier/StrategyAbstractTest.php';
require_once 'HTMLPurifier/StrategyHarness.php';
require_once 'HTMLPurifier/Strategy/Core.php';
class HTMLPurifier_Strategy_CoreTest
extends HTMLPurifier_StrategyAbstractTest
extends HTMLPurifier_StrategyHarness
{
function test() {

View File

@ -1,10 +1,10 @@
<?php
require_once 'HTMLPurifier/StrategyAbstractTest.php';
require_once 'HTMLPurifier/StrategyHarness.php';
require_once 'HTMLPurifier/Strategy/FixNesting.php';
class HTMLPurifier_Strategy_FixNestingTest
extends HTMLPurifier_StrategyAbstractTest
extends HTMLPurifier_StrategyHarness
{
function test() {

View File

@ -1,10 +1,10 @@
<?php
require_once 'HTMLPurifier/StrategyAbstractTest.php';
require_once 'HTMLPurifier/StrategyHarness.php';
require_once 'HTMLPurifier/Strategy/MakeWellFormed.php';
class HTMLPurifier_Strategy_MakeWellFormedTest
extends HTMLPurifier_StrategyAbstractTest
extends HTMLPurifier_StrategyHarness
{
function test() {

View File

@ -1,10 +1,10 @@
<?php
require_once 'HTMLPurifier/StrategyAbstractTest.php';
require_once 'HTMLPurifier/StrategyHarness.php';
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
class HTMLPurifier_Strategy_RemoveForeignElementsTest
extends HTMLPurifier_StrategyAbstractTest
extends HTMLPurifier_StrategyHarness
{
function test() {

View File

@ -1,11 +1,11 @@
<?php
require_once('HTMLPurifier/Config.php');
require_once('HTMLPurifier/StrategyAbstractTest.php');
require_once('HTMLPurifier/StrategyHarness.php');
require_once('HTMLPurifier/Strategy/ValidateAttributes.php');
class HTMLPurifier_Strategy_ValidateAttributesTest extends
HTMLPurifier_StrategyAbstractTest
HTMLPurifier_StrategyHarness
{
function test() {

View File

@ -3,12 +3,12 @@
require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_StrategyAbstractTest extends UnitTestCase
class HTMLPurifier_StrategyHarness extends UnitTestCase
{
var $lex, $gen;
function HTMLPurifier_StrategyAbstractTest() {
function HTMLPurifier_StrategyHarness() {
$this->UnitTestCase();
// we can't use the DOM lexer since it does too much stuff