Use OutputPage to replace link placeholders; add links, quotes tests
[lhc/web/wiklou.git] / maintenance / parserTests.php
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 /** */
9 require_once( 'commandLine.inc' );
10 include_once( 'InitialiseMessages.inc' );
11
12 $wgTitle = Title::newFromText( 'Parser test script' );
13
14 class ParserTest {
15 function runTestsFromFile( $filename ) {
16 $infile = fopen( $filename, 'rt' );
17 if( !$infile ) {
18 die( "Couldn't open parserTests.txt\n" );
19 }
20
21 $data = array();
22 $section = null;
23 $success = 0;
24 $total = 0;
25 $n = 0;
26 while( false !== ($line = fgets( $infile ) ) ) {
27 $n++;
28 if( preg_match( '/^!!\s*(\w+)/', $line, $matches ) ) {
29 $section = strtolower( $matches[1] );
30 if( $section == 'end' ) {
31 if (isset ($data['disabled'])) {
32 # disabled test
33 $data = array();
34 $section = null;
35 continue;
36 }
37 if( !isset( $data['test'] ) ) {
38 die( "'end' without 'test' at line $n\n" );
39 }
40 if( !isset( $data['input'] ) ) {
41 die( "'end' without 'input' at line $n\n" );
42 }
43 if( !isset( $data['result'] ) ) {
44 die( "'end' without 'result' at line $n\n" );
45 }
46 if( $this->runTest(
47 rtrim( $data['test'] ),
48 rtrim( $data['input'] ),
49 rtrim( $data['result'] ) ) ) {
50 $success++;
51 }
52 $total++;
53 $data = array();
54 $section = null;
55 continue;
56 }
57 $data[$section] = '';
58 continue;
59 }
60 if( $section ) {
61 $data[$section] .= $line;
62 }
63 }
64 if( $total > 0 ) {
65 $ratio = IntVal( 100.0 * $success / $total );
66 print "\nPassed $success of $total tests ($ratio%)\n";
67 }
68 }
69
70 /**
71 * @param string $input Wikitext to try rendering
72 * @param string $result Result to output
73 * @return bool
74 */
75 function runTest( $desc, $input, $result ) {
76 print "Running test $desc...";
77
78 $user =& new User();
79 $options =& ParserOptions::newFromUser( $user );
80 $parser =& new Parser();
81 $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
82
83 $output =& $parser->parse( $input, $title, $options );
84
85 $html = $output->getText();
86 # $languageLinks = $output->getLanguageLinks();
87 # $categoryLinks = $output->getCategoryLinks();
88
89 $op = new OutputPage();
90 $op->replaceLinkHolders($html);
91
92 if( $result == rtrim( $html ) ) {
93 return $this->showSuccess( $desc );
94 } else {
95 return $this->showFailure( $desc, $result, $html );
96 }
97 }
98
99 function showSuccess( $desc ) {
100 print "ok\n";
101 return true;
102 }
103
104 function showFailure( $desc, $result, $html ) {
105 print "FAILED\n";
106 print "!! Expected:\n$result\n";
107 print "!! Received:\n$html\n!!\n";
108 return false;
109 }
110 }
111
112 $tester =& new ParserTest();
113 $tester->runTestsFromFile( 'maintenance/parserTests.txt' );
114
115 ?>