[1.2.0] Allow configuration directives to permit null values. ConfigDoc updated accor...
[htmlpurifier.git] / tests / HTMLPurifier / ConfigSchemaTest.php
blob364caa91496f190c0f33cdefe19601fc9f73337b
1 <?php
3 require_once 'HTMLPurifier/ConfigSchema.php';
5 class HTMLPurifier_ConfigSchemaTest extends UnitTestCase
8 var $old_copy;
9 var $our_copy;
11 function setUp() {
12 // yes, I know this is slightly convoluted, but that's the price
13 // you pay for using Singletons. Good thing we can overload it.
15 // first, let's get a clean copy to do tests
16 $our_copy = new HTMLPurifier_ConfigSchema();
17 // get the old copy
18 $this->old_copy = HTMLPurifier_ConfigSchema::instance();
19 // put in our copy, and reassign to the REAL reference
20 $this->our_copy =& HTMLPurifier_ConfigSchema::instance($our_copy);
23 function tearDown() {
24 // testing is done, restore the old copy
25 HTMLPurifier_ConfigSchema::instance($this->old_copy);
28 function testNormal() {
30 $file = $this->our_copy->mungeFilename(__FILE__);
32 // define a namespace
33 $description = 'Configuration that is always available.';
34 HTMLPurifier_ConfigSchema::defineNamespace(
35 'Core', $description
37 $this->assertIdentical($this->our_copy->defaults, array(
38 'Core' => array()
39 ));
40 $this->assertIdentical($this->our_copy->info, array(
41 'Core' => array()
42 ));
43 $namespace = new HTMLPurifier_ConfigEntity_Namespace();
44 $namespace->description = $description;
45 $this->assertIdentical($this->our_copy->info_namespace, array(
46 'Core' => $namespace
47 ));
51 // define a directive
52 $description = 'This is a description of the directive.';
53 HTMLPurifier_ConfigSchema::define(
54 'Core', 'Name', 'default value', 'string',
55 $description
56 ); $line = __LINE__;
57 $this->assertIdentical($this->our_copy->defaults, array(
58 'Core' => array(
59 'Name' => 'default value'
61 ));
62 $directive = new HTMLPurifier_ConfigEntity_Directive();
63 $directive->type = 'string';
64 $directive->addDescription($file, $line, $description);
65 $this->assertIdentical($this->our_copy->info, array(
66 'Core' => array(
67 'Name' => $directive
69 ));
73 // define a directive in an undefined namespace
74 HTMLPurifier_ConfigSchema::define(
75 'Extension', 'Name', false, 'bool',
76 'This is for an extension, but we have not defined its namespace!'
78 $this->assertError('Cannot define directive for undefined namespace');
79 $this->assertNoErrors();
80 $this->swallowErrors();
84 // redefine a value in a valid manner
85 $description = 'Alternative configuration definition';
86 HTMLPurifier_ConfigSchema::define(
87 'Core', 'Name', 'default value', 'string',
88 $description
89 ); $line = __LINE__;
90 $this->assertNoErrors();
91 $directive->addDescription($file, $line, $description);
92 $this->assertIdentical($this->our_copy->info, array(
93 'Core' => array(
94 'Name' => $directive
96 ));
100 // redefine a directive in an invalid manner
101 HTMLPurifier_ConfigSchema::define(
102 'Core', 'Name', 'different default', 'string',
103 'Inconsistent default or type, cannot redefine'
105 $this->assertError('Inconsistent default or type, cannot redefine');
106 $this->assertNoErrors();
107 $this->swallowErrors();
111 // make an enumeration
112 HTMLPurifier_ConfigSchema::defineAllowedValues(
113 'Core', 'Name', array(
114 'Real Value',
115 'Real Value 2'
118 $directive->allowed = array(
119 'Real Value' => true,
120 'Real Value 2' => true
122 $this->assertIdentical($this->our_copy->info, array(
123 'Core' => array(
124 'Name' => $directive
130 // redefinition of enumeration is cumulative
131 HTMLPurifier_ConfigSchema::defineAllowedValues(
132 'Core', 'Name', array(
133 'Real Value 3',
136 $directive->allowed['Real Value 3'] = true;
137 $this->assertIdentical($this->our_copy->info, array(
138 'Core' => array(
139 'Name' => $directive
145 // cannot define enumeration for undefined directive
146 HTMLPurifier_ConfigSchema::defineAllowedValues(
147 'Core', 'Foobar', array(
148 'Real Value 9',
151 $this->assertError('Cannot define allowed values for undefined directive');
152 $this->assertNoErrors();
153 $this->swallowErrors();
157 // test defining value aliases for an enumerated value
158 HTMLPurifier_ConfigSchema::defineValueAliases(
159 'Core', 'Name', array(
160 'Aliased Value' => 'Real Value'
163 $directive->aliases['Aliased Value'] = 'Real Value';
164 $this->assertIdentical($this->our_copy->info, array(
165 'Core' => array(
166 'Name' => $directive
172 // redefine should be cumulative
173 HTMLPurifier_ConfigSchema::defineValueAliases(
174 'Core', 'Name', array(
175 'Aliased Value 2' => 'Real Value 2'
178 $directive->aliases['Aliased Value 2'] = 'Real Value 2';
179 $this->assertIdentical($this->our_copy->info, array(
180 'Core' => array(
181 'Name' => $directive
187 // cannot create alias to not-allowed value
188 HTMLPurifier_ConfigSchema::defineValueAliases(
189 'Core', 'Name', array(
190 'Aliased Value 3' => 'Invalid Value'
193 $this->assertError('Cannot define alias to value that is not allowed');
194 $this->assertNoErrors();
195 $this->swallowErrors();
199 // cannot create alias for already allowed value
200 HTMLPurifier_ConfigSchema::defineValueAliases(
201 'Core', 'Name', array(
202 'Real Value' => 'Real Value 2'
205 $this->assertError('Cannot define alias over allowed value');
206 $this->assertNoErrors();
207 $this->swallowErrors();
211 // define a directive with an invalid type
212 HTMLPurifier_ConfigSchema::define(
213 'Core', 'Foobar', false, 'omen',
214 'Omen is not a valid type, so we reject this.'
217 $this->assertError('Invalid type for configuration directive');
218 $this->assertNoErrors();
219 $this->swallowErrors();
223 // define a directive with inconsistent type
224 HTMLPurifier_ConfigSchema::define(
225 'Core', 'Foobaz', 10, 'string',
226 'If we say string, we should mean it, not integer 10.'
229 $this->assertError('Default value does not match directive type');
230 $this->assertNoErrors();
231 $this->swallowErrors();
235 // define a directive that allows null
236 HTMLPurifier_ConfigSchema::define(
237 'Core', 'Foobaz', null, 'string/null',
238 'Nulls are allowed if you add on /null, cool huh?'
241 $this->assertNoErrors();
242 $this->swallowErrors();
245 // define a directive with bad characters
246 HTMLPurifier_ConfigSchema::define(
247 'Core', 'Core.Attr', 10, 'int',
248 'No periods! >:-('
251 $this->assertError('Directive name must be alphanumeric');
252 $this->assertNoErrors();
253 $this->swallowErrors();
255 // define a namespace with bad characters
256 HTMLPurifier_ConfigSchema::defineNamespace(
257 'Foobar&Gromit', $description
260 $this->assertError('Namespace name must be alphanumeric');
261 $this->assertNoErrors();
262 $this->swallowErrors();
266 function assertValid($var, $type, $ret = null) {
267 $ret = ($ret === null) ? $var : $ret;
268 $this->assertIdentical($this->our_copy->validate($var, $type), $ret);
271 function assertInvalid($var, $type) {
272 $this->assertTrue(
273 $this->our_copy->isError(
274 $this->our_copy->validate($var, $type)
279 function testValidate() {
281 $this->assertValid('foobar', 'string');
282 $this->assertValid('FOOBAR', 'istring', 'foobar');
283 $this->assertValid(34, 'int');
284 $this->assertValid(3.34, 'float');
285 $this->assertValid(false, 'bool');
286 $this->assertValid(0, 'bool', false);
287 $this->assertValid(1, 'bool', true);
288 $this->assertInvalid(34, 'bool');
289 $this->assertInvalid(null, 'bool');
290 $this->assertValid(array('1', '2', '3'), 'list');
291 $this->assertValid(array('1' => true, '2' => true), 'lookup');
292 $this->assertValid(array('1', '2'), 'lookup', array('1' => true, '2' => true));
293 $this->assertValid(array('foo' => 'bar'), 'hash');
294 $this->assertInvalid(array(0 => 'moo'), 'hash');
295 $this->assertValid(array(1 => 'moo'), 'hash');
296 $this->assertValid(23, 'mixed');
300 function testValidate_null() {
302 $this->assertTrue(
303 $this->our_copy->isError(
304 $this->our_copy->validate(null, 'string', false)
308 $this->assertFalse(
309 $this->our_copy->isError(
310 $this->our_copy->validate(null, 'string', true)
316 function assertMungeFilename($oldname, $newname) {
317 $this->assertIdentical(
318 $this->our_copy->mungeFilename($oldname),
319 $newname
323 function testMungeFilename() {
325 $this->assertMungeFilename(
326 'C:\\php\\libs\\htmlpurifier\\library\\HTMLPurifier\\AttrDef.php',
327 'HTMLPurifier/AttrDef.php'
330 $this->assertMungeFilename(
331 'C:\\php\\libs\\htmlpurifier\\library\\HTMLPurifier.php',
332 'HTMLPurifier.php'