012bfea4e1de2a434f2c121c5ec35752e1beea58
[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
48 function runTestsFromFile( $filename ) {
49 $infile = fopen( $filename, 'rt' );
50 if( !$infile ) {
51 die( "Couldn't open parserTests.txt\n" );
52 }
53
54 $data = array();
55 $section = null;
56 $success = 0;
57 $total = 0;
58 $n = 0;
59 while( false !== ($line = fgets( $infile ) ) ) {
60 $n++;
61 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
62 $section = strtolower( $matches[1] );
63 if( $section == 'end' ) {
64 if (isset ($data['disabled'])) {
65 # disabled test
66 $data = array();
67 $section = null;
68 continue;
69 }
70 if( !isset( $data['test'] ) ) {
71 die( "'end' without 'test' at line $n\n" );
72 }
73 if( !isset( $data['input'] ) ) {
74 die( "'end' without 'input' at line $n\n" );
75 }
76 if( !isset( $data['result'] ) ) {
77 die( "'end' without 'result' at line $n\n" );
78 }
79 if( $this->runTest(
80 rtrim( $data['test'] ),
81 rtrim( $data['input'] ),
82 rtrim( $data['result'] ) ) ) {
83 $success++;
84 }
85 $total++;
86 $data = array();
87 $section = null;
88 continue;
89 }
90 $data[$section] = '';
91 continue;
92 }
93 if( $section ) {
94 $data[$section] .= $line;
95 }
96 }
97 if( $total > 0 ) {
98 $ratio = IntVal( 100.0 * $success / $total );
99 print "\nPassed $success of $total tests ($ratio%)\n";
100 return ($success == $total);
101 } else {
102 die( "No tests found.\n" );
103 }
104 }
105
106 /**
107 * @param string $input Wikitext to try rendering
108 * @param string $result Result to output
109 * @return bool
110 */
111 function runTest( $desc, $input, $result ) {
112 print "Running test $desc... ";
113
114 $this->setupGlobals();
115
116 $user =& new User();
117 $options =& ParserOptions::newFromUser( $user );
118 $parser =& new Parser();
119 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
120
121 $output =& $parser->parse( $input, $title, $options );
122
123 $html = $output->getText();
124 # $languageLinks = $output->getLanguageLinks();
125 # $categoryLinks = $output->getCategoryLinks();
126
127 $op = new OutputPage();
128 $op->replaceLinkHolders($html);
129
130 global $wgUseTidy;
131 if ($wgUseTidy) {
132 # Using Parser here is probably theoretically
133 # wrong, because we shouldn't use Parser to
134 # validate itself, but this should be safe
135 # in practice.
136 $result = Parser::tidy($result);
137 }
138
139 $this->teardownGlobals();
140
141 if( rtrim($result) === rtrim($html) ) {
142 return $this->showSuccess( $desc );
143 } else {
144 return $this->showFailure( $desc, $result, $html );
145 }
146 }
147
148 function setupGlobals() {
149 static $settings = array(
150 'wgServer' => 'http://localhost',
151 'wgScript' => '/index.php',
152 'wgScriptPath' => '/',
153 'wgArticlePath' => '/wiki/$1',
154 );
155 $this->savedGlobals = array();
156 foreach( $settings as $var => $val ) {
157 $this->savedGlobals[$var] = $GLOBALS[$var];
158 $GLOBALS[$var] = $val;
159 }
160 }
161
162 function teardownGlobals() {
163 foreach( $this->savedGlobals as $var => $val ) {
164 $GLOBALS[$var] = $val;
165 }
166 }
167
168 function showSuccess( $desc ) {
169 print $this->termColor( '1;32' ) . 'PASSED' . $this->termReset() . "\n";
170 return true;
171 }
172
173 function showFailure( $desc, $result, $html ) {
174 print $this->termColor( '1;31' ) . 'FAILED!' . $this->termReset() . "\n";
175 #print "!! Expected:\n$result\n";
176 #print "!! Received:\n$html\n!!\n";
177 print $this->quickDiff( $result, $html );
178 return false;
179 }
180
181 function quickDiff( $input, $output ) {
182 $prefix = "/tmp/mwParser-" . mt_rand();
183
184 $infile = "$prefix-in";
185 $this->dumpToFile( $input, $infile );
186
187 $outfile = "$prefix-out";
188 $this->dumpToFile( $output, $outfile );
189
190 $diff = `diff -u $infile $outfile`;
191 unlink( $infile );
192 unlink( $outfile );
193
194 return $this->colorDiff( $diff );
195 }
196
197 function dumpToFile( $data, $filename ) {
198 $file = fopen( $filename, "wt" );
199 fwrite( $file, rtrim( $data ) . "\n" );
200 fclose( $file );
201 }
202
203 function termColor( $color ) {
204 return $this->color ? "\x1b[{$color}m" : '';
205 }
206
207 function termReset() {
208 return $this->color ? "\x1b[0m" : '';
209 }
210
211 function colorDiff( $text ) {
212 return preg_replace(
213 array( '/^(-.*)$/m', '/^(\+.*)$/m' ),
214 array( $this->termColor( 34 ) . '$1' . $this->termReset(),
215 $this->termColor( 31 ) . '$1' . $this->termReset() ),
216 $text );
217 }
218 }
219
220 $tester =& new ParserTest();
221
222 # Note: the command line setup changes the current working directory
223 # to the parent, which is why we have to put the subdir here:
224 $ok = $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
225
226 exit ($ok ? 0 : -1);
227
228 ?>