common.php 7.9 KB

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