mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-06 06:07:26 +02:00
[1.7.0] Refactor HTMLModule unit tests
- AttrCollections does not barf when an inclusion is not present - HTMLDefinition configuration directives now use new syntax - Added %HTML.AllowedModules and %HTML.CoreModules for testing - Extend Harness so that it can accept a default configuration object member variable - Refactor modules to use Scaffolding, which defines some custom attributes that allows for the easy testing of attribute collections git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1082 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -70,7 +70,7 @@ class HTMLPurifier_AttrCollectionsTest extends UnitTestCase
|
||||
$types = new HTMLPurifier_AttrTypesMock($this);
|
||||
$collections = new HTMLPurifier_AttrCollections($types, array());
|
||||
$collections->info = array(
|
||||
'Core' => array(0 => array('Inclusion'), 'attr-original' => 'Type'),
|
||||
'Core' => array(0 => array('Inclusion', 'Undefined'), 'attr-original' => 'Type'),
|
||||
'Inclusion' => array(0 => array('SubInclusion'), 'attr' => 'Type'),
|
||||
'SubInclusion' => array('attr2' => 'Type')
|
||||
);
|
||||
|
@@ -7,21 +7,15 @@ class HTMLPurifier_HTMLModule_BdoTest extends HTMLPurifier_HTMLModuleHarness
|
||||
|
||||
function test() {
|
||||
|
||||
$this->setupScaffold('Bdo');
|
||||
|
||||
// max
|
||||
$this->assertResult(
|
||||
'<span>
|
||||
<bdo
|
||||
id="test-id"
|
||||
class="class-name"
|
||||
style="font-weight:bold;"
|
||||
title="Title of tag"
|
||||
lang="en"
|
||||
xml:lang="en"
|
||||
dir="rtl"
|
||||
>
|
||||
<bdo ac:core="yes" dir="rtl">
|
||||
#PCDATA <span>Inline</span>
|
||||
</bdo>
|
||||
</span>', true, array('Attr.EnableID' => true)
|
||||
</span>'
|
||||
);
|
||||
|
||||
// min
|
||||
|
@@ -7,21 +7,23 @@ class HTMLPurifier_HTMLModule_EditTest extends HTMLPurifier_HTMLModuleHarness
|
||||
|
||||
function test() {
|
||||
|
||||
$this->setupScaffold('Edit');
|
||||
|
||||
// max
|
||||
$this->assertResult(
|
||||
'<span>
|
||||
<ins cite="http://www.example.com/">
|
||||
<ins cite="http://www.example.com/" ac:common="yes">
|
||||
#PCDATA <span></span>
|
||||
</ins>
|
||||
<del cite="http://www.example.com/">
|
||||
<del cite="http://www.example.com/" ac:common="yes">
|
||||
#PCDATA <span></span>
|
||||
</del>
|
||||
</span>
|
||||
<div>
|
||||
<ins cite="http://www.example.com/">
|
||||
<ins cite="http://www.example.com/" ac:common="yes">
|
||||
#PCDATA <div></div> <span></span>
|
||||
</ins>
|
||||
<del cite="http://www.example.com/">
|
||||
<del cite="http://www.example.com/" ac:common="yes">
|
||||
#PCDATA <div></div> <span></span>
|
||||
</del>
|
||||
</div>'
|
||||
|
@@ -7,6 +7,11 @@ class HTMLPurifier_HTMLModule_HypertextTest extends HTMLPurifier_HTMLModuleHarne
|
||||
|
||||
function test() {
|
||||
|
||||
$this->setupScaffold('Hypertext', array(
|
||||
'Attr.AllowedRel' => 'nofollow',
|
||||
'Attr.AllowedRev' => 'index'
|
||||
));
|
||||
|
||||
// max
|
||||
$this->assertResult(
|
||||
'<span>
|
||||
@@ -14,13 +19,11 @@ class HTMLPurifier_HTMLModule_HypertextTest extends HTMLPurifier_HTMLModuleHarne
|
||||
href="http://www.example.com/"
|
||||
rel="nofollow"
|
||||
rev="index"
|
||||
ac:common="true"
|
||||
>
|
||||
#PCDATA <span>Inline</span>
|
||||
</a>
|
||||
</span>', true, array(
|
||||
'Attr.AllowedRel' => 'nofollow',
|
||||
'Attr.AllowedRev' => 'index'
|
||||
)
|
||||
</span>', true
|
||||
);
|
||||
|
||||
// invalid children
|
||||
|
@@ -7,6 +7,8 @@ class HTMLPurifier_HTMLModule_ImageTest extends HTMLPurifier_HTMLModuleHarness
|
||||
|
||||
function test() {
|
||||
|
||||
$this->setupScaffold('Image');
|
||||
|
||||
// max
|
||||
$this->assertResult(
|
||||
'<span>
|
||||
@@ -16,6 +18,7 @@ class HTMLPurifier_HTMLModule_ImageTest extends HTMLPurifier_HTMLModuleHarness
|
||||
longdesc="example.description.txt"
|
||||
height="42"
|
||||
width="42"
|
||||
ac:common="yes"
|
||||
/>
|
||||
</span>'
|
||||
);
|
||||
|
@@ -7,12 +7,14 @@ class HTMLPurifier_HTMLModule_LegacyTest extends HTMLPurifier_HTMLModuleHarness
|
||||
|
||||
function test() {
|
||||
|
||||
$this->setupScaffold('Legacy');
|
||||
|
||||
// max
|
||||
$this->assertResult(
|
||||
'<span>
|
||||
<u>Text<span></span></u>
|
||||
<s>Text<span></span></s>
|
||||
<strike>Text<span></span></strike>
|
||||
<u ac:common="yes">Text<span></span></u>
|
||||
<s ac:common="yes">Text<span></span></s>
|
||||
<strike ac:common="yes">Text<span></span></strike>
|
||||
</span>'
|
||||
);
|
||||
|
||||
|
@@ -9,6 +9,27 @@ class HTMLPurifier_HTMLModuleHarness extends HTMLPurifier_StrategyHarness
|
||||
parent::setup();
|
||||
$this->obj = new HTMLPurifier_Strategy_Core();
|
||||
}
|
||||
|
||||
function setupScaffold($module, $config = array()) {
|
||||
|
||||
$this->config = HTMLPurifier_Config::create($config);
|
||||
$this->config->set('HTML', 'AllowedModules', $module);
|
||||
$def =& $this->config->getHTMLDefinition(true);
|
||||
$def->manager->addModule(new HTMLPurifier_HTMLModuleHarness_Scaffold());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Special module that defines scaffolding for easy unit testing
|
||||
*/
|
||||
class HTMLPurifier_HTMLModuleHarness_Scaffold extends HTMLPurifier_HTMLModule
|
||||
{
|
||||
var $name = 'Scaffold';
|
||||
var $attr_collections = array(
|
||||
'Common' => array('ac:common' => 'Text'),
|
||||
'Core' => array('ac:core' => 'Text')
|
||||
);
|
||||
}
|
||||
|
||||
?>
|
@@ -72,6 +72,34 @@ class HTMLPurifier_HTMLModuleManagerTest extends UnitTestCase
|
||||
|
||||
}
|
||||
|
||||
function testAllowedModules() {
|
||||
|
||||
$manager = new HTMLPurifier_HTMLModuleManager();
|
||||
$manager->doctypes->register(
|
||||
'Fantasy Inventory 1.0', true,
|
||||
array('Weapons', 'Magic')
|
||||
);
|
||||
|
||||
// register these modules so it doesn't blow up
|
||||
$weapons_module = new HTMLPurifier_HTMLModule();
|
||||
$weapons_module->name = 'Weapons';
|
||||
$manager->registerModule($weapons_module);
|
||||
|
||||
$magic_module = new HTMLPurifier_HTMLModule();
|
||||
$magic_module->name = 'Magic';
|
||||
$manager->registerModule($magic_module);
|
||||
|
||||
$config = HTMLPurifier_Config::create(array(
|
||||
'HTML.Doctype' => 'Fantasy Inventory 1.0',
|
||||
'HTML.AllowedModules' => 'Weapons'
|
||||
));
|
||||
$manager->setup($config);
|
||||
|
||||
$this->assertTrue( isset($manager->modules['Weapons']));
|
||||
$this->assertFalse(isset($manager->modules['Magic']));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -42,6 +42,11 @@ class HTMLPurifier_Harness extends UnitTestCase
|
||||
*/
|
||||
var $generator;
|
||||
|
||||
/**
|
||||
* Default config to fall back on if no config is available
|
||||
*/
|
||||
var $config;
|
||||
|
||||
function HTMLPurifier_Harness() {
|
||||
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
|
||||
$this->generator = new HTMLPurifier_Generator();
|
||||
@@ -52,22 +57,24 @@ class HTMLPurifier_Harness extends UnitTestCase
|
||||
* Asserts a specific result from a one parameter + config/context function
|
||||
* @param $input Input parameter
|
||||
* @param $expect Expectation
|
||||
* @param $config_array Configuration array in form of
|
||||
* Namespace.Directive => Value or an actual config
|
||||
* object.
|
||||
* @param $config Configuration array in form of Ns.Directive => Value.
|
||||
* Has no effect if $this->config is set.
|
||||
* @param $context_array Context array in form of Key => Value or an actual
|
||||
* context object.
|
||||
*/
|
||||
function assertResult($input, $expect = true,
|
||||
$config_array = array(), $context_array = array()
|
||||
$config_array = false, $context_array = array()
|
||||
) {
|
||||
|
||||
// setup config object
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$config->loadArray($config_array);
|
||||
// setup config
|
||||
if ($this->config) {
|
||||
$config = HTMLPurifier_Config::create($this->config);
|
||||
} else {
|
||||
$config = HTMLPurifier_Config::create($config_array);
|
||||
}
|
||||
|
||||
// setup context object. Note that we are operating on a copy of it!
|
||||
// We will extend the test harness to allow you to do post-tests
|
||||
// When necessary, extend the test harness to allow post-tests
|
||||
// on the context object
|
||||
$context = new HTMLPurifier_Context();
|
||||
$context->loadArray($context_array);
|
||||
|
Reference in New Issue
Block a user