Support for inline-block.
[htmlpurifier.git] / tests / common.php
blob223a5bf1fc90c154cde3c8c42b90f32bfec81b3d
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 elseif (file_exists('../test-settings.php')) include '../test-settings.php';
38 else {
39 throw new Exception('Please create a test-settings.php file by copying test-settings.sample.php and configuring accordingly');
42 // load SimpleTest
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';
49 // load CSS Tidy
50 if ($csstidy_location !== false) {
51 $old = error_reporting(E_ALL);
52 require $csstidy_location . 'class.csstidy.php';
53 error_reporting($old);
56 // load PEAR to include path
57 if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
58 // if PEAR is true, there's no need to add it to the path
59 set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR .
60 get_include_path());
63 // after external libraries are loaded, turn on compile time errors
64 error_reporting(E_ALL | E_STRICT);
66 // initialize extra HTML Purifier libraries
67 require '../extras/HTMLPurifierExtras.auto.php';
69 // load SimpleTest addon functions
70 require 'generate_mock_once.func.php';
71 require 'path2class.func.php';
73 /**
74 * Arguments parser, is cli and web agnostic.
75 * @warning
76 * There are some quirks about the argument format:
77 * - Short boolean flags cannot be chained together
78 * - Only strings, integers and booleans are accepted
79 * @param $AC
80 * Arguments array to populate. This takes a simple format of 'argument'
81 * => default value. Depending on the type of the default value,
82 * arguments will be typecast accordingly. For example, if
83 * 'flag' => false is passed, all arguments for that will be cast to
84 * boolean. Do *not* pass null, as it will not be recognized.
85 * @param $aliases
88 function htmlpurifier_parse_args(&$AC, $aliases) {
89 if (empty($_GET) && !empty($_SERVER['argv'])) {
90 array_shift($_SERVER['argv']);
91 $o = false;
92 $bool = false;
93 $val_is_bool = false;
94 foreach ($_SERVER['argv'] as $opt) {
95 if ($o !== false) {
96 $v = $opt;
97 } else {
98 if ($opt === '') continue;
99 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
100 $o = substr($opt, 2);
101 } elseif ($opt[0] == '-') {
102 $o = substr($opt, 1);
103 } else {
104 $lopt = strtolower($opt);
105 if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
106 $o = $bool;
107 $v = false;
108 $val_is_bool = true;
109 } elseif (isset($aliases[''])) {
110 $o = $aliases[''];
113 $bool = false;
114 if (!isset($AC[$o]) || !is_bool($AC[$o])) {
115 if (strpos($o, '=') === false) {
116 continue;
118 list($o, $v) = explode('=', $o);
119 } elseif (!$val_is_bool) {
120 $v = true;
121 $bool = $o;
123 $val_is_bool = false;
125 if ($o === false) continue;
126 htmlpurifier_args($AC, $aliases, $o, $v);
127 $o = false;
129 } else {
130 foreach ($_GET as $o => $v) {
131 if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
132 $v = stripslashes($v);
134 htmlpurifier_args($AC, $aliases, $o, $v);
140 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
141 * @param $AC Arguments array to write to
142 * @param $aliases Aliases for options
143 * @param $o Argument name
144 * @param $v Argument value
146 function htmlpurifier_args(&$AC, $aliases, $o, $v) {
147 if (isset($aliases[$o])) $o = $aliases[$o];
148 if (!isset($AC[$o])) return;
149 if (is_string($AC[$o])) $AC[$o] = $v;
150 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ? true :(bool) $v;
151 if (is_int($AC[$o])) $AC[$o] = (int) $v;
155 * Adds a test-class; we use file extension to determine which class to use.
157 function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
158 switch (strrchr($test_file, ".")) {
159 case '.phpt':
160 return $test->add(new PHPT_Controller_SimpleTest($test_file));
161 case '.php':
162 require_once $test_file;
163 return $test->add(path2class($test_file));
164 case '.vtest':
165 return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
166 case '.htmlt':
167 return $test->add(new HTMLPurifier_HTMLT($test_file));
168 default:
169 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
174 * Debugging function that prints tokens in a user-friendly manner.
176 function printTokens($tokens, $index = null) {
177 $string = '<pre>';
178 $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
179 foreach ($tokens as $i => $token) {
180 if ($index === $i) $string .= '[<strong>';
181 $string .= "<sup>$i</sup>";
182 $string .= $generator->escape($generator->generateFromToken($token));
183 if ($index === $i) $string .= '</strong>]';
185 $string .= '</pre>';
186 echo $string;
190 * Convenient "insta-fail" test-case to add if any outside things fail
192 class FailedTest extends UnitTestCase {
193 protected $msg, $details;
194 public function __construct($msg, $details = null) {
195 $this->msg = $msg;
196 $this->details = $details;
198 public function test() {
199 $this->fail($this->msg);
200 if ($this->details) $this->reporter->paintFormattedMessage($this->details);
205 * Flushes all caches, and fatally errors out if there's a problem.
207 function htmlpurifier_flush($php, $reporter) {
208 exec($php . ' ../maintenance/flush.php ' . $php . ' 2>&1', $out, $status);
209 if ($status) {
210 $test = new FailedTest(
211 'maintenance/flush.php returned non-zero exit status',
212 wordwrap(implode("\n", $out), 80)
214 $test->run($reporter);
215 exit(1);
220 * Dumps error queue, useful if there has been a fatal error.
222 function htmlpurifier_dump_error_queue() {
223 $context = SimpleTest::getContext();
224 $queue = $context->get('SimpleErrorQueue');
225 while (($error = $queue->extract()) !== false) {
226 var_dump($error);
229 register_shutdown_function('htmlpurifier_dump_error_queue');
231 // vim: et sw=4 sts=4