From 7d2fe4c5d7bdef2535c8fc9926263ed301f51cc8 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 21 Oct 2006 18:18:36 +0000 Subject: [PATCH] [1.2.0] - 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 --- library/HTMLPurifier/AttrDef/ID.php | 2 +- library/HTMLPurifier/Config.php | 21 +++++++++++++ library/HTMLPurifier/Context.php | 10 ++++++ tests/HTMLPurifier/AttrDef/IDTest.php | 3 +- tests/HTMLPurifier/AttrDef/URITest.php | 4 +-- tests/HTMLPurifier/AttrDefHarness.php | 7 +++-- tests/HTMLPurifier/ConfigTest.php | 42 ++++++++++++++++++++++++++ tests/HTMLPurifier/ContextTest.php | 30 ++++++++++++++++++ tests/HTMLPurifier/Harness.php | 13 +++----- 9 files changed, 118 insertions(+), 14 deletions(-) diff --git a/library/HTMLPurifier/AttrDef/ID.php b/library/HTMLPurifier/AttrDef/ID.php index 05ceaa36..03d80b46 100644 --- a/library/HTMLPurifier/AttrDef/ID.php +++ b/library/HTMLPurifier/AttrDef/ID.php @@ -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 diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index 9bffaab3..18631cc8 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -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); + } + } + } + } + } ?> \ No newline at end of file diff --git a/library/HTMLPurifier/Context.php b/library/HTMLPurifier/Context.php index 93942ada..08e17655 100644 --- a/library/HTMLPurifier/Context.php +++ b/library/HTMLPurifier/Context.php @@ -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]); + } + } + } ?> \ No newline at end of file diff --git a/tests/HTMLPurifier/AttrDef/IDTest.php b/tests/HTMLPurifier/AttrDef/IDTest.php index 87b5670c..eebb3f08 100644 --- a/tests/HTMLPurifier/AttrDef/IDTest.php +++ b/tests/HTMLPurifier/AttrDef/IDTest.php @@ -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'); diff --git a/tests/HTMLPurifier/AttrDef/URITest.php b/tests/HTMLPurifier/AttrDef/URITest.php index 1b9287b4..209f2915 100644 --- a/tests/HTMLPurifier/AttrDef/URITest.php +++ b/tests/HTMLPurifier/AttrDef/URITest.php @@ -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"); diff --git a/tests/HTMLPurifier/AttrDefHarness.php b/tests/HTMLPurifier/AttrDefHarness.php index d98eb9bf..e6c6359e 100644 --- a/tests/HTMLPurifier/AttrDefHarness.php +++ b/tests/HTMLPurifier/AttrDefHarness.php @@ -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) { diff --git a/tests/HTMLPurifier/ConfigTest.php b/tests/HTMLPurifier/ConfigTest.php index 6812c4d2..bec55540 100644 --- a/tests/HTMLPurifier/ConfigTest.php +++ b/tests/HTMLPurifier/ConfigTest.php @@ -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); + + } + } ?> \ No newline at end of file diff --git a/tests/HTMLPurifier/ContextTest.php b/tests/HTMLPurifier/ContextTest.php index caf8e367..232c1c76 100644 --- a/tests/HTMLPurifier/ContextTest.php +++ b/tests/HTMLPurifier/ContextTest.php @@ -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); + + } + } ?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Harness.php b/tests/HTMLPurifier/Harness.php index ff59d997..c9ccf58f 100644 --- a/tests/HTMLPurifier/Harness.php +++ b/tests/HTMLPurifier/Harness.php @@ -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);