From 2135597553a4b3a6525872968518b40b29b8a53f Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Tue, 5 Feb 2008 01:54:20 +0000 Subject: [PATCH] - Implement StringHash wrapper ArrayObject - Implement namespace parsing for StringHashAdapter, as well as rudimentary error-checking git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1529 48356398-32a2-884e-a903-53898d9a118a --- extras/ConfigSchema/StringHash.php | 33 ++++++++++++++++++++ extras/ConfigSchema/StringHashAdapter.php | 29 ++++++++++++++--- tests/ConfigSchema/StringHashAdapterTest.php | 22 ++++++++++++- tests/ConfigSchema/StringHashTest.php | 18 +++++++++++ tests/test_files.php | 1 + 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 extras/ConfigSchema/StringHash.php create mode 100644 tests/ConfigSchema/StringHashTest.php diff --git a/extras/ConfigSchema/StringHash.php b/extras/ConfigSchema/StringHash.php new file mode 100644 index 00000000..1e105907 --- /dev/null +++ b/extras/ConfigSchema/StringHash.php @@ -0,0 +1,33 @@ +accessed[$index] = true; + return parent::offsetGet($index); + } + + /** + * Returns a lookup array of all array indexes that have been accessed. + * @return Array in form array($index => true). + */ + public function getAccessed() { + return $this->accessed; + } + + /** + * Resets the access array. + */ + public function resetAccessed() { + $this->accessed = array(); + } +} diff --git a/extras/ConfigSchema/StringHashAdapter.php b/extras/ConfigSchema/StringHashAdapter.php index d1098f30..ca607b7c 100644 --- a/extras/ConfigSchema/StringHashAdapter.php +++ b/extras/ConfigSchema/StringHashAdapter.php @@ -12,11 +12,32 @@ class ConfigSchema_StringHashAdapter * based on its values. */ public function adapt($hash, $schema) { + + if (!isset($hash['ID'])) { + trigger_error('Missing key ID in string hash'); + return; + } + + // Check namespace: + if (strpos($hash['ID'], '.') === false) { + // This will cause problems if we decide to support nested + // namespaces, but for now it's ok. + $schema->addNamespace($hash['ID'], $hash['DESCRIPTION']); + return; + } + list($ns, $directive) = explode('.', $hash['ID'], 2); - $default = eval("return {$hash['DEFAULT']};"); - $type = $hash['TYPE']; - $description = $hash['DESCRIPTION']; - $schema->add($ns, $directive, $default, $type, $description); + + if (isset($hash['DEFAULT'], $hash['DESCRIPTION'], $hash['TYPE'])) { + $raw_default = $hash['DEFAULT']; + $default = eval("return $raw_default;"); + $description = $hash['DESCRIPTION']; + $type = $hash['TYPE']; + $schema->add($ns, $directive, $default, $type, $description); + } + + + } } diff --git a/tests/ConfigSchema/StringHashAdapterTest.php b/tests/ConfigSchema/StringHashAdapterTest.php index 087d6099..d29bc347 100644 --- a/tests/ConfigSchema/StringHashAdapterTest.php +++ b/tests/ConfigSchema/StringHashAdapterTest.php @@ -7,7 +7,7 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase parent::UnitTestCase(); } - function assertAdapt($input, $calls) { + function assertAdapt($input, $calls = array()) { $schema = new HTMLPurifier_ConfigSchemaMock(); foreach ($calls as $func => $params) { $schema->expectOnce($func, $params); @@ -32,4 +32,24 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase ) ); } + + function testNamespace() { + $this->assertAdapt( + array( + 'ID' => 'Namespace', + 'DESCRIPTION' => 'Description of namespace' + ), + array( + 'addNamespace' => array('Namespace', 'Description of namespace'), + ) + ); + } + + function testMissingId() { + $this->expectError('Missing key ID in string hash'); + $this->assertAdapt(array()); + } + + + } diff --git a/tests/ConfigSchema/StringHashTest.php b/tests/ConfigSchema/StringHashTest.php new file mode 100644 index 00000000..138741ff --- /dev/null +++ b/tests/ConfigSchema/StringHashTest.php @@ -0,0 +1,18 @@ + 'value', + 'key2' => 'value2' + )); + $this->assertIdentical($hash->getAccessed(), array()); + $t = $hash['key']; + $this->assertIdentical($hash->getAccessed(), array('key' => true)); + $hash->resetAccessed(); + $this->assertIdentical($hash->getAccessed(), array()); + } + +} diff --git a/tests/test_files.php b/tests/test_files.php index 73527851..d4f115d5 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -138,3 +138,4 @@ $test_files[] = 'FSTools/FileTest.php'; $test_files[] = 'ConfigSchema/StringHashAdapterTest.php'; $test_files[] = 'ConfigSchema/StringHashParserTest.php'; +$test_files[] = 'ConfigSchema/StringHashTest.php';