1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-08 07:06:46 +02:00

Revamp Configuration classes, breaking backwards configuration compatibility (not that there was much to broken to begin with). Fix bug involving PHP 4 object typecasting.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@203 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2006-08-11 20:23:41 +00:00
parent 30a8266fc1
commit 0db1cbb7ac
9 changed files with 222 additions and 83 deletions

View File

@@ -0,0 +1,63 @@
<?php
require_once 'HTMLPurifier/ConfigDef.php';
class HTMLPurifier_ConfigDefTest extends UnitTestCase
{
var $old_copy;
var $our_copy;
function setUp() {
// yes, I know this is slightly convoluted, but that's the price
// you pay for using Singletons. Good thing we can overload it.
// first, let's get a clean copy to do tests
$our_copy = new HTMLPurifier_ConfigDef();
// get the old copy
$this->old_copy = HTMLPurifier_ConfigDef::instance();
// put in our copy, and reassign to the REAL reference
$this->our_copy =& HTMLPurifier_ConfigDef::instance($our_copy);
}
function tearDown() {
// testing is done, restore the old copy
HTMLPurifier_ConfigDef::instance($this->old_copy);
}
function testNormal() {
HTMLPurifier_ConfigDef::defineNamespace('Core', 'Configuration that '.
'is always available.');
$this->assertIdentical( array(
'Core' => array()
), $this->our_copy->info);
// note that the description is silently dropped
HTMLPurifier_ConfigDef::define('Core', 'Name', 'default value',
'This is a description of the directive.');
$this->assertIdentical( array(
'Core' => array(
'Name' => 'default value'
)
), $this->our_copy->info);
// test an invalid namespace
HTMLPurifier_ConfigDef::define('Extension', 'Name', false, 'This is '.
'for an extension, but we have not defined its namespace!');
$this->assertError('Cannot define directive for undefined namespace');
$this->assertNoErrors();
$this->swallowErrors();
// test overloading already defined value
HTMLPurifier_ConfigDef::define('Core', 'Name', 89,
'What, you\'re not allowed to overload directives? Bummer!');
$this->assertError('Cannot redefine directive');
$this->assertNoErrors();
$this->swallowErrors();
}
}
?>

View File

@@ -0,0 +1,44 @@
<?php
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_ConfigTest extends UnitTestCase
{
function test() {
$def = new HTMLPurifier_ConfigDef();
$def->info = array(
'Core' => array('Key' => false),
'Attr' => array('Key' => 42),
'Extension' => array('Pert' => 'moo')
);
$config = new HTMLPurifier_Config($def);
// test default value retrieval
$this->assertIdentical($config->get('Core', 'Key'), false);
$this->assertIdentical($config->get('Attr', 'Key'), 42);
$this->assertIdentical($config->get('Extension', 'Pert'), 'moo');
// set some values
$config->set('Core', 'Key', 'foobar');
$this->assertIdentical($config->get('Core', 'Key'), 'foobar');
// try to retrieve undefined value
$config->get('Core', 'NotDefined');
$this->assertError('Cannot retrieve value of undefined directive');
$this->assertNoErrors();
$this->swallowErrors();
// try to set undefined value
$config->set('Foobar', 'Key', 'foobar');
$this->assertError('Cannot set undefined directive to value');
$this->assertNoErrors();
$this->swallowErrors();
}
}
?>

View File

@@ -46,7 +46,7 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
$inputs[7] = '<div id="invalid">Invalid</div>';
$expect[7] = '<div>Invalid</div>';
$config[7] = HTMLPurifier_Config::createDefault();
$config[7]->attr_id_blacklist = array('invalid');
$config[7]->set('Attr', 'IDBlacklist', array('invalid'));
// test classes
$inputs[8] = '<div class="valid">Valid</div>';

View File

@@ -34,11 +34,16 @@ function generate_mock_once($name) {
Mock::generate($name, $mock_name);
}
// this has to be defined before we do any includes of library files
require_once 'HTMLPurifier/ConfigDef.php';
// define callable test files
$test_files = array();
$test_files[] = 'ConfigTest.php';
$test_files[] = 'ConfigDefTest.php';
$test_files[] = 'LexerTest.php';
$test_files[] = 'Lexer/DirectLexTest.php';
//$test_files[] = 'TokenTest.php';
$test_files[] = 'TokenTest.php';
$test_files[] = 'ChildDefTest.php';
$test_files[] = 'GeneratorTest.php';
$test_files[] = 'EntityLookupTest.php';
@@ -57,28 +62,45 @@ $test_files[] = 'AttrDef/LangTest.php';
$test_files[] = 'AttrDef/PixelsTest.php';
$test_files[] = 'AttrDef/LengthTest.php';
$test_files[] = 'AttrDef/NumberSpanTest.php';
//$test_files[] = 'AttrDef/URITest.php';
$test_files[] = 'IDAccumulatorTest.php';
$test_files[] = 'TagTransformTest.php';
$test_files[] = 'AttrTransform/LangTest.php';
$test_files[] = 'AttrTransform/TextAlignTest.php';
$test_file_lookup = array_flip($test_files);
function htmlpurifier_path2class($path) {
$temp = $path;
$temp = str_replace('./', '', $temp); // remove leading './'
$temp = str_replace('.\\', '', $temp); // remove leading '.\'
$temp = str_replace('\\', '_', $temp); // normalize \ to _
$temp = str_replace('/', '_', $temp); // normalize / to _
while(strpos($temp, '__') !== false) $temp = str_replace('__', '_', $temp);
$temp = str_replace('.php', '', $temp);
return $temp;
}
// we can't use addTestFile because SimpleTest chokes on E_STRICT warnings
if (isset($_GET['file']) && isset($test_file_lookup[$_GET['file']])) {
// execute only one test
$test_file = $_GET['file'];
$test = new GroupTest('HTMLPurifier - ' . $test_file);
$test->addTestFile('HTMLPurifier/' . $test_file);
$path = 'HTMLPurifier/' . $test_file;
require_once $path;
$test->addTestClass(htmlpurifier_path2class($path));
} else {
$test = new GroupTest('HTMLPurifier');
foreach ($test_files as $test_file) {
$test->addTestFile('HTMLPurifier/' . $test_file);
$path = 'HTMLPurifier/' . $test_file;
require_once $path;
$test->addTestClass(htmlpurifier_path2class($path));
}
}
@@ -88,4 +110,4 @@ else $reporter = new HTMLReporter();
$test->run($reporter);
?>
?>