parent::setUp();
$this->setMwGlobals( [ 'wgAuth' => null ] );
- $this->stashMwGlobals( [ 'wgHooks' ] );
}
/**
* @return object $mock->expects( $expect )->method( ... ).
*/
protected function hook( $hook, $expect ) {
- global $wgHooks;
$mock = $this->getMockBuilder( __CLASS__ )
->setMethods( [ "on$hook" ] )
->getMock();
- $wgHooks[$hook] = [ $mock ];
+ $this->setTemporaryHook( $hook, $mock );
return $mock->expects( $expect )->method( "on$hook" );
}
return new \Message( $key, $params, \Language::factory( 'en' ) );
}
+ /**
+ * Test two AuthenticationResponses for equality. We don't want to use regular assertEquals
+ * because that recursively compares members, which leads to false negatives if e.g. Language
+ * caches are reset.
+ *
+ * @param AuthenticationResponse $response1
+ * @param AuthenticationResponse $response2
+ * @param string $msg
+ * @return bool
+ */
+ private function assertResponseEquals(
+ AuthenticationResponse $expected, AuthenticationResponse $actual, $msg = ''
+ ) {
+ foreach ( ( new \ReflectionClass( $expected ) )->getProperties() as $prop ) {
+ $name = $prop->getName();
+ $usedMsg = ltrim( "$msg ($name)" );
+ if ( $name === 'message' && $expected->message ) {
+ $this->assertSame( $expected->message->serialize(), $actual->message->serialize(),
+ $usedMsg );
+ } else {
+ $this->assertEquals( $expected->$name, $actual->$name, $usedMsg );
+ }
+ }
+ }
+
/**
* Initialize the AuthManagerConfig variable in $this->config
*
);
}
- public function testSetDefaultUserOptions() {
+ /**
+ * @dataProvider provideSetDefaultUserOptions
+ */
+ public function testSetDefaultUserOptions(
+ $contLang, $useContextLang, $expectedLang, $expectedVariant
+ ) {
$this->initializeManager();
+ $this->setContentLang( $contLang );
$context = \RequestContext::getMain();
$reset = new ScopedCallback( [ $context, 'setLanguage' ], [ $context->getLanguage() ] );
$context->setLanguage( 'de' );
- $this->setMwGlobals( 'wgContLang', \Language::factory( 'zh' ) );
-
- $user = \User::newFromName( self::usernameForCreation() );
- $user->addToDatabase();
- $oldToken = $user->getToken();
- $this->managerPriv->setDefaultUserOptions( $user, false );
- $user->saveSettings();
- $this->assertNotEquals( $oldToken, $user->getToken() );
- $this->assertSame( 'zh', $user->getOption( 'language' ) );
- $this->assertSame( 'zh', $user->getOption( 'variant' ) );
$user = \User::newFromName( self::usernameForCreation() );
$user->addToDatabase();
$oldToken = $user->getToken();
- $this->managerPriv->setDefaultUserOptions( $user, true );
+ $this->managerPriv->setDefaultUserOptions( $user, $useContextLang );
$user->saveSettings();
$this->assertNotEquals( $oldToken, $user->getToken() );
- $this->assertSame( 'de', $user->getOption( 'language' ) );
- $this->assertSame( 'zh', $user->getOption( 'variant' ) );
-
- $this->setMwGlobals( 'wgContLang', \Language::factory( 'fr' ) );
+ $this->assertSame( $expectedLang, $user->getOption( 'language' ) );
+ $this->assertSame( $expectedVariant, $user->getOption( 'variant' ) );
+ }
- $user = \User::newFromName( self::usernameForCreation() );
- $user->addToDatabase();
- $oldToken = $user->getToken();
- $this->managerPriv->setDefaultUserOptions( $user, true );
- $user->saveSettings();
- $this->assertNotEquals( $oldToken, $user->getToken() );
- $this->assertSame( 'de', $user->getOption( 'language' ) );
- $this->assertSame( null, $user->getOption( 'variant' ) );
+ public function provideSetDefaultUserOptions() {
+ return [
+ [ 'zh', false, 'zh', 'zh' ],
+ [ 'zh', true, 'de', 'zh' ],
+ [ 'fr', true, 'de', null ],
+ ];
}
public function testForcePrimaryAuthenticationProviders() {
$this->assertSame( 'http://localhost/', $req->returnToUrl );
$ret->message = $this->message( $ret->message );
- $this->assertEquals( $response, $ret, "Response $i, response" );
+ $this->assertResponseEquals( $response, $ret, "Response $i, response" );
if ( $success ) {
$this->assertSame( $id, $session->getUser()->getId(),
"Response $i, authn" );
"Response $i, login marker" );
}
$ret->message = $this->message( $ret->message );
- $this->assertEquals( $response, $ret, "Response $i, response" );
+ $this->assertResponseEquals( $response, $ret, "Response $i, response" );
if ( $success || $response->status === AuthenticationResponse::FAIL ) {
$this->assertNull(
$this->request->getSession()->getSecret( 'AuthManager::accountCreationState' ),
$wgGroupPermissions['*']['createaccount'] = true;
$wgGroupPermissions['*']['autocreateaccount'] = false;
- \ObjectCache::$instances[__METHOD__] = new \HashBagOStuff();
+ $this->mergeMwGlobalArrayValue( 'wgObjectCaches',
+ [ __METHOD__ => [ 'class' => 'HashBagOStuff' ] ] );
$this->setMwGlobals( [ 'wgMainCacheType' => __METHOD__ ] );
// Set up lots of mocks...
$this->assertSame( 'http://localhost/', $req->returnToUrl );
$ret->message = $this->message( $ret->message );
- $this->assertEquals( $response, $ret, "Response $i, response" );
+ $this->assertResponseEquals( $response, $ret, "Response $i, response" );
if ( $response->status === AuthenticationResponse::PASS ||
$response->status === AuthenticationResponse::FAIL
) {