2006-08-11 20:23:41 +00:00
|
|
|
<?php
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-08-11 20:23:41 +00:00
|
|
|
}
|
|
|
|
|