Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / tests / parser / ParserTestRunner.php
index 9255733..149ba80 100644 (file)
@@ -168,7 +168,7 @@ class ParserTestRunner {
                global $wgParserTestFiles;
 
                // Add core test files
-               $files = array_map( function( $item ) {
+               $files = array_map( function ( $item ) {
                        return __DIR__ . "/$item";
                }, self::$coreTestFiles );
 
@@ -182,13 +182,18 @@ class ParserTestRunner {
                        if ( !file_exists( $dir ) ) {
                                continue;
                        }
+                       $counter = 1;
                        $dirIterator = new RecursiveIteratorIterator(
                                new RecursiveDirectoryIterator( $dir )
                        );
                        foreach ( $dirIterator as $fileInfo ) {
                                /** @var SplFileInfo $fileInfo */
                                if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) {
-                                       $files[] = $fileInfo->getPathname();
+                                       $name = $info['name'] . $counter;
+                                       while ( isset( $files[$name] ) ) {
+                                               $name = $info['name'] . '_' . $counter++;
+                                       }
+                                       $files[$name] = $fileInfo->getPathname();
                                }
                        }
                }
@@ -341,8 +346,9 @@ class ParserTestRunner {
                MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
                MediaWikiServices::getInstance()->redefineService(
                        'MediaHandlerFactory',
-                       function () {
-                               return new MockMediaHandlerFactory();
+                       function ( MediaWikiServices $services ) {
+                               $handlers = $services->getMainConfig()->get( 'ParserTestMediaHandlers' );
+                               return new MediaHandlerFactory( $handlers );
                        }
                );
                $teardown[] = function () {
@@ -378,7 +384,7 @@ class ParserTestRunner {
                // Changing wgExtraNamespaces invalidates caches in MWNamespace and
                // any live Language object, both on setup and teardown
                $reset = function () {
-                       MWNamespace::getCanonicalNamespaces( true );
+                       MWNamespace::clearCaches();
                        $GLOBALS['wgContLang']->resetNamespaces();
                };
                $setup[] = $reset;
@@ -477,7 +483,7 @@ class ParserTestRunner {
         * @see staticSetup
         *
         * @param array $teardown The snippet array
-        * @param ScopedCallback|null A ScopedCallback to consume
+        * @param ScopedCallback|null $nextTeardown A ScopedCallback to consume
         * @return ScopedCallback
         */
        protected function createTeardownObject( $teardown, $nextTeardown = null ) {
@@ -512,6 +518,8 @@ class ParserTestRunner {
        /**
         * Ensure a given setup stage has been done, throw an exception if it has
         * not.
+        * @param string $funcName
+        * @param string|null $funcName2
         */
        protected function checkSetupDone( $funcName, $funcName2 = null ) {
                if ( !$this->setupDone[$funcName]
@@ -526,7 +534,7 @@ class ParserTestRunner {
         * Determine whether a particular setup function has been run
         *
         * @param string $funcName
-        * @return boolean
+        * @return bool
         */
        public function isSetupDone( $funcName ) {
                return isset( $this->setupDone[$funcName] ) ? $this->setupDone[$funcName] : false;
@@ -694,6 +702,8 @@ class ParserTestRunner {
        /**
         * Determine whether the current parser has the hooks registered in it
         * that are required by a file read by TestFileReader.
+        * @param array $requirements
+        * @return bool
         */
        public function meetsRequirements( $requirements ) {
                foreach ( $requirements as $requirement ) {
@@ -1036,6 +1046,9 @@ class ParserTestRunner {
                $linkHolderBatchSize =
                        self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
 
+               // Default to fallback skin, but allow it to be overridden
+               $skin = self::getOptionValue( 'skin', $opts, 'fallback' );
+
                $setup = [
                        'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
                        'wgLanguageCode' => $langCode,
@@ -1052,6 +1065,8 @@ class ParserTestRunner {
                        // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
                        'wgEnableMagicLinks' => self::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
                                + [ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
+                       // Test with legacy encoding by default until HTML5 is very stable and default
+                       'wgFragmentMode' => [ 'legacy' ],
                ];
 
                if ( $config ) {
@@ -1105,7 +1120,13 @@ class ParserTestRunner {
                $context = RequestContext::getMain();
                $context->setUser( $user );
                $context->setLanguage( $lang );
-               $teardown[] = function () use ( $context ) {
+               // And the skin!
+               $oldSkin = $context->getSkin();
+               $skinFactory = MediaWikiServices::getInstance()->getSkinFactory();
+               $context->setSkin( $skinFactory->makeSkin( $skin ) );
+               $context->setOutput( new OutputPage( $context ) );
+               $setup['wgOut'] = $context->getOutput();
+               $teardown[] = function () use ( $context, $oldSkin ) {
                        // Clear language conversion tables
                        $wrapper = TestingAccessWrapper::newFromObject(
                                $context->getLanguage()->getConverter()
@@ -1114,6 +1135,8 @@ class ParserTestRunner {
                        // Reset context to the restored globals
                        $context->setUser( $GLOBALS['wgUser'] );
                        $context->setLanguage( $GLOBALS['wgContLang'] );
+                       $context->setSkin( $oldSkin );
+                       $context->setOutput( $GLOBALS['wgOut'] );
                };
 
                $teardown[] = $this->executeSetupSnippets( $setup );
@@ -1128,7 +1151,7 @@ class ParserTestRunner {
         */
        private function listTables() {
                $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
-                       'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
+                       'protected_titles', 'revision', 'ip_changes', 'text', 'pagelinks', 'imagelinks',
                        'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
                        'site_stats', 'ipblocks', 'image', 'oldimage',
                        'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
@@ -1516,7 +1539,7 @@ class ParserTestRunner {
        /**
         * Add articles to the test DB.
         *
-        * @param $articles Article info array from TestFileReader
+        * @param array $articles Article info array from TestFileReader
         */
        public function addArticles( $articles ) {
                global $wgContLang;
@@ -1569,11 +1592,21 @@ class ParserTestRunner {
                        throw new MWException( "invalid title '$name' at $file:$line\n" );
                }
 
+               $newContent = ContentHandler::makeContent( $text, $title );
+
                $page = WikiPage::factory( $title );
                $page->loadPageData( 'fromdbmaster' );
 
                if ( $page->exists() ) {
-                       throw new MWException( "duplicate article '$name' at $file:$line\n" );
+                       $content = $page->getContent( Revision::RAW );
+                       // Only reject the title, if the content/content model is different.
+                       // This makes it easier to create Template:(( or Template:)) in different extensions
+                       if ( $newContent->equals( $content ) ) {
+                               return;
+                       }
+                       throw new MWException(
+                               "duplicate article '$name' with different content at $file:$line\n"
+                       );
                }
 
                // Use mock parser, to make debugging of actual parser tests simpler.
@@ -1581,12 +1614,15 @@ class ParserTestRunner {
                // get a reference to the mock object.
                MessageCache::singleton()->getParser();
                $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
-               $status = $page->doEditContent(
-                       ContentHandler::makeContent( $text, $title ),
-                       '',
-                       EDIT_NEW | EDIT_INTERNAL
-               );
-               $restore();
+               try {
+                       $status = $page->doEditContent(
+                               $newContent,
+                               '',
+                               EDIT_NEW | EDIT_INTERNAL
+                       );
+               } finally {
+                       $restore();
+               }
 
                if ( !$status->isOK() ) {
                        throw new MWException( $status->getWikiText( false, false, 'en' ) );