* Remove @static from the last few places it's left in core. Please don't use this...
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / NewParserTest.php
index d81d69f..70e8d77 100644 (file)
@@ -1,9 +1,18 @@
 <?php
 
 /**
+ * Although marked as a stub, can work independently.
+ *
  * @group Database
+ * @group Parser
+ * @group Stub
  */
 class NewParserTest extends MediaWikiTestCase {
+
+       static protected $articles = array();   // Array of test articles defined by the tests
+       /* The dataProvider is run on a different instance than the test, so it must be static
+        * When running tests from several files, all tests will see all articles.
+        */
        
        public $uploadDir;
        public $keepUploads = false;
@@ -15,22 +24,23 @@ class NewParserTest extends MediaWikiTestCase {
        public $savedGlobals = array();
        public $hooks = array();
        public $functionHooks = array();
-       
+
        //Fuzz test
        public $maxFuzzTestLength = 300;
        public $fuzzSeed = 0;
        public $memoryLimit = 50;
+
+       protected $file = false;
        
-       
-       
-       //PHPUnit + MediaWikiTestCase functions
+       /*function __construct($a = null,$b = array(),$c = null ) {
+               parent::__construct($a,$b,$c);
+       }*/
        
        function setUp() {
-               global $wgContLang, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $wgNamespaceProtection, $wgNamespaceAliases;
+               global $wgContLang, $wgNamespaceProtection, $wgNamespaceAliases;
+               global $wgHooks, $IP;
                $wgContLang = Language::factory( 'en' );
                
-               
-               
                //Setup CLI arguments
                if ( $this->getCliArg( 'regex=' ) ) {
                        $this->regex = $this->getCliArg( 'regex=' );
@@ -60,21 +70,20 @@ class NewParserTest extends MediaWikiTestCase {
                );
                
                $tmpGlobals['wgEnableParserCache'] = false;
+               $tmpGlobals['wgHooks'] = $wgHooks;
                $tmpGlobals['wgDeferredUpdateList'] = array();
-               $tmpGlobals['wgMemc'] = &wfGetMainCache();
-               $messageMemc = &wfGetMessageCacheStorage();
-               $tmpGlobals['parserMemc'] = &wfGetParserCacheStorage();
+               $tmpGlobals['wgMemc'] = wfGetMainCache();
+               $tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
+               $tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
 
                // $tmpGlobals['wgContLang'] = new StubContLang;
                $tmpGlobals['wgUser'] = new User;
-               $tmpGlobals['wgLang'] = new StubUserLang;
-               $tmpGlobals['wgOut'] = new StubObject( 'wgOut', 'OutputPage' );
+               $context = new RequestContext();
+               $tmpGlobals['wgLang'] = $context->getLang();
+               $tmpGlobals['wgOut'] = $context->getOutput();
                $tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
                $tmpGlobals['wgRequest'] = new WebRequest;
 
-               $tmpGlobals['wgMessageCache'] = new StubObject( 'wgMessageCache', 'MessageCache',
-                                                                                 array( $messageMemc, $wgUseDatabaseMessages,
-                                                                                                $wgMsgCacheExpiry ) );
                if ( $GLOBALS['wgStyleDirectory'] === false ) {
                        $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
                }
@@ -145,22 +154,33 @@ class NewParserTest extends MediaWikiTestCase {
                                   'iw_api'    => '',
                                   'iw_wikiid' => '',
                                   'iw_local'  => 1 ),
-                       ) );
+                       /**
+                        * @todo Fixme! Why are we inserting duplicate data here? Shouldn't
+                        * need this IGNORE or shouldn't need the insert at all.
+                        */
+                       ), __METHOD__, array( 'IGNORE' ) );
 
 
                # Update certain things in site_stats
-               $this->db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
+               $this->db->insert( 'site_stats', 
+                       array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
+                       __METHOD__,
+                       /**
+                        * @todo Fixme! Same as above!
+                        */
+                       array( 'IGNORE' )
+               );
 
                # Reinitialise the LocalisationCache to match the database state
                Language::getLocalisationCache()->unloadAll();
 
-               # Make a new message cache
-               global $wgMessageCache, $wgMemc;
-               $wgMessageCache = new MessageCache( $wgMemc, true, 3600 );
+               # Clear the message cache
+               MessageCache::singleton()->clear();
 
                $this->uploadDir = $this->setupUploadDir();
-               
+
                $user = User::newFromId( 0 );
+               LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
                
                $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
                $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', array(
