You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.9KB

  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. define('HTMLPurifierTest', 1);
  33. define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
  34. chdir(dirname(__FILE__));
  35. $php = 'php'; // for safety
  36. ini_set('memory_limit', '64M');
  37. require 'common.php';
  38. $AC = array(); // parameters
  39. $AC['flush'] = false;
  40. $AC['standalone'] = false;
  41. $AC['file'] = '';
  42. $AC['xml'] = false;
  43. $AC['dry'] = false;
  44. $AC['php'] = $php;
  45. $AC['help'] = false;
  46. $AC['verbose'] = false;
  47. $AC['txt'] = false;
  48. $AC['type'] = '';
  49. $AC['disable-phpt'] = false;
  50. $AC['only-phpt'] = false; // alias for --type=phpt
  51. $aliases = array(
  52. 'f' => 'file',
  53. 'h' => 'help',
  54. 'v' => 'verbose',
  55. );
  56. // It's important that this does not call the autoloader. Not a problem
  57. // with a function, but could be if we put this in a class.
  58. htmlpurifier_parse_args($AC, $aliases);
  59. if ($AC['help']) {
  60. ?>HTML Purifier test suite
  61. Allowed options:
  62. --flush
  63. --standalone
  64. --file (-f) HTMLPurifier/NameOfTest.php
  65. --xml
  66. --txt
  67. --dry
  68. --php /path/to/php
  69. --type ( htmlpurifier | configdoc | fstools | htmlt | vtest | phpt )
  70. --disable-phpt
  71. --verbose (-v)
  72. <?php
  73. exit;
  74. }
  75. // Disable PHPT tests if they're not enabled
  76. if (!$GLOBALS['HTMLPurifierTest']['PHPT']) {
  77. $AC['disable-phpt'] = true;
  78. } elseif (!$AC['type'] && $AC['only-phpt']) {
  79. // backwards-compat
  80. $AC['type'] = 'phpt';
  81. }
  82. if (!SimpleReporter::inCli()) {
  83. // Undo any dangerous parameters
  84. $AC['php'] = $php;
  85. }
  86. // initialize and load HTML Purifier
  87. // use ?standalone to load the alterative standalone stub
  88. if ($AC['standalone']) {
  89. require '../library/HTMLPurifier.standalone.php';
  90. } else {
  91. require '../library/HTMLPurifier.path.php';
  92. require 'HTMLPurifier.includes.php';
  93. }
  94. require '../library/HTMLPurifier.autoload.php';
  95. require 'HTMLPurifier/Harness.php';
  96. // immediately load external libraries, so we can bail out early if
  97. // they're bad
  98. if ($GLOBALS['HTMLPurifierTest']['PEAR']) {
  99. if ($GLOBALS['HTMLPurifierTest']['Net_IDNA2']) {
  100. require_once 'Net/IDNA2.php';
  101. }
  102. }
  103. // Shell-script code is executed
  104. if ($AC['xml']) {
  105. if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
  106. $reporter = new XmlReporter();
  107. } elseif (SimpleReporter::inCli() || $AC['txt']) {
  108. if (!SimpleReporter::inCli()) header('Content-Type: text/plain;charset=UTF-8');
  109. $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
  110. } else {
  111. $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
  112. }
  113. if ($AC['flush']) {
  114. htmlpurifier_flush($AC['php'], $reporter);
  115. }
  116. // Now, userland code begins to be executed
  117. // setup special DefinitionCacheFactory decorator
  118. $factory = HTMLPurifier_DefinitionCacheFactory::instance();
  119. $factory->addDecorator('Memory'); // since we deal with a lot of config objects
  120. if (!$AC['disable-phpt']) {
  121. $phpt = PHPT_Registry::getInstance();
  122. $phpt->php = $AC['php'];
  123. }
  124. // load tests
  125. require 'test_files.php';
  126. $FS = new FSTools();
  127. // handle test dirs
  128. foreach ($test_dirs as $dir) {
  129. $raw_files = $FS->globr($dir, '*Test.php');
  130. foreach ($raw_files as $file) {
  131. $file = str_replace('\\', '/', $file);
  132. if (isset($test_dirs_exclude[$file])) continue;
  133. $test_files[] = $file;
  134. }
  135. }
  136. // handle vtest dirs
  137. foreach ($vtest_dirs as $dir) {
  138. $raw_files = $FS->globr($dir, '*.vtest');
  139. foreach ($raw_files as $file) {
  140. $test_files[] = str_replace('\\', '/', $file);
  141. }
  142. }
  143. // handle phpt files
  144. foreach ($phpt_dirs as $dir) {
  145. $phpt_files = $FS->globr($dir, '*.phpt');
  146. foreach ($phpt_files as $file) {
  147. $test_files[] = str_replace('\\', '/', $file);
  148. }
  149. }
  150. // handle htmlt dirs
  151. foreach ($htmlt_dirs as $dir) {
  152. $htmlt_files = $FS->globr($dir, '*.htmlt');
  153. foreach ($htmlt_files as $file) {
  154. $test_files[] = str_replace('\\', '/', $file);
  155. }
  156. }
  157. array_unique($test_files);
  158. sort($test_files); // for the SELECT
  159. $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
  160. $test_file_lookup = array_flip($test_files);
  161. // determine test file
  162. if ($AC['file']) {
  163. if (!isset($test_file_lookup[$AC['file']])) {
  164. echo "Invalid file passed\n";
  165. exit;
  166. }
  167. }
  168. if ($AC['file']) {
  169. $test = new TestSuite($AC['file']);
  170. htmlpurifier_add_test($test, $AC['file']);
  171. } else {
  172. $standalone = '';
  173. if ($AC['standalone']) $standalone = ' (standalone)';
  174. $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
  175. foreach ($test_files as $test_file) {
  176. htmlpurifier_add_test($test, $test_file);
  177. }
  178. }
  179. if ($AC['dry']) $reporter->makeDry();
  180. $test->run($reporter);
  181. // vim: et sw=4 sts=4