0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00
htmlpurifier/tests/HTMLPurifier/ConfigTest.php
Edward Z. Yang 7d2fe4c5d7 [1.2.0]
- Factor out Config and Context object population through arrays
- Bring dependent assertions together in IDTest.php
- AttrDefHarness.php now resets context and configuration between tests
- Add missing reference operator in AttrDef/ID.php

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@494 48356398-32a2-884e-a903-53898d9a118a
2006-10-21 18:18:36 +00:00

152 lines
5.4 KiB
PHP

<?php
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_ConfigTest extends UnitTestCase
{
var $our_copy, $old_copy;
function setUp() {
// set up a dummy schema object for testing
$our_copy = new HTMLPurifier_ConfigSchema();
$this->old_copy = HTMLPurifier_ConfigSchema::instance();
$this->our_copy =& HTMLPurifier_ConfigSchema::instance($our_copy);
}
function tearDown() {
HTMLPurifier_ConfigSchema::instance($this->old_copy);
}
function test() {
HTMLPurifier_ConfigSchema::defineNamespace('Core', 'Corestuff');
HTMLPurifier_ConfigSchema::defineNamespace('Attr', 'Attributes');
HTMLPurifier_ConfigSchema::defineNamespace('Extension', 'Extensible');
HTMLPurifier_ConfigSchema::define(
'Core', 'Key', false, 'bool', 'A boolean directive.'
);
HTMLPurifier_ConfigSchema::define(
'Attr', 'Key', 42, 'int', 'An integer directive.'
);
HTMLPurifier_ConfigSchema::define(
'Extension', 'Pert', 'foo', 'string', 'A string directive.'
);
HTMLPurifier_ConfigSchema::define(
'Core', 'Encoding', 'utf-8', 'istring', 'Case insensitivity!'
);
HTMLPurifier_ConfigSchema::defineAllowedValues(
'Extension', 'Pert', array('foo', 'moo')
);
HTMLPurifier_ConfigSchema::defineValueAliases(
'Extension', 'Pert', array('cow' => 'moo')
);
HTMLPurifier_ConfigSchema::defineAllowedValues(
'Core', 'Encoding', array('utf-8', 'iso-8859-1')
);
$config = HTMLPurifier_Config::createDefault();
// test default value retrieval
$this->assertIdentical($config->get('Core', 'Key'), false);
$this->assertIdentical($config->get('Attr', 'Key'), 42);
$this->assertIdentical($config->get('Extension', 'Pert'), 'foo');
// set some values
$config->set('Core', 'Key', true);
$this->assertIdentical($config->get('Core', 'Key'), true);
// try to retrieve undefined value
$config->get('Core', 'NotDefined');
$this->assertError('Cannot retrieve value of undefined directive');
$this->assertNoErrors();
$this->swallowErrors();
// try to set undefined value
$config->set('Foobar', 'Key', 'foobar');
$this->assertError('Cannot set undefined directive to value');
$this->assertNoErrors();
$this->swallowErrors();
// try to set not allowed value
$config->set('Extension', 'Pert', 'wizard');
$this->assertError('Value not supported');
$this->assertNoErrors();
$this->swallowErrors();
// try to set not allowed value
$config->set('Extension', 'Pert', 34);
$this->assertError('Value is of invalid type');
$this->assertNoErrors();
$this->swallowErrors();
// set aliased value
$config->set('Extension', 'Pert', 'cow');
$this->assertNoErrors();
$this->assertIdentical($config->get('Extension', 'Pert'), 'moo');
// case-insensitive attempt to set value that is allowed
$config->set('Core', 'Encoding', 'ISO-8859-1');
$this->assertNoErrors();
$this->assertIdentical($config->get('Core', 'Encoding'), 'iso-8859-1');
}
function test_getDefinition() {
$config = HTMLPurifier_Config::createDefault();
$def = $config->getHTMLDefinition();
$this->assertIsA($def, 'HTMLPurifier_HTMLDefinition');
$def = $config->getCSSDefinition();
$this->assertIsA($def, 'HTMLPurifier_CSSDefinition');
}
function test_loadArray() {
// setup a few dummy namespaces/directives for our testing
HTMLPurifier_ConfigSchema::defineNamespace('Zoo', 'Animals we have.');
HTMLPurifier_ConfigSchema::define('Zoo', 'Aadvark', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define('Zoo', 'Boar', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define('Zoo', 'Camel', 0, 'int', 'Have?');
HTMLPurifier_ConfigSchema::define(
'Zoo', 'Others', array(), 'list', 'Other animals we have one of.'
);
$config_manual = HTMLPurifier_Config::createDefault();
$config_loadabbr = HTMLPurifier_Config::createDefault();
$config_loadfull = HTMLPurifier_Config::createDefault();
$config_manual->set('Zoo', 'Aadvark', 3);
$config_manual->set('Zoo', 'Boar', 5);
$config_manual->set('Zoo', 'Camel', 2000); // that's a lotta camels!
$config_manual->set('Zoo', 'Others', array('Peacock', 'Dodo')); // wtf!
// condensed form
$config_loadabbr->loadArray(array(
'Zoo.Aadvark' => 3,
'Zoo.Boar' => 5,
'Zoo.Camel' => 2000,
'Zoo.Others' => array('Peacock', 'Dodo')
));
// fully expanded form
$config_loadfull->loadArray(array(
'Zoo' => array(
'Aadvark' => 3,
'Boar' => 5,
'Camel' => 2000,
'Others' => array('Peacock', 'Dodo')
)
));
$this->assertEqual($config_manual, $config_loadabbr);
$this->assertEqual($config_manual, $config_loadfull);
}
}
?>