3 require_once 'HTMLPurifier/ConfigSchema.php';
5 class HTMLPurifier_ConfigSchemaTest
extends UnitTestCase
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();
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);
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__
);
33 $description = 'Configuration that is always available.';
34 HTMLPurifier_ConfigSchema
::defineNamespace(
37 $this->assertIdentical($this->our_copy
->defaults
, array(
40 $this->assertIdentical($this->our_copy
->info
, array(
43 $namespace = new HTMLPurifier_ConfigEntity_Namespace();
44 $namespace->description
= $description;
45 $this->assertIdentical($this->our_copy
->info_namespace
, array(
52 $description = 'This is a description of the directive.';
53 HTMLPurifier_ConfigSchema
::define(
54 'Core', 'Name', 'default value', 'string',
57 $this->assertIdentical($this->our_copy
->defaults
, array(
59 'Name' => 'default value'
62 $directive = new HTMLPurifier_ConfigEntity_Directive();
63 $directive->type
= 'string';
64 $directive->addDescription($file, $line, $description);
65 $this->assertIdentical($this->our_copy
->info
, array(
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',
90 $this->assertNoErrors();
91 $directive->addDescription($file, $line, $description);
92 $this->assertIdentical($this->our_copy
->info
, array(
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(
118 $directive->allowed
= array(
119 'Real Value' => true,
120 'Real Value 2' => true
122 $this->assertIdentical($this->our_copy
->info
, array(
130 // redefinition of enumeration is cumulative
131 HTMLPurifier_ConfigSchema
::defineAllowedValues(
132 'Core', 'Name', array(
136 $directive->allowed
['Real Value 3'] = true;
137 $this->assertIdentical($this->our_copy
->info
, array(
145 // cannot define enumeration for undefined directive
146 HTMLPurifier_ConfigSchema
::defineAllowedValues(
147 'Core', 'Foobar', array(
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(
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(
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',
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) {
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() {
303 $this->our_copy
->isError(
304 $this->our_copy
->validate(null, 'string', false)
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),
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',