test: rework interwiki lookup in parser tests
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / NewParserTest.php
index 994bc1c..93923fd 100644 (file)
@@ -31,6 +31,11 @@ class NewParserTest extends MediaWikiTestCase {
 
        protected $file = false;
 
+       public static function setUpBeforeClass() {
+               // Inject ParserTest well-known interwikis
+               ParserTest::setupInterwikis();
+       }
+
        protected function setUp() {
                global $wgNamespaceAliases;
                global $wgHooks, $IP;
@@ -59,7 +64,6 @@ class NewParserTest extends MediaWikiTestCase {
                $tmpGlobals['wgActionPaths'] = array();
                $tmpGlobals['wgVariantArticlePath'] = false;
                $tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
-               $tmpGlobals['wgStyleSheetPath'] = '/skins';
                $tmpGlobals['wgStylePath'] = '/skins';
                $tmpGlobals['wgEnableUploads'] = true;
                $tmpGlobals['wgThumbnailScriptPath'] = false;
@@ -84,7 +88,6 @@ class NewParserTest extends MediaWikiTestCase {
                $tmpGlobals['wgRawHtml'] = false;
                $tmpGlobals['wgUseTidy'] = false;
                $tmpGlobals['wgAlwaysUseTidy'] = false;
-               $tmpGlobals['wgHtml5'] = true;
                $tmpGlobals['wgWellFormedXml'] = true;
                $tmpGlobals['wgAllowMicrodataAttributes'] = true;
                $tmpGlobals['wgExperimentalHtmlIds'] = false;
@@ -101,16 +104,27 @@ class NewParserTest extends MediaWikiTestCase {
                                'createtalk' => true,
                ) );
                $tmpGlobals['wgNamespaceProtection'] = array( NS_MEDIAWIKI => 'editinterface' );
-               $tmpGlobals['wgMemc'] = new EmptyBagOStuff;
-               $tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
-               $tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
 
                $tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
 
+               $tmpGlobals['wgFileExtensions'][] = 'svg';
+               $tmpGlobals['wgSVGConverter'] = 'rsvg';
+               $tmpGlobals['wgSVGConverters']['rsvg'] = '$path/rsvg-convert -w $width -h $height $input -o $output';
+
                if ( $GLOBALS['wgStyleDirectory'] === false ) {
                        $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
                }
 
+               # Replace all media handlers with a mock. We do not need to generate
+               # actual thumbnails to do parser testing, we only care about receiving
+               # a ThumbnailImage properly initialized.
+               global $wgMediaHandlers;
+               foreach ( $wgMediaHandlers as $type => $handler ) {
+                       $tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
+               }
+               // Vector images have to be handled slightly differently
+               $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
+
                $tmpHooks = $wgHooks;
                $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
                $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
@@ -135,67 +149,26 @@ class NewParserTest extends MediaWikiTestCase {
                RepoGroup::destroySingleton();
                FileBackendGroup::destroySingleton();
 
+               // Remove temporary pages from the link cache
+               LinkCache::singleton()->clear();
+
+               // Restore message cache (temporary pages and $wgUseDatabaseMessages)
+               MessageCache::destroyInstance();
+
                parent::tearDown();
        }
 
        function addDBData() {
                $this->tablesUsed[] = 'site_stats';
-               $this->tablesUsed[] = 'interwiki';
                # disabled for performance
                #$this->tablesUsed[] = 'image';
 
-               # Hack: insert a few Wikipedia in-project interwiki prefixes,
-               # for testing inter-language links
-               $this->db->insert( 'interwiki', array(
-                               array( 'iw_prefix' => 'wikipedia',
-                                       'iw_url' => 'http://en.wikipedia.org/wiki/$1',
-                                       'iw_api' => '',
-                                       'iw_wikiid' => '',
-                                       'iw_local' => 0 ),
-                               array( 'iw_prefix' => 'meatball',
-                                       'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
-                                       'iw_api' => '',
-                                       'iw_wikiid' => '',
-                                       'iw_local' => 0 ),
-                               array( 'iw_prefix' => 'zh',
-                                       'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
-                                       'iw_api' => '',
-                                       'iw_wikiid' => '',
-                                       'iw_local' => 1 ),
-                               array( 'iw_prefix' => 'es',
-                                       'iw_url' => 'http://es.wikipedia.org/wiki/$1',
-                                       'iw_api' => '',
-                                       'iw_wikiid' => '',
-                                       'iw_local' => 1 ),
-                               array( 'iw_prefix' => 'fr',
-                                       'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
-                                       'iw_api' => '',
-                                       'iw_wikiid' => '',
-                                       'iw_local' => 1 ),
-                               array( 'iw_prefix' => 'ru',
-                                       'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
-                                       '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 ),
                        __METHOD__
                );
 
-               # Reinitialise the LocalisationCache to match the database state
-               Language::getLocalisationCache()->unloadAll();
-
-               # Clear the message cache
-               MessageCache::singleton()->clear();
-
                $user = User::newFromId( 0 );
                LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
 
@@ -267,6 +240,20 @@ class NewParserTest extends MediaWikiTestCase {
                                $this->db->timestamp( '20010115123500' ), $user
                        );
                }
+               $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
+               if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
+                       $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', array(
+                                       'size'        => 12345,
+                                       'width'       => 200,
+                                       'height'      => 200,
+                                       'bits'        => 24,
+                                       'media_type'  => MEDIATYPE_DRAWING,
+                                       'mime'        => 'image/svg+xml',
+                                       'metadata'    => serialize( array() ),
+                                       'sha1'        => wfBaseConvert( '', 16, 36, 31 ),
+                                       'fileExists'  => true
+                       ), $this->db->timestamp( '20010115123500' ), $user );
+               }
        }
 
        //ParserTest setup/teardown functions
@@ -305,7 +292,10 @@ class NewParserTest extends MediaWikiTestCase {
                                $backend = self::$backendToUse;
                        }
                } else {
-                       $backend = new FSFileBackend( array(
+                       # Replace with a mock. We do not care about generating real
+                       # files on the filesystem, just need to expose the file
+                       # informations.
+                       $backend = new MockFileBackend( array(
                                'name' => 'local-backend',
                                'lockManager' => 'nullLockManager',
                                'containerPaths' => array(
@@ -369,6 +359,8 @@ class NewParserTest extends MediaWikiTestCase {
                }
 
                MagicWord::clearCache();
+
+               # The entries saved into RepoGroup cache with previous globals will be wrong.
                RepoGroup::destroySingleton();
                FileBackendGroup::destroySingleton();
 
@@ -378,9 +370,6 @@ class NewParserTest extends MediaWikiTestCase {
                # 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();
-               FileBackendGroup::destroySingleton();
                MessageCache::destroyInstance();
 
                return $context;
@@ -435,6 +424,19 @@ class NewParserTest extends MediaWikiTestCase {
                $backend->store( array(
                        'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
                ) );
+
+               // No helpful SVG file to copy, so make one ourselves
+               $tmpDir = wfTempDir();
+               $tempFsFile = new TempFSFile( "$tmpDir/Foobar.svg" );
+               $tempFsFile->autocollect(); // destroy file when $tempFsFile leaves scope
+               file_put_contents( "$tmpDir/Foobar.svg",
+                       '<?xml version="1.0" encoding="utf-8"?>' .
+                       '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><text>Foo</text></svg>' );
+
+               $backend->prepare( array( 'dir' => "$base/local-public/f/ff" ) );
+               $backend->quickStore( array(
+                       'src' => "$tmpDir/Foobar.svg", 'dst' => "$base/local-public/f/ff/Foobar.svg"
+               ) );
        }
 
        /**
@@ -447,9 +449,6 @@ class NewParserTest extends MediaWikiTestCase {
                foreach ( $this->savedGlobals as $var => $val ) {
                        $GLOBALS[$var] = $val;
                }
-
-               RepoGroup::destroySingleton();
-               LinkCache::singleton()->clear();
        }
 
        /**
@@ -460,6 +459,12 @@ class NewParserTest extends MediaWikiTestCase {
                        return;
                }
 
+               $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
+               if ( $backend instanceof MockFileBackend ) {
+                       # In memory backend, so dont bother cleaning them up.
+                       return;
+               }
+
                $base = $this->getBaseDir();
                // delete the files first, then the dirs.
                self::deleteFiles(
@@ -484,6 +489,14 @@ class NewParserTest extends MediaWikiTestCase {
 
                                "$base/local-public/0/09/Bad.jpg",
 
+                               "$base/local-public/f/ff/Foobar.svg",
+                               "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.jpg",
+                               "$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.jpg",
+                               "$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.jpg",
+                               "$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.jpg",
+                               "$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.jpg",
+                               "$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.jpg",
+
                                "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
                        )
                );
@@ -541,7 +554,7 @@ class NewParserTest extends MediaWikiTestCase {
 
                if ( !$this->isWikitextNS( NS_MAIN ) ) {
                        // parser tests frequently assume that the main namespace contains wikitext.
-                       // @todo: When setting up pages, force the content model. Only skip if
+                       // @todo When setting up pages, force the content model. Only skip if
                        //        $wgtContentModelUseDB is false.
                        $this->markTestSkipped( "Main namespace does not support wikitext,"
                                . "skipping parser test: $desc" );