Doc fix.
[htmlpurifier.git] / tests / index.php
blob808b2ce18b08b6c6b449991df35974cccaf22fce
1 <?php
3 /** @file
4 * Unit tester
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
14 * - type, the type of tests to run, can be 'htmlpurifier', 'configdoc',
15 * 'fstools', 'htmlt', 'vtest' or 'phpt'
17 * If you're interested in running the test-cases, mosey over to
18 * ../test-settings.sample.php, copy the file to test-settings.php and follow
19 * the enclosed instructions.
21 * @warning File setup does not exactly match with autoloader; make sure that
22 * non-test classes (i.e. classes that are not retrieved using
23 * $test_files) do not have underscores in their names.
26 // HTML Purifier runs error free on E_STRICT, so if code reports
27 // errors, we want to know about it.
28 error_reporting(E_ALL | E_STRICT);
30 // Because we always want to know about errors, and because SimpleTest
31 // will notify us about them, logging the errors to stderr is
32 // counterproductive and in fact the wrong thing when a test case
33 // exercises an error condition to detect for it.
34 ini_set('log_errors', false);
36 define('HTMLPurifierTest', 1);
37 define('HTMLPURIFIER_SCHEMA_STRICT', true); // validate schemas
38 chdir(dirname(__FILE__));
40 $php = 'php'; // for safety
41 ini_set('memory_limit', '64M');
43 require 'common.php';
44 $AC = array(); // parameters
45 $AC['flush'] = false;
46 $AC['standalone'] = false;
47 $AC['file'] = '';
48 $AC['xml'] = false;
49 $AC['dry'] = false;
50 $AC['php'] = $php;
51 $AC['help'] = false;
52 $AC['verbose'] = false;
53 $AC['txt'] = false;
55 $AC['type'] = '';
56 $AC['disable-phpt'] = false;
57 $AC['only-phpt'] = false; // alias for --type=phpt
59 $aliases = array(
60 'f' => 'file',
61 'h' => 'help',
62 'v' => 'verbose',
65 // It's important that this does not call the autoloader. Not a problem
66 // with a function, but could be if we put this in a class.
67 htmlpurifier_parse_args($AC, $aliases);
69 if ($AC['help']) {
70 ?>HTML Purifier test suite
71 Allowed options:
72 --flush
73 --standalone
74 --file (-f) HTMLPurifier/NameOfTest.php
75 --xml
76 --txt
77 --dry
78 --php /path/to/php
79 --type ( htmlpurifier | configdoc | fstools | htmlt | vtest | phpt )
80 --disable-phpt
81 --verbose (-v)
82 <?php
83 exit;
86 // Disable PHPT tests if they're not enabled
87 if (!$GLOBALS['HTMLPurifierTest']['PHPT']) {
88 $AC['disable-phpt'] = true;
89 } elseif (!$AC['type'] && $AC['only-phpt']) {
90 // backwards-compat
91 $AC['type'] = 'phpt';
94 if (!SimpleReporter::inCli()) {
95 // Undo any dangerous parameters
96 $AC['php'] = $php;
99 // initialize and load HTML Purifier
100 // use ?standalone to load the alterative standalone stub
101 if ($AC['standalone']) {
102 require '../library/HTMLPurifier.standalone.php';
103 } else {
104 require '../library/HTMLPurifier.path.php';
105 require 'HTMLPurifier.includes.php';
107 require '../library/HTMLPurifier.autoload.php';
108 require 'HTMLPurifier/Harness.php';
110 // immediately load external libraries, so we can bail out early if
111 // they're bad
112 if ($GLOBALS['HTMLPurifierTest']['PEAR']) {
113 if ($GLOBALS['HTMLPurifierTest']['Net_IDNA2']) {
114 require_once 'Net/IDNA2.php';
118 // Shell-script code is executed
120 if ($AC['xml']) {
121 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
122 $reporter = new XmlReporter();
123 } elseif (SimpleReporter::inCli() || $AC['txt']) {
124 if (!SimpleReporter::inCli()) header('Content-Type: text/plain;charset=UTF-8');
125 $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
126 } else {
127 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
130 if ($AC['flush']) {
131 htmlpurifier_flush($AC['php'], $reporter);
134 // Now, userland code begins to be executed
136 // setup special DefinitionCacheFactory decorator
137 $factory = HTMLPurifier_DefinitionCacheFactory::instance();
138 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
140 if (!$AC['disable-phpt']) {
141 $phpt = PHPT_Registry::getInstance();
142 $phpt->php = $AC['php'];
145 // load tests
146 require 'test_files.php';
148 $FS = new FSTools();
150 // handle test dirs
151 foreach ($test_dirs as $dir) {
152 $raw_files = $FS->globr($dir, '*Test.php');
153 foreach ($raw_files as $file) {
154 $file = str_replace('\\', '/', $file);
155 if (isset($test_dirs_exclude[$file])) continue;
156 $test_files[] = $file;
160 // handle vtest dirs
161 foreach ($vtest_dirs as $dir) {
162 $raw_files = $FS->globr($dir, '*.vtest');
163 foreach ($raw_files as $file) {
164 $test_files[] = str_replace('\\', '/', $file);
168 // handle phpt files
169 foreach ($phpt_dirs as $dir) {
170 $phpt_files = $FS->globr($dir, '*.phpt');
171 foreach ($phpt_files as $file) {
172 $test_files[] = str_replace('\\', '/', $file);
176 // handle htmlt dirs
177 foreach ($htmlt_dirs as $dir) {
178 $htmlt_files = $FS->globr($dir, '*.htmlt');
179 foreach ($htmlt_files as $file) {
180 $test_files[] = str_replace('\\', '/', $file);
184 array_unique($test_files);
185 sort($test_files); // for the SELECT
186 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
187 $test_file_lookup = array_flip($test_files);
189 // determine test file
190 if ($AC['file']) {
191 if (!isset($test_file_lookup[$AC['file']])) {
192 echo "Invalid file passed\n";
193 exit;
197 if ($AC['file']) {
199 $test = new TestSuite($AC['file']);
200 htmlpurifier_add_test($test, $AC['file']);
202 } else {
204 $standalone = '';
205 if ($AC['standalone']) $standalone = ' (standalone)';
206 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
207 foreach ($test_files as $test_file) {
208 htmlpurifier_add_test($test, $test_file);
213 if ($AC['dry']) $reporter->makeDry();
215 $test->run($reporter);
217 // vim: et sw=4 sts=4