Release 2.1.0, merged in 1313 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / HTMLModuleManagerTest.php
blob50dcb154f473d085b165891ead1ab5766521814d
1 <?php
3 require_once 'HTMLPurifier/HTMLModuleManager.php';
5 class HTMLPurifier_HTMLModuleManagerTest extends HTMLPurifier_Harness
8 function test_addModule() {
9 $manager = new HTMLPurifier_HTMLModuleManager();
10 $manager->doctypes->register('Blank'); // doctype normally is blank...
12 $attrdef_nmtokens = new HTMLPurifier_AttrDef();
13 $attrdef_nmtokens->_name = 'nmtokens'; // for testing only
15 generate_mock_once('HTMLPurifier_AttrDef');
16 $attrdef = new HTMLPurifier_AttrDefMock();
17 $attrdef->setReturnValue('make', $attrdef_nmtokens);
18 $manager->attrTypes->info['NMTOKENS'] =& $attrdef;
20 // ...but we add user modules
22 $common_module = new HTMLPurifier_HTMLModule();
23 $common_module->name = 'Common';
24 $common_module->attr_collections['Common'] = array('class' => 'NMTOKENS');
25 $common_module->content_sets['Flow'] = 'Block | Inline';
26 $manager->addModule($common_module);
28 $structural_module = new HTMLPurifier_HTMLModule();
29 $structural_module->name = 'Structural';
30 $structural_module->addElement('p', true, 'Block', 'Inline', 'Common');
31 $structural_module->addElement('div', false, 'Block', 'Flow');
32 $manager->addModule($structural_module);
34 $formatting_module = new HTMLPurifier_HTMLModule();
35 $formatting_module->name = 'Formatting';
36 $formatting_module->addElement('em', true, 'Inline', 'Inline', 'Common');
37 $manager->addModule($formatting_module);
39 $config = HTMLPurifier_Config::createDefault();
40 $config->set('HTML', 'Trusted', false);
41 $config->set('HTML', 'CustomDoctype', 'Blank');
43 $manager->setup($config);
45 $p = new HTMLPurifier_ElementDef();
46 $p->attr['class'] = $attrdef_nmtokens;
47 $p->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
48 $p->content_model = 'em | #PCDATA';
49 $p->content_model_type = 'optional';
50 $p->descendants_are_inline = true;
51 $p->safe = true;
53 $em = new HTMLPurifier_ElementDef();
54 $em->attr['class'] = $attrdef_nmtokens;
55 $em->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
56 $em->content_model = 'em | #PCDATA';
57 $em->content_model_type = 'optional';
58 $em->descendants_are_inline = true;
59 $em->safe = true;
61 $this->assertEqual(
62 array('p' => $p, 'em' => $em),
63 $manager->getElements()
66 // test trusted parameter override
68 $div = new HTMLPurifier_ElementDef();
69 $div->child = new HTMLPurifier_ChildDef_Optional(array('p', 'div', 'em', '#PCDATA'));
70 $div->content_model = 'p | div | em | #PCDATA';
71 $div->content_model_type = 'optional';
72 $div->descendants_are_inline = false;
73 $div->safe = false;
75 $this->assertEqual($div, $manager->getElement('div', true));
79 function testAllowedModules() {
81 $manager = new HTMLPurifier_HTMLModuleManager();
82 $manager->doctypes->register(
83 'Fantasy Inventory 1.0', true,
84 array('Weapons', 'Magic')
87 // register these modules so it doesn't blow up
88 $weapons_module = new HTMLPurifier_HTMLModule();
89 $weapons_module->name = 'Weapons';
90 $manager->registerModule($weapons_module);
92 $magic_module = new HTMLPurifier_HTMLModule();
93 $magic_module->name = 'Magic';
94 $manager->registerModule($magic_module);
96 $config = HTMLPurifier_Config::create(array(
97 'HTML.CustomDoctype' => 'Fantasy Inventory 1.0',
98 'HTML.AllowedModules' => 'Weapons'
99 ));
100 $manager->setup($config);
102 $this->assertTrue( isset($manager->modules['Weapons']));
103 $this->assertFalse(isset($manager->modules['Magic']));