Fix newline issues in tests.
[htmlpurifier/darkodev.git] / tests / HTMLPurifier / ConfigSchema / ValidatorTest.php
blob25dfa43cdbf0abe71d791060fc56bd009b2fea3a
1 <?php
3 /**
4 * Special test-case for cases that can't be tested using
5 * HTMLPurifier_ConfigSchema_ValidatorTestCase.
6 */
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 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 function testNamespaceNamespaceIsString() {
24 $this->makeNamespace(3);
25 $this->expectValidationException("Namespace in namespace '3' must be a string");
26 $this->validator->validate($this->interchange);
29 function testNamespaceDescriptionIsString() {
30 $ns = $this->makeNamespace('Ns');
31 $ns->description = 3;
32 $this->expectValidationException("Description in namespace 'Ns' must be a string");
33 $this->validator->validate($this->interchange);
36 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 function testDirectiveTypeNotEmpty() {
44 $this->makeNamespace('Ns');
45 $d = $this->makeDirective('Ns', 'Dir');
46 $d->default = 0;
47 $d->description = 'Description';
49 $this->expectValidationException("Type in directive 'Ns.Dir' must not be empty");
50 $this->validator->validate($this->interchange);
53 function testDirectiveDefaultInvalid() {
54 $this->makeNamespace('Ns');
55 $d = $this->makeDirective('Ns', 'Dir');
56 $d->default = 'asdf';
57 $d->type = 'int';
58 $d->description = 'Description';
60 $this->expectValidationException("Default in directive 'Ns.Dir' had error: Expected type int, got string");
61 $this->validator->validate($this->interchange);
64 function testDirectiveIdDirectiveIsString() {
65 $this->makeNamespace('Ns');
66 $d = $this->makeDirective('Ns', 3);
67 $d->default = 0;
68 $d->type = 'int';
69 $d->description = 'Description';
71 $this->expectValidationException("Directive in id 'Ns.3' in directive 'Ns.3' must be a string");
72 $this->validator->validate($this->interchange);
75 function testDirectiveTypeAllowsNullIsBool() {
76 $this->makeNamespace('Ns');
77 $d = $this->makeDirective('Ns', 'Dir');
78 $d->default = 0;
79 $d->type = 'int';
80 $d->description = 'Description';
81 $d->typeAllowsNull = 'yes';
83 $this->expectValidationException("TypeAllowsNull in directive 'Ns.Dir' must be a boolean");
84 $this->validator->validate($this->interchange);
87 function testDirectiveValueAliasesIsArray() {
88 $this->makeNamespace('Ns');
89 $d = $this->makeDirective('Ns', 'Dir');
90 $d->default = 'a';
91 $d->type = 'string';
92 $d->description = 'Description';
93 $d->valueAliases = 2;
95 $this->expectValidationException("ValueAliases in directive 'Ns.Dir' must be an array");
96 $this->validator->validate($this->interchange);
99 function testDirectiveAllowedIsLookup() {
100 $this->makeNamespace('Ns');
101 $d = $this->makeDirective('Ns', 'Dir');
102 $d->default = 'foo';
103 $d->type = 'string';
104 $d->description = 'Description';
105 $d->allowed = array('foo' => 1);
107 $this->expectValidationException("Allowed in directive 'Ns.Dir' must be a lookup array");
108 $this->validator->validate($this->interchange);
111 // helper functions
113 protected function makeNamespace($n) {
114 $namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
115 $namespace->namespace = $n;
116 $namespace->description = 'Description'; // non-essential, but we won't set it most of the time
117 $this->interchange->addNamespace($namespace);
118 return $namespace;
121 protected function makeDirective($n, $d) {
122 $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive();
123 $directive->id = new HTMLPurifier_ConfigSchema_Interchange_Id($n, $d);
124 $this->interchange->addDirective($directive);
125 return $directive;
128 protected function expectValidationException($msg) {
129 $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg));