aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/Harness.php
blob: d6490a117d91a5e08f6b0f070125a3a0162b29d7 (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
<?php

/**
 * All-use harness, use this rather than SimpleTest's
 */
class HTMLPurifier_Harness extends UnitTestCase
{

    public function __construct($name = null) {
        parent::__construct($name);
    }

    protected $config, $context, $purifier;

    /**
     * Generates easily accessible default config/context, as well as
     * a convenience purifier for integration testing.
     */
    public function setUp() {
        list($this->config, $this->context) = $this->createCommon();
        $this->config->set('Output.Newline', '
');
        $this->purifier = new HTMLPurifier();
    }

    /**
     * Asserts a purification. Good for integration testing.
     */
    function assertPurification($input, $expect = null) {
        if ($expect === null) $expect = $input;
        $result = $this->purifier->purify($input, $this->config);
        $this->assertIdentical($expect, $result);
    }


    /**
     * Accepts config and context and prepares them into a valid state
     * @param &$config Reference to config variable
     * @param &$context Reference to context variable
     */
    protected function prepareCommon(&$config, &$context) {
        $config = HTMLPurifier_Config::create($config);
        if (!$context) $context = new HTMLPurifier_Context();
    }

    /**
     * Generates default configuration and context objects
     * @return Defaults in form of array($config, $context)
     */
    protected function createCommon() {
        return array(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
    }

    /**
     * Normalizes a string to Unix (\n) endings
     */
    protected function normalize(&$string) {
        $string = str_replace(array("\r\n", "\r"), "\n", $string);
    }

    /**
     * If $expect is false, ignore $result and check if status failed.
     * Otherwise, check if $status if true and $result === $expect.
     * @param $status Boolean status
     * @param $result Mixed result from processing
     * @param $expect Mixed expectation for result
     */
    protected function assertEitherFailOrIdentical($status, $result, $expect) {
        if ($expect === false) {
            $this->assertFalse($status, 'Expected false result, got true');
        } else {
            $this->assertTrue($status, 'Expected true result, got false');
            $this->assertIdentical($result, $expect);
        }
    }

    public function getTests() {
        // __onlytest makes only one test get triggered
        foreach (get_class_methods(get_class($this)) as $method) {
            if (strtolower(substr($method, 0, 10)) == '__onlytest') {
                $this->reporter->paintSkip('All test methods besides ' . $method);
                return array($method);
            }
        }
        return parent::getTests();
    }

}

// vim: et sw=4 sts=4