- /**
- * @param string $input Wikitext to try rendering
- * @param string $result Result to output
- * @return bool
- */
- function runTest( $desc, $input, $result ) {
- print "Running test $desc...";
-
- $this->setupGlobals();
-
- $user =& new User();
- $options =& ParserOptions::newFromUser( $user );
- $parser =& new Parser();
- $title =& Title::makeTitle( NS_MAIN, 'Parser_test' );
-
- $output =& $parser->parse( $input, $title, $options );
-
- $html = $output->getText();
- # $languageLinks = $output->getLanguageLinks();
- # $categoryLinks = $output->getCategoryLinks();
-
- $op = new OutputPage();
- $op->replaceLinkHolders($html);
-
- global $wgUseTidy;
- if ($wgUseTidy) {
- # Using Parser here is probably theoretically
- # wrong, because we shouldn't use Parser to
- # validate itself, but this should be safe
- # in practice.
- $result = Parser::tidy($result);
- }
-
- $this->teardownGlobals();
-
- if( rtrim($result) === rtrim($html) ) {
- return $this->showSuccess( $desc );
- } else {
- return $this->showFailure( $desc, $result, $html );
- }
- }
-
- function setupGlobals() {
- static $settings = array(
- 'wgServer' => 'http://localhost',
- 'wgScript' => '/index.php',
- 'wgScriptPath' => '/',
- 'wgArticlePath' => '/wiki/$1',
- );
- $this->savedGlobals = array();
- foreach( $settings as $var => $val ) {
- $this->savedGlobals[$var] = $GLOBALS[$var];
- $GLOBALS[$var] = $val;
- }
- }
-
- function teardownGlobals() {
- foreach( $this->savedGlobals as $var => $val ) {
- $GLOBALS[$var] = $val;
- }
- }
-
- function showSuccess( $desc ) {
- print "ok\n";
- return true;
- }
-
- function showFailure( $desc, $result, $html ) {
- print "FAILED\n";
- #print "!! Expected:\n$result\n";
- #print "!! Received:\n$html\n!!\n";
- print $this->quickDiff( $result, $html );
- return false;
- }
-
- function quickDiff( $input, $output ) {
- $prefix = "/tmp/mwParser-" . mt_rand();
-
- $infile = "$prefix-in";
- $this->dumpToFile( $input, $infile );
-
- $outfile = "$prefix-out";
- $this->dumpToFile( $output, $outfile );
-
- $diff = `diff -u $infile $outfile`;
- unlink( $infile );
- unlink( $outfile );
-
- return $diff;
- }
-
- function dumpToFile( $data, $filename ) {
- $file = fopen( $filename, "wt" );
- fwrite( $file, rtrim( $data ) . "\n" );
- fclose( $file );
- }