0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

Complete Adapter implementation, also unit-test keys with dashes in them in parser.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1531 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-02-05 21:04:00 +00:00
parent b8a46821f3
commit dea117032f
4 changed files with 136 additions and 9 deletions

View File

@ -13,6 +13,10 @@ class ConfigSchema_StringHashAdapter
*/ */
public function adapt($hash, $schema) { public function adapt($hash, $schema) {
if (! $hash instanceof ConfigSchema_StringHash) {
$hash = new ConfigSchema_StringHash($hash);
}
if (!isset($hash['ID'])) { if (!isset($hash['ID'])) {
trigger_error('Missing key ID in string hash'); trigger_error('Missing key ID in string hash');
return; return;
@ -23,6 +27,7 @@ class ConfigSchema_StringHashAdapter
// This will cause problems if we decide to support nested // This will cause problems if we decide to support nested
// namespaces, but for now it's ok. // namespaces, but for now it's ok.
$schema->addNamespace($hash['ID'], $hash['DESCRIPTION']); $schema->addNamespace($hash['ID'], $hash['DESCRIPTION']);
$this->_findUnused($hash);
return; return;
} }
@ -36,8 +41,43 @@ class ConfigSchema_StringHashAdapter
$schema->add($ns, $directive, $default, $type, $description); $schema->add($ns, $directive, $default, $type, $description);
} }
if (isset($hash['VALUE-ALIASES'])) {
$raw_value_aliases = $hash['VALUE-ALIASES'];
$value_aliases = eval("return array($raw_value_aliases);");
$schema->addValueAliases($ns, $directive, $value_aliases);
}
if (isset($hash['ALLOWED'])) {
$raw_allowed = $hash['ALLOWED'];
$allowed = eval("return array($raw_allowed);");
$schema->addAllowedValues($ns, $directive, $allowed);
}
if (isset($hash['ALIASES'])) {
$raw_aliases = $hash['ALIASES'];
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
foreach ($aliases as $alias) {
list($new_ns, $new_directive) = explode('.', $alias, 2);
$schema->addAlias($ns, $directive, $new_ns, $new_directive);
}
}
$this->_findUnused($hash);
} }
/**
* Triggers errors for any unused keys passed in the hash; such keys
* may indicate typos, missing values, etc.
* @param $hash Instance of ConfigSchema_StringHash to check.
*/
protected function _findUnused($hash) {
$accessed = $hash->getAccessed();
foreach ($hash as $k => $v) {
if (!isset($accessed[$k])) {
trigger_error("String hash key '$k' not used by adapter", E_USER_NOTICE);
}
}
}
} }

View File

@ -9,8 +9,11 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase
function assertAdapt($input, $calls = array()) { function assertAdapt($input, $calls = array()) {
$schema = new HTMLPurifier_ConfigSchemaMock(); $schema = new HTMLPurifier_ConfigSchemaMock();
foreach ($calls as $func => $params) { $called = array();
$schema->expectOnce($func, $params); foreach ($calls as $signature) {
list($func, $params) = $signature;
if (!isset($called[$func])) $called[$func] = 0;
$schema->expectAt($called[$func]++, $func, $params);
} }
$adapter = new ConfigSchema_StringHashAdapter(); $adapter = new ConfigSchema_StringHashAdapter();
$adapter->adapt($input, $schema); $adapter->adapt($input, $schema);
@ -25,10 +28,10 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase
'DESCRIPTION' => "Description of default.\n", 'DESCRIPTION' => "Description of default.\n",
), ),
array( array(
'add' => array( array('add', array(
'Namespace', 'Directive', 'defaultbar', 'string', 'Namespace', 'Directive', 'defaultbar', 'string',
"Description of default.\n" "Description of default.\n"
) )),
) )
); );
} }
@ -37,19 +40,98 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase
$this->assertAdapt( $this->assertAdapt(
array( array(
'ID' => 'Namespace', 'ID' => 'Namespace',
'DESCRIPTION' => 'Description of namespace' 'DESCRIPTION' => 'Description of namespace',
), ),
array( array(
'addNamespace' => array('Namespace', 'Description of namespace'), array('addNamespace', array('Namespace', 'Description of namespace')),
) )
); );
} }
function testMissingId() { function testValueAliases() {
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'VALUE-ALIASES' => "
'milk' => 'dairy',
'cheese' => 'dairy',
",
),
array(
array('addValueAliases', array('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'))),
)
);
}
function testAllowedValues() {
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'ALLOWED' => "'val1', 'val2'",
),
array(
array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))),
)
);
}
function testAlias() {
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'ALIASES' => "Ns.Dir2, Ns2.Dir",
),
array(
array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')),
array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')),
)
);
}
function testCombo() {
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'DEFAULT' => "'default' . 'bar'",
'TYPE' => 'string',
'DESCRIPTION' => "Description of default.\n",
'VALUE-ALIASES' => "
'milk' => 'dairy',
'cheese' => 'dairy',
",
'ALLOWED' => "'val1', 'val2'",
'ALIASES' => "Ns.Dir2, Ns2.Dir",
),
array(
array('add', array(
'Ns', 'Dir', 'defaultbar', 'string',
"Description of default.\n"
)),
array('addValueAliases', array('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'))),
array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))),
array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')),
array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')),
)
);
}
function testMissingIdError() {
$this->expectError('Missing key ID in string hash'); $this->expectError('Missing key ID in string hash');
$this->assertAdapt(array()); $this->assertAdapt(array());
} }
function testExtraError() {
$this->expectError("String hash key 'FOOBAR' not used by adapter");
$this->assertAdapt(
array(
'ID' => 'Namespace',
'DESCRIPTION' => 'Description of namespace',
'FOOBAR' => 'Extra stuff',
),
array(
array('addNamespace', array('Namespace', 'Description of namespace')),
)
);
}
} }

View File

@ -1,5 +1,8 @@
Namespace.Directive Namespace.Directive
TYPE: string TYPE: string
CHAIN-ME: 2
--DESCRIPTION-- --DESCRIPTION--
Multiline Multiline
stuff stuff
--FOR-WHO--
Single multiline

View File

@ -27,7 +27,9 @@ class ConfigSchema_StringHashParserTest extends UnitTestCase
$this->assertParse('Simple.txt', array( $this->assertParse('Simple.txt', array(
'ID' => 'Namespace.Directive', 'ID' => 'Namespace.Directive',
'TYPE' => 'string', 'TYPE' => 'string',
'DESCRIPTION' => "Multiline\nstuff\n" 'CHAIN-ME' => '2',
'DESCRIPTION' => "Multiline\nstuff\n",
'FOR-WHO' => "Single multiline\n",
)); ));
} }