1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-05 05:37:49 +02:00

[3.1.0] Move ConfigSchema to HTMLPurifier core

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1576 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-02-24 06:19:28 +00:00
parent d3c04de9dc
commit 002fe649f7
19 changed files with 43 additions and 55 deletions

View File

@@ -0,0 +1,137 @@
<?php
class HTMLPurifier_ConfigSchema_StringHashAdapterTest extends UnitTestCase
{
function __construct() {
generate_mock_once('HTMLPurifier_ConfigSchema');
parent::UnitTestCase();
}
function assertAdapt($input, $calls = array()) {
$schema = new HTMLPurifier_ConfigSchemaMock();
$called = array();
foreach ($calls as $signature) {
list($func, $params) = $signature;
if (!isset($called[$func])) $called[$func] = 0;
$schema->expectAt($called[$func]++, $func, $params);
}
$adapter = new HTMLPurifier_ConfigSchema_StringHashAdapter();
$adapter->adapt($input, $schema);
}
function testBasic() {
$this->assertAdapt(
array(
'ID' => 'Namespace.Directive',
'DEFAULT' => "'default' . 'bar'",
'TYPE' => 'string',
'DESCRIPTION' => "Description of default.\n",
),
array(
array('add', array(
'Namespace', 'Directive', 'defaultbar', 'string',
"Description of default.\n"
)),
)
);
}
function testNamespace() {
$this->assertAdapt(
array(
'ID' => 'Namespace',
'DESCRIPTION' => 'Description of namespace',
),
array(
array('addNamespace', array('Namespace', 'Description of namespace')),
)
);
}
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', 'Dir2', 'Ns', 'Dir')),
array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')),
)
);
}
function testCombo() {
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'DEFAULT' => "'val' . '1'",
'TYPE' => 'string',
'DESCRIPTION' => "Description of default.\n",
'VALUE-ALIASES' => "
'milk' => 'val1',
'cheese' => 'val1',
",
'ALLOWED' => "'val1', 'val2'",
'ALIASES' => "Ns.Dir2, Ns2.Dir",
),
array(
array('add', array(
'Ns', 'Dir', 'val1', 'string',
"Description of default.\n"
)),
array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))),
array('addValueAliases', array('Ns', 'Dir', array('milk' => 'val1', 'cheese' => 'val1'))),
array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')),
array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')),
)
);
}
function testMissingIdError() {
$this->expectError('Missing key ID in string hash');
$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

@@ -0,0 +1,4 @@
--KEY--
Line1
--KEY--
Line2

View File

@@ -0,0 +1 @@
DefaultValue

View File

@@ -0,0 +1,2 @@
KEY: Original
KEY: New

View File

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

View File

@@ -0,0 +1,55 @@
<?php
/**
* @note Sample input files are located in the StringHashParser/ directory.
*/
class HTMLPurifier_ConfigSchema_StringHashParserTest extends UnitTestCase
{
/**
* Instance of ConfigSchema_StringHashParser being tested.
*/
protected $parser;
function setup() {
$this->parser = new HTMLPurifier_ConfigSchema_StringHashParser();
}
/**
* Assert that $file gets parsed into the form of $expect
*/
function assertParse($file, $expect) {
$result = $this->parser->parseFile(dirname(__FILE__) . '/StringHashParser/' . $file);
$this->assertIdentical($result, $expect);
}
function testSimple() {
$this->assertParse('Simple.txt', array(
'ID' => 'Namespace.Directive',
'TYPE' => 'string',
'CHAIN-ME' => '2',
'DESCRIPTION' => "Multiline\nstuff\n",
'FOR-WHO' => "Single multiline\n",
));
}
function testOverrideSingle() {
$this->assertParse('OverrideSingle.txt', array(
'KEY' => 'New',
));
}
function testAppendMultiline() {
$this->assertParse('AppendMultiline.txt', array(
'KEY' => "Line1\nLine2\n",
));
}
function testDefault() {
$this->parser->default = 'NEW-ID';
$this->assertParse('Default.txt', array(
'NEW-ID' => 'DefaultValue',
));
}
}

View File

@@ -0,0 +1,102 @@
<?php
class HTMLPurifier_ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase
{
function makeSchema() {
$schema = new HTMLPurifier_ConfigSchema();
$schema->addNamespace('Ns', 'Description of ns.');
$schema->addNamespace('Ns2', 'Description of ns2.');
$schema->add('Ns', 'Dir', 'dairy', 'string',
"Description of default.\nThis directive has been available since 1.2.0.");
$schema->addAllowedValues('Ns', 'Dir', array('dairy', 'meat'));
$schema->addValueAliases('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'));
$schema->addAlias('Ns', 'Dir2', 'Ns', 'Dir');
$schema->addAlias('Ns2', 'Dir', 'Ns', 'Dir');
return $schema;
}
function testNamespace() {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->get('Ns');
$expect = array(
'ID' => 'Ns',
'DESCRIPTION' => "Description of ns.",
);
$this->assertIdentical($result, $expect);
}
function testBadNamespace() {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$this->expectError("Namespace 'BadNs' doesn't exist in schema");
$adapter->get('BadNs');
}
function testDirective() {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->get('Ns', 'Dir');
$expect = array(
'ID' => 'Ns.Dir',
'TYPE' => 'string',
'VERSION' => '1.2.0',
'DEFAULT' => "'dairy'",
'DESCRIPTION' => "Description of default.\n",
'ALLOWED' => "'dairy', 'meat'",
'VALUE-ALIASES' => "'milk' => 'dairy',\n'cheese' => 'dairy',\n",
'ALIASES' => "Ns.Dir2, Ns2.Dir",
);
$this->assertIdentical($result, $expect);
}
function testBadDirective() {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$this->expectError("Directive 'BadNs.BadDir' doesn't exist in schema");
$adapter->get('BadNs', 'BadDir');
}
function assertMethod($func, $input, $expect) {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->$func($input);
$this->assertIdentical($result, $expect);
}
function testExportEmptyHash() {
$this->assertMethod('exportHash', array(), '');
}
function testExportHash() {
$this->assertMethod('exportHash', array('foo' => 'bar'), "'foo' => 'bar',\n");
}
function testExportEmptyLookup() {
$this->assertMethod('exportLookup', array(), '');
}
function testExportSingleLookup() {
$this->assertMethod('exportLookup', array('key' => true), "'key'");
}
function testExportLookup() {
$this->assertMethod('exportLookup', array('key' => true, 'key2' => true, 3 => true), "'key', 'key2', 3");
}
function assertExtraction($desc, $expect_desc, $expect_version) {
$adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema());
list($result_desc, $result_version) = $adapter->extractVersion($desc);
$this->assertIdentical($result_desc, $expect_desc);
$this->assertIdentical($result_version, $expect_version);
}
function testExtractSimple() {
$this->assertExtraction("Desc.\nThis directive has been available since 2.0.0.", "Desc.\n", '2.0.0');
}
function testExtractMultiline() {
$this->assertExtraction("Desc.\nThis directive was available\n since 23.4.333.", "Desc.\n", '23.4.333');
}
}

View File

@@ -0,0 +1,18 @@
<?php
class HTMLPurifier_ConfigSchema_StringHashTest extends UnitTestCase
{
public function testUsed() {
$hash = new HTMLPurifier_ConfigSchema_StringHash(array(
'key' => 'value',
'key2' => 'value2'
));
$this->assertIdentical($hash->getAccessed(), array());
$t = $hash->offsetGet('key');
$this->assertIdentical($hash->getAccessed(), array('key' => true));
$hash->resetAccessed();
$this->assertIdentical($hash->getAccessed(), array());
}
}