3 if (!defined('HTMLPurifierTest')) {
4 echo "Invalid entry point\n";
8 // setup our own autoload, checking for HTMLPurifier library if spl_autoload_register
10 function __autoload($class) {
11 if (!function_exists('spl_autoload_register')) {
12 if (HTMLPurifier_Bootstrap
::autoload($class)) return true;
13 if (HTMLPurifierExtras
::autoload($class)) return true;
15 require str_replace('_', '/', $class) . '.php';
18 if (function_exists('spl_autoload_register')) {
19 spl_autoload_register('__autoload');
22 // default settings (protect against register_globals)
23 $GLOBALS['HTMLPurifierTest'] = array();
24 $GLOBALS['HTMLPurifierTest']['PEAR'] = false; // do PEAR tests
25 $GLOBALS['HTMLPurifierTest']['PHPT'] = true; // do PHPT tests
26 $GLOBALS['HTMLPurifierTest']['PH5P'] = class_exists('DOMDocument');
28 // default library settings
29 $simpletest_location = 'simpletest/'; // reasonable guess
30 $csstidy_location = false;
31 $versions_to_test = array();
36 if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
37 if (file_exists('../test-settings.php')) include '../test-settings.php';
40 require_once $simpletest_location . 'unit_tester.php';
41 require_once $simpletest_location . 'reporter.php';
42 require_once $simpletest_location . 'mock_objects.php';
43 require_once $simpletest_location . 'xml.php';
44 require_once $simpletest_location . 'remote.php';
47 if ($csstidy_location !== false) {
48 require $csstidy_location . 'class.csstidy.php';
51 // load PEAR to include path
52 if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
53 // if PEAR is true, there's no need to add it to the path
54 set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR
.
58 // after external libraries are loaded, turn on compile time errors
59 error_reporting(E_ALL | E_STRICT
);
61 // initialize extra HTML Purifier libraries
62 require '../extras/HTMLPurifierExtras.auto.php';
64 // load SimpleTest addon functions
65 require 'generate_mock_once.func.php';
66 require 'path2class.func.php';
67 require 'tally_errors.func.php'; // compat
70 * Arguments parser, is cli and web agnostic.
72 * There are some quirks about the argument format:
73 * - Short boolean flags cannot be chained together
74 * - Only strings, integers and booleans are accepted
76 * Arguments array to populate. This takes a simple format of 'argument'
77 * => default value. Depending on the type of the default value,
78 * arguments will be typecast accordingly. For example, if
79 * 'flag' => false is passed, all arguments for that will be cast to
80 * boolean. Do *not* pass null, as it will not be recognized.
84 function htmlpurifier_parse_args(&$AC, $aliases) {
86 array_shift($_SERVER['argv']);
90 foreach ($_SERVER['argv'] as $opt) {
94 if ($opt === '') continue;
95 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
97 } elseif ($opt[0] == '-') {
100 $lopt = strtolower($opt);
101 if ($bool !== false && ($opt === '0' ||
$lopt === 'off' ||
$lopt === 'no')) {
105 } elseif (isset($aliases[''])) {
110 if (!isset($AC[$o]) ||
!is_bool($AC[$o])) {
111 if (strpos($o, '=') === false) {
114 list($o, $v) = explode('=', $o);
115 } elseif (!$val_is_bool) {
119 $val_is_bool = false;
121 if ($o === false) continue;
122 htmlpurifier_args($AC, $aliases, $o, $v);
126 foreach ($_GET as $o => $v) {
127 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
128 $v = stripslashes($v);
130 htmlpurifier_args($AC, $aliases, $o, $v);
136 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
137 * @param $AC Arguments array to write to
138 * @param $aliases Aliases for options
139 * @param $o Argument name
140 * @param $v Argument value
142 function htmlpurifier_args(&$AC, $aliases, $o, $v) {
143 if (isset($aliases[$o])) $o = $aliases[$o];
144 if (!isset($AC[$o])) return;
145 if (is_string($AC[$o])) $AC[$o] = $v;
146 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ?
true :(bool) $v;
147 if (is_int($AC[$o])) $AC[$o] = (int) $v;
151 * Adds a test-class; we use file extension to determine which class to use.
153 function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
154 switch (strrchr($test_file, ".")) {
156 return $test->addTestCase(new PHPT_Controller_SimpleTest($test_file));
158 require_once $test_file;
159 return $test->addTestClass(path2class($test_file));
161 return $test->addTestCase(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
163 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR
);
168 * Debugging function that prints tokens in a user-friendly manner.
170 function printTokens($tokens, $index = null) {
172 $generator = new HTMLPurifier_Generator();
173 foreach ($tokens as $i => $token) {
174 if ($index === $i) $string .= '[<strong>';
175 $string .= "<sup>$i</sup>";
176 $string .= $generator->escape($generator->generateFromToken($token));
177 if ($index === $i) $string .= '</strong>]';
184 * Convenient "insta-fail" test-case to add if any outside things fail
186 class FailedTest
extends UnitTestCase
{
187 protected $msg, $details;
188 public function __construct($msg, $details = null) {
190 $this->details
= $details;
192 public function test() {
193 $this->fail($this->msg
);
194 if ($this->details
) $this->_reporter
->paintFormattedMessage($this->details
);
199 * Flushes all caches, and fatally errors out if there's a problem.
201 function htmlpurifier_flush($php, $reporter) {
202 exec($php . ' ../maintenance/flush.php', $out, $status);
204 $test = new FailedTest(
205 'maintenance/flush.php returned non-zero exit status',
206 wordwrap(implode("\n", $out), 80)
208 $test->run($reporter);
214 * Dumps error queue, useful if there has been a fatal error.
216 function htmlpurifier_dump_error_queue() {
217 $context = &SimpleTest
::getContext();
218 $queue = &$context->get('SimpleErrorQueue');
219 if ($queue && !empty($queue->_queue
)) {
220 // replace this with something prettier
221 var_dump($queue->_queue
);
224 register_shutdown_function('htmlpurifier_dump_error_queue');