Fix autoparagraph bug with non-inline elements.
[htmlpurifier.git] / tests / common.php
blob01d6666cabaf384e1c17adff1daea79696a08ba6
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 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 .
58 get_include_path());
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';
71 /**
72 * Arguments parser, is cli and web agnostic.
73 * @warning
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
77 * @param $AC
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.
83 * @param $aliases
86 function htmlpurifier_parse_args(&$AC, $aliases) {
87 if (empty($_GET)) {
88 array_shift($_SERVER['argv']);
89 $o = false;
90 $bool = false;
91 $val_is_bool = false;
92 foreach ($_SERVER['argv'] as $opt) {
93 if ($o !== false) {
94 $v = $opt;
95 } else {
96 if ($opt === '') continue;
97 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
98 $o = substr($opt, 2);
99 } elseif ($opt[0] == '-') {
100 $o = substr($opt, 1);
101 } else {
102 $lopt = strtolower($opt);
103 if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
104 $o = $bool;
105 $v = false;
106 $val_is_bool = true;
107 } elseif (isset($aliases[''])) {
108 $o = $aliases[''];
111 $bool = false;
112 if (!isset($AC[$o]) || !is_bool($AC[$o])) {
113 if (strpos($o, '=') === false) {
114 continue;
116 list($o, $v) = explode('=', $o);
117 } elseif (!$val_is_bool) {
118 $v = true;
119 $bool = $o;
121 $val_is_bool = false;
123 if ($o === false) continue;
124 htmlpurifier_args($AC, $aliases, $o, $v);
125 $o = false;
127 } else {
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, ".")) {
157 case '.phpt':
158 return $test->add(new PHPT_Controller_SimpleTest($test_file));
159 case '.php':
160 require_once $test_file;
161 return $test->add(path2class($test_file));
162 case '.vtest':
163 return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
164 case '.htmlt':
165 return $test->add(new HTMLPurifier_HTMLT($test_file));
166 default:
167 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
172 * Debugging function that prints tokens in a user-friendly manner.
174 function printTokens($tokens, $index = null) {
175 $string = '<pre>';
176 $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
177 foreach ($tokens as $i => $token) {
178 if ($index === $i) $string .= '[<strong>';
179 $string .= "<sup>$i</sup>";
180 $string .= $generator->escape($generator->generateFromToken($token));
181 if ($index === $i) $string .= '</strong>]';
183 $string .= '</pre>';
184 echo $string;
188 * Convenient "insta-fail" test-case to add if any outside things fail
190 class FailedTest extends UnitTestCase {
191 protected $msg, $details;
192 public function __construct($msg, $details = null) {
193 $this->msg = $msg;
194 $this->details = $details;
196 public function test() {
197 $this->fail($this->msg);
198 if ($this->details) $this->reporter->paintFormattedMessage($this->details);
203 * Flushes all caches, and fatally errors out if there's a problem.
205 function htmlpurifier_flush($php, $reporter) {
206 exec($php . ' ../maintenance/flush.php ' . $php . ' 2>&1', $out, $status);
207 if ($status) {
208 $test = new FailedTest(
209 'maintenance/flush.php returned non-zero exit status',
210 wordwrap(implode("\n", $out), 80)
212 $test->run($reporter);
213 exit(1);
218 * Dumps error queue, useful if there has been a fatal error.
220 function htmlpurifier_dump_error_queue() {
221 $context = SimpleTest::getContext();
222 $queue = $context->get('SimpleErrorQueue');
223 while (($error = $queue->extract()) !== false) {
224 var_dump($error);
227 register_shutdown_function('htmlpurifier_dump_error_queue');
229 // vim: et sw=4 sts=4