4 * Special test-case for cases that can't be tested using
5 * HTMLPurifier_ConfigSchema_ValidatorTestCase.
7 class HTMLPurifier_ConfigSchema_ValidatorTest
extends UnitTestCase
9 public $validator, $interchange;
11 public function setup() {
12 $this->validator
= new HTMLPurifier_ConfigSchema_Validator();
13 $this->interchange
= new HTMLPurifier_ConfigSchema_Interchange();
16 public function testNamespaceIntegrityViolation() {
17 $ns = $this->makeNamespace('Ns');
18 $ns->namespace = 'AltNs';
19 $this->expectValidationException("Integrity violation: key 'Ns' does not match internal id 'AltNs'");
20 $this->validator
->validate($this->interchange
);
23 public function testNamespaceNamespaceIsString() {
24 $this->makeNamespace(3);
25 $this->expectValidationException("Namespace in namespace '3' must be a string");
26 $this->validator
->validate($this->interchange
);
29 public function testNamespaceDescriptionIsString() {
30 $ns = $this->makeNamespace('Ns');
32 $this->expectValidationException("Description in namespace 'Ns' must be a string");
33 $this->validator
->validate($this->interchange
);
36 public function testDirectiveIntegrityViolation() {
37 $d = $this->makeDirective('Ns', 'Dir');
38 $d->id
= new HTMLPurifier_ConfigSchema_Interchange_Id('Ns', 'Dir2');
39 $this->expectValidationException("Integrity violation: key 'Ns.Dir' does not match internal id 'Ns.Dir2'");
40 $this->validator
->validate($this->interchange
);
43 public function testDirectiveIdInstanceof() {
44 // This example is somewhat contrived
45 $this->makeNamespace('Ns');
46 $d = new HTMLPurifier_ConfigSchema_Interchange_Directive();
50 $d->description
= 'Description';
51 $this->interchange
->addDirective($d);
53 $this->expectValidationException("Id '3' in directive '3' is not an instance of HTMLPurifier_ConfigSchema_Interchange_Id");
54 $this->validator
->validate($this->interchange
);
57 public function testDirectiveTypeNotEmpty() {
58 $this->makeNamespace('Ns');
59 $d = $this->makeDirective('Ns', 'Dir');
61 $d->description
= 'Description';
63 $this->expectValidationException("Type in directive 'Ns.Dir' must not be empty");
64 $this->validator
->validate($this->interchange
);
67 public function testDirectiveDefaultInvalid() {
68 $this->makeNamespace('Ns');
69 $d = $this->makeDirective('Ns', 'Dir');
72 $d->description
= 'Description';
74 $this->expectValidationException("Default in directive 'Ns.Dir' had error: Expected type int, got string");
75 $this->validator
->validate($this->interchange
);
78 public function testDirectiveIdDirectiveIsString() {
79 $this->makeNamespace('Ns');
80 $d = $this->makeDirective('Ns', 3);
83 $d->description
= 'Description';
85 $this->expectValidationException("Directive in id 'Ns.3' in directive 'Ns.3' must be a string");
86 $this->validator
->validate($this->interchange
);
91 protected function makeNamespace($n) {
92 $namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
93 $namespace->namespace = $n;
94 $namespace->description
= 'Description'; // non-essential, but we won't set it most of the time
95 $this->interchange
->addNamespace($namespace);
99 protected function makeDirective($n, $d) {
100 $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
101 $directive->id
= new HTMLPurifier_ConfigSchema_Interchange_Id($n, $d);
102 $this->interchange
->addDirective($directive);
106 protected function expectValidationException($msg) {
107 $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));