* touch.
*/
private function doSetMwGlobals( $pairs, $value = null ) {
- $this->stashMwGlobals( array_keys( $pairs ) );
+ $this->doStashMwGlobals( array_keys( $pairs ) );
foreach ( $pairs as $key => $value ) {
$GLOBALS[$key] = $value;
* call overrideMwServices().
*
* @since 1.23
+ * @deprecated since 1.32, use setMwGlobals() and don't alter globals directly
*/
protected function stashMwGlobals( $globalKeys ) {
+ wfDeprecated( __METHOD__, '1.32' );
+ $this->doStashMwGlobals( $globalKeys );
+ }
+
+ private function doStashMwGlobals( $globalKeys ) {
if ( is_string( $globalKeys ) ) {
$globalKeys = [ $globalKeys ];
}
public function setGroupPermissions( $newPerms, $newKey = null, $newValue = null ) {
global $wgGroupPermissions;
- $this->stashMwGlobals( 'wgGroupPermissions' );
-
if ( is_string( $newPerms ) ) {
$newPerms = [ $newPerms => [ $newKey => $newValue ] ];
}
+ $newPermissions = $wgGroupPermissions;
foreach ( $newPerms as $group => $permissions ) {
foreach ( $permissions as $key => $value ) {
- $wgGroupPermissions[$group][$key] = $value;
+ $newPermissions[$group][$key] = $value;
}
}
+
+ $this->setMwGlobals( 'wgGroupPermissions', $newPermissions );
}
/**
// @todo File moving
public function testPingLimiter() {
- global $wgRateLimits;
-
$this->setExpectedException( ApiUsageException::class,
"You've exceeded your rate limit. Please wait some time and try again." );
$this->setMwGlobals( 'wgMainCacheType', 'hash' );
- $this->stashMwGlobals( 'wgRateLimits' );
- $wgRateLimits['move'] = [ '&can-bypass' => false, 'user' => [ 1, 60 ] ];
+ $this->mergeMwGlobalArrayValue( 'wgRateLimits',
+ [ 'move' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
$id = $this->createPage( $name );
}
public function testMoveSubpages() {
- global $wgNamespacesWithSubpages;
-
$name = ucfirst( __FUNCTION__ );
- $this->stashMwGlobals( 'wgNamespacesWithSubpages' );
- $wgNamespacesWithSubpages[NS_MAIN] = true;
+ $this->mergeMwGlobalArrayValue( 'wgNamespacesWithSubpages', [ NS_MAIN => true ] );
$pages = [ $name, "$name/1", "$name/2", "Talk:$name", "Talk:$name/1", "Talk:$name/3" ];
$ids = [];
public function testFileExtensions() {
global $wgFileExtensions;
- $this->stashMwGlobals( 'wgFileExtensions' );
// Add duplicate
- $wgFileExtensions[] = 'png';
+ $this->setMwGlobals( 'wgFileExtensions', array_merge( $wgFileExtensions, [ 'png' ] ) );
$expected = array_map(
function ( $val ) {
}
public function testPingLimiter() {
- global $wgRateLimits;
-
- $this->stashMwGlobals( 'wgRateLimits' );
- $wgRateLimits['stashedit'] = [ '&can-bypass' => false, 'user' => [ 1, 60 ] ];
+ $this->mergeMwGlobalArrayValue( 'wgRateLimits',
+ [ 'stashedit' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
$this->doStash( [ 'text' => 'A' ] );
* @param array|bool $remove Groups bureaucrats should be allowed to remove, true for all
*/
protected function setPermissions( $add = [], $remove = [] ) {
- global $wgAddGroups, $wgRemoveGroups;
-
$this->setGroupPermissions( 'bureaucrat', 'userrights', false );
if ( $add ) {
- $this->stashMwGlobals( 'wgAddGroups' );
- $wgAddGroups['bureaucrat'] = $add;
+ $this->mergeMwGlobalArrayValue( 'wgAddGroups', [ 'bureaucrat' => $add ] );
}
if ( $remove ) {
- $this->stashMwGlobals( 'wgRemoveGroups' );
- $wgRemoveGroups['bureaucrat'] = $remove;
+ $this->mergeMwGlobalArrayValue( 'wgRemoveGroups', [ 'bureaucrat' => $remove ] );
}
}
}
public function testWithoutTagPermission() {
- global $wgGroupPermissions;
-
ChangeTags::defineTag( 'custom tag' );
- $this->stashMwGlobals( 'wgGroupPermissions' );
- $wgGroupPermissions['user']['applychangetags'] = false;
+ $this->setGroupPermissions( 'user', 'applychangetags', false );
$this->doFailedRightsChange(
'You do not have permission to apply change tags along with your changes.',
}
public function testCheckAccountCreatePermissions() {
- global $wgGroupPermissions;
-
- $this->stashMwGlobals( [ 'wgGroupPermissions' ] );
-
$this->initializeManager( true );
- $wgGroupPermissions['*']['createaccount'] = true;
+ $this->setGroupPermissions( '*', 'createaccount', true );
$this->assertEquals(
\Status::newGood(),
$this->manager->checkAccountCreatePermissions( new \User )
);
$readOnlyMode->setReason( false );
- $wgGroupPermissions['*']['createaccount'] = false;
+ $this->setGroupPermissions( '*', 'createaccount', false );
$status = $this->manager->checkAccountCreatePermissions( new \User );
$this->assertFalse( $status->isOK() );
$this->assertTrue( $status->hasMessage( 'badaccess-groups' ) );
- $wgGroupPermissions['*']['createaccount'] = true;
+ $this->setGroupPermissions( '*', 'createaccount', true );
$user = \User::newFromName( 'UTBlockee' );
if ( $user->getID() == 0 ) {
}
public function testAutoAccountCreation() {
- global $wgGroupPermissions, $wgHooks;
+ global $wgHooks;
// PHPUnit seems to have a bug where it will call the ->with()
// callbacks for our hooks again after the test is run (WTF?), which
$username = self::usernameForCreation();
$this->initializeManager();
- $this->stashMwGlobals( [ 'wgGroupPermissions' ] );
- $wgGroupPermissions['*']['createaccount'] = true;
- $wgGroupPermissions['*']['autocreateaccount'] = false;
+ $this->setGroupPermissions( '*', 'createaccount', true );
+ $this->setGroupPermissions( '*', 'autocreateaccount', false );
$this->mergeMwGlobalArrayValue( 'wgObjectCaches',
[ __METHOD__ => [ 'class' => 'HashBagOStuff' ] ] );
$this->assertSame( 'noname', $session->get( 'AuthManager::AutoCreateBlacklist' ) );
// IP unable to create accounts
- $wgGroupPermissions['*']['createaccount'] = false;
- $wgGroupPermissions['*']['autocreateaccount'] = false;
+ $this->setGroupPermissions( '*', 'createaccount', false );
+ $this->setGroupPermissions( '*', 'autocreateaccount', false );
$session->clear();
$user = \User::newFromName( $username );
$this->hook( 'LocalUserCreated', $this->never() );
// Test that both permutations of permissions are allowed
// (this hits the two "ok" entries in $mocks['pre'])
- $wgGroupPermissions['*']['createaccount'] = false;
- $wgGroupPermissions['*']['autocreateaccount'] = true;
+ $this->setGroupPermissions( '*', 'createaccount', false );
+ $this->setGroupPermissions( '*', 'autocreateaccount', true );
$session->clear();
$user = \User::newFromName( $username );
$this->hook( 'LocalUserCreated', $this->never() );
$this->unhook( 'LocalUserCreated' );
$this->assertEquals( \Status::newFatal( 'ok' ), $ret );
- $wgGroupPermissions['*']['createaccount'] = true;
- $wgGroupPermissions['*']['autocreateaccount'] = false;
+ $this->setGroupPermissions( '*', 'createaccount', true );
+ $this->setGroupPermissions( '*', 'autocreateaccount', false );
$session->clear();
$user = \User::newFromName( $username );
$this->hook( 'LocalUserCreated', $this->never() );
public function testNewRandom() {
global $wgPasswordPolicy;
- $this->stashMwGlobals( 'wgPasswordPolicy' );
- $wgPasswordPolicy['policies']['default'] += [
+ $policy = $wgPasswordPolicy;
+ $policy['policies']['default'] += [
'MinimalPasswordLength' => 1,
'MinimalPasswordLengthToLogin' => 1,
];
+ $this->setMwGlobals( 'wgPasswordPolicy', $policy );
+
$ret1 = TemporaryPasswordAuthenticationRequest::newRandom();
$ret2 = TemporaryPasswordAuthenticationRequest::newRandom();
$this->assertNotSame( '', $ret1->password );
public function testNewInstance() {
$config = GlobalVarConfig::newInstance();
$this->assertInstanceOf( GlobalVarConfig::class, $config );
- $this->maybeStashGlobal( 'wgBaz' );
- $GLOBALS['wgBaz'] = 'somevalue';
+ $this->setMwGlobals( 'wgBaz', 'somevalue' );
// Check prefix is set to 'wg'
$this->assertEquals( 'somevalue', $config->get( 'Baz' ) );
}
public function testConstructor( $prefix ) {
$var = $prefix . 'GlobalVarConfigTest';
$rand = wfRandomString();
- $this->maybeStashGlobal( $var );
- $GLOBALS[$var] = $rand;
+ $this->setMwGlobals( $var, $rand );
$config = new GlobalVarConfig( $prefix );
$this->assertInstanceOf( GlobalVarConfig::class, $config );
$this->assertEquals( $rand, $config->get( 'GlobalVarConfigTest' ) );
* @covers GlobalVarConfig::hasWithPrefix
*/
public function testHas() {
- $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' );
- $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString();
- $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' );
+ $this->setMwGlobals( 'wgGlobalVarConfigTestHas', wfRandomString() );
$config = new GlobalVarConfig();
$this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
$this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
}
$this->assertEquals( $config->get( $name ), $expected );
}
-
- private function maybeStashGlobal( $var ) {
- if ( array_key_exists( $var, $GLOBALS ) ) {
- // Will be reset after this test is over
- $this->stashMwGlobals( $var );
- }
- }
}
private $localUsers = [];
protected function setUp() {
- global $wgGroupPermissions;
-
parent::setUp();
- $this->stashMwGlobals( [ 'wgGroupPermissions' ] );
- $wgGroupPermissions['local-id-lookup-test']['hideuser'] = true;
+ $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
}
public function addDBData() {
* @covers MediaWikiTestCase::tearDown
*/
public function testStashedGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
+ $this->hideDeprecated( 'MediaWikiTestCase::stashMwGlobals' );
$this->stashMwGlobals( $globalKey );
$GLOBALS[$globalKey] = $newValue;
$this->assertEquals(