In FileBackend:
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 0dc32f3..0a2f58b 100644 (file)
@@ -11,6 +11,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected $db;
        protected $oldTablePrefix;
        protected $useTemporaryTables = true;
+       protected $reuseDB = false;
+       protected $tablesUsed = array(); // tables with data
+
        private static $dbSetup = false;
 
        /**
@@ -41,8 +44,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
 
                if( $this->needsDB() ) {
-
                        global $wgDBprefix;
+                       
+                       $this->useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
+                       $this->reuseDB = $this->getCliArg('reuse-db');
 
                        $this->db = wfGetDB( DB_MASTER );
 
@@ -82,6 +87,30 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        function addDBData() {}
 
        private function addCoreDBData() {
+               if ( $this->db->getType() == 'oracle' ) {
+
+                       # Insert 0 user to prevent FK violations
+                       # Anonymous user
+                       $this->db->insert( 'user', array(
+                               'user_id'               => 0,
+                               'user_name'     => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
+
+                       # Insert 0 page to prevent FK violations
+                       # Blank page
+                       $this->db->insert( 'page', array(
+                               'page_id' => 0,
+                               'page_namespace' => 0,
+                               'page_title' => ' ',
+                               'page_restrictions' => NULL,
+                               'page_counter' => 0,
+                               'page_is_redirect' => 0,
+                               'page_is_new' => 0,
+                               'page_random' => 0,
+                               'page_touched' => $this->db->timestamp(),
+                               'page_latest' => 0,
+                               'page_len' => 0 ), __METHOD__, array( 'IGNORE' ) );
+
+               }
 
                User::resetIdByNameCache();
 
@@ -99,12 +128,12 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
 
                //Make 1 page with 1 revision
-               $article = new Article( Title::newFromText( 'UTPage' ) );
-               $article->doEdit( 'UTContent',
-                                                       'UTPageSummary',
-                                                       EDIT_NEW,
-                                                       false,
-                                                       User::newFromName( 'UTSysop' ) );
+               $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
+               $page->doEdit( 'UTContent',
+                                               'UTPageSummary',
+                                               EDIT_NEW,
+                                               false,
+                                               User::newFromName( 'UTSysop' ) );
        }
 
        private function initDB() {
@@ -113,34 +142,20 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        throw new MWException( 'Cannot run unit tests, the database prefix is already "unittest_"' );
                }
 
-               $dbClone = new CloneDatabase( $this->db, $this->listTables(), $this->dbPrefix() );
+               $tablesCloned = $this->listTables();
+               $dbClone = new CloneDatabase( $this->db, $tablesCloned, $this->dbPrefix() );
                $dbClone->useTemporaryTables( $this->useTemporaryTables );
-               $dbClone->cloneTableStructure();
+
+               if ( ( $this->db->getType() == 'oracle' || !$this->useTemporaryTables ) && $this->reuseDB ) {
+                       CloneDatabase::changePrefix( $this->dbPrefix() );
+                       $this->resetDB();
+                       return;
+               } else {
+                       $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' ) );
-
-                       # Insert 0 page to prevent FK violations
-                       # Blank page
-                       $this->db->insert( 'page', array(
-                               'page_id' => 0,
-                               'page_namespace' => 0,
-                               'page_title' => ' ',
-                               'page_restrictions' => NULL,
-                               'page_counter' => 0,
-                               'page_is_redirect' => 0,
-                               'page_is_new' => 0,
-                               'page_random' => 0,
-                               'page_touched' => $this->db->timestamp(),
-                               'page_latest' => 0,
-                               'page_len' => 0 ) );
-
                }
        }
 
@@ -149,55 +164,25 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        private function resetDB() {
                if( $this->db ) {
-                       foreach( $this->listTables() as $tbl ) {
-                               if( $tbl == 'interwiki' || $tbl == 'user' || $tbl == 'MWUSER' ) continue;
-                               if ( $this->db->getType() == 'oracle' ) 
-                                       $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
-                               else
+                       if ( $this->db->getType() == 'oracle' )  {
+                               if ( $this->useTemporaryTables ) {
+                                       wfGetLB()->closeAll();
+                                       $this->db = wfGetDB( DB_MASTER );
+                               } else {
+                                       foreach( $this->tablesUsed as $tbl ) {
+                                               if( $tbl == 'interwiki') continue;
+                                               $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
+                                       }
+                               }
+                       } else {
+                               foreach( $this->tablesUsed as $tbl ) {
+                                       if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
                                        $this->db->delete( $tbl, '*', __METHOD__ );
-                       }
-
-                       if ( $this->db->getType() == 'oracle' ) {
-                               # Insert 0 page to prevent FK violations
-                               # Blank page
-                               $this->db->insert( 'page', array(
-                                       'page_id' => 0,
-                                       'page_namespace' => 0,
-                                       'page_title' => ' ',
-                                       'page_restrictions' => NULL,
-                                       'page_counter' => 0,
-                                       'page_is_redirect' => 0,
-                                       'page_is_new' => 0,
-                                       'page_random' => 0,
-                                       'page_touched' => $this->db->timestamp(),
-                                       'page_latest' => 0,
-                                       'page_len' => 0 ) );
+                               }
                        }
                }
        }
 
-       protected function destroyDB() {
-               if ( $this->useTemporaryTables || is_null( $this->db ) ) {
-                       # Don't need to do anything
-                       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( 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',