1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 03:10:09 +02:00

[1.7.0] Add unit test for AttrCollections

- Fixed bug where recursive attribute collections would result in infinite loop
- Fixed bug with deep inclusions in attribute collections
- Reset doctype object if HTML or Attr is changed
- Add accessor functions to AttrTypes, unit tested class

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1077 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-05-20 19:29:05 +00:00
parent e4b621eec2
commit 3f06d8316c
7 changed files with 192 additions and 16 deletions

View File

@@ -0,0 +1,131 @@
<?php
require_once 'HTMLPurifier/AttrCollections.php';
Mock::generatePartial(
'HTMLPurifier_AttrCollections',
'HTMLPurifier_AttrCollections_TestForConstruct',
array('performInclusions', 'expandIdentifiers')
);
class HTMLPurifier_AttrCollectionsTest extends UnitTestCase
{
function testConstruction() {
generate_mock_once('HTMLPurifier_AttrTypes');
$collections = new HTMLPurifier_AttrCollections_TestForConstruct($this);
$types = new HTMLPurifier_AttrTypesMock($this);
$modules = array();
$modules['Module1'] = new HTMLPurifier_HTMLModule();
$modules['Module1']->attr_collections = array(
'Core' => array(
0 => array('Soup', 'Undefined'),
'attribute' => 'Type',
'attribute-2' => 'Type2',
),
'Soup' => array(
'attribute-3' => 'Type3-old' // overwritten
)
);
$modules['Module2'] = new HTMLPurifier_HTMLModule();
$modules['Module2']->attr_collections = array(
'Core' => array(
0 => array('Brocolli')
),
'Soup' => array(
'attribute-3' => 'Type3'
),
'Brocolli' => array()
);
$collections->HTMLPurifier_AttrCollections($types, $modules);
// this is without identifier expansion or inclusions
$this->assertIdentical(
$collections->info,
array(
'Core' => array(
0 => array('Soup', 'Undefined', 'Brocolli'),
'attribute' => 'Type',
'attribute-2' => 'Type2'
),
'Soup' => array(
'attribute-3' => 'Type3'
),
'Brocolli' => array()
)
);
}
function test_performInclusions() {
generate_mock_once('HTMLPurifier_AttrTypes');
$types = new HTMLPurifier_AttrTypesMock($this);
$collections = new HTMLPurifier_AttrCollections($types, array());
$collections->info = array(
'Core' => array(0 => array('Inclusion'), 'attr-original' => 'Type'),
'Inclusion' => array(0 => array('SubInclusion'), 'attr' => 'Type'),
'SubInclusion' => array('attr2' => 'Type')
);
$collections->performInclusions($collections->info['Core']);
$this->assertIdentical(
$collections->info['Core'],
array(
'attr-original' => 'Type',
'attr' => 'Type',
'attr2' => 'Type'
)
);
// test recursive
$collections->info = array(
'One' => array(0 => array('Two'), 'one' => 'Type'),
'Two' => array(0 => array('One'), 'two' => 'Type')
);
$collections->performInclusions($collections->info['One']);
$this->assertIdentical(
$collections->info['One'],
array(
'one' => 'Type',
'two' => 'Type'
)
);
}
function test_expandIdentifiers() {
generate_mock_once('HTMLPurifier_AttrTypes');
$types = new HTMLPurifier_AttrTypesMock($this);
$collections = new HTMLPurifier_AttrCollections($types, array());
$attr = array(
'attr1' => 'Color',
'attr2' => 'URI'
);
$types->setReturnValue('get', 'ColorObject', array('Color'));
$types->setReturnValue('get', 'URIObject', array('URI'));
$collections->expandIdentifiers($attr, $types);
$this->assertIdentical(
$attr,
array(
'attr1' => 'ColorObject',
'attr2' => 'URIObject'
)
);
}
}
?>

View File

@@ -0,0 +1,23 @@
<?php
require_once 'HTMLPurifier/AttrTypes.php';
class HTMLPurifier_AttrTypesTest extends UnitTestCase
{
function test_get() {
$types = new HTMLPurifier_AttrTypes();
$this->assertIdentical(
$types->get('CDATA'),
$types->info['CDATA']
);
$this->expectError('Cannot retrieve undefined attribute type foobar');
$types->get('foobar');
}
}
?>

View File

@@ -3,6 +3,7 @@
if (!defined('HTMLPurifierTest')) exit;
// define callable test files (sorted alphabetically)
$test_files[] = 'AttrCollectionsTest.php';
$test_files[] = 'AttrDef/CSS/BackgroundPositionTest.php';
$test_files[] = 'AttrDef/CSS/BackgroundTest.php';
$test_files[] = 'AttrDef/CSS/BorderTest.php';
@@ -46,6 +47,7 @@ $test_files[] = 'AttrTransform/ImgSpaceTest.php';
$test_files[] = 'AttrTransform/LangTest.php';
$test_files[] = 'AttrTransform/LengthTest.php';
$test_files[] = 'AttrTransform/NameTest.php';
$test_files[] = 'AttrTypesTest.php';
$test_files[] = 'ChildDef/ChameleonTest.php';
$test_files[] = 'ChildDef/CustomTest.php';
$test_files[] = 'ChildDef/OptionalTest.php';