323b9313b8943a58298ddd3cd54e3d74ac82bfe2
[lhc/web/wiklou.git] / maintenance / parserTests.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19
20 /**
21 * @todo Make this more independent of the configuration (and if possible the database)
22 * @todo document
23 * @package MediaWiki
24 * @subpackage Maintenance
25 */
26
27 /** */
28 require_once( 'commandLine.inc' );
29 include_once( 'InitialiseMessages.inc' );
30
31 $wgTitle = Title::newFromText( 'Parser test script' );
32
33 class ParserTest {
34 function ParserTest() {
35 if( isset( $_SERVER['argv'] ) && in_array( '--color', $_SERVER['argv'] ) ) {
36 $this->color = true;
37 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=yes', $_SERVER['argv'] ) ) {
38 $this->color = true;
39 } elseif( isset( $_SERVER['argv'] ) && in_array( '--color=no', $_SERVER['argv'] ) ) {
40 $this->color = false;
41 } elseif( wfIsWindows() ) {
42 $this->color = false;
43 } else {
44 $this->color = true;
45 }
46
47 if( isset( $_SERVER['argv'] ) && in_array( '--quick', $_SERVER['argv'] ) ) {
48 $this->showDiffs = false;
49 } else {
50 $this->showDiffs = true;
51 }
52 }
53
54 function runTestsFromFile( $filename ) {
55 $infile = fopen( $filename, 'rt' );
56 if( !$infile ) {
57 die( "Couldn't open parserTests.txt\n" );
58 }
59
60 $data = array();
61 $section = null;
62 $success = 0;
63 $total = 0;
64 $n = 0;
65 while( false !== ($line = fgets( $infile ) ) ) {
66 $n++;
67 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
68 $section = strtolower( $matches[1] );
69 if( $section == 'end' ) {
70 if (isset ($data['disabled'])) {
71 # disabled test
72 $data = array();
73 $section = null;
74 continue;
75 }
76 if( !isset( $data['test'] ) ) {
77 die( "'end' without 'test' at line $n\n" );
78 }
79 if( !isset( $data['input'] ) ) {
80 die( "'end' without 'input' at line $n\n" );
81 }
82 if( !isset( $data['result'] ) ) {
83 die( "'end' without 'result' at line $n\n" );
84 }
85 if( $this->runTest(
86 rtrim( $data['test'] ),
87 rtrim( $data['input'] ),
88 rtrim( $data['result'] ) ) ) {
89 $success++;
90 }
91 $total++;
92 $data = array();
93 $section = null;
94 continue;
95 }
96 $data[$section] = '';
97 continue;
98 }
99 if( $section ) {
100 $data[$section] .= $line;
101 }
102 }
103 if( $total > 0 ) {
104 $ratio = IntVal( 100.0 * $success / $total );
105 print "\nPassed $success of $total tests ($ratio%)\n";
106 return ($success == $total);
107 } else {
108 die( "No tests found.\n" );
109 }
110 }
111
112 /**
113 * @param string $input Wikitext to try rendering
114 * @param string $result Result to output
115 * @return bool
116 */
117 function runTest( $desc, $input, $result ) {
118 print "Running test $desc... ";
119
120 $this->setupGlobals();
121
122 $user =& new User();
123 $options =& ParserOptions::newFromUser( $user );
124 $parser =& new Parser();
125 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
126
127 $output =& $parser->parse( $input, $title, $options );
128
129 $html = $output->getText();
130 # $languageLinks = $output->getLanguageLinks();
131 # $categoryLinks = $output->getCategoryLinks();
132
133 $op = new OutputPage();
134 $op->replaceLinkHolders($html);
135
136 global $wgUseTidy;
137 if ($wgUseTidy) {
138 # Using Parser here is probably theoretically
139 # wrong, because we shouldn't use Parser to
140 # validate itself, but this should be safe
141 # in practice.
142 $result = Parser::tidy($result);
143 }
144
145 $this->teardownGlobals();
146
147 if( rtrim($result) === rtrim($html) ) {
148 return $this->showSuccess( $desc );
149 } else {
150 return $this->showFailure( $desc, $result, $html );
151 }
152 }
153
154 function setupGlobals() {
155 static $settings = array(
156 'wgServer' => 'http://localhost',
157 'wgScript' => '/index.php',
158 'wgScriptPath' => '/',
159 'wgArticlePath' => '/wiki/$1',
160 'wgSitename' => 'MediaWiki',
161 );
162 $this->savedGlobals = array();
163 foreach( $settings as $var => $val ) {
164 $this->savedGlobals[$var] = $GLOBALS[$var];
165 $GLOBALS[$var] = $val;
166 }
167 }
168
169 function teardownGlobals() {
170 foreach( $this->savedGlobals as $var => $val ) {
171 $GLOBALS[$var] = $val;
172 }
173 }
174
175 function showSuccess( $desc ) {
176 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
177 return true;
178 }
179
180 function showFailure( $desc, $result, $html ) {
181 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
182 if( $this->showDiffs ) {
183 print $this->quickDiff( $result, $html );
184 }
185 return false;
186 }
187
188 function quickDiff( $input, $output ) {
189 $prefix = "/tmp/mwParser-" . mt_rand();
190
191 $infile = "$prefix-in";
192 $this->dumpToFile( $input, $infile );
193
194 $outfile = "$prefix-out";
195 $this->dumpToFile( $output, $outfile );
196
197 $diff = `diff -u $infile $outfile`;
198 unlink( $infile );
199 unlink( $outfile );
200
201 return $this->colorDiff( $diff );
202 }
203
204 function dumpToFile( $data, $filename ) {
205 $file = fopen( $filename, "wt" );
206 fwrite( $file, rtrim( $data ) . "\n" );
207 fclose( $file );
208 }
209
210 function termColor( $color ) {
211 return $this->color ? "\x1b[{$color}m" : '';
212 }
213
214 function termReset() {
215 return $this->color ? "\x1b[0m" : '';
216 }
217
218 function colorDiff( $text ) {
219 return preg_replace(
220 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
221 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
222 $this->termColor( 31 ) . '$1' . $this->termReset() ),
223 $text );
224 }
225 }
226
227 $tester =& new ParserTest();
228
229 # Note: the command line setup changes the current working directory
230 # to the parent, which is why we have to put the subdir here:
231 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
232
233 exit ($ok ? 0 : -1);
234
235 ?>