Merge "Reset interwiki table between tests"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 2 Aug 2018 02:14:50 +0000 (02:14 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 2 Aug 2018 02:14:50 +0000 (02:14 +0000)
tests/phpunit/MediaWikiTestCase.php

index 382bf50..af6acad 100644 (file)
@@ -97,6 +97,12 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         */
        private $mwGlobalsToUnset = [];
 
+       /**
+        * Holds original contents of interwiki table
+        * @var IResultWrapper
+        */
+       private $interwikiTable = null;
+
        /**
         * Holds original loggers which have been replaced by setLogger()
         * @var LoggerInterface[]
@@ -563,6 +569,12 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                        }
                }
 
+               // Store contents of interwiki table in case it changes.  Unfortunately, we seem to have no
+               // way to do this only when needed, because tablesUsed can be changed mid-test.
+               if ( $this->db ) {
+                       $this->interwikiTable = $this->db->select( 'interwiki', '*', '', __METHOD__ );
+               }
+
                // Reset all caches between tests.
                $this->doLightweightServiceReset();
 
@@ -1707,11 +1719,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
 
                        $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
                        foreach ( $tablesUsed as $tbl ) {
-                               // TODO: reset interwiki table to its original content.
-                               if ( $tbl == 'interwiki' ) {
-                                       continue;
-                               }
-
                                if ( !$db->tableExists( $tbl ) ) {
                                        continue;
                                }
@@ -1727,6 +1734,19 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                                        $db->resetSequenceForTable( $tbl, __METHOD__ );
                                }
 
+                               if ( $tbl === 'interwiki' ) {
+                                       if ( !$this->interwikiTable ) {
+                                               // @todo We should probably throw here, but this causes test failures that I
+                                               // can't figure out, so for now we silently continue.
+                                               continue;
+                                       }
+                                       $db->insert(
+                                               'interwiki',
+                                               array_map( 'get_object_vars', iterator_to_array( $this->interwikiTable ) ),
+                                               __METHOD__
+                                       );
+                               }
+
                                if ( $tbl === 'page' ) {
                                        // Forget about the pages since they don't
                                        // exist in the DB.