X-Git-Url: http://git.cyclocoop.org/%28?a=blobdiff_plain;f=tests%2Fphpunit%2FMediaWikiTestCase.php;h=27f1454dca2472bc0d06ebedf9bfabd67131641d;hb=f7429252f85c5835b291def55fc04b8196c1bb39;hp=9672cdc3cdc06357142d449d80080cb71451227d;hpb=5fd224c00b940f75c1f83e56b99961be94bde70a;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/MediaWikiTestCase.php b/tests/phpunit/MediaWikiTestCase.php index 9672cdc3cd..27f1454dca 100644 --- a/tests/phpunit/MediaWikiTestCase.php +++ b/tests/phpunit/MediaWikiTestCase.php @@ -127,6 +127,42 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { self::prepareServices( new GlobalVarConfig() ); } + /** + * Convenience method for getting an immutable test user + * + * @since 1.28 + * + * @param string[] $groups Groups the test user should be in. + * @return TestUser + */ + public static function getTestUser( $groups = [] ) { + return TestUserRegistry::getImmutableTestUser( $groups ); + } + + /** + * Convenience method for getting a mutable test user + * + * @since 1.28 + * + * @param string[] $groups Groups the test user should be added in. + * @return TestUser + */ + public static function getMutableTestUser( $groups = [] ) { + return TestUserRegistry::getMutableTestUser( __CLASS__, $groups ); + } + + /** + * Convenience method for getting an immutable admin test user + * + * @since 1.28 + * + * @param string[] $groups Groups the test user should be added to. + * @return TestUser + */ + public static function getTestSysop() { + return self::getTestUser( [ 'sysop', 'bureaucrat' ] ); + } + /** * Prepare service configuration for unit testing. * @@ -305,7 +341,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { // TODO: move global state into MediaWikiServices RequestContext::resetMain(); - MediaHandler::resetCache(); if ( session_id() !== '' ) { session_write_close(); session_id( '' ); @@ -321,7 +356,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { $needsResetDB = false; - if ( $this->needsDB() ) { + if ( !self::$dbSetup || $this->needsDB() ) { // set up a DB connection for this test to use self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' ); @@ -494,7 +529,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { // TODO: move global state into MediaWikiServices RequestContext::resetMain(); - MediaHandler::resetCache(); if ( session_id() !== '' ) { session_write_close(); session_id( '' ); @@ -883,7 +917,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) { $title = Title::newFromText( $pageName, 0 ); - $user = User::newFromName( 'UTSysop' ); + $user = static::getTestSysop()->getUser(); $comment = __METHOD__ . ': Sample page for unit test.'; // Avoid memory leak...? @@ -934,36 +968,33 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { # Insert 0 user to prevent FK violations # Anonymous user - $this->db->insert( 'user', [ - 'user_id' => 0, - 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] ); + if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) { + $this->db->insert( 'user', [ + 'user_id' => 0, + 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] ); + } # Insert 0 page to prevent FK violations # Blank page - $this->db->insert( 'page', [ - 'page_id' => 0, - 'page_namespace' => 0, - 'page_title' => ' ', - 'page_restrictions' => null, - 'page_is_redirect' => 0, - 'page_is_new' => 0, - 'page_random' => 0, - 'page_touched' => $this->db->timestamp(), - 'page_latest' => 0, - 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] ); + if ( !$this->db->selectField( 'page', '1', [ 'page_id' => 0 ] ) ) { + $this->db->insert( 'page', [ + 'page_id' => 0, + 'page_namespace' => 0, + 'page_title' => ' ', + 'page_restrictions' => null, + 'page_is_redirect' => 0, + 'page_is_new' => 0, + 'page_random' => 0, + 'page_touched' => $this->db->timestamp(), + 'page_latest' => 0, + 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] ); + } } User::resetIdByNameCache(); // Make sysop user - $user = User::newFromName( 'UTSysop' ); - - if ( $user->idForName() == 0 ) { - $user->addToDatabase(); - TestUser::setPasswordForUser( $user, 'UTSysopPassword' ); - $user->addGroup( 'sysop' ); - $user->addGroup( 'bureaucrat' ); - } + $user = static::getTestSysop()->getUser(); // Make 1 page with 1 revision $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); @@ -1182,11 +1213,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { // 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(); + TestUserRegistry::clear(); } $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] ); @@ -1621,32 +1648,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { } } - /** - * Check whether we have the 'gzip' commandline utility, will skip - * the test whenever "gzip -V" fails. - * - * Result is cached at the process level. - * - * @return bool - * - * @since 1.21 - */ - protected function checkHasGzip() { - static $haveGzip; - - if ( $haveGzip === null ) { - $retval = null; - wfShellExec( 'gzip -V', $retval ); - $haveGzip = ( $retval === 0 ); - } - - if ( !$haveGzip ) { - $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" ); - } - - return $haveGzip; - } - /** * Check if $extName is a loaded PHP extension, will skip the * test whenever it is not loaded. @@ -1772,4 +1773,15 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase { return $buffer; } + /** + * Create a temporary hook handler which will be reset by tearDown. + * This replaces other handlers for the same hook. + * @param string $hookName Hook name + * @param mixed $handler Value suitable for a hook handler + * @since 1.28 + */ + protected function setTemporaryHook( $hookName, $handler ) { + $this->mergeMwGlobalArrayValue( 'wgHooks', [ $hookName => [ $handler ] ] ); + } + }