From a754d43c066140c4ee41abeac978ec1fccf165ed Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Mon, 11 Apr 2016 13:36:38 -0400 Subject: [PATCH] Fix core DB data in unit testing Iec4ed4c8 made it so addCoreDBData() only gets called once. Which is nice, except for the fact that it means any test that puts 'user_groups' into $this->tablesUsed is going to cause UTSysop to no longer be a sysop, so any later test that expects it to be one is going to mysteriously fail. The fix is to make resetDB() actually clear out the 'user' table when requested, but then to re-call addCoreDBData() so UTSysop is still there. This might break extension tests if they're relying on the 'user' table never being cleared even if it's specified in $this->tablesUsed, but hopefully that's not often the case. Bug: T132411 Change-Id: If251739fd486544f54a0c07edcc24aeef0998342 Depends-On: I0546a4f18e0751d209b6b7c6cd42973f25828313 Depends-On: I4924ae941b3844b39dd3f44c6986c3bf29b0d62a --- tests/phpunit/MediaWikiTestCase.php | 29 ++++++++++++++++------ tests/phpunit/includes/api/ApiTestCase.php | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index a99b4b926b..d846b57251 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -637,12 +637,10 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { if ( $user->idForName() == 0 ) { $user->addToDatabase(); TestUser::setPasswordForUser( $user, 'UTSysopPassword' ); + $user->addGroup( 'sysop' ); + $user->addGroup( 'bureaucrat' ); } - // Always set groups, because $this->resetDB() wipes them out - $user->addGroup( 'sysop' ); - $user->addGroup( 'bureaucrat' ); - // Make 1 page with 1 revision $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); if ( $page->getId() == 0 ) { @@ -851,17 +849,29 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { */ private function resetDB( $db, $tablesUsed ) { if ( $db ) { + $userTables = [ 'user', 'user_groups', 'user_properties' ]; + $coreDBDataTables = array_merge( $userTables, [ 'page', 'revision' ] ); + + // If any of the user tables were marked as used, we should clear all of them. + if ( array_intersect( $tablesUsed, $userTables ) ) { + $tablesUsed = array_unique( array_merge( $tablesUsed, $userTables ) ); + + // Totally clear User class in-process cache to avoid CAS errors + TestingAccessWrapper::newFromClass( 'User' ) + ->getInProcessCache() + ->clear(); + } + $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] ); foreach ( $tablesUsed as $tbl ) { - // TODO: reset interwiki and user tables to their original content. - if ( $tbl == 'interwiki' || $tbl == 'user' ) { + // TODO: reset interwiki table to its original content. + if ( $tbl == 'interwiki' ) { continue; } if ( $truncate ) { $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tbl ), __METHOD__ ); } else { - $db->delete( $tbl, '*', __METHOD__ ); } @@ -871,6 +881,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { LinkCache::singleton()->clear(); } } + + if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) { + // Re-add core DB data that was deleted + $this->addCoreDBData(); + } } } diff --git a/tests/phpunit/includes/api/ApiTestCase.php b/tests/phpunit/includes/api/ApiTestCase.php index 62b1115e7d..246ea3d235 100644 --- a/tests/phpunit/includes/api/ApiTestCase.php +++ b/tests/phpunit/includes/api/ApiTestCase.php @@ -37,7 +37,7 @@ abstract class ApiTestCase extends MediaWikiLangTestCase { ]; $this->setMwGlobals( [ - 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ), + 'wgAuth' => new AuthPlugin, 'wgRequest' => new FauxRequest( [] ), 'wgUser' => self::$users['sysop']->user, ] ); -- 2.20.1