summaryrefslogtreecommitdiffstats
path: root/vendor/ezyang/htmlpurifier/tests/common.php
blob: 8f35a410cb550776b243f5bc0a3cc06f3261bbf3 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php

if (!defined('HTMLPurifierTest')) {
    echo "Invalid entry point\n";
    exit;
}

// setup our own autoload, checking for HTMLPurifier library if spl_autoload_register
// is not allowed
function __autoload($class)
{
    if (!function_exists('spl_autoload_register')) {
        if (HTMLPurifier_Bootstrap::autoload($class)) return true;
        if (HTMLPurifierExtras::autoload($class)) return true;
    }
    require str_replace('_', '/', $class) . '.php';
    return true;
}
if (function_exists('spl_autoload_register')) {
    spl_autoload_register('__autoload');
}

// default settings (protect against register_globals)
$GLOBALS['HTMLPurifierTest'] = array();
$GLOBALS['HTMLPurifierTest']['PEAR'] = false; // do PEAR tests
$GLOBALS['HTMLPurifierTest']['PHPT'] = true; // do PHPT tests
$GLOBALS['HTMLPurifierTest']['PH5P'] = class_exists('DOMDocument');

// default library settings
$simpletest_location = 'simpletest/'; // reasonable guess
$csstidy_location = false;
$versions_to_test = array();
$php  = 'php';
$phpv = 'phpv';

// load configuration
if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
elseif (file_exists('../test-settings.php')) include '../test-settings.php';
else {
    throw new Exception('Please create a test-settings.php file by copying test-settings.sample.php and configuring accordingly');
}

// load SimpleTest
require_once $simpletest_location . 'unit_tester.php';
require_once $simpletest_location . 'reporter.php';
require_once $simpletest_location . 'mock_objects.php';
require_once $simpletest_location . 'xml.php';
require_once $simpletest_location . 'remote.php';

// load CSS Tidy
if ($csstidy_location !== false) {
    $old = error_reporting(E_ALL);
    require $csstidy_location . 'class.csstidy.php';
    error_reporting($old);
}

// load PEAR to include path
if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
    // if PEAR is true, there's no need to add it to the path
    set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR .
        get_include_path());
}

// after external libraries are loaded, turn on compile time errors
error_reporting(E_ALL | E_STRICT);

// initialize extra HTML Purifier libraries
require '../extras/HTMLPurifierExtras.auto.php';

// load SimpleTest addon functions
require 'generate_mock_once.func.php';
require 'path2class.func.php';

/**
 * Arguments parser, is cli and web agnostic.
 * @warning
 *   There are some quirks about the argument format:
 *     - Short boolean flags cannot be chained together
 *     - Only strings, integers and booleans are accepted
 * @param $AC
 *   Arguments array to populate. This takes a simple format of 'argument'
 *   => default value. Depending on the type of the default value,
 *   arguments will be typecast accordingly. For example, if
 *   'flag' => false is passed, all arguments for that will be cast to
 *   boolean. Do *not* pass null, as it will not be recognized.
 * @param $aliases
 *
 */
function htmlpurifier_parse_args(&$AC, $aliases)
{
    if (empty($_GET) && !empty($_SERVER['argv'])) {
        array_shift($_SERVER['argv']);
        $o = false;
        $bool = false;
        $val_is_bool = false;
        foreach ($_SERVER['argv'] as $opt) {
            if ($o !== false) {
                $v = $opt;
            } else {
                if ($opt === '') continue;
                if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
                    $o = substr($opt, 2);
                } elseif ($opt[0] == '-') {
                    $o = substr($opt, 1);
                } else {
                    $lopt = strtolower($opt);
                    if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
                        $o = $bool;
                        $v = false;
                        $val_is_bool = true;
                    } elseif (isset($aliases[''])) {
                        $o = $aliases[''];
                    }
                }
                $bool = false;
                if (!isset($AC[$o]) || !is_bool($AC[$o])) {
                    if (strpos($o, '=') === false) {
                        continue;
                    }
                    list($o, $v) = explode('=', $o);
                } elseif (!$val_is_bool) {
                    $v = true;
                    $bool = $o;
                }
                $val_is_bool = false;
            }
            if ($o === false) continue;
            htmlpurifier_args($AC, $aliases, $o, $v);
            $o = false;
        }
    } else {
        foreach ($_GET as $o => $v) {
            if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
                $v = stripslashes($v);
            }
            htmlpurifier_args($AC, $aliases, $o, $v);
        }
    }
}

