2006-08-11 20:23:41 +00:00
|
|
|
<?php
|
|
|
|
|
2006-09-16 22:36:58 +00:00
|
|
|
require_once 'HTMLPurifier/ConfigSchema.php';
|
2006-08-11 20:23:41 +00:00
|
|
|
|
2007-01-21 04:37:02 +00:00
|
|
|
if (!class_exists('CS')) {
|
|
|
|
class CS extends HTMLPurifier_ConfigSchema {}
|
|
|
|
}
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
|
2006-08-11 20:23:41 +00:00
|
|
|
{
|
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
protected $schema;
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
function setup() {
|
|
|
|
$this->schema = new HTMLPurifier_ConfigSchema();
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
2007-02-17 19:37:48 +00:00
|
|
|
tally_errors($this);
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_defineNamespace() {
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('http', $d = 'This is an internet protocol.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->info_namespace, array(
|
2007-02-05 03:22:32 +00:00
|
|
|
'http' => new HTMLPurifier_ConfigDef_Namespace($d)
|
2006-08-27 18:49:16 +00:00
|
|
|
));
|
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Cannot redefine namespace');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('http', 'It is used to serve webpages.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Namespace name must be alphanumeric');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('ssh+http', 'This http is tunneled through SSH.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Description must be non-empty');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('ftp', null);
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_define() {
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('Car', 'Automobiles, those gas-guzzlers!');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'Seats', 5, 'int', $d = 'Standard issue.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->defaults['Car']['Seats'], 5);
|
|
|
|
$this->assertIdentical($this->schema->info['Car']['Seats'],
|
|
|
|
new HTMLPurifier_ConfigDef_Directive('int', $d)
|
2006-08-27 18:49:16 +00:00
|
|
|
);
|
2006-08-11 20:23:41 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'Age', null, 'int/null', $d = 'Not always known.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->defaults['Car']['Age'], null);
|
|
|
|
$this->assertIdentical($this->schema->info['Car']['Age'],
|
|
|
|
new HTMLPurifier_ConfigDef_Directive('int', $d, true)
|
2007-01-20 22:59:20 +00:00
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Cannot define directive for undefined namespace');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Train', 'Cars', 10, 'int', 'Including the caboose.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Directive name must be alphanumeric');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'Is it shiny?', true, 'bool', 'Indicates regular waxing.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Invalid type for configuration directive');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'Efficiency', 50, 'mpg', 'The higher the better.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Default value does not match directive type');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'Producer', 'Ford', 'int', 'ID of the company that made the car.');
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Description must be non-empty');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Car', 'ComplexAttribute', 'lawyers', 'istring', null);
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_defineAllowedValues() {
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('QuantumNumber', 'D');
|
|
|
|
$this->schema->add('QuantumNumber', 'Spin', 0.5, 'float',
|
2007-01-20 22:59:20 +00:00
|
|
|
'Spin of particle. Fourth quantum number, represented by s.');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('QuantumNumber', 'Current', 's', 'string',
|
2007-01-20 22:59:20 +00:00
|
|
|
'Currently selected quantum number.');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('QuantumNumber', 'Difficulty', null, 'string/null', $d = 'How hard are the problems?');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAllowedValues( // okay, since default is null
|
2007-01-20 22:59:20 +00:00
|
|
|
'QuantumNumber', 'Difficulty', array('easy', 'medium', 'hard')
|
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->defaults['QuantumNumber']['Difficulty'], null);
|
|
|
|
$this->assertIdentical($this->schema->info['QuantumNumber']['Difficulty'],
|
2007-02-05 03:22:32 +00:00
|
|
|
new HTMLPurifier_ConfigDef_Directive(
|
2007-01-20 22:59:20 +00:00
|
|
|
'string',
|
2008-01-27 20:21:31 +00:00
|
|
|
$d,
|
2007-01-20 22:59:20 +00:00
|
|
|
true,
|
|
|
|
array(
|
|
|
|
'easy' => true,
|
|
|
|
'medium' => true,
|
|
|
|
'hard' => true
|
|
|
|
)
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2007-01-20 19:22:55 +00:00
|
|
|
$this->expectError('Cannot define allowed values for undefined directive');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAllowedValues(
|
2007-01-20 22:59:20 +00:00
|
|
|
'SpaceTime', 'Symmetry', array('time', 'spatial', 'projective')
|
2006-08-27 18:49:16 +00:00
|
|
|
);
|
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Cannot define allowed values for directive whose type is not string');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAllowedValues(
|
2007-01-20 22:59:20 +00:00
|
|
|
'QuantumNumber', 'Spin', array(0.5, -0.5)
|
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Default value must be in allowed range of variables');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAllowedValues(
|
2007-01-20 22:59:20 +00:00
|
|
|
'QuantumNumber', 'Current', array('n', 'l', 'm') // forgot s!
|
2006-08-27 18:49:16 +00:00
|
|
|
);
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_defineValueAliases() {
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('Abbrev', 'Stuff on abbreviations.');
|
|
|
|
$this->schema->add('Abbrev', 'HTH', 'Happy to Help', 'string', $d = 'Three-letters');
|
|
|
|
$this->schema->addAllowedValues(
|
2007-01-20 22:59:20 +00:00
|
|
|
'Abbrev', 'HTH', array(
|
|
|
|
'Happy to Help',
|
|
|
|
'Hope that Helps',
|
|
|
|
'HAIL THE HAND!'
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
2007-01-20 22:59:20 +00:00
|
|
|
);
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addValueAliases(
|
2007-01-20 22:59:20 +00:00
|
|
|
'Abbrev', 'HTH', array(
|
|
|
|
'happy' => 'Happy to Help',
|
|
|
|
'hope' => 'Hope that Helps'
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
|
|
|
);
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addValueAliases( // delayed addition
|
2007-01-20 22:59:20 +00:00
|
|
|
'Abbrev', 'HTH', array(
|
|
|
|
'hail' => 'HAIL THE HAND!'
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
2007-01-20 22:59:20 +00:00
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->defaults['Abbrev']['HTH'], 'Happy to Help');
|
|
|
|
$this->assertIdentical($this->schema->info['Abbrev']['HTH'],
|
2007-02-05 03:22:32 +00:00
|
|
|
new HTMLPurifier_ConfigDef_Directive(
|
2007-01-20 22:59:20 +00:00
|
|
|
'string',
|
2008-01-27 20:21:31 +00:00
|
|
|
$d,
|
2007-01-20 22:59:20 +00:00
|
|
|
false,
|
|
|
|
array(
|
|
|
|
'Happy to Help' => true,
|
|
|
|
'Hope that Helps' => true,
|
|
|
|
'HAIL THE HAND!' => true
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'happy' => 'Happy to Help',
|
|
|
|
'hope' => 'Hope that Helps',
|
|
|
|
'hail' => 'HAIL THE HAND!'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
|
2007-01-20 19:22:55 +00:00
|
|
|
$this->expectError('Cannot define alias to value that is not allowed');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addValueAliases(
|
2007-01-20 22:59:20 +00:00
|
|
|
'Abbrev', 'HTH', array(
|
|
|
|
'head' => 'Head to Head'
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
|
|
|
);
|
2006-08-11 20:23:41 +00:00
|
|
|
|
2007-01-20 19:22:55 +00:00
|
|
|
$this->expectError('Cannot define alias over allowed value');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addValueAliases(
|
2007-01-20 22:59:20 +00:00
|
|
|
'Abbrev', 'HTH', array(
|
|
|
|
'Hope that Helps' => 'Happy to Help'
|
2006-08-27 18:49:16 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAlias() {
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addNamespace('Home', 'Sweet home.');
|
|
|
|
$this->schema->add('Home', 'Rug', 3, 'int', 'ID.');
|
|
|
|
$this->schema->addAlias('Home', 'Carpet', 'Home', 'Rug');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertTrue(!isset($this->schema->defaults['Home']['Carpet']));
|
|
|
|
$this->assertIdentical($this->schema->info['Home']['Carpet'],
|
2007-02-05 03:22:32 +00:00
|
|
|
new HTMLPurifier_ConfigDef_DirectiveAlias('Home', 'Rug')
|
2006-09-06 02:07:46 +00:00
|
|
|
);
|
|
|
|
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->expectError('Cannot define directive alias in undefined namespace');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAlias('Store', 'Rug', 'Home', 'Rug');
|
2007-01-20 18:43:58 +00:00
|
|
|
|
|
|
|
$this->expectError('Directive name must be alphanumeric');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAlias('Home', 'R.g', 'Home', 'Rug');
|
2007-01-20 18:43:58 +00:00
|
|
|
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->add('Home', 'Rugger', 'Bob Max', 'string', 'Name of.');
|
2007-01-20 18:43:58 +00:00
|
|
|
$this->expectError('Cannot define alias over directive');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAlias('Home', 'Rugger', 'Home', 'Rug');
|
2007-01-20 18:43:58 +00:00
|
|
|
|
|
|
|
$this->expectError('Cannot define alias to undefined directive');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAlias('Home', 'Rug2', 'Home', 'Rugavan');
|
2007-01-20 18:43:58 +00:00
|
|
|
|
|
|
|
$this->expectError('Cannot define alias to alias');
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->addAlias('Home', 'Rug2', 'Home', 'Carpet');
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertValid($var, $type, $ret = null) {
|
|
|
|
$ret = ($ret === null) ? $var : $ret;
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->assertIdentical($this->schema->validate($var, $type), $ret);
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertInvalid($var, $type) {
|
2006-11-12 02:59:36 +00:00
|
|
|
$this->assertTrue(
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->isError(
|
|
|
|
$this->schema->validate($var, $type)
|
2006-11-12 02:59:36 +00:00
|
|
|
)
|
|
|
|
);
|
2006-08-27 18:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testValidate() {
|
|
|
|
|
|
|
|
$this->assertValid('foobar', 'string');
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertValid('foobar', 'text'); // aliases, lstring = long string
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid('FOOBAR', 'istring', 'foobar');
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertValid('FOOBAR', 'itext', 'foobar');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(34, 'int');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(3.34, 'float');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(false, 'bool');
|
|
|
|
$this->assertValid(0, 'bool', false);
|
|
|
|
$this->assertValid(1, 'bool', true);
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->assertValid('true', 'bool', true);
|
|
|
|
$this->assertValid('false', 'bool', false);
|
|
|
|
$this->assertValid('1', 'bool', true);
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertInvalid(34, 'bool');
|
2006-11-12 02:59:36 +00:00
|
|
|
$this->assertInvalid(null, 'bool');
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(array('1', '2', '3'), 'list');
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->assertValid('foo,bar, cow', 'list', array('foo', 'bar', 'cow'));
|
2007-04-30 01:14:21 +00:00
|
|
|
$this->assertValid('', 'list', array());
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertValid("foo\nbar", 'list', array('foo', 'bar'));
|
|
|
|
$this->assertValid("foo\nbar,baz", 'list', array('foo', 'bar', 'baz'));
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(array('1' => true, '2' => true), 'lookup');
|
|
|
|
$this->assertValid(array('1', '2'), 'lookup', array('1' => true, '2' => true));
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->assertValid('foo,bar', 'lookup', array('foo' => true, 'bar' => true));
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertValid("foo\nbar", 'lookup', array('foo' => true, 'bar' => true));
|
|
|
|
$this->assertValid("foo\nbar,baz", 'lookup', array('foo' => true, 'bar' => true, 'baz' => true));
|
2007-04-30 01:14:21 +00:00
|
|
|
$this->assertValid('', 'lookup', array());
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(array('foo' => 'bar'), 'hash');
|
|
|
|
$this->assertValid(array(1 => 'moo'), 'hash');
|
2007-01-20 22:59:20 +00:00
|
|
|
$this->assertInvalid(array(0 => 'moo'), 'hash');
|
2007-04-30 01:14:21 +00:00
|
|
|
$this->assertValid('', 'hash', array());
|
2007-05-28 03:55:36 +00:00
|
|
|
$this->assertValid('foo:bar,too:two', 'hash', array('foo' => 'bar', 'too' => 'two'));
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertValid("foo:bar\ntoo:two,three:free", 'hash', array('foo' => 'bar', 'too' => 'two', 'three' => 'free'));
|
2007-05-28 03:55:36 +00:00
|
|
|
$this->assertValid('foo:bar,too', 'hash', array('foo' => 'bar'));
|
|
|
|
$this->assertValid('foo:bar,', 'hash', array('foo' => 'bar'));
|
|
|
|
$this->assertValid('foo:bar:baz', 'hash', array('foo' => 'bar:baz'));
|
2007-01-20 22:59:20 +00:00
|
|
|
|
2006-08-27 18:49:16 +00:00
|
|
|
$this->assertValid(23, 'mixed');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-11-12 02:59:36 +00:00
|
|
|
function testValidate_null() {
|
|
|
|
|
|
|
|
$this->assertTrue(
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->isError(
|
|
|
|
$this->schema->validate(null, 'string', false)
|
2006-11-12 02:59:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
2008-01-27 20:21:31 +00:00
|
|
|
$this->schema->isError(
|
|
|
|
$this->schema->validate(null, 'string', true)
|
2006-11-12 02:59:36 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|