[3.1.1] General munge improvements
[htmlpurifier.git] / tests / index.php
blobc3b15694781d7460bc0d303909532ddefa700ec2
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
29 ini_set('memory_limit', '64M');
31 require 'common.php';
33 $AC = array(); // parameters
34 $AC['flush'] = false;
35 $AC['standalone'] = false;
36 $AC['file'] = '';
37 $AC['xml'] = false;
38 $AC['dry'] = false;
39 $AC['php'] = $php;
41 // Convenience parameters for running quicker tests; ideally all tests
42 // should be performed.
43 $AC['disable-phpt'] = false;
44 $AC['only-phpt'] = false;
46 $aliases = array(
47 'f' => 'file',
50 // It's important that this does not call the autoloader. Not a problem
51 // with a function, but could be if we put this in a class.
52 htmlpurifier_parse_args($AC, $aliases);
54 // Disable PHPT tests if they're not enabled
55 if (!$GLOBALS['HTMLPurifierTest']['PHPT']) $AC['disable-phpt'] = true;
57 if (!SimpleReporter::inCli()) {
58 // Undo any dangerous parameters
59 $AC['php'] = $php;
62 if ($AC['disable-phpt'] && $AC['only-phpt']) {
63 echo "Cannot disable and allow only PHPT tests!\n";
64 exit(1);
67 // Shell-script code is executed
69 if ($AC['xml']) {
70 if (!SimpleReporter::inCli()) header('Content-Type: text/xml;charset=UTF-8');
71 $reporter = new XmlReporter();
72 } elseif (SimpleReporter::inCli()) {
73 $reporter = new TextReporter();
74 } else {
75 $reporter = new HTMLPurifier_SimpleTest_Reporter('UTF-8', $AC);
78 if ($AC['flush']) {
79 htmlpurifier_flush($AC['php'], $reporter);
82 // initialize and load HTML Purifier
83 // use ?standalone to load the alterative standalone stub
84 if ($AC['standalone']) {
85 require '../library/HTMLPurifier.standalone.php';
86 } else {
87 require '../library/HTMLPurifier.path.php';
88 require 'HTMLPurifier.includes.php';
90 require '../library/HTMLPurifier.autoload.php';
91 require 'HTMLPurifier/Harness.php';
93 // Now, userland code begins to be executed
95 // setup special DefinitionCacheFactory decorator
96 $factory = HTMLPurifier_DefinitionCacheFactory::instance();
97 $factory->addDecorator('Memory'); // since we deal with a lot of config objects
99 if (!$AC['disable-phpt']) {
100 $phpt = PHPT_Registry::getInstance();
101 $phpt->php = $AC['php'];
104 // load tests
106 $test_files = array();
107 $test_dirs = array();
108 $test_dirs_exclude = array();
109 $vtest_dirs = array();
110 $phpt_dirs = array();
112 require 'test_files.php'; // populates $test_files array
114 $FS = new FSTools();
116 // handle test dirs
117 foreach ($test_dirs as $dir) {
118 $raw_files = $FS->globr($dir, '*Test.php');
119 foreach ($raw_files as $file) {
120 $file = str_replace('\\', '/', $file);
121 if (isset($test_dirs_exclude[$file])) continue;
122 $test_files[] = $file;
126 // handle vtest dirs
127 foreach ($vtest_dirs as $dir) {
128 $raw_files = $FS->globr($dir, '*.vtest');
129 foreach ($raw_files as $file) {
130 $test_files[] = str_replace('\\', '/', $file);
134 // handle phpt files
135 foreach ($phpt_dirs as $dir) {
136 $phpt_files = $FS->globr($dir, '*.phpt');
137 foreach ($phpt_files as $file) {
138 $test_files[] = str_replace('\\', '/', $file);
142 array_unique($test_files);
143 sort($test_files); // for the SELECT
144 $GLOBALS['HTMLPurifierTest']['Files'] = $test_files; // for the reporter
145 $test_file_lookup = array_flip($test_files);
147 // determine test file
148 if ($AC['file']) {
149 if (!isset($test_file_lookup[$AC['file']])) {
150 echo "Invalid file passed\n";
151 exit;
155 if ($AC['file']) {
157 $test = new TestSuite($AC['file']);
158 htmlpurifier_add_test($test, $AC['file']);
160 } else {
162 $standalone = '';
163 if ($AC['standalone']) $standalone = ' (standalone)';
164 $test = new TestSuite('All HTML Purifier tests on PHP ' . PHP_VERSION . $standalone);
165 foreach ($test_files as $test_file) {
166 htmlpurifier_add_test($test, $test_file);
171 if ($AC['dry']) $reporter->makeDry();
173 $test->run($reporter);