test: rework interwiki lookup in parser tests
authorAntoine Musso <hashar@free.fr>
Mon, 20 May 2013 13:21:02 +0000 (15:21 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 17 Jun 2013 20:58:42 +0000 (20:58 +0000)
Some of our parser tests lookup interwikis.  This was originally done
(parser/parserTest.inc) by inserting a set of interwikis in the database
and was later lamely copy pasted in the PHPUnit wrapped test suite
(phpunit/includes/parser/NewParserTest.php).

Since that time, we had duplicate code and had the test hitting the
database to fetch interwiki.  Nowadays, we can trick the Interwiki
lookup class by using the InterwikiLoadPrefix hook, that let us skip
database lookup entirely (by having the hook returning false) and get
rid of the duplicate code.

The good old parserTests.php still pass the tests :-]

Change-Id: I36865e3890e08a05b8a6aaafa309a87556e235b9

tests/parser/parserTest.inc
tests/phpunit/includes/parser/NewParserTest.php

index 1c3ba1d..4efd708 100644 (file)
@@ -212,6 +212,61 @@ class ParserTest {
                        $wgStyleDirectory = "$IP/skins";
                }
 
+               self::setupInterwikis();
+       }
+
+       /**
+        * Insert hardcoded interwiki in the lookup table.
+        *
+        * This function insert a set of well known interwikis that are used in
+        * the parser tests. They can be considered has fixtures are injected in
+        * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
+        * Since we are not interested in looking up interwikis in the database,
+        * the hook completely replace the existing mechanism (hook returns false).
+        */
+       public static function setupInterwikis() {
+               # Hack: insert a few Wikipedia in-project interwiki prefixes,
+               # for testing inter-language links
+               Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
+                       static $testInterwikis = array(
+                               'wikipedia' => array(
+                                       'iw_url' => 'http://en.wikipedia.org/wiki/$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 0 ),
+                               'meatball' => array(
+                                       'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 0 ),
+                               'zh' => array(
+                                       'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 1 ),
+                               'es' => array(
+                                       'iw_url' => 'http://es.wikipedia.org/wiki/$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 1 ),
+                               'fr' => array(
+                                       'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 1 ),
+                               'ru' => array(
+                                       'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
+                                       'iw_api' => '',
+                                       'iw_wikiid' => '',
+                                       'iw_local' => 1 ),
+                       );
+                       if( array_key_exists( $prefix, $testInterwikis ) ) {
+                               $iwData = $testInterwikis[$prefix];
+                       }
+
+                       // We only want to rely on the above fixtures
+                       return false;
+               } );// hooks::register
        }
 
        public function setupRecorder( $options ) {
@@ -829,41 +884,6 @@ class ParserTest {
                                'user_name' => 'Anonymous' ) );
                }
 
-               # 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 ),
-               ) );
-
                # Update certain things in site_stats
                $this->db->insert( 'site_stats', array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ) );
 
index 6975939..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;
@@ -155,50 +160,9 @@ class NewParserTest extends MediaWikiTestCase {
 
        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 ),