PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/ezyang/htmlpurifier/tests/common.php

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