w/s changes.
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 3363e3f..a88c357 100644 (file)
@@ -18,7 +18,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        const DB_PREFIX = 'unittest_';
        const ORA_DB_PREFIX = 'ut_';
-       
+
        protected $supportedDBs = array(
                'mysql',
                'sqlite',
@@ -31,7 +31,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->backupGlobals = false;
                $this->backupStaticAttributes = false;
        }
-       
+
        function run( PHPUnit_Framework_TestResult $result = NULL ) {
                /* Some functions require some kind of caching, and will end up using the db,
                 * which we can't allow, as that would open a new connection for mysql.
@@ -40,13 +40,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
 
                if( $this->needsDB() ) {
-               
+
                        global $wgDBprefix;
-                       
+
                        $this->db = wfGetDB( DB_MASTER );
-                       
+
                        $this->checkDbIsSupported();
-                       
+
                        $this->oldTablePrefix = $wgDBprefix;
 
                        if( !self::$dbSetup ) {
@@ -54,26 +54,21 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                self::$dbSetup = true;
                        }
 
-                       $this->resetDB();
                        $this->addCoreDBData();
                        $this->addDBData();
-                       
+
                        parent::run( $result );
+
+                       $this->resetDB();
                } else {
                        parent::run( $result );
                }
        }
-       
-       function __destruct() {
-               if( $this->needsDB() ) {
-                       $this->destroyDB();
-               }
-       }
 
        function dbPrefix() {
                return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
        }
-       
+
        function needsDB() {
                $rc = new ReflectionClass( $this );
                return strpos( $rc->getDocComment(), '@group Database' ) !== false;
@@ -84,14 +79,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * implement this method and do so
         */
        function addDBData() {}
-       
+
        private function addCoreDBData() {
 
                User::resetIdByNameCache();
 
                //Make sysop user
                $user = User::newFromName( 'UTSysop' );
-               
+
                if ( $user->idForName() == 0 ) {
                        $user->addToDatabase();
                        $user->setPassword( 'UTSysopPassword' );
@@ -101,7 +96,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        $user->saveSettings();
                }
 
-               
+
                //Make 1 page with 1 revision
                $article = new Article( Title::newFromText( 'UTPage' ) );
                $article->doEdit( 'UTContent',
@@ -110,7 +105,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                                                        false,
                                                        User::newFromName( 'UTSysop' ) );
        }
-       
+
        private function initDB() {
                global $wgDBprefix;
                if ( $wgDBprefix === $this->dbPrefix() ) {
@@ -120,6 +115,16 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
                $dbClone->useTemporaryTables( $this->useTemporaryTables );
                $dbClone->cloneTableStructure();
+
+               if ( $this->db->getType() == 'oracle' ) {
+                       $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
+
+                       # Insert 0 user to prevent FK violations
+                       # Anonymous user
+                       $this->db->insert( 'user', array(
+                               'user_id'               => 0,
+                               'user_name'     => 'Anonymous' ) );
+               }
        }
 
        /**
@@ -128,49 +133,34 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private function resetDB() {
                if( $this->db ) {
                        foreach( $this->listTables() as $tbl ) {
+                               if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
                                $this->db->delete( $tbl, '*', __METHOD__ );
                        }
-
-                       if ( $this->db->getType() == 'oracle' ) {
-                               $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
-
-                               # Insert 0 user to prevent FK violations
-                               # Anonymous user
-                               $this->db->insert( 'user', array(
-                                       'user_id'               => 0,
-                                       'user_name'     => 'Anonymous' ) );
-                       }
                }
        }
-       
+
        protected function destroyDB() {
-               
-               if ( $this->useTemporaryTables ) {
+               if ( $this->useTemporaryTables || is_null( $this->db ) ) {
                        # Don't need to do anything
-                       //return;
-                       //Temporary tables seem to be broken ATM, delete anyway
-               }
-               
-               if( is_null( $this->db ) ) {
                        return;
                }
-               
+
                $tables = $this->db->listTables( $this->dbPrefix(), __METHOD__ );
-               
+
                foreach ( $tables as $table ) {
                        try {
                                $sql = $this->db->getType() == 'oracle' ? "DROP TABLE $table CASCADE CONSTRAINTS PURGE" : "DROP TABLE `$table`";
                                $this->db->query( $sql, __METHOD__ );
-                       } catch( Exception $e ) {
-                       }
+                       } catch( MWException $mwe ) {}
                }
-               
+
                if ( $this->db->getType() == 'oracle' )
                        $this->db->query( 'BEGIN FILL_WIKI_INFO; END;', __METHOD__ );
-               
+
                CloneDatabase::changePrefix( $this->oldTablePrefix );
        }
 
+
        function __call( $func, $args ) {
                static $compatibility = array(
                        'assertInternalType' => 'assertType',
@@ -192,18 +182,25 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        private function assertEmpty2( $value, $msg ) {
                return $this->assertTrue( $value == '', $msg );
        }
-       
+
        static private function unprefixTable( $tableName ) {
                global $wgDBprefix;
                return substr( $tableName, strlen( $wgDBprefix ) );
        }
 
+       static private function isNotUnittest( $table ) {
+               return strpos( $table, 'unittest_' ) !== 0;
+       }
+
        protected function listTables() {
                global $wgDBprefix;
-               
+
                $tables = $this->db->listTables( $wgDBprefix, __METHOD__ );
                $tables = array_map( array( __CLASS__, 'unprefixTable' ), $tables );
 
+               // Don't duplicate test tables from the previous fataled run
+               $tables = array_filter( $tables, array( __CLASS__, 'isNotUnittest' ) );
+
                if ( $this->db->getType() == 'sqlite' ) {
                        $tables = array_flip( $tables );
                        // these are subtables of searchindex and don't need to be duped/dropped separately
@@ -213,27 +210,26 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        $tables = array_flip( $tables );
                }
                return $tables;
-               
        }
-       
+
        protected function checkDbIsSupported() {
                if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
                        throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
                }
        }
-       
+
        public function getCliArg( $offset ) {
-       
+
                if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
                        return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
                }
-               
+
        }
-       
+
        public function setCliArg( $offset, $value ) {
-       
+
                MediaWikiPHPUnitCommand::$additionalOptions[$offset] = $value;
-               
+
        }
 }