Blame view

htmlpurifier/tests/HTMLPurifier/HTMLModule/TidyTest.php 6.86 KB
Maulyanda authored
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
<?php

Mock::generatePartial(
    'HTMLPurifier_HTMLModule_Tidy',
    'HTMLPurifier_HTMLModule_Tidy_TestForConstruct',
    array('makeFixes', 'makeFixesForLevel', 'populate')
);

class HTMLPurifier_HTMLModule_TidyTest extends HTMLPurifier_Harness
{

    public function test_getFixesForLevel()
    {
        $module = new HTMLPurifier_HTMLModule_Tidy();
        $module->fixesForLevel['light'][]  = 'light-fix';
        $module->fixesForLevel['medium'][] = 'medium-fix';
        $module->fixesForLevel['heavy'][]  = 'heavy-fix';

        $this->assertIdentical(
            array(),
            $module->getFixesForLevel('none')
        );
        $this->assertIdentical(
            array('light-fix' => true),
            $module->getFixesForLevel('light')
        );
        $this->assertIdentical(
            array('light-fix' => true, 'medium-fix' => true),
            $module->getFixesForLevel('medium')
        );
        $this->assertIdentical(
            array('light-fix' => true, 'medium-fix' => true, 'heavy-fix' => true),
            $module->getFixesForLevel('heavy')
        );

        $this->expectError('Tidy level turbo not recognized');
        $module->getFixesForLevel('turbo');

    }

    public function test_setup()
    {
        $i = 0; // counter, helps us isolate expectations

        // initialize partial mock
        $module = new HTMLPurifier_HTMLModule_Tidy_TestForConstruct();
        $module->fixesForLevel['light']  = array('light-fix-1', 'light-fix-2');
        $module->fixesForLevel['medium'] = array('medium-fix-1', 'medium-fix-2');
        $module->fixesForLevel['heavy']  = array('heavy-fix-1', 'heavy-fix-2');

        $j = 0;
        $fixes = array(
            'light-fix-1'  => $lf1 = $j++,
            'light-fix-2'  => $lf2 = $j++,
            'medium-fix-1' => $mf1 = $j++,
            'medium-fix-2' => $mf2 = $j++,
            'heavy-fix-1'  => $hf1 = $j++,
            'heavy-fix-2'  => $hf2 = $j++
        );
        $module->setReturnValue('makeFixes', $fixes);

        $config = HTMLPurifier_Config::create(array(
            'HTML.TidyLevel' => 'none'
        ));
        $module->expectAt($i++, 'populate', array(array()));
        $module->setup($config);

        // basic levels

        $config = HTMLPurifier_Config::create(array(
            'HTML.TidyLevel' => 'light'
        ));
        $module->expectAt($i++, 'populate', array(array(
            'light-fix-1' => $lf1,
            'light-fix-2' => $lf2
        )));
        $module->setup($config);

        $config = HTMLPurifier_Config::create(array(
            'HTML.TidyLevel' => 'heavy'
        ));
        $module->expectAt($i++, 'populate', array(array(
            'light-fix-1'  => $lf1,
            'light-fix-2'  => $lf2,
            'medium-fix-1' => $mf1,
            'medium-fix-2' => $mf2,
            'heavy-fix-1'  => $hf1,
            'heavy-fix-2'  => $hf2
        )));
        $module->setup($config);

        // fine grained tuning

        $config = HTMLPurifier_Config::create(array(
            'HTML.TidyLevel' => 'none',
            'HTML.TidyAdd'   => array('light-fix-1', 'medium-fix-1')
        ));
        $module->expectAt($i++, 'populate', array(array(
            'light-fix-1' => $lf1,
            'medium-fix-1' => $mf1
        )));
        $module->setup($config);

        $config = HTMLPurifier_Config::create(array(
            'HTML.TidyLevel' => 'medium',
            'HTML.TidyRemove'   => array('light-fix-1', 'medium-fix-1')
        ));
        $module->expectAt($i++, 'populate', array(array(
            'light-fix-2' => $lf2,
            'medium-fix-2' => $mf2
        )));
        $module->setup($config);

    }

    public function test_makeFixesForLevel()
    {
        $module = new HTMLPurifier_HTMLModule_Tidy();
        $module->defaultLevel = 'heavy';

        $module->makeFixesForLevel(array(
            'fix-1' => 0,
            'fix-2' => 1,
            'fix-3' => 2
        ));

        $this->assertIdentical($module->fixesForLevel['heavy'], array('fix-1', 'fix-2', 'fix-3'));
        $this->assertIdentical($module->fixesForLevel['medium'], array());
        $this->assertIdentical($module->fixesForLevel['light'], array());

    }
    public function test_makeFixesForLevel_undefinedLevel()
    {
        $module = new HTMLPurifier_HTMLModule_Tidy();
        $module->defaultLevel = 'bananas';

        $this->expectError('Default level bananas does not exist');

        $module->makeFixesForLevel(array(
            'fix-1' => 0
        ));

    }

    public function test_getFixType()
    {
        // syntax needs documenting

        $module = new HTMLPurifier_HTMLModule_Tidy();

        $this->assertIdentical(
            $module->getFixType('a'),
            array('tag_transform', array('element' => 'a'))
        );

        $this->assertIdentical(
            $module->getFixType('a@href'),
            $reuse = array('attr_transform_pre', array('element' => 'a', 'attr' => 'href'))
        );

        $this->assertIdentical(
            $module->getFixType('a@href#pre'),
            $reuse
        );

        $this->assertIdentical(
            $module->getFixType('a@href#post'),
            array('attr_transform_post', array('element' => 'a', 'attr' => 'href'))
        );

        $this->assertIdentical(
            $module->getFixType('xml:foo@xml:bar'),
            array('attr_transform_pre', array('element' => 'xml:foo', 'attr' => 'xml:bar'))
        );

        $this->assertIdentical(
            $module->getFixType('blockquote#child'),
            array('child', array('element' => 'blockquote'))
        );

        $this->assertIdentical(
            $module->getFixType('@lang'),
            array('attr_transform_pre', array('attr' => 'lang'))
        );

        $this->assertIdentical(
            $module->getFixType('@lang#post'),
            array('attr_transform_post', array('attr' => 'lang'))
        );

    }

    public function test_populate()
    {
        $i = 0;

        $module = new HTMLPurifier_HTMLModule_Tidy();
        $module->populate(array(
            'element' => $element = $i++,
            'element@attr' => $attr = $i++,
            'element@attr#post' => $attr_post = $i++,
            'element#child' => $child = $i++,
            'element#content_model_type' => $content_model_type = $i++,
            '@attr' => $global_attr = $i++,
            '@attr#post' => $global_attr_post = $i++
        ));

        $module2 = new HTMLPurifier_HTMLModule_Tidy();
        $e = $module2->addBlankElement('element');
        $e->attr_transform_pre['attr'] = $attr;
        $e->attr_transform_post['attr'] = $attr_post;
        $e->child = $child;
        $e->content_model_type = $content_model_type;
        $module2->info_tag_transform['element'] = $element;
        $module2->info_attr_transform_pre['attr'] = $global_attr;
        $module2->info_attr_transform_post['attr'] = $global_attr_post;

        $this->assertEqual($module, $module2);

    }

}

// vim: et sw=4 sts=4