/**
 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
 * @param $AC Arguments array to write to
 * @param $aliases Aliases for options
 * @param $o Argument name
 * @param $v Argument value
 */
function htmlpurifier_args(&$AC, $aliases, $o, $v)
{
    if (isset($aliases[$o])) $o = $aliases[$o];
    if (!isset($AC[$o])) return;
    if (is_string($AC[$o])) $AC[$o] = $v;
    if (is_bool($AC[$o]))   $AC[$o] = ($v === '') ? true :(bool) $v;
    if (is_int($AC[$o]))    $AC[$o] = (int) $v;
}

/**
 * Adds a test-class; we use file extension to determine which class to use.
 */
function htmlpurifier_add_test($test, $test_file, $only_phpt = false)
{
    switch (strrchr($test_file, ".")) {
        case '.phpt':
            return $test->add(new PHPT_Controller_SimpleTest($test_file));
        case '.php':
            require_once $test_file;
            return $test->add(path2class($test_file));
        case '.vtest':
            return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
        case '.htmlt':
            return $test->add(new HTMLPurifier_HTMLT($test_file));
        default:
            trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
    }
}

/**
 * Debugging function that prints tokens in a user-friendly manner.
 */
function printTokens($tokens, $index = null)
{
    $string = '<pre>';
    $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
    foreach ($tokens as $i => $token) {
        $string .= printToken($generator, $token, $i, $index == $i);
    }
    $string .= '</pre>';
    echo $string;
}

function printToken($generator, $token, $i, $isCursor)
{
    $string = "";
    if ($isCursor) $string .= '[<strong>';
    $string .= "<sup>$i</sup>";
    $string .= $generator->escape($generator->generateFromToken($token));
    if ($isCursor) $string .= '</strong>]';
    return $string;
}

function printZipper($zipper, $token)
{
    $string = '<pre>';
    $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
    foreach ($zipper->front as $i => $t) {
        $string .= printToken($generator, $t, $i, false);
    }
    if ($token !== NULL) {
        $string .= printToken($generator, $token, "", true);
    }
    for ($i = count($zipper->back)-1; $i >= 0; $i--) {
        $string .= printToken($generator, $zipper->back[$i], $i, false);
    }
    $string .= '</pre>';
    echo $string;
}

/**
 * Convenient "insta-fail" test-case to add if any outside things fail
 */
class FailedTest extends UnitTestCase
{
    protected $msg, $details;
    public function __construct($msg, $details = null)
    {
        $this->msg = $msg;
        $this->details = $details;
    }
    public function test()
    {
        $this->fail($this->msg);
        if ($this->details) $this->reporter->paintFormattedMessage($this->details);
    }
}

/**
 * Flushes all caches, and fatally errors out if there's a problem.
 */
function htmlpurifier_flush($php, $reporter)
{
    exec($php . ' ../maintenance/flush.php ' . $php . ' 2>&1', $out, $status);
    if ($status) {
        $test = new FailedTest(
            'maintenance/flush.php returned non-zero exit status',
            wordwrap(implode("\n", $out), 80)
        );
        $test->run($reporter);
        exit(1);
    }
}

/**
 * Dumps error queue, useful if there has been a fatal error.
 */
function htmlpurifier_dump_error_queue()
{
    $context = SimpleTest::getContext();
    $queue = $context->get('SimpleErrorQueue');
    while (($error = $queue->extract()) !== false) {
        var_dump($error);
    }
}
register_shutdown_function('htmlpurifier_dump_error_queue');

// vim: et sw=4 sts=4