[3.1.0] Get testing working again for all versions
[htmlpurifier.git] / tests / index.php
blob4886732dc0a3c9289057649ce52aec801142c287
1 <?php
3 /** @file
4 * Unit tester
5 *
6 * The heart and soul of HTML Purifier's correctness; anything and everything
7 * is tested here! Arguments are specified like --arg=opt, allowed arguments
8 * are:
9 * - flush, whether or not to flush definition caches before running
10 * - standalone, whether or not to test the standalone version
11 * - file (f), a single file to test
12 * - xml, whether or not to output XML
13 * - dry, whether or not to do a dry run
15 * If you're interested in running the test-cases, mosey over to
16 * ../test-settings.sample.php, copy the file to test-settings.php and follow
17 * the enclosed instructions.
19 * @warning File setup does not exactly match with autoloader; make sure that
20 * non-test classes (i.e. classes that are not retrieved using
21 * $test_files) do not have underscores in their names.
24 define('HTMLPurifierTest', 1);
25 define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
26 chdir(dirname(__FILE__));
28 $php = 'php'; // for safety
30 require 'common.php';
32 $AC = array(); // parameters
33 $AC['flush'] = false;
34 $AC['standalone'] = false;
35 $AC['file'] = '';
36 $AC['xml'] = false;
37 $AC['dry'] = false;
38 $AC['php'] = $php;
40 // Convenience parameters for running quicker tests; ideally all tests
41 // should be performed.
42 $AC['disable-phpt'] = false;
43 $AC['only-phpt'] = false;
45 $aliases = array(
46 'f' => 'file',
49 // It's important that this does not call the autoloader. Not a problem
50 // with a function, but could be if we put this in a class.
51 htmlpurifier_parse_args($AC, $aliases);
53 // Disable PHPT tests if they're not enabled
54 if (!$GLOBALS['HTMLPurifierTest']['PHPT']) $AC['disable-phpt'] = true;
56 if (!SimpleReporter::inCli()) {
57 // Undo any dangerous parameters
58 $AC['php'] = $php;
61 if ($AC['disable-phpt'] && $AC['only-phpt']) {
62 echo "Cannot disable and allow only PHPT tests!\n";
63 exit(1);
66 // Shell-script code is executed
68 if ($AC['xml']) {
69 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
70 $reporter = new XmlReporter();
71 } elseif (SimpleReporter::inCli()) {
72 $reporter = new TextReporter();
73 } else {
74 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
77 if ($AC['flush']) {
78 htmlpurifier_flush($AC['php'], $reporter);
81 // initialize and load HTML Purifier
82 // use ?standalone to load the alterative standalone stub
83 if ($AC['standalone']) {
84 require '../library/HTMLPurifier.standalone.php';
85 } else {
86 require '../library/HTMLPurifier.path.php';
87 require 'HTMLPurifier.includes.php';
89 require '../library/HTMLPurifier.autoload.php';
90 require 'HTMLPurifier/Harness.php';
92 // Now, userland code begins to be executed
94 // setup special DefinitionCacheFactory decorator
95 $factory = HTMLPurifier_DefinitionCacheFactory::instance();
96 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
98 if (!$AC['disable-phpt']) {
99 $phpt = PHPT_Registry::getInstance();
100 $phpt->php = $AC['php'];
103 // load tests
105 $test_files = array();
106 $test_dirs = array();
107 $test_dirs_exclude = array();
108 $vtest_dirs = array();
109 $phpt_dirs = array();
111 require 'test_files.php'; // populates $test_files array
113 $FS = new FSTools();
115 // handle test dirs
116 foreach ($test_dirs as $dir) {
117 $raw_files = $FS->globr($dir, '*Test.php');
118 foreach ($raw_files as $file) {
119 $file = str_replace('\\', '/', $file);
120 if (isset($test_dirs_exclude[$file])) continue;
121 $test_files[] = $file;
125 // handle vtest dirs
126 foreach ($vtest_dirs as $dir) {
127 $raw_files = $FS->globr($dir, '*.vtest');
128 foreach ($raw_files as $file) {
129 $test_files[] = str_replace('\\', '/', $file);
133 // handle phpt files
134 foreach ($phpt_dirs as $dir) {
135 $phpt_files = $FS->globr($dir, '*.phpt');
136 foreach ($phpt_files as $file) {
137 $test_files[] = str_replace('\\', '/', $file);
141 array_unique($test_files);
142 sort($test_files); // for the SELECT
143 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
144 $test_file_lookup = array_flip($test_files);
146 // determine test file
147 if ($AC['file']) {
148 if (!isset($test_file_lookup[$AC['file']])) {
149 echo "Invalid file passed\n";
150 exit;
154 if ($AC['file']) {
156 $test = new TestSuite($AC['file']);
157 htmlpurifier_add_test($test, $AC['file']);
159 } else {
161 $standalone = '';
162 if ($AC['standalone']) $standalone = ' (standalone)';
163 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
164 foreach ($test_files as $test_file) {
165 htmlpurifier_add_test($test, $test_file);
170 if ($AC['dry']) $reporter->makeDry();
172 $test->run($reporter);