/** * Set up temporary DB tables. * * For best performance, call this once only for all tests. However, it can * be called at the start of each test if more isolation is desired. * * @todo: This is basically an unrefactored copy of * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow. * * Do not call this function from a MediaWikiTestCase subclass, since * MediaWikiTestCase does its own DB setup. Instead use setDatabase(). * * @see staticSetup() for more information about setup/teardown * * @param ScopedCallback|null $nextTeardown The next teardown object * @return ScopedCallback The teardown object */ public function setupDatabase($nextTeardown = null) { global $wgDBprefix; $this->db = wfGetDB(DB_MASTER); $dbType = $this->db->getType(); if ($dbType == 'oracle') { $suspiciousPrefixes = ['pt_', MediaWikiTestCase::ORA_DB_PREFIX]; } else { $suspiciousPrefixes = ['parsertest_', MediaWikiTestCase::DB_PREFIX]; } if (in_array($wgDBprefix, $suspiciousPrefixes)) { throw new MWException("\$wgDBprefix={$wgDBprefix} suggests DB setup is already done"); } $teardown = []; $teardown[] = $this->markSetupDone('setupDatabase'); # CREATE TEMPORARY TABLE breaks if there is more than one server if (wfGetLB()->getServerCount() != 1) { $this->useTemporaryTables = false; } $temporary = $this->useTemporaryTables || $dbType == 'postgres'; $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_'; $this->dbClone = new CloneDatabase($this->db, $this->listTables(), $prefix); $this->dbClone->useTemporaryTables($temporary); $this->dbClone->cloneTableStructure(); if ($dbType == 'oracle') { $this->db->query('BEGIN FILL_WIKI_INFO; END;'); # Insert 0 user to prevent FK violations # Anonymous user $this->db->insert('user', ['user_id' => 0, 'user_name' => 'Anonymous']); } $teardown[] = function () { $this->teardownDatabase(); }; // Wipe some DB query result caches on setup and teardown $reset = function () { LinkCache::singleton()->clear(); // Clear the message cache MessageCache::singleton()->clear(); }; $reset(); $teardown[] = $reset; return $this->createTeardownObject($teardown, $nextTeardown); }
/** * Creates an empty skeleton of the wiki database by cloning its structure * to equivalent tables using the given $prefix. Then sets MediaWiki to * use the new set of tables (aka schema) instead of the original set. * * This is used to generate a dummy table set, typically consisting of temporary * tables, that will be used by tests instead of the original wiki database tables. * * @since 1.21 * * @note the original table prefix is stored in self::$oldTablePrefix. This is used * by teardownTestDB() to return the wiki to using the original table set. * * @note this method only works when first called. Subsequent calls have no effect, * even if using different parameters. * * @param DatabaseBase $db The database connection * @param string $prefix The prefix to use for the new table set (aka schema). * * @throws MWException If the database table prefix is already $prefix */ public static function setupTestDB(DatabaseBase $db, $prefix) { global $wgDBprefix; if ($wgDBprefix === $prefix) { throw new MWException('Cannot run unit tests, the database prefix is already "' . $prefix . '"'); } if (self::$dbSetup) { return; } $tablesCloned = self::listTables($db); $dbClone = new CloneDatabase($db, $tablesCloned, $prefix); $dbClone->useTemporaryTables(self::$useTemporaryTables); self::$dbSetup = true; self::$oldTablePrefix = $wgDBprefix; if (($db->getType() == 'oracle' || !self::$useTemporaryTables) && self::$reuseDB) { CloneDatabase::changePrefix($prefix); return; } else { $dbClone->cloneTableStructure(); } if ($db->getType() == 'oracle') { $db->query('BEGIN FILL_WIKI_INFO; END;'); } }
private function initDB() { global $wgDBprefix; if ($wgDBprefix === $this->dbPrefix()) { throw new MWException('Cannot run unit tests, the database prefix is already "unittest_"'); } $tablesCloned = $this->listTables(); $dbClone = new CloneDatabase($this->db, $tablesCloned, $this->dbPrefix()); $dbClone->useTemporaryTables($this->useTemporaryTables); 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;'); } }
/** * Setups a database with the given prefix. * * If reuseDB is true and certain conditions apply, it will just change the prefix. * Otherwise, it will clone the tables and change the prefix. * * Clones all tables in the given database (whatever database that connection has * open), to versions with the test prefix. * * @param Database $db Database to use * @param string $prefix Prefix to use for test tables * @return bool True if tables were cloned, false if only the prefix was changed */ protected static function setupDatabaseWithTestPrefix(Database $db, $prefix) { $tablesCloned = self::listTables($db); $dbClone = new CloneDatabase($db, $tablesCloned, $prefix); $dbClone->useTemporaryTables(self::$useTemporaryTables); if (($db->getType() == 'oracle' || !self::$useTemporaryTables) && self::$reuseDB) { CloneDatabase::changePrefix($prefix); return false; } else { $dbClone->cloneTableStructure(); return true; } }
private function initDB() { global $wgDBprefix; if ($wgDBprefix === $this->dbPrefix()) { throw new MWException('Cannot run unit tests, the database prefix is already "unittest_"'); } $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')); } }