Fix core DB data in unit testing
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index f3306be..d846b57 100644 (file)
@@ -161,7 +161,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
 
        /**
-        * @return boolean
+        * @return bool
         */
        private function oncePerClass() {
                // Remember current test class in the database connection,
@@ -247,6 +247,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
 
                DeferredUpdates::clearPendingUpdates();
+               ObjectCache::getMainWANInstance()->clearProcessCache();
 
                ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' );
        }
@@ -636,12 +637,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                if ( $user->idForName() == 0 ) {
                        $user->addToDatabase();
                        TestUser::setPasswordForUser( $user, 'UTSysopPassword' );
+                       $user->addGroup( 'sysop' );
+                       $user->addGroup( 'bureaucrat' );
                }
 
-               // Always set groups, because $this->resetDB() wipes them out
-               $user->addGroup( 'sysop' );
-               $user->addGroup( 'bureaucrat' );
-
                // Make 1 page with 1 revision
                $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
                if ( $page->getId() == 0 ) {
@@ -764,6 +763,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        return;
                }
 
+               self::$dbSetup = true;
+
                if ( !self::setupDatabaseWithTestPrefix( $db, $prefix ) ) {
                        return;
                }
@@ -773,8 +774,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                if ( $db->getType() == 'oracle' ) {
                        $db->query( 'BEGIN FILL_WIKI_INFO; END;' );
                }
-
-               self::$dbSetup = true;
        }
 
        /**
@@ -850,17 +849,29 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        private function resetDB( $db, $tablesUsed ) {
                if ( $db ) {
+                       $userTables = [ 'user', 'user_groups', 'user_properties' ];
+                       $coreDBDataTables = array_merge( $userTables, [ 'page', 'revision' ] );
+
+                       // If any of the user tables were marked as used, we should clear all of them.
+                       if ( array_intersect( $tablesUsed, $userTables ) ) {
+                               $tablesUsed = array_unique( array_merge( $tablesUsed, $userTables ) );
+
+                               // Totally clear User class in-process cache to avoid CAS errors
+                               TestingAccessWrapper::newFromClass( 'User' )
+                                       ->getInProcessCache()
+                                       ->clear();
+                       }
+
                        $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
                        foreach ( $tablesUsed as $tbl ) {
-                               // TODO: reset interwiki and user tables to their original content.
-                               if ( $tbl == 'interwiki' || $tbl == 'user' ) {
+                               // TODO: reset interwiki table to its original content.
+                               if ( $tbl == 'interwiki' ) {
                                        continue;
                                }
 
                                if ( $truncate ) {
                                        $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tbl ), __METHOD__ );
                                } else {
-
                                        $db->delete( $tbl, '*', __METHOD__ );
                                }
 
@@ -870,6 +881,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                        LinkCache::singleton()->clear();
                                }
                        }
+
+                       if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) {
+                               // Re-add core DB data that was deleted
+                               $this->addCoreDBData();
+                       }
                }
        }
 
@@ -1321,33 +1337,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                return $loaded;
        }
 
-       /**
-        * Asserts that an exception of the specified type occurs when running
-        * the provided code.
-        *
-        * @since 1.21
-        * @deprecated since 1.22 Use setExpectedException
-        *
-        * @param callable $code
-        * @param string $expected
-        * @param string $message
-        */
-       protected function assertException( $code, $expected = 'Exception', $message = '' ) {
-               $pokemons = null;
-
-               try {
-                       call_user_func( $code );
-               } catch ( Exception $pokemons ) {
-                       // Gotta Catch 'Em All!
-               }
-
-               if ( $message === '' ) {
-                       $message = 'An exception of type "' . $expected . '" should have been thrown';
-               }
-
-               $this->assertInstanceOf( $expected, $pokemons, $message );
-       }
-
        /**
         * Asserts that the given string is a valid HTML snippet.
         * Wraps the given string in the required top level tags and