Implement %HTML.AllowedComments and %HTML.AllowedCommentsRegexp
[htmlpurifier.git] / tests / index.php
blob48cddcd213a75cc3ec291c11e59a8023d0d00e52
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 // Shell-script code is executed
112 if ($AC['xml']) {
113 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
114 $reporter = new XmlReporter();
115 } elseif (SimpleReporter::inCli() || $AC['txt']) {
116 if (!SimpleReporter::inCli()) header('Content-Type: text/plain;charset=UTF-8');
117 $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
118 } else {
119 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
122 if ($AC['flush']) {
123 htmlpurifier_flush($AC['php'], $reporter);
126 // Now, userland code begins to be executed
128 // setup special DefinitionCacheFactory decorator
129 $factory = HTMLPurifier_DefinitionCacheFactory::instance();
130 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
132 if (!$AC['disable-phpt']) {
133 $phpt = PHPT_Registry::getInstance();
134 $phpt->php = $AC['php'];
137 // load tests
138 require 'test_files.php';
140 $FS = new FSTools();
142 // handle test dirs
143 foreach ($test_dirs as $dir) {
144 $raw_files = $FS->globr($dir, '*Test.php');
145 foreach ($raw_files as $file) {
146 $file = str_replace('\\', '/', $file);
147 if (isset($test_dirs_exclude[$file])) continue;
148 $test_files[] = $file;
152 // handle vtest dirs
153 foreach ($vtest_dirs as $dir) {
154 $raw_files = $FS->globr($dir, '*.vtest');
155 foreach ($raw_files as $file) {
156 $test_files[] = str_replace('\\', '/', $file);
160 // handle phpt files
161 foreach ($phpt_dirs as $dir) {
162 $phpt_files = $FS->globr($dir, '*.phpt');
163 foreach ($phpt_files as $file) {
164 $test_files[] = str_replace('\\', '/', $file);
168 // handle htmlt dirs
169 foreach ($htmlt_dirs as $dir) {
170 $htmlt_files = $FS->globr($dir, '*.htmlt');
171 foreach ($htmlt_files as $file) {
172 $test_files[] = str_replace('\\', '/', $file);
176 array_unique($test_files);
177 sort($test_files); // for the SELECT
178 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
179 $test_file_lookup = array_flip($test_files);
181 // determine test file
182 if ($AC['file']) {
183 if (!isset($test_file_lookup[$AC['file']])) {
184 echo "Invalid file passed\n";
185 exit;
189 if ($AC['file']) {
191 $test = new TestSuite($AC['file']);
192 htmlpurifier_add_test($test, $AC['file']);
194 } else {
196 $standalone = '';
197 if ($AC['standalone']) $standalone = ' (standalone)';
198 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
199 foreach ($test_files as $test_file) {
200 htmlpurifier_add_test($test, $test_file);
205 if ($AC['dry']) $reporter->makeDry();
207 $test->run($reporter);
209 // vim: et sw=4 sts=4