1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 11:20:13 +02:00

[3.1.0] Implemented redundant validators and tests

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1627 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-03-23 02:35:47 +00:00
parent 82c9a737f4
commit 77302f845f
6 changed files with 147 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
ERROR: Expected type string, got integer in DEFAULT in directive hash 'Ns.Dir'
ERROR: Expected type string, got integer in TYPE/DEFAULT in directive hash 'Ns.Dir'
----
Ns
DESCRIPTION: Namespace

View File

@@ -1,4 +1,4 @@
ERROR: Invalid type 'foobar' in DEFAULT in directive hash 'Ns.Dir'
ERROR: Invalid type 'foobar' in TYPE/DEFAULT in directive hash 'Ns.Dir'
----
Ns
DESCRIPTION: Namespace

View File

@@ -0,0 +1,110 @@
<?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();
}
public function testNamespaceIntegrityViolation() {
$ns = $this->makeNamespace('Ns');
$ns->namespace = 'AltNs';
$this->expectValidationException("Integrity violation: key 'Ns' does not match internal id 'AltNs'");
$this->validator->validate($this->interchange);
}
public function testNamespaceNamespaceIsString() {
$this->makeNamespace(3);
$this->expectValidationException("Namespace in namespace '3' must be a string");
$this->validator->validate($this->interchange);
}
public function testNamespaceDescriptionIsString() {
$ns = $this->makeNamespace('Ns');
$ns->description = 3;
$this->expectValidationException("Description in namespace 'Ns' must be a string");
$this->validator->validate($this->interchange);
}
public 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);
}
public function testDirectiveIdInstanceof() {
// This example is somewhat contrived
$this->makeNamespace('Ns');
$d = new HTMLPurifier_ConfigSchema_Interchange_Directive();
$d->id = 3;
$d->default = 0;
$d->type = 'int';
$d->description = 'Description';
$this->interchange->addDirective($d);
$this->expectValidationException("Id '3' in directive '3' is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id");
$this->validator->validate($this->interchange);
}
public function testDirectiveTypeNotEmpty() {
$this->makeNamespace('Ns');
$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);
}
public function testDirectiveDefaultInvalid() {
$this->makeNamespace('Ns');
$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);
}
public function testDirectiveIdDirectiveIsString() {
$this->makeNamespace('Ns');
$d = $this->makeDirective('Ns', 3);
$d->default = 0;
$d->type = 'int';
$d->description = 'Description';
$this->expectValidationException("Directive in id 'Ns.3' in directive 'Ns.3' must be a string");
$this->validator->validate($this->interchange);
}
// helper functions
protected function makeNamespace($n) {
$namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
$namespace->namespace = $n;
$namespace->description = 'Description'; // non-essential, but we won't set it most of the time
$this->interchange->addNamespace($namespace);
return $namespace;
}
protected function makeDirective($n, $d) {
$directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
$directive->id = new HTMLPurifier_ConfigSchema_Interchange_Id($n, $d);
$this->interchange->addDirective($directive);
return $directive;
}
protected function expectValidationException($msg) {
$this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));
}
}