@@ -265,7 +285,8 @@ class NewParserTest extends MediaWikiTestCase {
                        'wgHtml5' => true,
                        'wgWellFormedXml' => true,
                        'wgAllowMicrodataAttributes' => true,
-                       'wgAdaptiveMessageCache' => true
+                       'wgAdaptiveMessageCache' => true,
+                       'wgUseDatabaseMessages' => true,
                );
 
                if ( $config ) {
@@ -289,10 +310,12 @@ class NewParserTest extends MediaWikiTestCase {
                }
 
                $langObj = Language::factory( $lang );
-               $GLOBALS['wgLang'] = $langObj;
                $GLOBALS['wgContLang'] = $langObj;
-               $GLOBALS['wgMemc'] = new FakeMemCachedClient;
-               $GLOBALS['wgOut'] = new OutputPage;
+               $context = new RequestContext();
+               $GLOBALS['wgLang'] = $context->getLang();
+
+               $GLOBALS['wgMemc'] = new EmptyBagOStuff;
+               $GLOBALS['wgOut'] = $context->getOutput();
 
                global $wgHooks;
 
@@ -302,6 +325,13 @@ class NewParserTest extends MediaWikiTestCase {
 
                MagicWord::clearCache();
 
+               # Publish the articles after we have the final language set
+               $this->publishTestArticles();
+
+               # The entries saved into RepoGroup cache with previous globals will be wrong.
+               RepoGroup::destroySingleton();
+               MessageCache::singleton()->destroyInstance();
+               
                global $wgUser;
                $wgUser = new User();
        }
@@ -352,114 +382,96 @@ class NewParserTest extends MediaWikiTestCase {
                return $dir;
        }
        
+       public function parserTestProvider() {
+               if ( $this->file === false ) {
+                       global $wgParserTestFiles;
+                       $this->file = $wgParserTestFiles[0];
+               }
+               return new TestFileIterator( $this->file, $this );
+       }
        
+       /**
+        * Set the file from whose tests will be run by this instance
+        */
+       public function setParserTestFile( $filename ) {
+               $this->file = $filename;
+       }
        
-       
-       
-       //Actual test suites
+       /** @dataProvider parserTestProvider */
+       public function testParserTest( $desc, $input, $result, $opts, $config ) {
+               if ( !preg_match( '/' . $this->regex . '/', $desc ) ) return; //$this->markTestSkipped( 'Filtered out by the user' );
 
-       public function testParserTests() {
-               
-               global $wgParserTestFiles;
-               
-               $files = $wgParserTestFiles;
-               
-               if( $this->getCliArg( 'file=' ) ) {
-                       $files = array( $this->getCliArg( 'file=' ) );
+               wfDebug( "Running parser test: $desc\n" );
+
+               $opts = $this->parseOptions( $opts );
+               $this->setupGlobals( $opts, $config );
+
+               $user = new User();
+               $options = ParserOptions::newFromUser( $user );
+
+               if ( isset( $opts['title'] ) ) {
+                       $titleText = $opts['title'];
+               }
+               else {
+                       $titleText = 'Parser test';
                }
+
+               $local = isset( $opts['local'] );
+               $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
+               $parser = $this->getParser( $preprocessor );
                
-               foreach( $files as $file ) {
-                       
-                       $iter = new ParserTestFileIterator( $file, $this );
-                       
-                       foreach ( $iter as $t ) {
-                               
-                               try {
-                                       
-                                       $desc = $t['test'];
-                                       $input = $t['input'];
-                                       $result = $t['result'];
-                                       $opts = $t['options'];
-                                       $config = $t['config'];
-                                       
-                                       
-                                       $opts = $this->parseOptions( $opts );
-                                       $this->setupGlobals( $opts, $config );
-                       
-                                       $user = new User();
-                                       $options = ParserOptions::newFromUser( $user );
-                       
-                                       if ( isset( $opts['title'] ) ) {
-                                               $titleText = $opts['title'];
-                                       }
-                                       else {
-                                               $titleText = 'Parser test';
-                                       }
-                       
-                                       $local = isset( $opts['local'] );
-                                       $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
-                                       $parser = $this->getParser( $preprocessor );
-                                       $title = Title::newFromText( $titleText );
-                       
-                                       if ( isset( $opts['pst'] ) ) {
-                                               $out = $parser->preSaveTransform( $input, $title, $user, $options );
-                                       } elseif ( isset( $opts['msg'] ) ) {
-                                               $out = $parser->transformMsg( $input, $options );
-                                       } elseif ( isset( $opts['section'] ) ) {
-                                               $section = $opts['section'];
-                                               $out = $parser->getSection( $input, $section );
-                                       } elseif ( isset( $opts['replace'] ) ) {
-                                               $section = $opts['replace'][0];
-                                               $replace = $opts['replace'][1];
-                                               $out = $parser->replaceSection( $input, $section, $replace );
-                                       } elseif ( isset( $opts['comment'] ) ) {
-                                               $linker = $user->getSkin();
-                                               $out = $linker->formatComment( $input, $title, $local );
-                                       } elseif ( isset( $opts['preload'] ) ) {
-                                               $out = $parser->getpreloadText( $input, $title, $options );
-                                       } else {
-                                               $output = $parser->parse( $input, $title, $options, true, true, 1337 );
-                                               $out = $output->getText();
-                       
-                                               if ( isset( $opts['showtitle'] ) ) {
-                                                       if ( $output->getTitleText() ) {
-                                                               $title = $output->getTitleText();
-                                                       }
-                       
-                                                       $out = "$title\n$out";
-                                               }
-                       
-                                               if ( isset( $opts['ill'] ) ) {
-                                                       $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
-                                               } elseif ( isset( $opts['cat'] ) ) {
-                                                       global $wgOut;
-                       
-                                                       $wgOut->addCategoryLinks( $output->getCategories() );
-                                                       $cats = $wgOut->getCategoryLinks();
-                       
-                                                       if ( isset( $cats['normal'] ) ) {
-                                                               $out = $this->tidy( implode( ' ', $cats['normal'] ) );
-                                                       } else {
-                                                               $out = '';
-                                                       }
-                                               }
-                       
-                                               $result = $this->tidy( $result );
-                                       }
-                       
-                                       $this->teardownGlobals();
-                                       
-                                       $this->assertEquals( $result, $out, $desc );
-                               
+               $title = Title::newFromText( $titleText );
+
+               if ( isset( $opts['pst'] ) ) {
+                       $out = $parser->preSaveTransform( $input, $title, $user, $options );
+               } elseif ( isset( $opts['msg'] ) ) {
+                       $out = $parser->transformMsg( $input, $options );
+               } elseif ( isset( $opts['section'] ) ) {
+                       $section = $opts['section'];
+                       $out = $parser->getSection( $input, $section );
+               } elseif ( isset( $opts['replace'] ) ) {
+                       $section = $opts['replace'][0];
+                       $replace = $opts['replace'][1];
+                       $out = $parser->replaceSection( $input, $section, $replace );
+               } elseif ( isset( $opts['comment'] ) ) {
+                       $linker = $user->getSkin();
+                       $out = $linker->formatComment( $input, $title, $local );
+               } elseif ( isset( $opts['preload'] ) ) {
+                       $out = $parser->getpreloadText( $input, $title, $options );
+               } else {
+                       $output = $parser->parse( $input, $title, $options, true, true, 1337 );
+                       $out = $output->getText();
+
+                       if ( isset( $opts['showtitle'] ) ) {
+                               if ( $output->getTitleText() ) {
+                                       $title = $output->getTitleText();
                                }
-                               catch( Exception $e ) {
-                                       $this->assertTrue( false, $t['test'] . ' (failed: ' . $e->getMessage() . ')' );
+
+                               $out = "$title\n$out";
+                       }
+
+                       if ( isset( $opts['ill'] ) ) {
+                               $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
+                       } elseif ( isset( $opts['cat'] ) ) {
+                               global $wgOut;
+
+                               $wgOut->addCategoryLinks( $output->getCategories() );
+                               $cats = $wgOut->getCategoryLinks();
+
+                               if ( isset( $cats['normal'] ) ) {
+                                       $out = $this->tidy( implode( ' ', $cats['normal'] ) );
+                               } else {
+                                       $out = '';
                                }
-                               
                        }
-               
+                       $parser->mPreprocessor = null;
+
+                       $result = $this->tidy( $result );
                }
-                       
+
+               $this->teardownGlobals();
+               
+               $this->assertEquals( $result, $out, $desc );
        }
        
        /**
@@ -468,6 +480,8 @@ class NewParserTest extends MediaWikiTestCase {
         */
        function testFuzzTests() {
                
+               $this->markTestIncomplete( 'Breaks tesla due to memory restrictions' );
+               
                global $wgParserTestFiles;
                
                $files = $wgParserTestFiles;
@@ -510,11 +524,7 @@ class NewParserTest extends MediaWikiTestCase {
                                $parser->parse( $input, $title, $opts );
                                $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
                        } catch ( Exception $exception ) {
-                               
-                               ob_start();
-                               var_dump( $input );
-                               $input_dump = ob_get_contents();
-                               ob_end_clean();
+                               $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
                                
                                $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
                        }
@@ -534,7 +544,6 @@ class NewParserTest extends MediaWikiTestCase {
                                        }
                                        
                                        throw new MWException( $ret );
-                                       return;
                                }
                        }
                        
@@ -542,13 +551,7 @@ class NewParserTest extends MediaWikiTestCase {
                        
                }
        }
-       
-       
-       
-       
-       
-       
-       
+
        //Various getter functions
        
        /**
@@ -613,60 +616,29 @@ class NewParserTest extends MediaWikiTestCase {
                $class = $wgParserConf['class'];
                $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
 
-               foreach ( $this->hooks as $tag => $callback ) {
-                       $parser->setHook( $tag, $callback );
-               }
-
-               foreach ( $this->functionHooks as $tag => $bits ) {
-                       list( $callback, $flags ) = $bits;
-                       $parser->setFunctionHook( $tag, $callback, $flags );
-               }
-
                wfRunHooks( 'ParserTestParser', array( &$parser ) );
 
                return $parser;
        }
-       
-       
-       
-       
-       
-       
-       
-       //Various action functions
-       
-       /**
-        * Insert a temporary test article
-        * @param $name String: the title, including any prefix
-        * @param $text String: the article text
-        * @param $line Integer: the input line number, for reporting errors
-        */
-       public function addArticle( $name, $text, $line = 'unknown' ) {
-               global $wgCapitalLinks;
-
-               $text = $this->removeEndingNewline($text);
-
-               $oldCapitalLinks = $wgCapitalLinks;
-               $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
 
-               $name = $this->removeEndingNewline( $name );
-               $title = Title::newFromText( $name );
+       //Various action functions
 
-               if ( is_null( $title ) ) {
-                       throw new MWException( "invalid title ('$name' => '$title') at line $line\n" );
+       public function addArticle( $name, $text, $line ) {
+               self::$articles[$name] = $text;
+       }       
+       
+       public function publishTestArticles() {
+               if ( empty( self::$articles ) ) {
+                       return;
                }
 
-               $aid = $title->getArticleID( Title::GAID_FOR_UPDATE );
+               foreach ( self::$articles as $name => $text ) {
+                       $title = Title::newFromText( $name );
 
-               if ( $aid != 0 ) {
-                       debug_print_backtrace();
-                       throw new MWException( "duplicate article '$name' at line $line\n" );
+                       if ( $title->getArticleID( Title::GAID_FOR_UPDATE ) == 0 ) {
+                               ParserTest::addArticle( $name, $text );
+                       }
                }
-
-               $art = new Article( $title );
-               $art->doEdit( $text, '', EDIT_NEW );
-
-               $wgCapitalLinks = $oldCapitalLinks;
        }
        
        /**
@@ -679,23 +651,15 @@ class NewParserTest extends MediaWikiTestCase {
         */
        public function requireHook( $name ) {
                global $wgParser;
-
                $wgParser->firstCallInit( ); // make sure hooks are loaded.
+               return isset( $wgParser->mTagHooks[$name] );
+       }
 
-               if ( isset( $wgParser->mTagHooks[$name] ) ) {
-                       $this->hooks[$name] = $wgParser->mTagHooks[$name];
-               } else {
-                       echo "   This test suite requires the '$name' hook extension, skipping.\n";
-                       return false;
-               }
-
-               return true;
+       public function requireFunctionHook( $name ) {
+               global $wgParser;
+               $wgParser->firstCallInit( ); // make sure hooks are loaded.
+               return isset( $wgParser->mFunctionHooks[$name] );
        }
-       
-       
-       
-       
-       
        //Various "cleanup" functions
        
        /*
@@ -704,7 +668,6 @@ class NewParserTest extends MediaWikiTestCase {
         *
         * @param $text String: the text to tidy
         * @return String
-        * @static
         */
        protected function tidy( $text ) {
                global $wgUseTidy;
@@ -727,12 +690,11 @@ class NewParserTest extends MediaWikiTestCase {
                        return $s;
                }
        }
-       
-       
-       
-       
-       
-       
+
+       public function showRunFile( $file ) {
+               /* NOP */
+       }
+
        //Test options parser functions
        
        protected function parseOptions( $instring ) {
@@ -817,4 +779,3 @@ class NewParserTest extends MediaWikiTestCase {
                }
        }
 }
-