Release 2.1.0, merged in 1313 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / ConfigSchemaTest.php
blob580553ebdc62f7c6e85af898fb5a48cabdb27ad1
1 <?php
3 require_once 'HTMLPurifier/ConfigSchema.php';
5 if (!class_exists('CS')) {
6 class CS extends HTMLPurifier_ConfigSchema {}
9 class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
12 /**
13 * Munged name of current file.
15 var $file;
17 /**
18 * Copy of the real ConfigSchema to revert to.
20 var $old_copy;
22 /**
23 * Copy of dummy ConfigSchema for testing purposes.
25 var $our_copy;
27 function setUp() {
28 // yes, I know this is slightly convoluted, but that's the price
29 // you pay for using Singletons. Good thing we can overload it.
31 // first, let's get a clean copy to do tests
32 $our_copy = new HTMLPurifier_ConfigSchema();
33 // get the old copy
34 $this->old_copy = HTMLPurifier_ConfigSchema::instance();
35 // put in our copy, and reassign to the REAL reference
36 $this->our_copy =& HTMLPurifier_ConfigSchema::instance($our_copy);
38 $this->file = $this->our_copy->mungeFilename(__FILE__);
41 function tearDown() {
42 // testing is done, restore the old copy
43 HTMLPurifier_ConfigSchema::instance($this->old_copy);
44 tally_errors($this);
47 function test_defineNamespace() {
48 CS::defineNamespace('http', $d = 'This is an internet protocol.');
50 $this->assertIdentical($this->our_copy->info_namespace, array(
51 'http' => new HTMLPurifier_ConfigDef_Namespace($d)
52 ));
54 $this->expectError('Cannot redefine namespace');
55 CS::defineNamespace('http', 'It is used to serve webpages.');
57 $this->expectError('Namespace name must be alphanumeric');
58 CS::defineNamespace('ssh+http', 'This http is tunneled through SSH.');
60 $this->expectError('Description must be non-empty');
61 CS::defineNamespace('ftp', null);
64 function test_define() {
65 CS::defineNamespace('Car', 'Automobiles, those gas-guzzlers!');
67 CS::define('Car', 'Seats', 5, 'int', $d = 'Standard issue.'); $l = __LINE__;
69 $this->assertIdentical($this->our_copy->defaults['Car']['Seats'], 5);
70 $this->assertIdentical($this->our_copy->info['Car']['Seats'],
71 new HTMLPurifier_ConfigDef_Directive('int',
72 array($this->file => array($l => $d))
76 CS::define('Car', 'Age', null, 'int/null', $d = 'Not always known.'); $l = __LINE__;
78 $this->assertIdentical($this->our_copy->defaults['Car']['Age'], null);
79 $this->assertIdentical($this->our_copy->info['Car']['Age'],
80 new HTMLPurifier_ConfigDef_Directive('int',
81 array($this->file => array($l => $d)), true
85 $this->expectError('Cannot define directive for undefined namespace');
86 CS::define('Train', 'Cars', 10, 'int', 'Including the caboose.');
88 $this->expectError('Directive name must be alphanumeric');
89 CS::define('Car', 'Is it shiny?', true, 'bool', 'Indicates regular waxing.');
91 $this->expectError('Invalid type for configuration directive');
92 CS::define('Car', 'Efficiency', 50, 'mpg', 'The higher the better.');
94 $this->expectError('Default value does not match directive type');
95 CS::define('Car', 'Producer', 'Ford', 'int', 'ID of the company that made the car.');
97 $this->expectError('Description must be non-empty');
98 CS::define('Car', 'ComplexAttribute', 'lawyers', 'istring', null);
101 function testRedefinition_define() {
102 CS::defineNamespace('Cat', 'Belongs to Schrodinger.');
104 CS::define('Cat', 'Dead', false, 'bool', $d1 = 'Well, is it?'); $l1 = __LINE__;
105 CS::define('Cat', 'Dead', false, 'bool', $d2 = 'It is difficult to say.'); $l2 = __LINE__;
107 $this->assertIdentical($this->our_copy->defaults['Cat']['Dead'], false);
108 $this->assertIdentical($this->our_copy->info['Cat']['Dead'],
109 new HTMLPurifier_ConfigDef_Directive('bool',
110 array($this->file => array($l1 => $d1, $l2 => $d2))
114 $this->expectError('Inconsistent default or type, cannot redefine');
115 CS::define('Cat', 'Dead', true, 'bool', 'Quantum mechanics does not know.');
117 $this->expectError('Inconsistent default or type, cannot redefine');
118 CS::define('Cat', 'Dead', 'maybe', 'string', 'Perhaps if we look we will know.');
121 function test_defineAllowedValues() {
122 CS::defineNamespace('QuantumNumber', 'D');
123 CS::define('QuantumNumber', 'Spin', 0.5, 'float',
124 'Spin of particle. Fourth quantum number, represented by s.');
125 CS::define('QuantumNumber', 'Current', 's', 'string',
126 'Currently selected quantum number.');
127 CS::define('QuantumNumber', 'Difficulty', null, 'string/null', $d = 'How hard are the problems?'); $l = __LINE__;
129 CS::defineAllowedValues( // okay, since default is null
130 'QuantumNumber', 'Difficulty', array('easy', 'medium', 'hard')
133 $this->assertIdentical($this->our_copy->defaults['QuantumNumber']['Difficulty'], null);
134 $this->assertIdentical($this->our_copy->info['QuantumNumber']['Difficulty'],
135 new HTMLPurifier_ConfigDef_Directive(
136 'string',
137 array($this->file => array($l => $d)),
138 true,
139 array(
140 'easy' => true,
141 'medium' => true,
142 'hard' => true
147 $this->expectError('Cannot define allowed values for undefined directive');
148 CS::defineAllowedValues(
149 'SpaceTime', 'Symmetry', array('time', 'spatial', 'projective')
152 $this->expectError('Cannot define allowed values for directive whose type is not string');
153 CS::defineAllowedValues(
154 'QuantumNumber', 'Spin', array(0.5, -0.5)
157 $this->expectError('Default value must be in allowed range of variables');
158 CS::defineAllowedValues(
159 'QuantumNumber', 'Current', array('n', 'l', 'm') // forgot s!
163 function test_defineValueAliases() {
164 CS::defineNamespace('Abbrev', 'Stuff on abbreviations.');
165 CS::define('Abbrev', 'HTH', 'Happy to Help', 'string', $d = 'Three-letters'); $l = __LINE__;
166 CS::defineAllowedValues(
167 'Abbrev', 'HTH', array(
168 'Happy to Help',
169 'Hope that Helps',
170 'HAIL THE HAND!'
173 CS::defineValueAliases(
174 'Abbrev', 'HTH', array(
175 'happy' => 'Happy to Help',
176 'hope' => 'Hope that Helps'
179 CS::defineValueAliases( // delayed addition
180 'Abbrev', 'HTH', array(
181 'hail' => 'HAIL THE HAND!'
185 $this->assertIdentical($this->our_copy->defaults['Abbrev']['HTH'], 'Happy to Help');
186 $this->assertIdentical($this->our_copy->info['Abbrev']['HTH'],
187 new HTMLPurifier_ConfigDef_Directive(
188 'string',
189 array($this->file => array($l => $d)),
190 false,
191 array(
192 'Happy to Help' => true,
193 'Hope that Helps' => true,
194 'HAIL THE HAND!' => true
196 array(
197 'happy' => 'Happy to Help',
198 'hope' => 'Hope that Helps',
199 'hail' => 'HAIL THE HAND!'
204 $this->expectError('Cannot define alias to value that is not allowed');
205 CS::defineValueAliases(
206 'Abbrev', 'HTH', array(
207 'head' => 'Head to Head'
211 $this->expectError('Cannot define alias over allowed value');
212 CS::defineValueAliases(
213 'Abbrev', 'HTH', array(
214 'Hope that Helps' => 'Happy to Help'
220 function testAlias() {
221 CS::defineNamespace('Home', 'Sweet home.');
222 CS::define('Home', 'Rug', 3, 'int', 'ID.');
223 CS::defineAlias('Home', 'Carpet', 'Home', 'Rug');
225 $this->assertTrue(!isset($this->our_copy->defaults['Home']['Carpet']));
226 $this->assertIdentical($this->our_copy->info['Home']['Carpet'],
227 new HTMLPurifier_ConfigDef_DirectiveAlias('Home', 'Rug')
230 $this->expectError('Cannot define directive alias in undefined namespace');
231 CS::defineAlias('Store', 'Rug', 'Home', 'Rug');
233 $this->expectError('Directive name must be alphanumeric');
234 CS::defineAlias('Home', 'R.g', 'Home', 'Rug');
236 CS::define('Home', 'Rugger', 'Bob Max', 'string', 'Name of.');
237 $this->expectError('Cannot define alias over directive');
238 CS::defineAlias('Home', 'Rugger', 'Home', 'Rug');
240 $this->expectError('Cannot define alias to undefined directive');
241 CS::defineAlias('Home', 'Rug2', 'Home', 'Rugavan');
243 $this->expectError('Cannot define alias to alias');
244 CS::defineAlias('Home', 'Rug2', 'Home', 'Carpet');
247 function assertValid($var, $type, $ret = null) {
248 $ret = ($ret === null) ? $var : $ret;
249 $this->assertIdentical($this->our_copy->validate($var, $type), $ret);
252 function assertInvalid($var, $type) {
253 $this->assertTrue(
254 $this->our_copy->isError(
255 $this->our_copy->validate($var, $type)
260 function testValidate() {
262 $this->assertValid('foobar', 'string');
263 $this->assertValid('FOOBAR', 'istring', 'foobar');
265 $this->assertValid(34, 'int');
267 $this->assertValid(3.34, 'float');
269 $this->assertValid(false, 'bool');
270 $this->assertValid(0, 'bool', false);
271 $this->assertValid(1, 'bool', true);
272 $this->assertValid('true', 'bool', true);
273 $this->assertValid('false', 'bool', false);
274 $this->assertValid('1', 'bool', true);
275 $this->assertInvalid(34, 'bool');
276 $this->assertInvalid(null, 'bool');
278 $this->assertValid(array('1', '2', '3'), 'list');
279 $this->assertValid('foo,bar, cow', 'list', array('foo', 'bar', 'cow'));
280 $this->assertValid('', 'list', array());
282 $this->assertValid(array('1' => true, '2' => true), 'lookup');
283 $this->assertValid(array('1', '2'), 'lookup', array('1' => true, '2' => true));
284 $this->assertValid('foo,bar', 'lookup', array('foo' => true, 'bar' => true));
285 $this->assertValid('', 'lookup', array());
287 $this->assertValid(array('foo' => 'bar'), 'hash');
288 $this->assertValid(array(1 => 'moo'), 'hash');
289 $this->assertInvalid(array(0 => 'moo'), 'hash');
290 $this->assertValid('', 'hash', array());
291 $this->assertValid('foo:bar,too:two', 'hash', array('foo' => 'bar', 'too' => 'two'));
292 $this->assertValid('foo:bar,too', 'hash', array('foo' => 'bar'));
293 $this->assertValid('foo:bar,', 'hash', array('foo' => 'bar'));
294 $this->assertValid('foo:bar:baz', 'hash', array('foo' => 'bar:baz'));
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\\My Libraries\\htmlpurifier\\library\\HTMLPurifier\\AttrDef.php',
327 'HTMLPurifier/AttrDef.php'
330 $this->assertMungeFilename(
331 'C:\\php\\My Libraries\\htmlpurifier\\library\\HTMLPurifier.php',
332 'HTMLPurifier.php'