return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
}
+ /**
+ * Returns a WikiPage representing an existing page.
+ *
+ * @since 1.32
+ *
+ * @param Title|string|null $title
+ * @return WikiPage
+ * @throws MWException
+ */
+ protected function getExistingTestPage( $title = null ) {
+ $title = ( $title === null ) ? 'UTPage' : $title;
+ $title = is_string( $title ) ? Title::newFromText( $title ) : $title;
+ $page = WikiPage::factory( $title );
+
+ if ( !$page->exists() ) {
+ $user = self::getTestSysop()->getUser();
+ $page->doEditContent(
+ new WikitextContent( 'UTContent' ),
+ 'UTPageSummary',
+ EDIT_NEW | EDIT_SUPPRESS_RC,
+ false,
+ $user
+ );
+ }
+
+ return $page;
+ }
+
+ /**
+ * Returns a WikiPage representing a non-existing page.
+ *
+ * @since 1.32
+ *
+ * @param Title|string|null $title
+ * @return WikiPage
+ * @throws MWException
+ */
+ protected function getNonexistingTestPage( $title = null ) {
+ $title = ( $title === null ) ? 'UTPage-' . rand( 0, 100000 ) : $title;
+ $title = is_string( $title ) ? Title::newFromText( $title ) : $title;
+ $page = WikiPage::factory( $title );
+
+ if ( $page->exists() ) {
+ $page->doDeleteArticle( 'Testing' );
+ }
+
+ return $page;
+ }
+
/**
* Prepare service configuration for unit testing.
*
if ( !self::$dbSetup ) {
$this->setupAllTestDBs();
$this->addCoreDBData();
-
- if ( ( $this->db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
- $this->resetDB( $this->db, $this->tablesUsed );
- }
}
// TODO: the DB setup should be done in setUpBeforeClass(), so the test DB
// This would also remove the need for the HACK that is oncePerClass().
if ( $this->oncePerClass() ) {
$this->setUpSchema( $this->db );
+ $this->resetDB( $this->db, $this->tablesUsed );
$this->addDBDataOnce();
}
* Should be called from addDBData().
*
* @since 1.25 ($namespace in 1.28)
- * @param string|title $pageName Page name or title
+ * @param string|Title $pageName Page name or title
* @param string $text Page's content
* @param int $namespace Namespace id (name cannot already contain namespace)
* @return array Title object and page id
public function addDBData() {
}
- private function addCoreDBData() {
+ /**
+ * @since 1.32
+ */
+ protected function addCoreDBData() {
if ( $this->db->getType() == 'oracle' ) {
# Insert 0 user to prevent FK violations
# Anonymous user
/**
* @throws LogicException if the given database connection is not a set up to use
* mock tables.
+ *
+ * @since 1.31 this is no longer private.
*/
- private function ensureMockDatabaseConnection( IDatabase $db ) {
+ protected function ensureMockDatabaseConnection( IDatabase $db ) {
if ( $db->tablePrefix() !== $this->dbPrefix() ) {
throw new LogicException(
'Trying to delete mock tables, but table prefix does not indicate a mock database.'
}
/**
- * Undoes the dpecified schema overrides..
+ * Undoes the specified schema overrides..
* Called once per test class, just before addDataOnce().
*
* @param IMaintainableDatabase $db
$this->ensureMockDatabaseConnection( $db );
$oldOverrides = $oldOverrides + self::$schemaOverrideDefaults;
- $originalTables = $this->listOriginalTables( $db );
+ $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
// Drop tables that need to be restored or removed.
$tablesToDrop = array_merge( $oldOverrides['create'], $oldOverrides['alter'] );
$this->ensureMockDatabaseConnection( $db );
// Drop the tables that will be created by the schema scripts.
- $originalTables = $this->listOriginalTables( $db );
+ $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
$tablesToDrop = array_intersect( $originalTables, $overrides['create'] );
if ( $tablesToDrop ) {
if ( $tbl === 'page' ) {
// Forget about the pages since they don't
// exist in the DB.
- LinkCache::singleton()->clear();
+ MediaWikiServices::getInstance()->getLinkCache()->clear();
}
}
}
* Lists all tables in the live database schema.
*
* @param IMaintainableDatabase $db
+ * @param string $prefix Either 'prefixed' or 'unprefixed'
* @return array
*/
- private function listOriginalTables( IMaintainableDatabase $db ) {
+ private function listOriginalTables( IMaintainableDatabase $db, $prefix = 'prefixed' ) {
if ( !isset( $db->_originalTablePrefix ) ) {
throw new LogicException( 'No original table prefix know, cannot list tables!' );
}
$originalTables = $db->listTables( $db->_originalTablePrefix, __METHOD__ );
+ if ( $prefix === 'unprefixed' ) {
+ $originalPrefixRegex = '/^' . preg_quote( $db->_originalTablePrefix ) . '/';
+ $originalTables = array_map(
+ function ( $pt ) use ( $originalPrefixRegex ) {
+ return preg_replace( $originalPrefixRegex, '', $pt );
+ },
+ $originalTables
+ );
+ }
+
return $originalTables;
}
throw new LogicException( 'No original table prefix know, cannot restore tables!' );
}
- $originalTables = $this->listOriginalTables( $db );
+ $originalTables = $this->listOriginalTables( $db, 'unprefixed' );
$tables = array_intersect( $tables, $originalTables );
$dbClone = new CloneDatabase( $db, $tables, $db->tablePrefix(), $db->_originalTablePrefix );
private function resetDB( $db, $tablesUsed ) {
if ( $db ) {
$userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ];
- $pageTables = [ 'page', 'revision', 'ip_changes', 'revision_comment_temp',
- 'revision_actor_temp', 'comment', 'archive' ];
+ $pageTables = [
+ 'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive',
+ 'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles',
+ ];
$coreDBDataTables = array_merge( $userTables, $pageTables );
// If any of the user or page tables were marked as used, we should clear all of them.
$tablesUsed = array_unique( array_merge( $tablesUsed, $pageTables ) );
}
+ // Postgres, Oracle, and MSSQL all use mwuser/pagecontent
+ // instead of user/text. But Postgres does not remap the
+ // table name in tableExists(), so we mark the real table
+ // names as being used.
+ if ( $db->getType() === 'postgres' ) {
+ if ( in_array( 'user', $tablesUsed ) ) {
+ $tablesUsed[] = 'mwuser';
+ }
+ if ( in_array( 'text', $tablesUsed ) ) {
+ $tablesUsed[] = 'pagecontent';
+ }
+ }
+
$truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
foreach ( $tablesUsed as $tbl ) {
// TODO: reset interwiki table to its original content.
if ( $tbl === 'page' ) {
// Forget about the pages since they don't
// exist in the DB.
- LinkCache::singleton()->clear();
+ MediaWikiServices::getInstance()->getLinkCache()->clear();
}
}