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 elseif (file_exists('../test-settings.php')) include '../test-settings.php';
39 throw new Exception('Please create a test-settings.php file by copying test-settings.sample.php and configuring accordingly');
43 require_once $simpletest_location . 'unit_tester.php';
44 require_once $simpletest_location . 'reporter.php';
45 require_once $simpletest_location . 'mock_objects.php';
46 require_once $simpletest_location . 'xml.php';
47 require_once $simpletest_location . 'remote.php';
50 if ($csstidy_location !== false) {
51 require $csstidy_location . 'class.csstidy.php';
54 // load PEAR to include path
55 if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
56 // if PEAR is true, there's no need to add it to the path
57 set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR
.
61 // after external libraries are loaded, turn on compile time errors
62 error_reporting(E_ALL | E_STRICT
);
64 // initialize extra HTML Purifier libraries
65 require '../extras/HTMLPurifierExtras.auto.php';
67 // load SimpleTest addon functions
68 require 'generate_mock_once.func.php';
69 require 'path2class.func.php';
72 * Arguments parser, is cli and web agnostic.
74 * There are some quirks about the argument format:
75 * - Short boolean flags cannot be chained together
76 * - Only strings, integers and booleans are accepted
78 * Arguments array to populate. This takes a simple format of 'argument'
79 * => default value. Depending on the type of the default value,
80 * arguments will be typecast accordingly. For example, if
81 * 'flag' => false is passed, all arguments for that will be cast to
82 * boolean. Do *not* pass null, as it will not be recognized.
86 function htmlpurifier_parse_args(&$AC, $aliases) {
88 array_shift($_SERVER['argv']);
92 foreach ($_SERVER['argv'] as $opt) {
96 if ($opt === '') continue;
97 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
99 } elseif ($opt[0] == '-') {
100 $o = substr($opt, 1);
102 $lopt = strtolower($opt);
103 if ($bool !== false && ($opt === '0' ||
$lopt === 'off' ||
$lopt === 'no')) {
107 } elseif (isset($aliases[''])) {
112 if (!isset($AC[$o]) ||
!is_bool($AC[$o])) {
113 if (strpos($o, '=') === false) {
116 list($o, $v) = explode('=', $o);
117 } elseif (!$val_is_bool) {
121 $val_is_bool = false;
123 if ($o === false) continue;
124 htmlpurifier_args($AC, $aliases, $o, $v);
128 foreach ($_GET as $o => $v) {
129 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
130 $v = stripslashes($v);
132 htmlpurifier_args($AC, $aliases, $o, $v);
138 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
139 * @param $AC Arguments array to write to
140 * @param $aliases Aliases for options
141 * @param $o Argument name
142 * @param $v Argument value
144 function htmlpurifier_args(&$AC, $aliases, $o, $v) {
145 if (isset($aliases[$o])) $o = $aliases[$o];
146 if (!isset($AC[$o])) return;
147 if (is_string($AC[$o])) $AC[$o] = $v;
148 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ?
true :(bool) $v;
149 if (is_int($AC[$o])) $AC[$o] = (int) $v;
153 * Adds a test-class; we use file extension to determine which class to use.
155 function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
156 switch (strrchr($test_file, ".")) {
158 return $test->add(new PHPT_Controller_SimpleTest($test_file));
160 require_once $test_file;
161 return $test->add(path2class($test_file));
163 return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
165 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR
);
170 * Debugging function that prints tokens in a user-friendly manner.
172 function printTokens($tokens, $index = null) {
174 $generator = new HTMLPurifier_Generator();
175 foreach ($tokens as $i => $token) {
176 if ($index === $i) $string .= '[<strong>';
177 $string .= "<sup>$i</sup>";
178 $string .= $generator->escape($generator->generateFromToken($token));
179 if ($index === $i) $string .= '</strong>]';
186 * Convenient "insta-fail" test-case to add if any outside things fail
188 class FailedTest
extends UnitTestCase
{
189 protected $msg, $details;
190 public function __construct($msg, $details = null) {
192 $this->details
= $details;
194 public function test() {
195 $this->fail($this->msg
);
196 if ($this->details
) $this->reporter
->paintFormattedMessage($this->details
);
201 * Flushes all caches, and fatally errors out if there's a problem.
203 function htmlpurifier_flush($php, $reporter) {
204 exec($php . ' ../maintenance/flush.php ' . $php, $out, $status);
206 $test = new FailedTest(
207 'maintenance/flush.php returned non-zero exit status',
208 wordwrap(implode("\n", $out), 80)
210 $test->run($reporter);
216 * Dumps error queue, useful if there has been a fatal error.
218 function htmlpurifier_dump_error_queue() {
219 $context = SimpleTest
::getContext();
220 $queue = $context->get('SimpleErrorQueue');
221 while (($error = $queue->extract()) !== false) {
225 register_shutdown_function('htmlpurifier_dump_error_queue');