- Make suite flush remote tests
[htmlpurifier.git] / tests / common.php
blobdabc2d6dad1f504834b557486d3f61d3c41583d9
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']['PH5P'] = class_exists('DOMDocument');
27 // default library settings
28 $simpletest_location = 'simpletest/'; // reasonable guess
29 $csstidy_location = false;
30 $versions_to_test = array();
31 $php = 'php';
32 $phpv = 'phpv';
34 // load configuration
35 if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
36 if (file_exists('../test-settings.php')) include '../test-settings.php';
38 // load SimpleTest
39 require $simpletest_location . 'unit_tester.php';
40 require $simpletest_location . 'reporter.php';
41 require $simpletest_location . 'mock_objects.php';
42 require $simpletest_location . 'xml.php';
43 require $simpletest_location . 'remote.php';
45 // load CSS Tidy
46 if ($csstidy_location !== false) {
47 require $csstidy_location . 'class.csstidy.php';
50 // load PEAR to include path
51 if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
52 // if PEAR is true, there's no need to add it to the path
53 set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR .
54 get_include_path());
57 // after external libraries are loaded, turn on compile time errors
58 error_reporting(E_ALL | E_STRICT);
60 // initialize extra HTML Purifier libraries
61 require '../extras/HTMLPurifierExtras.auto.php';
63 // load SimpleTest addon functions
64 require 'generate_mock_once.func.php';
65 require 'path2class.func.php';
66 require 'tally_errors.func.php'; // compat
68 /**
69 * Arguments parser, is cli and web agnostic.
70 * @warning
71 * There are some quirks about the argument format:
72 * - Short boolean flags cannot be chained together
73 * - Only strings, integers and booleans are accepted
74 * @param $AC
75 * Arguments array to populate. This takes a simple format of 'argument'
76 * => default value. Depending on the type of the default value,
77 * arguments will be typecast accordingly. For example, if
78 * 'flag' => false is passed, all arguments for that will be cast to
79 * boolean. Do *not* pass null, as it will not be recognized.
80 * @param $aliases
83 function htmlpurifier_parse_args(&$AC, $aliases) {
84 if (empty($_GET)) {
85 array_shift($_SERVER['argv']);
86 $o = false;
87 $bool = false;
88 $val_is_bool = false;
89 foreach ($_SERVER['argv'] as $opt) {
90 if ($o !== false) {
91 $v = $opt;
92 } else {
93 if ($opt === '') continue;
94 if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
95 $o = substr($opt, 2);
96 } elseif ($opt[0] == '-') {
97 $o = substr($opt, 1);
98 } else {
99 $lopt = strtolower($opt);
100 if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
101 $o = $bool;
102 $v = false;
103 $val_is_bool = true;
104 } elseif (isset($aliases[''])) {
105 $o = $aliases[''];
108 $bool = false;
109 if (!isset($AC[$o]) || !is_bool($AC[$o])) {
110 if (strpos($o, '=') === false) {
111 continue;
113 list($o, $v) = explode('=', $o);
114 } elseif (!$val_is_bool) {
115 $v = true;
116 $bool = $o;
118 $val_is_bool = false;
120 if ($o === false) continue;
121 htmlpurifier_args($AC, $aliases, $o, $v);
122 $o = false;
124 } else {
125 foreach ($_GET as $o => $v) {
126 if (get_magic_quotes_gpc()) $v = stripslashes($v);
127 htmlpurifier_args($AC, $aliases, $o, $v);
133 * Actually performs assignment to $AC, see htmlpurifier_parse_args()
134 * @param $AC Arguments array to write to
135 * @param $aliases Aliases for options
136 * @param $o Argument name
137 * @param $v Argument value
139 function htmlpurifier_args(&$AC, $aliases, $o, $v) {
140 if (isset($aliases[$o])) $o = $aliases[$o];
141 if (!isset($AC[$o])) return;
142 if (is_string($AC[$o])) $AC[$o] = $v;
143 if (is_bool($AC[$o])) $AC[$o] = ($v === '') ? true :(bool) $v;
144 if (is_int($AC[$o])) $AC[$o] = (int) $v;
148 * Adds a test-class; depending on the file's extension this may involve
149 * a regular UnitTestCase or a special PHPT test
151 function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
152 switch (strrchr($test_file, ".")) {
153 case '.phpt':
154 return $test->addTestCase(new PHPT_Controller_SimpleTest($test_file));
155 case '.php':
156 require_once $test_file;
157 return $test->addTestClass(path2class($test_file));
158 default:
159 trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);