aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/HTMLModuleManagerTest.php
blob: 2750f8924cc4364d74f5cef4a6cf946e23180f7e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

class HTMLPurifier_HTMLModuleManagerTest extends HTMLPurifier_Harness
{

    protected function createManager() {
        $manager = new HTMLPurifier_HTMLModuleManager();

        $this->config->set('HTML.CustomDoctype', 'Blank');
        $manager->doctypes->register('Blank');

        $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();

        generate_mock_once('HTMLPurifier_AttrDef');
        $attrdef = new HTMLPurifier_AttrDefMock();
        $attrdef->setReturnValue('make', $attrdef_nmtokens);
        $manager->attrTypes->set('NMTOKENS', $attrdef);
        return $manager;
    }

    function test_addModule() {

        $manager = $this->createManager();

        // ...but we add user modules

        $common_module = new HTMLPurifier_HTMLModule();
        $common_module->name = 'Common';
        $common_module->attr_collections['Common'] = array('class' => 'NMTOKENS');
        $common_module->content_sets['Flow'] = 'Block | Inline';
        $manager->addModule($common_module);

        $structural_module = new HTMLPurifier_HTMLModule();
        $structural_module->name = 'Structural';
        $structural_module->addElement('p', 'Block', 'Inline', 'Common');
        $manager->addModule($structural_module);

        $formatting_module = new HTMLPurifier_HTMLModule();
        $formatting_module->name = 'Formatting';
        $formatting_module->addElement('em', 'Inline', 'Inline', 'Common');
        $manager->addModule($formatting_module);

        $unsafe_module = new HTMLPurifier_HTMLModule();
        $unsafe_module->name = 'Unsafe';
        $unsafe_module->safe = false;
        $unsafe_module->addElement('div', 'Block', 'Flow');
        $manager->addModule($unsafe_module);

        $config = HTMLPurifier_Config::createDefault();
        $config->set('HTML.Trusted', false);
        $config->set('HTML.CustomDoctype', 'Blank');

        $manager->setup($config);

        $attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();

        $p = new HTMLPurifier_ElementDef();
        $p->attr['class'] = $attrdef_nmtokens;
        $p->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
        $p->content_model = 'em | #PCDATA';
        $p->content_model_type = 'optional';
        $p->descendants_are_inline = true;

        $em = new HTMLPurifier_ElementDef();
        $em->attr['class'] = $attrdef_nmtokens;
        $em->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
        $em->content_model = 'em | #PCDATA';
        $em->content_model_type = 'optional';
        $em->descendants_are_inline = true;

        $this->assertEqual(
            array('p' => $p, 'em' => $em),
            $manager->getElements()
        );

        // test trusted parameter override

        $div = new HTMLPurifier_ElementDef();
        $div->child = new HTMLPurifier_ChildDef_Optional(array('p', 'div', 'em', '#PCDATA'));
        $div->content_model = 'p | div | em | #PCDATA';
        $div->content_model_type = 'optional';
        $div->descendants_are_inline = false;

        $this->assertEqual($div, $manager->getElement('div', true));

    }

    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.CustomDoctype' => 'Fantasy Inventory 1.0',
            'HTML.AllowedModules' => 'Weapons'
        ));
        $manager->setup($config);

        $this->assertTrue( isset($manager->modules['Weapons']));
        $this->assertFalse(isset($manager->modules['Magic']));

    }



}

// vim: et sw=4 sts=4