index.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /** @file
  3. * Unit tester
  4. *
  5. * The heart and soul of HTML Purifier's correctness; anything and everything
  6. * is tested here! Arguments are specified like --arg=opt, allowed arguments
  7. * are:
  8. * - flush, whether or not to flush definition caches before running
  9. * - standalone, whether or not to test the standalone version
  10. * - file (f), a single file to test
  11. * - xml, whether or not to output XML
  12. * - dry, whether or not to do a dry run
  13. * - type, the type of tests to run, can be 'htmlpurifier', 'configdoc',
  14. * 'fstools', 'htmlt', 'vtest' or 'phpt'
  15. *
  16. * If you're interested in running the test-cases, mosey over to
  17. * ../test-settings.sample.php, copy the file to test-settings.php and follow
  18. * the enclosed instructions.
  19. *
  20. * @warning File setup does not exactly match with autoloader; make sure that
  21. * non-test classes (i.e. classes that are not retrieved using
  22. * $test_files) do not have underscores in their names.
  23. */
  24. // HTML Purifier runs error free on E_STRICT, so if code reports
  25. // errors, we want to know about it.
  26. error_reporting(E_ALL | E_STRICT);
  27. // Because we always want to know about errors, and because SimpleTest
  28. // will notify us about them, logging the errors to stderr is
  29. // counterproductive and in fact the wrong thing when a test case
  30. // exercises an error condition to detect for it.
  31. ini_set('log_errors', false);
  32. // But make it easier for us to debug if there is some misconfiguration
  33. // in the initial setup of the tests.
  34. ini_set('display_errors', true);
  35. define('HTMLPurifierTest', 1);
  36. define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
  37. chdir(dirname(__FILE__));
  38. $php = 'php'; // for safety
  39. ini_set('memory_limit', '64M');
  40. require 'common.php';
  41. $AC = array(); // parameters
  42. $AC['flush'] = false;
  43. $AC['standalone'] = false;
  44. $AC['file'] = '';
  45. $AC['xml'] = false;
  46. $AC['dry'] = false;
  47. $AC['php'] = $php;
  48. $AC['help'] = false;
  49. $AC['verbose'] = false;
  50. $AC['txt'] = false;
  51. $AC['type'] = '';
  52. $AC['disable-phpt'] = false;
  53. $AC['only-phpt'] = false; // alias for --type=phpt
  54. $aliases = array(
  55. 'f' => 'file',
  56. 'h' => 'help',
  57. 'v' => 'verbose',
  58. );
  59. // It's important that this does not call the autoloader. Not a problem
  60. // with a function, but could be if we put this in a class.
  61. htmlpurifier_parse_args($AC, $aliases);
  62. if ($AC['help']) {
  63. ?>HTML Purifier test suite
  64. Allowed options:
  65. --flush
  66. --standalone
  67. --file (-f) HTMLPurifier/NameOfTest.php
  68. --xml
  69. --txt
  70. --dry
  71. --php /path/to/php
  72. --type ( htmlpurifier | configdoc | fstools | htmlt | vtest | phpt )
  73. --disable-phpt
  74. --verbose (-v)
  75. <?php
  76. exit;
  77. }
  78. // Disable PHPT tests if they're not enabled
  79. if (!$GLOBALS['HTMLPurifierTest']['PHPT']) {
  80. $AC['disable-phpt'] = true;
  81. } elseif (!$AC['type'] && $AC['only-phpt']) {
  82. // backwards-compat
  83. $AC['type'] = 'phpt';
  84. }
  85. if (!SimpleReporter::inCli()) {
  86. // Undo any dangerous parameters
  87. $AC['php'] = $php;
  88. }
  89. // initialize and load HTML Purifier
  90. // use ?standalone to load the alterative standalone stub
  91. if ($AC['standalone']) {
  92. require '../library/HTMLPurifier.standalone.php';
  93. } else {
  94. require '../library/HTMLPurifier.path.php';
  95. require 'HTMLPurifier.includes.php';
  96. }
  97. require '../library/HTMLPurifier.autoload.php';
  98. require 'HTMLPurifier/Harness.php';
  99. // immediately load external libraries, so we can bail out early if
  100. // they're bad
  101. if ($GLOBALS['HTMLPurifierTest']['PEAR']) {
  102. if ($GLOBALS['HTMLPurifierTest']['Net_IDNA2']) {
  103. require_once 'Net/IDNA2.php';
  104. }
  105. }
  106. // Shell-script code is executed
  107. if ($AC['xml']) {
  108. if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
  109. $reporter = new XmlReporter();
  110. } elseif (SimpleReporter::inCli() || $AC['txt']) {
  111. if (!SimpleReporter::inCli()) header('Content-Type: text/plain;charset=UTF-8');
  112. $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
  113. } else {
  114. $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
  115. }
  116. if ($AC['flush']) {
  117. htmlpurifier_flush($AC['php'], $reporter);
  118. }
  119. // Now, userland code begins to be executed
  120. // setup special DefinitionCacheFactory decorator
  121. $factory = HTMLPurifier_DefinitionCacheFactory::instance();
  122. $factory->addDecorator('Memory'); // since we deal with a lot of config objects
  123. if (!$AC['disable-phpt']) {
  124. $phpt = PHPT_Registry::getInstance();
  125. $phpt->php = $AC['php'];
  126. }
  127. // load tests
  128. require 'test_files.php';
  129. $FS = new FSTools();
  130. // handle test dirs
  131. foreach ($test_dirs as $dir) {
  132. $raw_files = $FS->globr($dir, '*Test.php');
  133. foreach ($raw_files as $file) {
  134. $file = str_replace('\\', '/', $file);
  135. if (isset($test_dirs_exclude[$file])) continue;
  136. $test_files[] = $file;
  137. }
  138. }
  139. // handle vtest dirs
  140. foreach ($vtest_dirs as $dir) {
  141. $raw_files = $FS->globr($dir, '*.vtest');
  142. foreach ($raw_files as $file) {
  143. $test_files[] = str_replace('\\', '/', $file);
  144. }
  145. }
  146. // handle phpt files
  147. foreach ($phpt_dirs as $dir) {
  148. $phpt_files = $FS->globr($dir, '*.phpt');
  149. foreach ($phpt_files as $file) {
  150. $test_files[] = str_replace('\\', '/', $file);
  151. }
  152. }
  153. // handle htmlt dirs
  154. foreach ($htmlt_dirs as $dir) {
  155. $htmlt_files = $FS->globr($dir, '*.htmlt');
  156. foreach ($htmlt_files as $file) {
  157. $test_files[] = str_replace('\\', '/', $file);
  158. }
  159. }
  160. array_unique($test_files);
  161. sort($test_files); // for the SELECT
  162. $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
  163. $test_file_lookup = array_flip($test_files);
  164. // determine test file
  165. if ($AC['file']) {
  166. if (!isset($test_file_lookup[$AC['file']])) {
  167. echo "Invalid file passed\n";
  168. exit;
  169. }
  170. }
  171. if ($AC['file']) {
  172. $test = new TestSuite($AC['file']);
  173. htmlpurifier_add_test($test, $AC['file']);
  174. } else {
  175. $standalone = '';
  176. if ($AC['standalone']) $standalone = ' (standalone)';
  177. $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
  178. foreach ($test_files as $test_file) {
  179. htmlpurifier_add_test($test, $test_file);
  180. }
  181. }
  182. if ($AC['dry']) $reporter->makeDry();
  183. $result = $test->run($reporter);
  184. if ($result) {
  185. exit(0); // Success!
  186. } else {
  187. exit(1); // Abject failure.
  188. }
  189. // vim: et sw=4 sts=4