0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 13:21:51 +00:00
- Factor out Config and Context object population through arrays
- Bring dependent assertions together in IDTest.php
- AttrDefHarness.php now resets context and configuration between tests
- Add missing reference operator in AttrDef/ID.php

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@494 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-10-21 18:18:36 +00:00
parent f3646a3a06
commit 7d2fe4c5d7
9 changed files with 118 additions and 14 deletions

View File

@ -21,7 +21,7 @@ class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
if ($id === '') return false;
$id_accumulator = $context->get('IDAccumulator');
$id_accumulator =& $context->get('IDAccumulator');
if (isset($id_accumulator->ids[$id])) return false;
// we purposely avoid using regex, hopefully this is faster

View File

@ -124,6 +124,27 @@ class HTMLPurifier_Config
return $this->css_definition;
}
/**
* Loads configuration values from an array with the following structure:
* Namespace.Directive => Value
* @param $config_array Configuration associative array
*/
function loadArray($config_array) {
foreach ($config_array as $key => $value) {
if (strpos($key, '.') !== false) {
// condensed form
list($namespace, $directive) = explode('.', $key);
$this->set($namespace, $directive, $value);
} else {
$namespace = $key;
$namespace_values = $value;
foreach ($namespace_values as $directive => $value) {
$this->set($namespace, $directive, $value);
}
}
}
}
}
?>

View File

@ -60,6 +60,16 @@ class HTMLPurifier_Context
return isset($this->_storage[$name]);
}
/**
* Loads a series of variables from an associative array
* @param $context_array Assoc array of variables to load
*/
function loadArray(&$context_array) {
foreach ($context_array as $key => $discard) {
$this->register($key, $context_array[$key]);
}
}
}
?>

View File

@ -26,7 +26,8 @@ class HTMLPurifier_AttrDef_IDTest extends HTMLPurifier_AttrDefHarness
$this->assertDef('.asa', false);
// test duplicate detection
$this->assertDef('a', false);
$this->assertDef('once');
$this->assertDef('once', false);
// valid once whitespace stripped, but needs to be amended
$this->assertDef(' whee ', 'whee');

View File

@ -188,8 +188,8 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
}
// the read in values
$this->config = isset($config[$i]) ? $config[$i] : null;
$this->context = isset($context[$i]) ? $context[$i] : null;
$this->config = isset($config[$i]) ? $config[$i] : HTMLPurifier_Config::createDefault();
$this->context = isset($context[$i]) ? $context[$i] : new HTMLPurifier_Context();
$this->assertDef($value, $expect_uri[$i], true, "Test $i: %s");

View File

@ -7,11 +7,14 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase
var $context;
var $config;
function setUp() {
$this->config = HTMLPurifier_Config::createDefault();
$this->context = new HTMLPurifier_Context();
}
// cannot be used for accumulator
function assertDef($string, $expect = true, $ini = false, $message = '%s') {
// $expect can be a string or bool
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
if (!$this->context) $this->context = new HTMLPurifier_Context();
if ($ini) $this->setUpAssertDef();
$result = $this->def->validate($string, $this->config, $this->context);
if ($expect === true) {

View File

@ -105,6 +105,48 @@ class HTMLPurifier_ConfigTest extends UnitTestCase
}
function test_loadArray() {
// setup a few dummy namespaces/directives for our testing
HTMLPurifier_ConfigSchema::defineNamespace('Zoo', 'Animals we have.');
HTMLPurifier_ConfigSchema::define('Zoo', 'Aadvark', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define('Zoo', 'Boar', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define('Zoo', 'Camel', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define(
'Zoo', 'Others', array(), 'list', 'Other animals we have one of.'
);
$config_manual = HTMLPurifier_Config::createDefault();
$config_loadabbr = HTMLPurifier_Config::createDefault();
$config_loadfull = HTMLPurifier_Config::createDefault();
$config_manual->set('Zoo', 'Aadvark', 3);
$config_manual->set('Zoo', 'Boar', 5);
$config_manual->set('Zoo', 'Camel', 2000); // that's a lotta camels!
$config_manual->set('Zoo', 'Others', array('Peacock', 'Dodo')); // wtf!
// condensed form
$config_loadabbr->loadArray(array(
'Zoo.Aadvark' => 3,
'Zoo.Boar' => 5,
'Zoo.Camel' => 2000,
'Zoo.Others' => array('Peacock', 'Dodo')
));
// fully expanded form
$config_loadfull->loadArray(array(
'Zoo' => array(
'Aadvark' => 3,
'Boar' => 5,
'Camel' => 2000,
'Others' => array('Peacock', 'Dodo')
)
));
$this->assertEqual($config_manual, $config_loadabbr);
$this->assertEqual($config_manual, $config_loadfull);
}
}
?>

View File

@ -57,6 +57,36 @@ class HTMLPurifier_ContextTest extends UnitTestCase
}
function test_loadArray() {
// references can be *really* wonky!
$context_manual = new HTMLPurifier_Context();
$context_load = new HTMLPurifier_Context();
$var1 = 1;
$var2 = 2;
$context_manual->register('var1', $var1);
$context_manual->register('var2', $var2);
// you MUST set up the references when constructing the array,
// otherwise the registered version will be a copy
$array = array(
'var1' => &$var1,
'var2' => &$var2
);
$context_load->loadArray($array);
$this->assertIdentical($context_manual, $context_load);
$var1 = 10;
$var2 = 20;
$this->assertIdentical($context_manual, $context_load);
}
}
?>

View File

@ -64,16 +64,13 @@ class HTMLPurifier_Harness extends UnitTestCase
// setup config object
$config = HTMLPurifier_Config::createDefault();
foreach ($config_array as $key => $value) {
list($namespace, $directive) = explode('.', $key);
$config->set($namespace, $directive, $value);
}
$config->loadArray($config_array);
// setup context object
// setup context object. Note that we are operating on a copy of it!
// We will extend the test harness to allow you to do post-tests
// on the context object
$context = new HTMLPurifier_Context();
foreach ($context_array as $key => $value) {
$context->register($key, $value);
}
$context->loadArray($context_array);
if ($this->to_tokens && is_string($input)) {
$input = $this->lexer->tokenizeHTML($input, $config, $context);