common.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. if (!defined('HTMLPurifierTest')) {
  3. echo "Invalid entry point\n";
  4. exit;
  5. }
  6. // setup our own autoload, checking for HTMLPurifier library if spl_autoload_register
  7. // is not allowed
  8. function __autoload($class) {
  9. if (!function_exists('spl_autoload_register')) {
  10. if (HTMLPurifier_Bootstrap::autoload($class)) return true;
  11. if (HTMLPurifierExtras::autoload($class)) return true;
  12. }
  13. require str_replace('_', '/', $class) . '.php';
  14. return true;
  15. }
  16. if (function_exists('spl_autoload_register')) {
  17. spl_autoload_register('__autoload');
  18. }
  19. // default settings (protect against register_globals)
  20. $GLOBALS['HTMLPurifierTest'] = array();
  21. $GLOBALS['HTMLPurifierTest']['PEAR'] = false; // do PEAR tests
  22. $GLOBALS['HTMLPurifierTest']['PHPT'] = true; // do PHPT tests
  23. $GLOBALS['HTMLPurifierTest']['PH5P'] = class_exists('DOMDocument');
  24. // default library settings
  25. $simpletest_location = 'simpletest/'; // reasonable guess
  26. $csstidy_location = false;
  27. $versions_to_test = array();
  28. $php = 'php';
  29. $phpv = 'phpv';
  30. // load configuration
  31. if (file_exists('../conf/test-settings.php')) include '../conf/test-settings.php';
  32. elseif (file_exists('../test-settings.php')) include '../test-settings.php';
  33. else {
  34. throw new Exception('Please create a test-settings.php file by copying test-settings.sample.php and configuring accordingly');
  35. }
  36. // load SimpleTest
  37. require_once $simpletest_location . 'unit_tester.php';
  38. require_once $simpletest_location . 'reporter.php';
  39. require_once $simpletest_location . 'mock_objects.php';
  40. require_once $simpletest_location . 'xml.php';
  41. require_once $simpletest_location . 'remote.php';
  42. // load CSS Tidy
  43. if ($csstidy_location !== false) {
  44. $old = error_reporting(E_ALL);
  45. require $csstidy_location . 'class.csstidy.php';
  46. error_reporting($old);
  47. }
  48. // load PEAR to include path
  49. if ( is_string($GLOBALS['HTMLPurifierTest']['PEAR']) ) {
  50. // if PEAR is true, there's no need to add it to the path
  51. set_include_path($GLOBALS['HTMLPurifierTest']['PEAR'] . PATH_SEPARATOR .
  52. get_include_path());
  53. }
  54. // after external libraries are loaded, turn on compile time errors
  55. error_reporting(E_ALL | E_STRICT);
  56. // initialize extra HTML Purifier libraries
  57. require '../extras/HTMLPurifierExtras.auto.php';
  58. // load SimpleTest addon functions
  59. require 'generate_mock_once.func.php';
  60. require 'path2class.func.php';
  61. /**
  62. * Arguments parser, is cli and web agnostic.
  63. * @warning
  64. * There are some quirks about the argument format:
  65. * - Short boolean flags cannot be chained together
  66. * - Only strings, integers and booleans are accepted
  67. * @param $AC
  68. * Arguments array to populate. This takes a simple format of 'argument'
  69. * => default value. Depending on the type of the default value,
  70. * arguments will be typecast accordingly. For example, if
  71. * 'flag' => false is passed, all arguments for that will be cast to
  72. * boolean. Do *not* pass null, as it will not be recognized.
  73. * @param $aliases
  74. *
  75. */
  76. function htmlpurifier_parse_args(&$AC, $aliases) {
  77. if (empty($_GET) && !empty($_SERVER['argv'])) {
  78. array_shift($_SERVER['argv']);
  79. $o = false;
  80. $bool = false;
  81. $val_is_bool = false;
  82. foreach ($_SERVER['argv'] as $opt) {
  83. if ($o !== false) {
  84. $v = $opt;
  85. } else {
  86. if ($opt === '') continue;
  87. if (strlen($opt) > 2 && strncmp($opt, '--', 2) === 0) {
  88. $o = substr($opt, 2);
  89. } elseif ($opt[0] == '-') {
  90. $o = substr($opt, 1);
  91. } else {
  92. $lopt = strtolower($opt);
  93. if ($bool !== false && ($opt === '0' || $lopt === 'off' || $lopt === 'no')) {
  94. $o = $bool;
  95. $v = false;
  96. $val_is_bool = true;
  97. } elseif (isset($aliases[''])) {
  98. $o = $aliases[''];
  99. }
  100. }
  101. $bool = false;
  102. if (!isset($AC[$o]) || !is_bool($AC[$o])) {
  103. if (strpos($o, '=') === false) {
  104. continue;
  105. }
  106. list($o, $v) = explode('=', $o);
  107. } elseif (!$val_is_bool) {
  108. $v = true;
  109. $bool = $o;
  110. }
  111. $val_is_bool = false;
  112. }
  113. if ($o === false) continue;
  114. htmlpurifier_args($AC, $aliases, $o, $v);
  115. $o = false;
  116. }
  117. } else {
  118. foreach ($_GET as $o => $v) {
  119. if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
  120. $v = stripslashes($v);
  121. }
  122. htmlpurifier_args($AC, $aliases, $o, $v);
  123. }
  124. }
  125. }
  126. /**
  127. * Actually performs assignment to $AC, see htmlpurifier_parse_args()
  128. * @param $AC Arguments array to write to
  129. * @param $aliases Aliases for options
  130. * @param $o Argument name
  131. * @param $v Argument value
  132. */
  133. function htmlpurifier_args(&$AC, $aliases, $o, $v) {
  134. if (isset($aliases[$o])) $o = $aliases[$o];
  135. if (!isset($AC[$o])) return;
  136. if (is_string($AC[$o])) $AC[$o] = $v;
  137. if (is_bool($AC[$o])) $AC[$o] = ($v === '') ? true :(bool) $v;
  138. if (is_int($AC[$o])) $AC[$o] = (int) $v;
  139. }
  140. /**
  141. * Adds a test-class; we use file extension to determine which class to use.
  142. */
  143. function htmlpurifier_add_test($test, $test_file, $only_phpt = false) {
  144. switch (strrchr($test_file, ".")) {
  145. case '.phpt':
  146. return $test->add(new PHPT_Controller_SimpleTest($test_file));
  147. case '.php':
  148. require_once $test_file;
  149. return $test->add(path2class($test_file));
  150. case '.vtest':
  151. return $test->add(new HTMLPurifier_ConfigSchema_ValidatorTestCase($test_file));
  152. case '.htmlt':
  153. return $test->add(new HTMLPurifier_HTMLT($test_file));
  154. default:
  155. trigger_error("$test_file is an invalid file for testing", E_USER_ERROR);
  156. }
  157. }
  158. /**
  159. * Debugging function that prints tokens in a user-friendly manner.
  160. */
  161. function printTokens($tokens, $index = null) {
  162. $string = '<pre>';
  163. $generator = new HTMLPurifier_Generator(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context);
  164. foreach ($tokens as $i => $token) {
  165. if ($index === $i) $string .= '[<strong>';
  166. $string .= "<sup>$i</sup>";
  167. $string .= $generator->escape($generator->generateFromToken($token));
  168. if ($index === $i) $string .= '</strong>]';
  169. }
  170. $string .= '</pre>';
  171. echo $string;
  172. }
  173. /**
  174. * Convenient "insta-fail" test-case to add if any outside things fail
  175. */
  176. class FailedTest extends UnitTestCase {
  177. protected $msg, $details;
  178. public function __construct($msg, $details = null) {
  179. $this->msg = $msg;
  180. $this->details = $details;
  181. }
  182. public function test() {
  183. $this->fail($this->msg);
  184. if ($this->details) $this->reporter->paintFormattedMessage($this->details);
  185. }
  186. }
  187. /**
  188. * Flushes all caches, and fatally errors out if there's a problem.
  189. */
  190. function htmlpurifier_flush($php, $reporter) {
  191. exec($php . ' ../maintenance/flush.php ' . $php . ' 2>&1', $out, $status);
  192. if ($status) {
  193. $test = new FailedTest(
  194. 'maintenance/flush.php returned non-zero exit status',
  195. wordwrap(implode("\n", $out), 80)
  196. );
  197. $test->run($reporter);
  198. exit(1);
  199. }
  200. }
  201. /**
  202. * Dumps error queue, useful if there has been a fatal error.
  203. */
  204. function htmlpurifier_dump_error_queue() {
  205. $context = SimpleTest::getContext();
  206. $queue = $context->get('SimpleErrorQueue');
  207. while (($error = $queue->extract()) !== false) {
  208. var_dump($error);
  209. }
  210. }
  211. register_shutdown_function('htmlpurifier_dump_error_queue');
  212. // vim: et sw=4 sts=4