X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=tests%2Fphpunit%2FMediaWikiTestCase.php;h=d325476f893db536fb0d3a7606de5ae3c0717587;hb=46d7645db2c4eab228553a4e40f4147987861667;hp=310ba2935a44021c1dcf6465439793c16fac5561;hpb=a0b490bbe7a87b54de49f075c59befa8232b2237;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 310ba2935a..d325476f89 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -8,6 +8,7 @@ use Psr\Log\LoggerInterface; use Wikimedia\Rdbms\IDatabase; use Wikimedia\Rdbms\IMaintainableDatabase; use Wikimedia\Rdbms\Database; +use Wikimedia\Rdbms\IResultWrapper; use Wikimedia\Rdbms\LBFactory; use Wikimedia\TestingAccessWrapper; @@ -947,7 +948,9 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { * @return MediaWikiServices * @throws MWException */ - protected function overrideMwServices( Config $configOverrides = null, array $services = [] ) { + protected static function overrideMwServices( + Config $configOverrides = null, array $services = [] + ) { if ( !$configOverrides ) { $configOverrides = new HashConfig(); } @@ -1246,7 +1249,7 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { $user ); // an edit always attempt to purge backlink links such as history - // pages. That is unneccessary. + // pages. That is unnecessary. JobQueueGroup::singleton()->get( 'htmlCacheUpdate' )->delete(); // WikiPages::doEditUpdates randomly adds RC purges JobQueueGroup::singleton()->get( 'recentChangesUpdate' )->delete(); @@ -1289,52 +1292,6 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { self::$dbSetup = false; } - /** - * Prepares the given database connection for usage in the context of usage tests. - * This sets up clones database tables and changes the table prefix as appropriate. - * If the database connection already has cloned tables, calling this method has no - * effect. The tables are not re-cloned or reset in that case. - * - * @param IMaintainableDatabase $db - */ - protected function prepareConnectionForTesting( IMaintainableDatabase $db ) { - if ( !self::$dbSetup ) { - throw new LogicException( - 'Cannot use prepareConnectionForTesting()' - . ' if the test case is not defined to use the database!' - ); - } - - if ( isset( $db->_originalTablePrefix ) ) { - // The DB connection was already prepared for testing. - return; - } - - $testPrefix = self::getTestPrefixFor( $db ); - $oldPrefix = $db->tablePrefix(); - - $tablesCloned = self::listTables( $db ); - - if ( $oldPrefix === $testPrefix ) { - // The database connection already has the test prefix, but presumably not - // the cloned tables. This is the typical case, since the LBFactory will - // have the prefix set during testing, but LoadBalancers will still return - // connections that don't have the cloned table structure. - $oldPrefix = self::$oldTablePrefix; - } - - $dbClone = new CloneDatabase( $db, $tablesCloned, $testPrefix, $oldPrefix ); - $dbClone->useTemporaryTables( self::$useTemporaryTables ); - - $db->_originalTablePrefix = $oldPrefix; - - if ( ( $db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) { - throw new LogicException( 'Cannot clone database tables' ); - } else { - $dbClone->cloneTableStructure(); - } - } - /** * Setups a database with cloned tables using the given prefix. * @@ -1730,10 +1687,14 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { */ private function resetDB( $db, $tablesUsed ) { if ( $db ) { + // NOTE: Do not reset the slot_roles and content_models tables, but let them + // leak across tests. Resetting them would require to reset all NamedTableStore + // instances for these tables, of which there may be several beyond the ones + // known to MediaWikiServices. See T202641. $userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ]; $pageTables = [ 'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive', - 'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles', + 'revision_actor_temp', 'slots', 'content', ]; $coreDBDataTables = array_merge( $userTables, $pageTables ); @@ -1759,41 +1720,8 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { } } - $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] ); foreach ( $tablesUsed as $tbl ) { - if ( !$db->tableExists( $tbl ) ) { - continue; - } - - if ( $truncate ) { - $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tbl ), __METHOD__ ); - } else { - $db->delete( $tbl, '*', __METHOD__ ); - } - - if ( in_array( $db->getType(), [ 'postgres', 'sqlite' ], true ) ) { - // Reset the table's sequence too. - $db->resetSequenceForTable( $tbl, __METHOD__ ); - } - - if ( $tbl === 'interwiki' ) { - if ( !$this->interwikiTable ) { - // @todo We should probably throw here, but this causes test failures that I - // can't figure out, so for now we silently continue. - continue; - } - $db->insert( - 'interwiki', - array_values( array_map( 'get_object_vars', iterator_to_array( $this->interwikiTable ) ) ), - __METHOD__ - ); - } - - if ( $tbl === 'page' ) { - // Forget about the pages since they don't - // exist in the DB. - MediaWikiServices::getInstance()->getLinkCache()->clear(); - } + $this->truncateTable( $tbl, $db ); } if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) { @@ -1803,6 +1731,56 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase { } } + /** + * Empties the given table and resets any auto-increment counters. + * Will also purge caches associated with some well known tables. + * If the table is not know, this method just returns. + * + * @param string $tableName + * @param IDatabase|null $db + */ + protected function truncateTable( $tableName, IDatabase $db = null ) { + if ( !$db ) { + $db = $this->db; + } + + if ( !$db->tableExists( $tableName ) ) { + return; + } + + $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] ); + + if ( $truncate ) { + $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tableName ), __METHOD__ ); + } else { + $db->delete( $tableName, '*', __METHOD__ ); + } + + if ( in_array( $db->getType(), [ 'postgres', 'sqlite' ], true ) ) { + // Reset the table's sequence too. + $db->resetSequenceForTable( $tableName, __METHOD__ ); + } + + if ( $tableName === 'interwiki' ) { + if ( !$this->interwikiTable ) { + // @todo We should probably throw here, but this causes test failures that I + // can't figure out, so for now we silently continue. + return; + } + $db->insert( + 'interwiki', + array_values( array_map( 'get_object_vars', iterator_to_array( $this->interwikiTable ) ) ), + __METHOD__ + ); + } + + if ( $tableName === 'page' ) { + // Forget about the pages since they don't + // exist in the DB. + MediaWikiServices::getInstance()->getLinkCache()->clear(); + } + } + private static function unprefixTable( &$tableName, $ind, $prefix ) { $tableName = substr( $tableName, strlen( $prefix ) ); }