Support for safe external scripts via explicit whitelist.
[htmlpurifier.git] / tests / multitest.php
blobef296cddad952121c0aa5d5356fd599bda4b742d
1 <?php
3 /** @file
4 * Multiple PHP Versions test
6 * This file tests HTML Purifier in all versions of PHP. Arguments
7 * are specified like --arg=opt, allowed arguments are:
8 * - quiet (q), if specified no informative messages are enabled (please use
9 * this if you're outputting XML)
10 * - distro, allowed values 'normal' or 'standalone', by default all
11 * distributions are tested. "--standalone" is a shortcut for
12 * "--distro=standalone".
13 * - quick, run only the most recent versions of each release series
14 * - disable-flush, by default flush is run, this disables it
15 * - file (f), xml, type: these correspond to the parameters in index.php
17 * @note
18 * It requires a script called phpv that takes an extra argument (the
19 * version number of PHP) before all other arguments. Contact me if you'd
20 * like to set up a similar script. The name of the script can be
21 * edited with $phpv
23 * @note
24 * Also, configuration must be set up with a variable called
25 * $versions_to_test specifying version numbers to pass to $phpv
28 define('HTMLPurifierTest', 1);
29 chdir(dirname(__FILE__));
30 $php = 'php'; // for safety
32 require_once 'common.php';
34 if (!SimpleReporter::inCli()) {
35 echo 'Multitest only available from command line';
36 exit;
39 $AC = array(); // parameters
40 $AC['file'] = '';
41 $AC['xml'] = false;
42 $AC['quiet'] = false;
43 $AC['php'] = $php;
44 $AC['disable-phpt'] = false;
45 $AC['disable-flush'] = false;
46 $AC['type'] = '';
47 $AC['distro'] = ''; // valid values are normal/standalone
48 $AC['quick'] = false; // run the latest version on each release series
49 $AC['standalone'] = false; // convenience for --distro=standalone
50 // Legacy parameters
51 $AC['only-phpt'] = false; // --type=phpt
52 $AC['exclude-normal'] = false; // --distro=standalone
53 $AC['exclude-standalone'] = false; // --distro=normal
54 $AC['verbose'] = false;
55 $aliases = array(
56 'f' => 'file',
57 'q' => 'quiet',
58 'v' => 'verbose',
60 htmlpurifier_parse_args($AC, $aliases);
62 // Backwards compat extra parsing
63 if ($AC['only-phpt']) {
64 $AC['type'] = 'phpt';
66 if ($AC['exclude-normal']) $AC['distro'] = 'standalone';
67 elseif ($AC['exclude-standalone']) $AC['distro'] = 'normal';
68 elseif ($AC['standalone']) $AC['distro'] = 'standalone';
70 if ($AC['xml']) {
71 $reporter = new XmlReporter();
72 } else {
73 $reporter = new HTMLPurifier_SimpleTest_TextReporter($AC);
76 // Regenerate any necessary files
77 if (!$AC['disable-flush']) htmlpurifier_flush($AC['php'], $reporter);
79 $file_arg = '';
80 require 'test_files.php';
81 if ($AC['file']) {
82 $test_files_lookup = array_flip($test_files);
83 if (isset($test_files_lookup[$AC['file']])) {
84 $file_arg = '--file=' . $AC['file'];
85 } else {
86 throw new Exception("Invalid file passed");
89 // This allows us to get out of having to do dry runs.
90 $size = count($test_files);
92 $type_arg = '';
93 if ($AC['type']) $type_arg = '--type=' . $AC['type'];
95 if ($AC['quick']) {
96 $seriesArray = array();
97 foreach ($versions_to_test as $version) {
98 $series = substr($version, 0, strpos($version, '.', strpos($version, '.') + 1));
99 if (!isset($seriesArray[$series])) {
100 $seriesArray[$series] = $version;
101 continue;
103 if (version_compare($version, $seriesArray[$series], '>')) {
104 $seriesArray[$series] = $version;
107 $versions_to_test = array_values($seriesArray);
110 // Setup the test
111 $test = new TestSuite('HTML Purifier Multiple Versions Test');
112 foreach ($versions_to_test as $version) {
113 // Support for arbitrarily forcing flushes by wrapping the suspect
114 // version name in an array()
115 $flush_arg = '';
116 if (is_array($version)) {
117 $version = $version[0];
118 $flush_arg = '--flush';
120 if ($AC['type'] !== 'phpt') {
121 $break = true;
122 switch ($AC['distro']) {
123 case '':
124 $break = false;
125 case 'normal':
126 $test->add(
127 new CliTestCase(
128 "$phpv $version index.php --xml $flush_arg $type_arg --disable-phpt $file_arg",
129 $AC['quiet'], $size
132 if ($break) break;
133 case 'standalone':
134 $test->add(
135 new CliTestCase(
136 "$phpv $version index.php --xml $flush_arg $type_arg --standalone --disable-phpt $file_arg",
137 $AC['quiet'], $size
140 if ($break) break;
143 if (!$AC['disable-phpt'] && (!$AC['type'] || $AC['type'] == 'phpt')) {
144 $test->add(
145 new CliTestCase(
146 $AC['php'] . " index.php --xml --php \"$phpv $version\" --type=phpt",
147 $AC['quiet'], $size
153 // This is the HTML Purifier website's test XML file. We could
154 // add more websites, i.e. more configurations to test.
155 // $test->add(new RemoteTestCase('http://htmlpurifier.org/dev/tests/?xml=1', 'http://htmlpurifier.org/dev/tests/?xml=1&dry=1&flush=1'));
157 $test->run($reporter);
159 // vim: et sw=4 sts=4