diff options
Diffstat (limited to 'lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema')
23 files changed, 396 insertions, 0 deletions
diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php new file mode 100644 index 000000000..0d539ea2c --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php @@ -0,0 +1,21 @@ +<?php + +class HTMLPurifier_ConfigSchema_InterchangeTest extends UnitTestCase +{ + + protected $interchange; + + public function setup() { + $this->interchange = new HTMLPurifier_ConfigSchema_Interchange(); + } + + function testAddDirective() { + $v = new HTMLPurifier_ConfigSchema_Interchange_Directive(); + $v->id = new HTMLPurifier_ConfigSchema_Interchange_Id('Namespace.Directive'); + $this->interchange->addDirective($v); + $this->assertIdentical($v, $this->interchange->directives['Namespace.Directive']); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest new file mode 100644 index 000000000..ed3857c40 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest @@ -0,0 +1,13 @@ +ERROR: Alias 'Ns.BothWantThisName' in aliases in directive 'Ns.Dir2' collides with alias for directive 'Ns.Dir' +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: int +DEFAULT: 3 +ALIASES: Ns.BothWantThisName +---- +Ns.Dir2 +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALIASES: Ns.BothWantThisName diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest new file mode 100644 index 000000000..582a481a4 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest @@ -0,0 +1,12 @@ +ERROR: Alias 'Ns.Innocent' in aliases in directive 'Ns.Dir' collides with another directive +---- +Ns.Innocent +DESCRIPTION: Innocent directive +TYPE: int +DEFAULT: 3 +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALIASES: Ns.Innocent diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedIsString.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedIsString.vtest new file mode 100644 index 000000000..9ecc5f930 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedIsString.vtest @@ -0,0 +1,7 @@ +ERROR: Value 3 in allowed in directive 'Ns.Dir' must be a string +---- +ID: Ns.Dir +TYPE: string +DESCRIPTION: Description +DEFAULT: 'asdf' +ALLOWED: 'asdf', 3 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedNotEmpty.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedNotEmpty.vtest new file mode 100644 index 000000000..1e08fab0d --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/allowedNotEmpty.vtest @@ -0,0 +1,7 @@ +ERROR: Allowed in directive 'Ns.Dir' must not be empty +---- +ID: Ns.Dir +TYPE: string +DESCRIPTION: Description +DEFAULT: 'asdf' +ALLOWED: diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest new file mode 100644 index 000000000..ef1944707 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest @@ -0,0 +1,7 @@ +ERROR: Default in directive 'Ns.Dir' must be an allowed value +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALLOWED: 'b' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultNullWithAllowed.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultNullWithAllowed.vtest new file mode 100644 index 000000000..7ba3c85c0 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultNullWithAllowed.vtest @@ -0,0 +1,5 @@ +Ns.Dir +DESCRIPTION: Directive +TYPE: string/null +DEFAULT: null +ALLOWED: 'a' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultType.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultType.vtest new file mode 100644 index 000000000..79ea043ac --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultType.vtest @@ -0,0 +1,6 @@ +ERROR: Expected type string, got integer in DEFAULT in directive hash 'Ns.Dir' +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 0 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/descriptionNotEmpty.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/descriptionNotEmpty.vtest new file mode 100644 index 000000000..d09ddc77b --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/descriptionNotEmpty.vtest @@ -0,0 +1,5 @@ +ERROR: Description in directive 'Ns.Dir' must not be empty +---- +Ns.Dir +TYPE: int +DEFAULT: 0 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/ignoreNamespace.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/ignoreNamespace.vtest new file mode 100644 index 000000000..3fc28d3a4 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/ignoreNamespace.vtest @@ -0,0 +1,3 @@ +Ns +DESCRIPTION: Namespace + diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeDefined.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeDefined.vtest new file mode 100644 index 000000000..4bb8abeb2 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeDefined.vtest @@ -0,0 +1,5 @@ +ERROR: TYPE in directive hash 'Ns.Dir' not defined +---- +Ns.Dir +DESCRIPTION: Notice that TYPE is missing +DEFAULT: 0 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeExists.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeExists.vtest new file mode 100644 index 000000000..f9851f536 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeExists.vtest @@ -0,0 +1,6 @@ +ERROR: Invalid type 'foobar' in DEFAULT in directive hash 'Ns.Dir' +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: foobar +DEFAULT: 0 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithAllowedIsStringType.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithAllowedIsStringType.vtest new file mode 100644 index 000000000..bf4e2b21b --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithAllowedIsStringType.vtest @@ -0,0 +1,7 @@ +ERROR: Type in directive 'Ns.Dir' must be a string type when used with allowed or value aliases +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: int +DEFAULT: 3 +ALLOWED: 1, 2, 3 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithValueAliasesIsStringType.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithValueAliasesIsStringType.vtest new file mode 100644 index 000000000..59cf4333a --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/typeWithValueAliasesIsStringType.vtest @@ -0,0 +1,7 @@ +ERROR: Type in directive 'Ns.Dir' must be a string type when used with allowed or value aliases +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: int +DEFAULT: 3 +VALUE-ALIASES: 2 => 3 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/unique.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/unique.vtest new file mode 100644 index 000000000..c83649e8d --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/unique.vtest @@ -0,0 +1,11 @@ +ERROR: Cannot redefine directive 'Ns.Dir' +---- +ID: Ns.Dir +DESCRIPTION: Version 1 +TYPE: int +DEFAULT: 0 +---- +ID: Ns.Dir +DESCRIPTION: Version 2 +TYPE: int +DEFAULT: 0 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasIsString.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasIsString.vtest new file mode 100644 index 000000000..b9f28eee1 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasIsString.vtest @@ -0,0 +1,7 @@ +ERROR: Alias 3 in valueAliases in directive 'Ns.Dir' must be a string +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +VALUE-ALIASES: 3 => 'a' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasNotAllowed.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasNotAllowed.vtest new file mode 100644 index 000000000..ef9c14883 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesAliasNotAllowed.vtest @@ -0,0 +1,8 @@ +ERROR: Alias 'b' in valueAliases in directive 'Ns.Dir' must not be an allowed value +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALLOWED: 'a', 'b', 'c' +VALUE-ALIASES: 'b' => 'c' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesNotAliasSelf.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesNotAliasSelf.vtest new file mode 100644 index 000000000..4c417fd5f --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesNotAliasSelf.vtest @@ -0,0 +1,7 @@ +ERROR: Alias 'bar' in valueAliases in directive 'Ns.Dir' must not be an alias to itself +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'foo' +VALUE-ALIASES: 'bar' => 'bar' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealAllowed.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealAllowed.vtest new file mode 100644 index 000000000..89502d34a --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealAllowed.vtest @@ -0,0 +1,8 @@ +ERROR: Alias 'c' in valueAliases in directive 'Ns.Dir' must be an alias to an allowed value +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALLOWED: 'a', 'b' +VALUE-ALIASES: 'c' => 'd' diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealIsString.vtest b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealIsString.vtest new file mode 100644 index 000000000..92ec197dc --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/Validator/directive/valueAliasesRealIsString.vtest @@ -0,0 +1,7 @@ +ERROR: Alias target 3 from alias 'b' in valueAliases in directive 'Ns.Dir' must be a string +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +VALUE-ALIASES: 'b' => 3 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorAtomTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorAtomTest.php new file mode 100644 index 000000000..38625136e --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorAtomTest.php @@ -0,0 +1,92 @@ +<?php + +class HTMLPurifier_ConfigSchema_ValidatorAtomTest extends UnitTestCase +{ + + protected function expectValidationException($msg) { + $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg)); + } + + protected function makeAtom($value) { + $obj = new stdClass(); + $obj->property = $value; + // Note that 'property' and 'context' are magic wildcard values + return new HTMLPurifier_ConfigSchema_ValidatorAtom('context', $obj, 'property'); + } + + function testAssertIsString() { + $this->makeAtom('foo')->assertIsString(); + } + + function testAssertIsStringFail() { + $this->expectValidationException("Property in context must be a string"); + $this->makeAtom(3)->assertIsString(); + } + + function testAssertNotNull() { + $this->makeAtom('foo')->assertNotNull(); + } + + function testAssertNotNullFail() { + $this->expectValidationException("Property in context must not be null"); + $this->makeAtom(null)->assertNotNull(); + } + + function testAssertAlnum() { + $this->makeAtom('foo2')->assertAlnum(); + } + + function testAssertAlnumFail() { + $this->expectValidationException("Property in context must be alphanumeric"); + $this->makeAtom('%a')->assertAlnum(); + } + + function testAssertAlnumFailIsString() { + $this->expectValidationException("Property in context must be a string"); + $this->makeAtom(3)->assertAlnum(); + } + + function testAssertNotEmpty() { + $this->makeAtom('foo')->assertNotEmpty(); + } + + function testAssertNotEmptyFail() { + $this->expectValidationException("Property in context must not be empty"); + $this->makeAtom('')->assertNotEmpty(); + } + + function testAssertIsBool() { + $this->makeAtom(false)->assertIsBool(); + } + + function testAssertIsBoolFail() { + $this->expectValidationException("Property in context must be a boolean"); + $this->makeAtom('0')->assertIsBool(); + } + + function testAssertIsArray() { + $this->makeAtom(array())->assertIsArray(); + } + + function testAssertIsArrayFail() { + $this->expectValidationException("Property in context must be an array"); + $this->makeAtom('asdf')->assertIsArray(); + } + + + function testAssertIsLookup() { + $this->makeAtom(array('foo' => true))->assertIsLookup(); + } + + function testAssertIsLookupFail() { + $this->expectValidationException("Property in context must be a lookup array"); + $this->makeAtom(array('foo' => 4))->assertIsLookup(); + } + + function testAssertIsLookupFailIsArray() { + $this->expectValidationException("Property in context must be an array"); + $this->makeAtom('asdf')->assertIsLookup(); + } +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTest.php new file mode 100644 index 000000000..9cbf36e2d --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTest.php @@ -0,0 +1,101 @@ +<?php + +/** + * Special test-case for cases that can't be tested using + * HTMLPurifier_ConfigSchema_ValidatorTestCase. + */ +class HTMLPurifier_ConfigSchema_ValidatorTest extends UnitTestCase +{ + public $validator, $interchange; + + public function setup() { + $this->validator = new HTMLPurifier_ConfigSchema_Validator(); + $this->interchange = new HTMLPurifier_ConfigSchema_Interchange(); + } + + function testDirectiveIntegrityViolation() { + $d = $this->makeDirective('Ns.Dir'); + $d->id = new HTMLPurifier_ConfigSchema_Interchange_Id('Ns.Dir2'); + $this->expectValidationException("Integrity violation: key 'Ns.Dir' does not match internal id 'Ns.Dir2'"); + $this->validator->validate($this->interchange); + } + + function testDirectiveTypeNotEmpty() { + $d = $this->makeDirective('Ns.Dir'); + $d->default = 0; + $d->description = 'Description'; + + $this->expectValidationException("Type in directive 'Ns.Dir' must not be empty"); + $this->validator->validate($this->interchange); + } + + function testDirectiveDefaultInvalid() { + $d = $this->makeDirective('Ns.Dir'); + $d->default = 'asdf'; + $d->type = 'int'; + $d->description = 'Description'; + + $this->expectValidationException("Default in directive 'Ns.Dir' had error: Expected type int, got string"); + $this->validator->validate($this->interchange); + } + + function testDirectiveIdIsString() { + $d = $this->makeDirective(3); + $d->default = 0; + $d->type = 'int'; + $d->description = 'Description'; + + $this->expectValidationException("Key in id '3' in directive '3' must be a string"); + $this->validator->validate($this->interchange); + } + + function testDirectiveTypeAllowsNullIsBool() { + $d = $this->makeDirective('Ns.Dir'); + $d->default = 0; + $d->type = 'int'; + $d->description = 'Description'; + $d->typeAllowsNull = 'yes'; + + $this->expectValidationException("TypeAllowsNull in directive 'Ns.Dir' must be a boolean"); + $this->validator->validate($this->interchange); + } + + function testDirectiveValueAliasesIsArray() { + $d = $this->makeDirective('Ns.Dir'); + $d->default = 'a'; + $d->type = 'string'; + $d->description = 'Description'; + $d->valueAliases = 2; + + $this->expectValidationException("ValueAliases in directive 'Ns.Dir' must be an array"); + $this->validator->validate($this->interchange); + } + + function testDirectiveAllowedIsLookup() { + $d = $this->makeDirective('Ns.Dir'); + $d->default = 'foo'; + $d->type = 'string'; + $d->description = 'Description'; + $d->allowed = array('foo' => 1); + + $this->expectValidationException("Allowed in directive 'Ns.Dir' must be a lookup array"); + $this->validator->validate($this->interchange); + } + + // helper functions + + + protected function makeDirective($key) { + $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive(); + $directive->id = new HTMLPurifier_ConfigSchema_Interchange_Id($key); + $this->interchange->addDirective($directive); + return $directive; + } + + protected function expectValidationException($msg) { + $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg)); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTestCase.php b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTestCase.php new file mode 100644 index 000000000..87fa14d0d --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ConfigSchema/ValidatorTestCase.php @@ -0,0 +1,44 @@ +<?php + +/** + * Controller for validator test-cases. + */ +class HTMLPurifier_ConfigSchema_ValidatorTestCase extends UnitTestCase +{ + + protected $_path, $_parser, $_builder; + public $validator; + + public function __construct($path) { + $this->_path = $path; + $this->_parser = new HTMLPurifier_StringHashParser(); + $this->_builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder(); + parent::__construct($path); + } + + public function setup() { + $this->validator = new HTMLPurifier_ConfigSchema_Validator(); + } + + function testValidator() { + $hashes = $this->_parser->parseMultiFile($this->_path); + $interchange = new HTMLPurifier_ConfigSchema_Interchange(); + $error = null; + foreach ($hashes as $hash) { + if (!isset($hash['ID'])) { + if (isset($hash['ERROR'])) { + $this->expectException( + new HTMLPurifier_ConfigSchema_Exception($hash['ERROR']) + ); + } + continue; + } + $this->_builder->build($interchange, new HTMLPurifier_StringHash($hash)); + } + $this->validator->validate($interchange); + $this->pass(); + } + +} + +// vim: et sw=4 sts=4 |