Merge pull request #121 from breathbath/master
[htmlpurifier/darkodev.git] / tests / common.php
blob8f35a410cb550776b243f5bc0a3cc06f3261bbf3
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)
12 if (!function_exists('spl_autoload_register')) {
13 if (HTMLPurifier_Bootstrap::autoload($class)) return true;
14 if (HTMLPurifierExtras::autoload($class)) return true;
16 require str_replace('_', '/', $class) . '.php';
17 return true;
19 if (function_exists('spl_autoload_register')) {
20 spl_autoload_register('__autoload');
23 // default settings (protect against register_globals)
24 $GLOBALS['HTMLPurifierTest'] = array();
25 $GLOBALS['HTMLPurifierTest']['PEAR'] = false; // do PEAR tests
26 $GLOBALS['HTMLPurifierTest']['PHPT'] = true; // do PHPT tests
27 $GLOBALS['HTMLPurifierTest']['PH5P'] = class_exists('DOMDocument');
29 // default library settings
30 $simpletest_location = 'simpletest/'; // reasonable guess
31 $csstidy_location = false;
32 $versions_to_test = array();
33 $php = 'php';
34 $phpv = 'phpv';
36 // load configuration
37 if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
38 elseif (file_exists('../test-settings.php')) include '../test-settings.php';
39 else {
40 throw new Exception('Please create a test-settings.php file by copying test-settings.sample.php and configuring accordingly');
43 // load SimpleTest
44 require_once $simpletest_location . 'unit_tester.php';
45 require_once $simpletest_location . 'reporter.php';
46 require_once $simpletest_location . 'mock_objects.php';
47 require_once $simpletest_location . 'xml.php';
48 require_once $simpletest_location . 'remote.php';
50 // load CSS Tidy
51 if ($csstidy_location !== false) {
52 $old = error_reporting(E_ALL);
53 require $csstidy_location . 'class.csstidy.php';
54 error_reporting($old);
57 // load PEAR to include path
58 if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
59 // if PEAR is true, there's no need to add it to the path
60 set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR .
61 get_include_path());
64 // after external libraries are loaded, turn on compile time errors
65 error_reporting(E_ALL | E_STRICT);
67 // initialize extra HTML Purifier libraries
68 require '../extras/HTMLPurifierExtras.auto.php';
70 // load SimpleTest addon functions
71 require 'generate_mock_once.func.php';
72 require 'path2class.func.php';
74 /**
75 * Arguments parser, is cli and web agnostic.
76 * @warning
77 * There are some quirks about the argument format:
78 * - Short boolean flags cannot be chained together
79 * - Only strings, integers and booleans are accepted
80 * @param $AC
81 * Arguments array to populate. This takes a simple format of 'argument'
82 * => default value. Depending on the type of the default value,
83 * arguments will be typecast accordingly. For example, if
84 * 'flag' => false is passed, all arguments for that will be cast to
85 * boolean. Do *not* pass null, as it will not be recognized.
86 * @param $aliases
89 function htmlpurifier_parse_args(&$AC, $aliases)
91 if (empty($_GET) && !empty($_SERVER['argv'])) {
92 array_shift($_SERVER['argv']);
93 $o = false;
94 $bool = false;
95 $val_is_bool = false;
96 foreach ($_SERVER['argv'] as $opt) {
97 if ($o !== false) {
98 $v = $opt;
99 } else {
100 if ($opt === '') continue;
101 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
102 $o = substr($opt, 2);
103 } elseif ($opt[0] == '-') {
104 $o = substr($opt, 1);
105 } else {
106 $lopt = strtolower($opt);
107 if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
108 $o = $bool;
109 $v = false;
110 $val_is_bool = true;
111 } elseif (isset($aliases[''])) {
112 $o = $aliases[''];
115 $bool = false;
116 if (!isset($AC[$o]) || !is_bool($AC[$o])) {
117 if (strpos($o, '=') === false) {
118 continue;
120 list($o, $v) = explode('=', $o);
121 } elseif (!$val_is_bool) {
122 $v = true;
123 $bool = $o;
125 $val_is_bool = false;
127 if ($o === false) continue;
128 htmlpurifier_args($AC, $aliases, $o, $v);
129 $o = false;
131 } else {
132 foreach ($_GET as $o => $v) {
133 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
134 $v = stripslashes($v);
136 htmlpurifier_args($AC, $aliases, $o, $v);
142 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
143 * @param $AC Arguments array to write to
144 * @param $aliases Aliases for options
145 * @param $o Argument name
146 * @param $v Argument value
148 function htmlpurifier_args(&$AC, $aliases, $o, $v)
150 if (isset($aliases[$o])) $o = $aliases[$o];
151 if (!isset($AC[$o])) return;
152 if (is_string($AC[$o])) $AC[$o] = $v;
153 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ? true :(bool) $v;
154 if (is_int($AC[$o])) $AC[$o] = (int) $v;
158 * Adds a test-class; we use file extension to determine which class to use.
160 function htmlpurifier_add_test($test, $test_file, $only_phpt = false)
162 switch (strrchr($test_file, ".")) {
163 case '.phpt':
164 return $test->add(new PHPT_Controller_SimpleTest($test_file));
165 case '.php':
166 require_once $test_file;
167 return $test->add(path2class($test_file));
168 case '.vtest':
169 return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
170 case '.htmlt':
171 return $test->add(new HTMLPurifier_HTMLT($test_file));
172 default:
173 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
178 * Debugging function that prints tokens in a user-friendly manner.
180 function printTokens($tokens, $index = null)
182 $string = '<pre>';
183 $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
184 foreach ($tokens as $i => $token) {
185 $string .= printToken($generator, $token, $i, $index == $i);
187 $string .= '</pre>';
188 echo $string;
191 function printToken($generator, $token, $i, $isCursor)
193 $string = "";
194 if ($isCursor) $string .= '[<strong>';
195 $string .= "<sup>$i</sup>";
196 $string .= $generator->escape($generator->generateFromToken($token));
197 if ($isCursor) $string .= '</strong>]';
198 return $string;
201 function printZipper($zipper, $token)
203 $string = '<pre>';
204 $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
205 foreach ($zipper->front as $i => $t) {
206 $string .= printToken($generator, $t, $i, false);
208 if ($token !== NULL) {
209 $string .= printToken($generator, $token, "", true);
211 for ($i = count($zipper->back)-1; $i >= 0; $i--) {
212 $string .= printToken($generator, $zipper->back[$i], $i, false);
214 $string .= '</pre>';
215 echo $string;
219 * Convenient "insta-fail" test-case to add if any outside things fail
221 class FailedTest extends UnitTestCase
223 protected $msg, $details;
224 public function __construct($msg, $details = null)
226 $this->msg = $msg;
227 $this->details = $details;
229 public function test()
231 $this->fail($this->msg);
232 if ($this->details) $this->reporter->paintFormattedMessage($this->details);
237 * Flushes all caches, and fatally errors out if there's a problem.
239 function htmlpurifier_flush($php, $reporter)
241 exec($php . ' ../maintenance/flush.php ' . $php . ' 2>&1', $out, $status);
242 if ($status) {
243 $test = new FailedTest(
244 'maintenance/flush.php returned non-zero exit status',
245 wordwrap(implode("\n", $out), 80)
247 $test->run($reporter);
248 exit(1);
253 * Dumps error queue, useful if there has been a fatal error.
255 function htmlpurifier_dump_error_queue()
257 $context = SimpleTest::getContext();
258 $queue = $context->get('SimpleErrorQueue');
259 while (($error = $queue->extract()) !== false) {
260 var_dump($error);
263 register_shutdown_function('htmlpurifier_dump_error_queue');
265 // vim: et sw=4 sts=4