mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-23 08:51: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:
parent
6232221c08
commit
1945ddca5c
@ -6,11 +6,11 @@ require_once 'HTMLPurifier/Config.php';
|
|||||||
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($raw_string, $config = null) {
|
function validate($string, $config = null) {
|
||||||
|
|
||||||
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
||||||
|
|
||||||
$string = trim($raw_string);
|
$string = trim($string);
|
||||||
|
|
||||||
// early abort: '' and '0' (strings that convert to false) are invalid
|
// early abort: '' and '0' (strings that convert to false) are invalid
|
||||||
if (!$string) return false;
|
if (!$string) return false;
|
||||||
@ -23,18 +23,20 @@ class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
|
|||||||
// and plus it would complicate optimization efforts (you never
|
// and plus it would complicate optimization efforts (you never
|
||||||
// see that anyway).
|
// see that anyway).
|
||||||
$matches = array();
|
$matches = array();
|
||||||
$pattern = '/(?:\s|\A)'.
|
$pattern = '/(?:(?<=\s)|\A)'.
|
||||||
'((?:-?[A-Za-z_]|--)[A-Za-z_\-0-9]*)'.
|
'((?:--|-?[A-Za-z_])[A-Za-z_\-0-9]*)'.
|
||||||
'(?:\s|\z)/';
|
'(?:(?=\s)|\z)/';
|
||||||
preg_match_all($pattern, $string, $matches);
|
preg_match_all($pattern, $string, $matches);
|
||||||
|
|
||||||
|
if (empty($matches[1])) return false;
|
||||||
|
|
||||||
$new_string = '';
|
$new_string = '';
|
||||||
foreach ($matches[1] as $class_names) {
|
foreach ($matches[1] as $class_names) {
|
||||||
$new_string .= $class_names . ' ';
|
$new_string .= $class_names . ' ';
|
||||||
}
|
}
|
||||||
$new_string = rtrim($new_string);
|
$new_string = rtrim($new_string);
|
||||||
|
|
||||||
return ($new_string == $raw_string) ? true : $new_string ? $new_string : false;
|
return $new_string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,16 +14,14 @@ class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
|
|||||||
$this->case_sensitive = $case_sensitive;
|
$this->case_sensitive = $case_sensitive;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validate($raw_string) {
|
function validate($string) {
|
||||||
$string = trim($raw_string);
|
$string = trim($string);
|
||||||
if (!$this->case_sensitive) {
|
if (!$this->case_sensitive) {
|
||||||
$string = ctype_lower($string) ? $string : strtolower($string);
|
$string = ctype_lower($string) ? $string : strtolower($string);
|
||||||
}
|
}
|
||||||
$result = isset($this->valid_values[$string]);
|
$result = isset($this->valid_values[$string]);
|
||||||
|
|
||||||
// if strings equal, return result, otherwise, return
|
return $result ? $string : false;
|
||||||
// the new string on a good result and false on a bad one
|
|
||||||
return ($string == $raw_string) ? $result : $result ? $string : false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,9 @@ require_once 'HTMLPurifier/IDAccumulator.php';
|
|||||||
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
|
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 ($id === '') return false;
|
||||||
if (isset($accumulator->ids[$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
|
// if no change was made to the ID, return the result
|
||||||
// else, return the new id if stripping whitespace made it
|
// else, return the new id if stripping whitespace made it
|
||||||
// valid, or return false.
|
// valid, or return false.
|
||||||
return ($id == $old_id) ? $result : ($result ? $id : false);
|
return $result ? $id : false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,7 @@ class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
|
|||||||
{
|
{
|
||||||
|
|
||||||
function validate($string) {
|
function validate($string) {
|
||||||
$new_string = $this->parseCDATA($string);
|
return $this->parseCDATA($string);
|
||||||
return ($string == $new_string) ? true : $new_string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,15 @@ class HTMLPurifier_Config
|
|||||||
var $attr_class_blacklist = array();
|
var $attr_class_blacklist = array();
|
||||||
var $attr_class_whitelist = 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() {
|
function createDefault() {
|
||||||
$config = new HTMLPurifier_Config();
|
$config = new HTMLPurifier_Config();
|
||||||
return $config;
|
return $config;
|
||||||
|
@ -1,29 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDefHarness.php';
|
||||||
require_once 'HTMLPurifier/AttrDef/Class.php';
|
require_once 'HTMLPurifier/AttrDef/Class.php';
|
||||||
require_once 'HTMLPurifier/Config.php';
|
require_once 'HTMLPurifier/Config.php';
|
||||||
|
|
||||||
class HTMLPurifier_AttrDef_ClassTest extends UnitTestCase
|
class HTMLPurifier_AttrDef_ClassTest extends HTMLPurifier_AttrDefHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function testDefault() {
|
function testDefault() {
|
||||||
|
|
||||||
$def = new HTMLPurifier_AttrDef_Class();
|
$this->def = new HTMLPurifier_AttrDef_Class();
|
||||||
|
|
||||||
$this->assertTrue($def->validate('valid'));
|
$this->assertDef('valid');
|
||||||
$this->assertTrue($def->validate('a0-_'));
|
$this->assertDef('a0-_');
|
||||||
$this->assertTrue($def->validate('-valid'));
|
$this->assertDef('-valid');
|
||||||
$this->assertTrue($def->validate('_valid'));
|
$this->assertDef('_valid');
|
||||||
$this->assertTrue($def->validate('double valid'));
|
$this->assertDef('double valid');
|
||||||
|
|
||||||
$this->assertFalse($def->validate('0invalid'));
|
$this->assertDef('0invalid', false);
|
||||||
$this->assertFalse($def->validate('-0'));
|
$this->assertDef('-0', false);
|
||||||
|
|
||||||
// test conditional replacement
|
// test conditional replacement
|
||||||
$this->assertEqual('validassoc', $def->validate('validassoc 0invalid'));
|
$this->assertDef('validassoc 0invalid', 'validassoc');
|
||||||
|
|
||||||
// test whitespace leniency
|
// test whitespace leniency
|
||||||
$this->assertTrue('double valid', $def->validate(" double\nvalid\r"));
|
$this->assertDef(" double\nvalid\r", 'double valid');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDefHarness.php';
|
||||||
require_once 'HTMLPurifier/AttrDef/Enum.php';
|
require_once 'HTMLPurifier/AttrDef/Enum.php';
|
||||||
|
|
||||||
class HTMLPurifier_AttrDef_EnumTest extends UnitTestCase
|
class HTMLPurifier_AttrDef_EnumTest extends HTMLPurifier_AttrDefHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function testCaseInsensitive() {
|
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->assertDef('one');
|
||||||
$this->assertTrue($def->validate('ONE'));
|
$this->assertDef('ONE', 'one');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCaseSensitive() {
|
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->assertDef('one');
|
||||||
$this->assertFalse($def->validate('ONE'));
|
$this->assertDef('ONE', false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testFixing() {
|
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');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,37 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDefHarness.php';
|
||||||
require_once 'HTMLPurifier/AttrDef/ID.php';
|
require_once 'HTMLPurifier/AttrDef/ID.php';
|
||||||
require_once 'HTMLPurifier/IDAccumulator.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() {
|
function test() {
|
||||||
|
|
||||||
$acc = new HTMLPurifier_IDAccumulator();
|
$this->id_accumulator = new HTMLPurifier_IDAccumulator();
|
||||||
$def = new HTMLPurifier_AttrDef_ID();
|
$this->def = new HTMLPurifier_AttrDef_ID();
|
||||||
|
|
||||||
generate_mock_once('HTMLPurifier_Config');
|
|
||||||
|
|
||||||
$config = new HTMLPurifier_ConfigMock();
|
|
||||||
|
|
||||||
// valid ID names
|
// valid ID names
|
||||||
$this->assertTrue($def->validate('alpha', $config, $acc));
|
$this->assertDef('alpha');
|
||||||
$this->assertTrue($def->validate('al_ha', $config, $acc));
|
$this->assertDef('al_ha');
|
||||||
$this->assertTrue($def->validate('a0-:.', $config, $acc));
|
$this->assertDef('a0-:.');
|
||||||
$this->assertTrue($def->validate('a' , $config, $acc));
|
$this->assertDef('a');
|
||||||
|
|
||||||
// invalid ID names
|
// invalid ID names
|
||||||
$this->assertFalse($def->validate('<asa', $config, $acc));
|
$this->assertDef('<asa', false);
|
||||||
$this->assertFalse($def->validate('0123', $config, $acc));
|
$this->assertDef('0123', false);
|
||||||
$this->assertFalse($def->validate('.asa', $config, $acc));
|
$this->assertDef('.asa', false);
|
||||||
|
|
||||||
// test duplicate detection
|
// test duplicate detection
|
||||||
$this->assertFalse($def->validate('a' , $config, $acc));
|
$this->assertDef('a', false);
|
||||||
|
|
||||||
// valid once whitespace stripped, but needs to be amended
|
// valid once whitespace stripped, but needs to be amended
|
||||||
$this->assertEqual('whee', $def->validate(' whee ', $config, $acc));
|
$this->assertDef(' whee ', 'whee');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDefHarness.php';
|
||||||
require_once 'HTMLPurifier/AttrDef/Text.php';
|
require_once 'HTMLPurifier/AttrDef/Text.php';
|
||||||
|
|
||||||
class HTMLPurifier_AttrDef_TextTest extends UnitTestCase
|
class HTMLPurifier_AttrDef_TextTest extends HTMLPurifier_AttrDefHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
|
||||||
$def = new HTMLPurifier_AttrDef_Text();
|
$this->def = new HTMLPurifier_AttrDef_Text();
|
||||||
|
|
||||||
$this->assertTrue($def->validate('This is spiffy text!'));
|
$this->assertDef('This is spiffy text!');
|
||||||
$this->assertEqual('Casual CDATA parsecheck.',
|
$this->assertDef(" Casual\tCDATA parse\ncheck. ", 'Casual CDATA parsecheck.');
|
||||||
$def->validate(" Casual\tCDATA parse\ncheck. "));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
24
tests/HTMLPurifier/AttrDefHarness.php
Normal file
24
tests/HTMLPurifier/AttrDefHarness.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/StrategyAbstractTest.php';
|
require_once 'HTMLPurifier/StrategyHarness.php';
|
||||||
require_once 'HTMLPurifier/Strategy/Core.php';
|
require_once 'HTMLPurifier/Strategy/Core.php';
|
||||||
|
|
||||||
class HTMLPurifier_Strategy_CoreTest
|
class HTMLPurifier_Strategy_CoreTest
|
||||||
extends HTMLPurifier_StrategyAbstractTest
|
extends HTMLPurifier_StrategyHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/StrategyAbstractTest.php';
|
require_once 'HTMLPurifier/StrategyHarness.php';
|
||||||
require_once 'HTMLPurifier/Strategy/FixNesting.php';
|
require_once 'HTMLPurifier/Strategy/FixNesting.php';
|
||||||
|
|
||||||
class HTMLPurifier_Strategy_FixNestingTest
|
class HTMLPurifier_Strategy_FixNestingTest
|
||||||
extends HTMLPurifier_StrategyAbstractTest
|
extends HTMLPurifier_StrategyHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/StrategyAbstractTest.php';
|
require_once 'HTMLPurifier/StrategyHarness.php';
|
||||||
require_once 'HTMLPurifier/Strategy/MakeWellFormed.php';
|
require_once 'HTMLPurifier/Strategy/MakeWellFormed.php';
|
||||||
|
|
||||||
class HTMLPurifier_Strategy_MakeWellFormedTest
|
class HTMLPurifier_Strategy_MakeWellFormedTest
|
||||||
extends HTMLPurifier_StrategyAbstractTest
|
extends HTMLPurifier_StrategyHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/StrategyAbstractTest.php';
|
require_once 'HTMLPurifier/StrategyHarness.php';
|
||||||
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
|
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
|
||||||
|
|
||||||
class HTMLPurifier_Strategy_RemoveForeignElementsTest
|
class HTMLPurifier_Strategy_RemoveForeignElementsTest
|
||||||
extends HTMLPurifier_StrategyAbstractTest
|
extends HTMLPurifier_StrategyHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once('HTMLPurifier/Config.php');
|
require_once('HTMLPurifier/Config.php');
|
||||||
require_once('HTMLPurifier/StrategyAbstractTest.php');
|
require_once('HTMLPurifier/StrategyHarness.php');
|
||||||
require_once('HTMLPurifier/Strategy/ValidateAttributes.php');
|
require_once('HTMLPurifier/Strategy/ValidateAttributes.php');
|
||||||
|
|
||||||
class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||||
HTMLPurifier_StrategyAbstractTest
|
HTMLPurifier_StrategyHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
@ -3,12 +3,12 @@
|
|||||||
require_once 'HTMLPurifier/Definition.php';
|
require_once 'HTMLPurifier/Definition.php';
|
||||||
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
||||||
|
|
||||||
class HTMLPurifier_StrategyAbstractTest extends UnitTestCase
|
class HTMLPurifier_StrategyHarness extends UnitTestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
var $lex, $gen;
|
var $lex, $gen;
|
||||||
|
|
||||||
function HTMLPurifier_StrategyAbstractTest() {
|
function HTMLPurifier_StrategyHarness() {
|
||||||
$this->UnitTestCase();
|
$this->UnitTestCase();
|
||||||
|
|
||||||
// we can't use the DOM lexer since it does too much stuff
|
// we can't use the DOM lexer since it does too much stuff
|
Loading…
Reference in New Issue
Block a user