SECURITY: blacklist CSS var()
[lhc/web/wiklou.git] / tests / parser / ParserTestPrinter.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Testing
20 */
21
22 use MediaWiki\Shell\Shell;
23
24 /**
25 * This is a TestRecorder responsible for printing information about progress,
26 * success and failure to the console. It is specific to the parserTests.php
27 * frontend.
28 */
29 class ParserTestPrinter extends TestRecorder {
30 private $total;
31 private $success;
32 private $skipped;
33 private $term;
34 private $showDiffs;
35 private $showProgress;
36 private $showFailure;
37 private $showOutput;
38 private $useDwdiff;
39 private $markWhitespace;
40 private $xmlError;
41
42 function __construct( $term, $options ) {
43 $this->term = $term;
44 $options += [
45 'showDiffs' => true,
46 'showProgress' => true,
47 'showFailure' => true,
48 'showOutput' => false,
49 'useDwdiff' => false,
50 'markWhitespace' => false,
51 ];
52 $this->showDiffs = $options['showDiffs'];
53 $this->showProgress = $options['showProgress'];
54 $this->showFailure = $options['showFailure'];
55 $this->showOutput = $options['showOutput'];
56 $this->useDwdiff = $options['useDwdiff'];
57 $this->markWhitespace = $options['markWhitespace'];
58 }
59
60 public function start() {
61 $this->total = 0;
62 $this->success = 0;
63 $this->skipped = 0;
64 }
65
66 public function startTest( $test ) {
67 if ( $this->showProgress ) {
68 $this->showTesting( $test['desc'] );
69 }
70 }
71
72 private function showTesting( $desc ) {
73 print "Running test $desc... ";
74 }
75
76 /**
77 * Show "Reading tests from ..."
78 *
79 * @param string $path
80 */
81 public function startSuite( $path ) {
82 print $this->term->color( 1 ) .
83 "Running parser tests from \"$path\"..." .
84 $this->term->reset() .
85 "\n";
86 }
87
88 public function endSuite( $path ) {
89 print "\n";
90 }
91
92 public function record( $test, ParserTestResult $result ) {
93 $this->total++;
94 $this->success += ( $result->isSuccess() ? 1 : 0 );
95
96 if ( $result->isSuccess() ) {
97 $this->showSuccess( $result );
98 } else {
99 $this->showFailure( $result );
100 }
101 }
102
103 /**
104 * Print a happy success message.
105 *
106 * @param ParserTestResult $testResult
107 * @return bool
108 */
109 private function showSuccess( ParserTestResult $testResult ) {
110 if ( $this->showProgress ) {
111 print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
112 }
113 }
114
115 /**
116 * Print a failure message and provide some explanatory output
117 * about what went wrong if so configured.
118 *
119 * @param ParserTestResult $testResult
120 * @return bool
121 */
122 private function showFailure( ParserTestResult $testResult ) {
123 if ( $this->showFailure ) {
124 if ( !$this->showProgress ) {
125 # In quiet mode we didn't show the 'Testing' message before the
126 # test, in case it succeeded. Show it now:
127 $this->showTesting( $testResult->getDescription() );
128 }
129
130 print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
131
132 if ( $this->showOutput ) {
133 print "--- Expected ---\n{$testResult->expected}\n";
134 print "--- Actual ---\n{$testResult->actual}\n";
135 }
136
137 if ( $this->showDiffs ) {
138 print $this->quickDiff( $testResult->expected, $testResult->actual );
139 if ( !$this->wellFormed( $testResult->actual ) ) {
140 print "XML error: $this->xmlError\n";
141 }
142 }
143 }
144
145 return false;
146 }
147
148 /**
149 * Run given strings through a diff and return the (colorized) output.
150 * Requires writable /tmp directory and a 'diff' command in the PATH.
151 *
152 * @param string $input
153 * @param string $output
154 * @param string $inFileTail Tailing for the input file name
155 * @param string $outFileTail Tailing for the output file name
156 * @return string
157 */
158 private function quickDiff( $input, $output,
159 $inFileTail = 'expected', $outFileTail = 'actual'
160 ) {
161 if ( $this->markWhitespace ) {
162 $pairs = [
163 "\n" => '¶',
164 ' ' => '·',
165 "\t" => '→'
166 ];
167 $input = strtr( $input, $pairs );
168 $output = strtr( $output, $pairs );
169 }
170
171 $infile = tempnam( wfTempDir(), "mwParser-$inFileTail" );
172 $this->dumpToFile( $input, $infile );
173
174 $outfile = tempnam( wfTempDir(), "mwParser-$outFileTail" );
175 $this->dumpToFile( $output, $outfile );
176
177 global $wgDiff3;
178 // we assume that people with diff3 also have usual diff
179 if ( $this->useDwdiff ) {
180 $shellCommand = 'dwdiff -Pc';
181 } else {
182 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
183 }
184
185 $result = Shell::command()
186 ->unsafeParams( $shellCommand )
187 ->params( $infile, $outfile )
188 ->execute();
189 $diff = $result->getStdout();
190
191 unlink( $infile );
192 unlink( $outfile );
193
194 if ( $this->useDwdiff ) {
195 return $diff;
196 } else {
197 return $this->colorDiff( $diff );
198 }
199 }
200
201 /**
202 * Write the given string to a file, adding a final newline.
203 *
204 * @param string $data
205 * @param string $filename
206 */
207 private function dumpToFile( $data, $filename ) {
208 $file = fopen( $filename, "wt" );
209 fwrite( $file, $data . "\n" );
210 fclose( $file );
211 }
212
213 /**
214 * Colorize unified diff output if set for ANSI color output.
215 * Subtractions are colored blue, additions red.
216 *
217 * @param string $text
218 * @return string
219 */
220 private function colorDiff( $text ) {
221 return preg_replace(
222 [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
223 [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
224 $this->term->color( 31 ) . '$1' . $this->term->reset() ],
225 $text );
226 }
227
228 private function wellFormed( $text ) {
229 $html =
230 Sanitizer::hackDocType() .
231 '<html>' .
232 $text .
233 '</html>';
234
235 $parser = xml_parser_create( "UTF-8" );
236
237 # case folding violates XML standard, turn it off
238 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
239
240 if ( !xml_parse( $parser, $html, true ) ) {
241 $err = xml_error_string( xml_get_error_code( $parser ) );
242 $position = xml_get_current_byte_index( $parser );
243 $fragment = $this->extractFragment( $html, $position );
244 $this->xmlError = "$err at byte $position:\n$fragment";
245 xml_parser_free( $parser );
246
247 return false;
248 }
249
250 xml_parser_free( $parser );
251
252 return true;
253 }
254
255 private function extractFragment( $text, $position ) {
256 $start = max( 0, $position - 10 );
257 $before = $position - $start;
258 $fragment = '...' .
259 $this->term->color( 34 ) .
260 substr( $text, $start, $before ) .
261 $this->term->color( 0 ) .
262 $this->term->color( 31 ) .
263 $this->term->color( 1 ) .
264 substr( $text, $position, 1 ) .
265 $this->term->color( 0 ) .
266 $this->term->color( 34 ) .
267 substr( $text, $position + 1, 9 ) .
268 $this->term->color( 0 ) .
269 '...';
270 $display = str_replace( "\n", ' ', $fragment );
271 $caret = ' ' .
272 str_repeat( ' ', $before ) .
273 $this->term->color( 31 ) .
274 '^' .
275 $this->term->color( 0 );
276
277 return "$display\n$caret";
278 }
279
280 /**
281 * Show a warning to the user
282 * @param string $message
283 */
284 public function warning( $message ) {
285 echo "$message\n";
286 }
287
288 /**
289 * Mark a test skipped
290 * @param string $test
291 * @param string $subtest
292 */
293 public function skipped( $test, $subtest ) {
294 if ( $this->showProgress ) {
295 print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
296 }
297 $this->skipped++;
298 }
299
300 public function report() {
301 if ( $this->total > 0 ) {
302 $this->reportPercentage( $this->success, $this->total );
303 } else {
304 print $this->term->color( 31 ) . "No tests found." . $this->term->reset() . "\n";
305 }
306 }
307
308 private function reportPercentage( $success, $total ) {
309 $ratio = wfPercent( 100 * $success / $total );
310 print $this->term->color( 1 ) . "Passed $success of $total tests ($ratio)";
311 if ( $this->skipped ) {
312 print ", skipped {$this->skipped}";
313 }
314 print "... ";
315
316 if ( $success == $total ) {
317 print $this->term->color( 32 ) . "ALL TESTS PASSED!";
318 } else {
319 $failed = $total - $success;
320 print $this->term->color( 31 ) . "$failed tests failed!";
321 }
322
323 print $this->term->reset() . "\n";
324
325 return ( $success == $total );
326 }
327 }