PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/htmlpurifier/tests/index.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 243 lines | 163 code | 37 blank | 43 comment | 16 complexity | 487d9102efb55497fce2a90b973ef57e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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('f' => 'file', 'h' => 'help', 'v' => 'verbose');
  52. // It's important that this does not call the autoloader. Not a problem
  53. // with a function, but could be if we put this in a class.
  54. htmlpurifier_parse_args($AC, $aliases);
  55. if ($AC['help'])
  56. {
  57. ?>HTML Purifier test suite
  58. Allowed options:
  59. --flush
  60. --standalone
  61. --file (-f) HTMLPurifier/NameOfTest.php
  62. --xml
  63. --txt
  64. --dry
  65. --php /path/to/php
  66. --type ( htmlpurifier | configdoc | fstools | htmlt | vtest | phpt )
  67. --disable-phpt
  68. --verbose (-v)
  69. <?php
  70. exit();
  71. }
  72. // Disable PHPT tests if they're not enabled
  73. if (! $GLOBALS['HTMLPurifierTest']['PHPT'])
  74. {
  75. $AC['disable-phpt'] = true;
  76. }
  77. elseif (! $AC['type'] && $AC['only-phpt'])
  78. {
  79. // backwards-compat
  80. $AC['type'] = 'phpt';
  81. }
  82. if (! SimpleReporter :: inCli())
  83. {
  84. // Undo any dangerous parameters
  85. $AC['php'] = $php;
  86. }
  87. // initialize and load HTML Purifier
  88. // use ?standalone to load the alterative standalone stub
  89. if ($AC['standalone'])
  90. {
  91. require '../library/HTMLPurifier.standalone.php';
  92. }
  93. else
  94. {
  95. require '../library/HTMLPurifier.path.php';
  96. require 'HTMLPurifier.includes.php';
  97. }
  98. require '../library/HTMLPurifier.autoload.php';
  99. require 'HTMLPurifier/Harness.php';
  100. // Shell-script code is executed
  101. if ($AC['xml'])
  102. {
  103. if (! SimpleReporter :: inCli())
  104. header('Content-Type: text/xml;charset=UTF-8');
  105. $reporter = new XmlReporter();
  106. }
  107. elseif (SimpleReporter :: inCli() || $AC['txt'])
  108. {
  109. if (! SimpleReporter :: inCli())
  110. header('Content-Type: text/plain;charset=UTF-8');
  111. $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
  112. }
  113. else
  114. {
  115. $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
  116. }
  117. if ($AC['flush'])
  118. {
  119. htmlpurifier_flush($AC['php'], $reporter);
  120. }
  121. // Now, userland code begins to be executed
  122. // setup special DefinitionCacheFactory decorator
  123. $factory = HTMLPurifier_DefinitionCacheFactory :: instance();
  124. $factory->addDecorator('Memory'); // since we deal with a lot of config objects
  125. if (! $AC['disable-phpt'])
  126. {
  127. $phpt = PHPT_Registry :: getInstance();
  128. $phpt->php = $AC['php'];
  129. }
  130. // load tests
  131. require 'test_files.php';
  132. $FS = new FSTools();
  133. // handle test dirs
  134. foreach ($test_dirs as $dir)
  135. {
  136. $raw_files = $FS->globr($dir, '*Test.php');
  137. foreach ($raw_files as $file)
  138. {
  139. $file = str_replace('\\', '/', $file);
  140. if (isset($test_dirs_exclude[$file]))
  141. continue;
  142. $test_files[] = $file;
  143. }
  144. }
  145. // handle vtest dirs
  146. foreach ($vtest_dirs as $dir)
  147. {
  148. $raw_files = $FS->globr($dir, '*.vtest');
  149. foreach ($raw_files as $file)
  150. {
  151. $test_files[] = str_replace('\\', '/', $file);
  152. }
  153. }
  154. // handle phpt files
  155. foreach ($phpt_dirs as $dir)
  156. {
  157. $phpt_files = $FS->globr($dir, '*.phpt');
  158. foreach ($phpt_files as $file)
  159. {
  160. $test_files[] = str_replace('\\', '/', $file);
  161. }
  162. }
  163. // handle htmlt dirs
  164. foreach ($htmlt_dirs as $dir)
  165. {
  166. $htmlt_files = $FS->globr($dir, '*.htmlt');
  167. foreach ($htmlt_files as $file)
  168. {
  169. $test_files[] = str_replace('\\', '/', $file);
  170. }
  171. }
  172. array_unique($test_files);
  173. sort($test_files); // for the SELECT
  174. $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
  175. $test_file_lookup = array_flip($test_files);
  176. // determine test file
  177. if ($AC['file'])
  178. {
  179. if (! isset($test_file_lookup[$AC['file']]))
  180. {
  181. echo "Invalid file passed\n";
  182. exit();
  183. }
  184. }
  185. if ($AC['file'])
  186. {
  187. $test = new TestSuite($AC['file']);
  188. htmlpurifier_add_test($test, $AC['file']);
  189. }
  190. else
  191. {
  192. $standalone = '';
  193. if ($AC['standalone'])
  194. $standalone = ' (standalone)';
  195. $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
  196. foreach ($test_files as $test_file)
  197. {
  198. htmlpurifier_add_test($test, $test_file);
  199. }
  200. }
  201. if ($AC['dry'])
  202. $reporter->makeDry();
  203. $test->run($reporter);
  204. // vim: et sw=4 sts=4