[3.1.0] When flush fails, fail SimpleTest
[htmlpurifier.git] / tests / common.php
blob773761c9c2f15ad543a75ace2f66511b03cfc7a2
1 <?php
3 if (!defined('HTMLPurifierTest')) {
4 echo "Invalid entry point\n";
5 exit;
8 // setup our own autoload, checking for HTMLPurifier library if spl_autoload_register
9 // is not allowed
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';
16 return true;
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();
32 $php = 'php';
33 $phpv = 'phpv';
35 // load configuration
36 if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
37 if (file_exists('../test-settings.php')) include '../test-settings.php';
39 // load SimpleTest
40 require $simpletest_location . 'unit_tester.php';
41 require $simpletest_location . 'reporter.php';
42 require $simpletest_location . 'mock_objects.php';
43 require $simpletest_location . 'xml.php';
44 require $simpletest_location . 'remote.php';
46 // load CSS Tidy
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 .
55 get_include_path());
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
69 /**
70 * Arguments parser, is cli and web agnostic.
71 * @warning
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
75 * @param $AC
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.
81 * @param $aliases
84 function htmlpurifier_parse_args(&$AC, $aliases) {
85 if (empty($_GET)) {
86 array_shift($_SERVER['argv']);
87 $o = false;
88 $bool = false;
89 $val_is_bool = false;
90 foreach ($_SERVER['argv'] as $opt) {
91 if ($o !== false) {
92 $v = $opt;
93 } else {
94 if ($opt === '') continue;
95 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
96 $o = substr($opt, 2);
97 } elseif ($opt[0] == '-') {
98 $o = substr($opt, 1);
99 } else {
100 $lopt = strtolower($opt);
101 if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
102 $o = $bool;
103 $v = false;
104 $val_is_bool = true;
105 } elseif (isset($aliases[''])) {
106 $o = $aliases[''];
109 $bool = false;
110 if (!isset($AC[$o]) || !is_bool($AC[$o])) {
111 if (strpos($o, '=') === false) {
112 continue;
114 list($o, $v) = explode('=', $o);
115 } elseif (!$val_is_bool) {
116 $v = true;
117 $bool = $o;
119 $val_is_bool = false;
121 if ($o === false) continue;
122 htmlpurifier_args($AC, $aliases, $o, $v);
123 $o = false;
125 } else {
126 foreach ($_GET as $o => $v) {
127 if (get_magic_quotes_gpc()) $v = stripslashes($v);
128 htmlpurifier_args($AC, $aliases, $o, $v);
134 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
135 * @param $AC Arguments array to write to
136 * @param $aliases Aliases for options
137 * @param $o Argument name
138 * @param $v Argument value
140 function htmlpurifier_args(&$AC, $aliases, $o, $v) {
141 if (isset($aliases[$o])) $o = $aliases[$o];
142 if (!isset($AC[$o])) return;
143 if (is_string($AC[$o])) $AC[$o] = $v;
144 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ? true :(bool) $v;
145 if (is_int($AC[$o])) $AC[$o] = (int) $v;
149 * Adds a test-class; depending on the file's extension this may involve
150 * a regular UnitTestCase or a special PHPT test
152 function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
153 switch (strrchr($test_file, ".")) {
154 case '.phpt':
155 return $test->addTestCase(new PHPT_Controller_SimpleTest($test_file));
156 case '.php':
157 require_once $test_file;
158 return $test->addTestClass(path2class($test_file));
159 case '.vtest':
160 return $test->addTestCase(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
161 default:
162 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
167 * Debugging function that prints tokens in a user-friendly manner.
169 function printTokens($tokens, $index = null) {
170 $string = '<pre>';
171 $generator = new HTMLPurifier_Generator();
172 foreach ($tokens as $i => $token) {
173 if ($index === $i) $string .= '[<strong>';
174 $string .= "<sup>$i</sup>";
175 $string .= $generator->escape($generator->generateFromToken($token));
176 if ($index === $i) $string .= '</strong>]';
178 $string .= '</pre>';
179 echo $string;
183 * Convenient "insta-fail" test-case to add if any outside things fail
185 class FailedTest extends UnitTestCase {
186 protected $msg, $details;
187 public function __construct($msg, $details = null) {
188 $this->msg = $msg;
189 $this->details = $details;
191 public function test() {
192 $this->fail($this->msg);
193 if ($this->details) $this->_reporter->paintFormattedMessage($this->details);
198 * Flushes all caches, and fatally errors out if there's a problem.
200 function htmlpurifier_flush($php, $reporter) {
201 exec($php . ' ../maintenance/flush.php', $out, $status);
202 if ($status) {
203 $test = new FailedTest(
204 'maintenance/flush.php returned non-zero exit status',
205 wordwrap(implode("\n", $out), 80)
207 $test->run($reporter);
208 exit(1);