Implement injecting of dependencies to an AuthenticationProvider

init() method was added to AuthenticationProvider. It helps to inject
dependecies. Overridable postInitSetup() method was added to
AbstractAuthenticationProvider. A provider can override this to do
any necessary setup.
AbstractAuthenticationProvider ::setLogger(), ::setManager(), ::setConfig(),
::setHookContainer() were soft deprecated. Now developers must use
AbstractAuthenticationProvider::init().

Bug: T275030
Change-Id: I6ca63eddac1b177eeadbdcce992e71c44a480160
This commit is contained in:
vladshapik 2021-04-16 16:17:10 +03:00
parent 9ba56ccb3b
commit 3a150275de
18 changed files with 479 additions and 210 deletions

View file

@ -98,6 +98,8 @@ because of Phabricator reports.
CachedAction, SpecialCachedPage, CacheHelper, ICacheHelper.
* The deprecated "es6-promise" alias ResourceLoader module has been removed. Use
"es6-polyfills" directly intead.
* AuthenticationProvider interface doesn't extend LoggerAwareInterface.
So AuthenticationProvider can't be typehinted as LoggerAwareInterface.
=== Deprecations in 1.37 ===
* JobQueue::getWiki, deprecated in 1.33, now emits deprecation warnings.
@ -116,6 +118,10 @@ because of Phabricator reports.
* Title::isWatchable() has been deprecated. Use WatchlistManager::isWatchable()
instead.
* wfIncrStats(), deprecated in 1.36, now emits deprecation warnings.
* AbstractAuthenticationProvider ::setLogger(), ::setManager(), ::setConfig(),
::setHookContainer() were soft deprecated. Use ::init() to inject
dependencies or override ::postInitSetup() to do any custom
post-initialization configuration.
* …
=== Other changes in 1.37 ===
@ -126,6 +132,9 @@ because of Phabricator reports.
WatchlistManager::removeWatchIgnoringRights(), which replace
User::removeWatch(), now call the UnwatchArticle and UnwatchArticleComplete
hooks.
* The overridable postInitSetup() method was added to the
AbstractAuthenticationProvider class. A provider can override postInitSetup()
to do any custom post-initialization configuration.
* …
== Compatibility ==

View file

@ -24,6 +24,7 @@ namespace MediaWiki\Auth;
use Config;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\User\UserNameUtils;
use Psr\Log\LoggerInterface;
/**
@ -35,31 +36,113 @@ use Psr\Log\LoggerInterface;
abstract class AbstractAuthenticationProvider implements AuthenticationProvider {
/** @var LoggerInterface */
protected $logger;
/** @var AuthManager */
protected $manager;
/** @var Config */
protected $config;
/** @var HookContainer */
private $hookContainer;
/** @var HookRunner */
private $hookRunner;
/** @var UserNameUtils */
protected $userNameUtils;
/**
* Initialise with dependencies of an AuthenticationProvider
*
* @since 1.37
* @internal
*
* @param LoggerInterface $logger
* @param AuthManager $manager
* @param HookContainer $hookContainer
* @param Config $config
* @param UserNameUtils $userNameUtils
*/
public function init(
LoggerInterface $logger,
AuthManager $manager,
HookContainer $hookContainer,
Config $config,
UserNameUtils $userNameUtils
) {
$this->logger = $logger;
$this->manager = $manager;
$this->hookContainer = $hookContainer;
$this->hookRunner = new HookRunner( $hookContainer );
// Since AuthenticationProvider::setConfig() is still overridden
// in some extensions, so we can't stop calling it until we will
// move the setup those extensions do to
// AbstractAuthenticationProvider::postInitSetup()
$this->setConfig( $config );
$this->userNameUtils = $userNameUtils;
$this->postInitSetup();
}
/**
* A provider can override this to do any necessary setup after init()
* is called.
*
* @since 1.37
* @stable to override
*/
protected function postInitSetup() {
}
/**
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer, or Logger) of an existing AuthenticationProvider object,
* please file a report on phabricator - there is no non-deprecated way to
* do this anymore.
*/
public function setLogger( LoggerInterface $logger ) {
$this->logger = $logger;
}
/**
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer, or Logger) of an existing AuthenticationProvider object,
* please file a report on phabricator - there is no non-deprecated way to
* do this anymore.
*/
public function setManager( AuthManager $manager ) {
$this->manager = $manager;
}
/**
* @stable to override
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer, or Logger) of an existing AuthenticationProvider object,
* please file a report on phabricator - there is no non-deprecated way to
* do this anymore.
* @param Config $config
*/
public function setConfig( Config $config ) {
$this->config = $config;
}
/**
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer, or Logger) of an existing AuthenticationProvider object,
* please file a report on phabricator - there is no non-deprecated way to
* do this anymore.
*/
public function setHookContainer( HookContainer $hookContainer ) {
$this->hookContainer = $hookContainer;
$this->hookRunner = new HookRunner( $hookContainer );

View file

@ -268,16 +268,13 @@ class AuthManager implements LoggerAwareInterface {
$this->primaryAuthenticationProviders = [];
foreach ( $providers as $provider ) {
if ( !$provider instanceof PrimaryAuthenticationProvider ) {
if ( !$provider instanceof AbstractPrimaryAuthenticationProvider ) {
throw new \RuntimeException(
'Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got ' .
'Expected instance of MediaWiki\\Auth\\AbstractPrimaryAuthenticationProvider, got ' .
get_class( $provider )
);
}
$provider->setLogger( $this->logger );
$provider->setManager( $this );
$provider->setConfig( $this->config );
$provider->setHookContainer( $this->hookContainer );
$provider->init( $this->logger, $this, $this->hookContainer, $this->config, $this->userNameUtils );
$id = $provider->getUniqueId();
if ( isset( $this->allAuthenticationProviders[$id] ) ) {
throw new \RuntimeException(
@ -2362,12 +2359,9 @@ class AuthManager implements LoggerAwareInterface {
$ret = [];
foreach ( $specs as $spec ) {
/** @var AuthenticationProvider $provider */
/** @var AbstractAuthenticationProvider $provider */
$provider = $this->objectFactory->createObject( $spec, [ 'assertClass' => $class ] );
$provider->setLogger( $this->logger );
$provider->setManager( $this );
$provider->setConfig( $this->config );
$provider->setHookContainer( $this->getHookContainer() );
$provider->init( $this->logger, $this, $this->getHookContainer(), $this->config, $this->userNameUtils );
$id = $provider->getUniqueId();
if ( isset( $this->allAuthenticationProviders[$id] ) ) {
throw new \RuntimeException(

View file

@ -25,7 +25,6 @@ namespace MediaWiki\Auth;
use Config;
use MediaWiki\HookContainer\HookContainer;
use Psr\Log\LoggerAwareInterface;
/**
* An AuthenticationProvider is used by AuthManager when authenticating users.
@ -37,21 +36,42 @@ use Psr\Log\LoggerAwareInterface;
* @ingroup Auth
* @since 1.27
*/
interface AuthenticationProvider extends LoggerAwareInterface {
interface AuthenticationProvider {
/**
* Set AuthManager
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer) of an existing AuthenticationProvider object, please
* file a report on phabricator - there is no non-deprecated way to do this
* anymore.
* @param AuthManager $manager
*/
public function setManager( AuthManager $manager );
/**
* Set configuration
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer) of an existing AuthenticationProvider object, please
* file a report on phabricator - there is no non-deprecated way to do this
* anymore.
* @param Config $config
*/
public function setConfig( Config $config );
/**
* @deprecated since 1.37. For extension-defined authentication providers
* that were using this method to trigger other work, please override
* AbstractAuthenticationProvider::postInitSetup instead. If your extension
* was using this to explicitly change the AuthManager (or Config, or
* HookContainer) of an existing AuthenticationProvider object, please
* file a report on phabricator - there is no non-deprecated way to do this
* anymore.
* @param HookContainer $hookContainer
*/
public function setHookContainer( HookContainer $hookContainer );

View file

@ -21,7 +21,6 @@
namespace MediaWiki\Auth;
use Config;
use MediaWiki\MediaWikiServices;
use StatusValue;
@ -47,9 +46,7 @@ class CheckBlocksSecondaryAuthenticationProvider extends AbstractSecondaryAuthen
}
}
public function setConfig( Config $config ) {
parent::setConfig( $config );
protected function postInitSetup() {
if ( $this->blockDisablesLogin === null ) {
$this->blockDisablesLogin = $this->config->get( 'BlockDisablesLogin' );
}

View file

@ -2,8 +2,6 @@
namespace MediaWiki\Auth;
use Config;
/**
* Handles email notification / email address confirmation for account creation.
*
@ -27,9 +25,7 @@ class EmailNotificationSecondaryAuthenticationProvider
}
}
public function setConfig( Config $config ) {
parent::setConfig( $config );
protected function postInitSetup() {
if ( $this->sendConfirmationEmail === null ) {
$this->sendConfirmationEmail = $this->config->get( 'EnableEmail' )
&& $this->config->get( 'EmailAuthentication' );

View file

@ -76,9 +76,7 @@ class TemporaryPasswordPrimaryAuthenticationProvider
}
}
public function setConfig( \Config $config ) {
parent::setConfig( $config );
protected function postInitSetup() {
if ( $this->emailEnabled === null ) {
$this->emailEnabled = $this->config->get( 'EnableEmail' );
}

View file

@ -22,7 +22,6 @@
namespace MediaWiki\Auth;
use BagOStuff;
use Config;
/**
* A pre-authentication provider to throttle authentication actions.
@ -61,9 +60,7 @@ class ThrottlePreAuthenticationProvider extends AbstractPreAuthenticationProvide
$this->cache = $params['cache'] ?? \ObjectCache::getLocalClusterInstance();
}
public function setConfig( Config $config ) {
parent::setConfig( $config );
protected function postInitSetup() {
$accountCreationThrottle = $this->config->get( 'AccountCreationThrottle' );
// Handle old $wgAccountCreationThrottle format (number of attempts per 24 hours)
if ( !is_array( $accountCreationThrottle ) ) {

View file

@ -3,6 +3,9 @@
namespace MediaWiki\Auth;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use MultiConfig;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -29,7 +32,13 @@ class AbstractPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegr
$provider = $this->getMockForAbstractClass(
AbstractPasswordPrimaryAuthenticationProvider::class
);
$provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$this->getServiceContainer()->getMainConfig(),
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$obj = $providerPriv->getPasswordFactory();
@ -41,8 +50,13 @@ class AbstractPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegr
$provider = $this->getMockForAbstractClass(
AbstractPasswordPrimaryAuthenticationProvider::class
);
$provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->init(
new NullLogger(),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
MediaWikiServices::getInstance()->getMainConfig(),
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$obj = $providerPriv->getPassword( null );
@ -57,12 +71,13 @@ class AbstractPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegr
$provider = $this->getMockForAbstractClass(
AbstractPasswordPrimaryAuthenticationProvider::class
);
$provider->setConfig( new \MultiConfig( [
$config,
MediaWikiServices::getInstance()->getMainConfig()
] ) );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setHookContainer( MediaWikiServices::getInstance()->getHookContainer() );
$provider->init(
new NullLogger(),
$this->createNoOpMock( AuthManager::class ),
$this->getServiceContainer()->getHookContainer(),
new MultiConfig( [ $config, $this->getServiceContainer()->getMainConfig() ] ),
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$this->mergeMwGlobalArrayValue( 'wgHooks', [ 'ResetPasswordExpiration' => [] ] );
@ -108,8 +123,13 @@ class AbstractPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegr
$provider = $this->getMockForAbstractClass(
AbstractPasswordPrimaryAuthenticationProvider::class
);
$provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->init(
new NullLogger(),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$this->getServiceContainer()->getMainConfig(),
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
@ -139,10 +159,13 @@ class AbstractPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegr
$provider = $this->getMockForAbstractClass(
AbstractPasswordPrimaryAuthenticationProvider::class
);
$provider->setConfig( $config );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setManager( $manager );
$provider->setHookContainer( $services->getHookContainer() );
$provider->init(
new NullLogger(),
$manager,
$services->getHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$manager->removeAuthenticationSessionData( null );

View file

@ -325,10 +325,10 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
public function testNormalizeUsername() {
$mocks = [
$this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
$this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
$this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
$this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
$this->createMock( AbstractPrimaryAuthenticationProvider::class ),
$this->createMock( AbstractPrimaryAuthenticationProvider::class ),
$this->createMock( AbstractPrimaryAuthenticationProvider::class ),
$this->createMock( AbstractPrimaryAuthenticationProvider::class ),
];
foreach ( $mocks as $key => $mock ) {
$mock->method( 'getUniqueId' )->willReturn( $key );
@ -519,13 +519,13 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
* @param bool $expect
*/
public function testUserCanAuthenticate( $primary1Can, $primary2Can, $expect ) {
$mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )
->willReturn( 'primary1' );
$mock1->method( 'testUserCanAuthenticate' )
->with( 'UTSysop' )
->willReturn( $primary1Can );
$mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock2->method( 'getUniqueId' )
->willReturn( 'primary2' );
$mock2->method( 'testUserCanAuthenticate' )
@ -549,7 +549,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
public function testRevokeAccessForUser() {
$this->initializeManager();
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )
->willReturn( 'primary' );
$mock->expects( $this->once() )->method( 'providerRevokeAccessForUser' )
@ -568,15 +568,13 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
public function testProviderCreation() {
$mocks = [
'pre' => $this->getMockForAbstractClass( PreAuthenticationProvider::class ),
'primary' => $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class ),
'secondary' => $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class ),
'pre' => $this->createMock( AbstractPreAuthenticationProvider::class ),
'primary' => $this->createMock( AbstractPrimaryAuthenticationProvider::class ),
'secondary' => $this->createMock( AbstractSecondaryAuthenticationProvider::class ),
];
foreach ( $mocks as $key => $mock ) {
$mock->method( 'getUniqueId' )->willReturn( $key );
$mock->expects( $this->once() )->method( 'setLogger' );
$mock->expects( $this->once() )->method( 'setManager' );
$mock->expects( $this->once() )->method( 'setConfig' );
$mock->expects( $this->once() )->method( 'init' );
}
$this->preauthMocks = [ $mocks['pre'] ];
$this->primaryauthMocks = [ $mocks['primary'] ];
@ -610,8 +608,8 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
);
// Duplicate IDs
$mock1 = $this->getMockForAbstractClass( PreAuthenticationProvider::class );
$mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPreAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )->willReturn( 'X' );
$mock2->method( 'getUniqueId' )->willReturn( 'X' );
$this->preauthMocks = [ $mock1 ];
@ -666,9 +664,9 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
}
// Sorting
$mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock3 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock3 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )->willReturn( 'A' );
$mock2->method( 'getUniqueId' )->willReturn( 'B' );
$mock3->method( 'getUniqueId' )->willReturn( 'C' );
@ -727,9 +725,9 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
}
public function testForcePrimaryAuthenticationProviders() {
$mockA = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mockB = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mockB2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mockA = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mockB = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mockB2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mockA->method( 'getUniqueId' )->willReturn( 'A' );
$mockB->method( 'getUniqueId' )->willReturn( 'B' );
$mockB2->method( 'getUniqueId' )->willReturn( 'B' );
@ -797,7 +795,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->fail( 'Expected exception not thrown' );
} catch ( \RuntimeException $ex ) {
$this->assertSame(
"Expected instance of MediaWiki\\Auth\\PrimaryAuthenticationProvider, got $class",
"Expected instance of MediaWiki\\Auth\\AbstractPrimaryAuthenticationProvider, got $class",
$ex->getMessage()
);
}
@ -976,15 +974,15 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mocks = [];
foreach ( [ 'pre', 'primary', 'secondary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockForAbstractClass(
"MediaWiki\\Auth\\$class", [], "Mock$class"
);
$mocks[$key] = $this->getMockBuilder( "MediaWiki\\Auth\\Abstract$class" )
->setMockClassName( "MockAbstract$class" )
->getMock();
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
$mocks[$key . '2'] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
$mocks[$key . '2'] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key . '2']->method( 'getUniqueId' )
->willReturn( $key . '2' );
$mocks[$key . '3'] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
$mocks[$key . '3'] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key . '3']->method( 'getUniqueId' )
->willReturn( $key . '3' );
}
@ -1264,7 +1262,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
[
$tmp,
new \DomainException(
'MockPrimaryAuthenticationProvider::continuePrimaryAuthentication() returned ABSTAIN'
'MockAbstractPrimaryAuthenticationProvider::continuePrimaryAuthentication() returned ABSTAIN'
)
]
],
@ -1300,7 +1298,9 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
],
[],
[
new \DomainException( 'MockPrimaryAuthenticationProvider returned an invalid username: <>' ),
new \DomainException(
'MockAbstractPrimaryAuthenticationProvider returned an invalid username: <>'
),
]
],
'Secondary fail' => [
@ -1349,13 +1349,13 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
* @param bool $expect
*/
public function testUserExists( $primary1Exists, $primary2Exists, $expect ) {
$mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )
->willReturn( 'primary1' );
$mock1->method( 'testUserExists' )
->with( 'UTSysop' )
->willReturn( $primary1Exists );
$mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock2->method( 'getUniqueId' )
->willReturn( 'primary2' );
$mock2->method( 'testUserExists' )
@ -1385,12 +1385,12 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
public function testAllowsAuthenticationDataChange( $primaryReturn, $secondaryReturn, $expect ) {
$req = $this->getMockForAbstractClass( AuthenticationRequest::class );
$mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )->willReturn( '1' );
$mock1->method( 'providerAllowsAuthenticationDataChange' )
->with( $req )
->willReturn( $primaryReturn );
$mock2 = $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractSecondaryAuthenticationProvider::class );
$mock2->method( 'getUniqueId' )->willReturn( '2' );
$mock2->method( 'providerAllowsAuthenticationDataChange' )
->with( $req )
@ -1466,11 +1466,11 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$req = $this->getMockForAbstractClass( AuthenticationRequest::class );
$req->username = 'UTSysop';
$mock1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock1->method( 'getUniqueId' )->willReturn( '1' );
$mock1->expects( $this->once() )->method( 'providerChangeAuthenticationData' )
->with( $req );
$mock2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock2->method( 'getUniqueId' )->willReturn( '2' );
$mock2->expects( $this->once() )->method( 'providerChangeAuthenticationData' )
->with( $req );
@ -1492,7 +1492,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
];
foreach ( $types as $type => $can ) {
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( $type );
$mock->method( 'accountCreationType' )
->willReturn( $type );
@ -1610,7 +1610,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->manager->canCreateAccount( $username )
);
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1625,7 +1625,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->manager->canCreateAccount( $username )
);
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1650,7 +1650,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->manager->canCreateAccount( $username )
);
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1689,7 +1689,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->request->getSession()->getSecret( 'AuthManager::accountCreationState' )
);
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1734,7 +1734,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'userexists', $ret->message->getKey() );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1751,7 +1751,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'fail', $ret->message->getKey() );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1775,7 +1775,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->assertSame( AuthenticationResponse::FAIL, $ret->status );
$this->assertSame( 'userexists', $ret->message->getKey() );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1844,7 +1844,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
}
$this->unhook( 'LocalUserCreated' );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_CREATE );
@ -1985,9 +1985,9 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mocks = [];
foreach ( [ 'pre', 'primary', 'secondary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockForAbstractClass(
"MediaWiki\\Auth\\$class", [], "Mock$class"
);
$mocks[$key] = $this->getMockBuilder( "MediaWiki\\Auth\\Abstract$class" )
->setMockClassName( "MockAbstract$class" )
->getMock();
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
$mocks[$key]->method( 'testUserForCreation' )
@ -2012,7 +2012,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
) );
for ( $i = 2; $i <= 3; $i++ ) {
$mocks[$key . $i] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
$mocks[$key . $i] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key . $i]->method( 'getUniqueId' )
->willReturn( $key . $i );
$mocks[$key . $i]->method( 'testUserForCreation' )
@ -2312,7 +2312,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
[
$tmp,
new \DomainException(
'MockPrimaryAuthenticationProvider::continuePrimaryAccountCreation() returned ABSTAIN'
'MockAbstractPrimaryAuthenticationProvider::continuePrimaryAccountCreation() returned ABSTAIN'
)
]
],
@ -2354,7 +2354,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
],
[
'created' => new \DomainException(
'MockSecondaryAuthenticationProvider::beginSecondaryAccountCreation() returned FAIL. ' .
'MockAbstractSecondaryAuthenticationProvider::beginSecondaryAccountCreation() returned FAIL. ' .
'Secondary providers are not allowed to fail account creation, ' .
'that should have been done via testForAccountCreation().'
)
@ -2375,9 +2375,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->initializeManager();
// Set up lots of mocks...
$mock = $this->getMockForAbstractClass(
\MediaWiki\Auth\PrimaryAuthenticationProvider::class, []
);
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )
->willReturn( 'primary' );
$mock->method( 'testUserForCreation' )
@ -2476,7 +2474,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mocks = [];
foreach ( [ 'pre', 'primary', 'secondary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
$mocks[$key] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
}
@ -2970,9 +2968,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$good = StatusValue::newGood();
$mocks = [];
$mocks['pre'] = $this->getMockBuilder( PreAuthenticationProvider::class )
->onlyMethods( [ 'getUniqueId', 'getAuthenticationRequests' ] )
->getMockForAbstractClass();
$mocks['pre'] = $this->createMock( AbstractPreAuthenticationProvider::class );
$mocks['pre']->method( 'getUniqueId' )
->willReturn( 'pre' );
$mocks['pre']->method( 'getAuthenticationRequests' )
@ -2981,11 +2977,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
} ) );
foreach ( [ 'primary', 'secondary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockBuilder( "MediaWiki\\Auth\\$class" )
->onlyMethods( [
'getUniqueId', 'getAuthenticationRequests', 'providerAllowsAuthenticationDataChange',
] )
->getMockForAbstractClass();
$mocks[$key] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
$mocks[$key]->method( 'getAuthenticationRequests' )
@ -3003,12 +2995,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
PrimaryAuthenticationProvider::TYPE_LINK
] as $type ) {
$class = 'PrimaryAuthenticationProvider';
$mocks["primary-$type"] = $this->getMockBuilder( "MediaWiki\\Auth\\$class" )
->onlyMethods( [
'getUniqueId', 'accountCreationType', 'getAuthenticationRequests',
'providerAllowsAuthenticationDataChange',
] )
->getMockForAbstractClass();
$mocks["primary-$type"] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks["primary-$type"]->method( 'getUniqueId' )
->willReturn( "primary-$type" );
$mocks["primary-$type"]->method( 'accountCreationType' )
@ -3022,12 +3009,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->primaryauthMocks[] = $mocks["primary-$type"];
}
$mocks['primary2'] = $this->getMockBuilder( PrimaryAuthenticationProvider::class )
->onlyMethods( [
'getUniqueId', 'accountCreationType', 'getAuthenticationRequests',
'providerAllowsAuthenticationDataChange',
] )
->getMockForAbstractClass();
$mocks['primary2'] = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mocks['primary2']->method( 'getUniqueId' )
->willReturn( 'primary2' );
$mocks['primary2']->method( 'accountCreationType' )
@ -3188,7 +3170,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$good = StatusValue::newGood();
$primary1 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$primary1 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$primary1->method( 'getUniqueId' )
->willReturn( 'primary1' );
$primary1->method( 'accountCreationType' )
@ -3205,7 +3187,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
];
} ) );
$primary2 = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$primary2 = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$primary2->method( 'getUniqueId' )
->willReturn( 'primary2' );
$primary2->method( 'accountCreationType' )
@ -3219,7 +3201,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
];
} ) );
$secondary = $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class );
$secondary = $this->createMock( AbstractSecondaryAuthenticationProvider::class );
$secondary->method( 'getUniqueId' )
->willReturn( 'secondary' );
$secondary->method( 'getAuthenticationRequests' )
@ -3277,7 +3259,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mocks = [];
foreach ( [ 'primary', 'secondary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockForAbstractClass( "MediaWiki\\Auth\\$class" );
$mocks[$key] = $this->createMock( "MediaWiki\\Auth\\Abstract$class" );
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
$mocks[$key]->method( 'providerAllowsPropertyChange' )
@ -3300,7 +3282,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$req = $this->createMock( AuthenticationRequest::class );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'primary' );
$mock->method( 'beginPrimaryAuthentication' )
->willReturn( AuthenticationResponse::newPass( $username ) );
@ -3310,7 +3292,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$mock->method( 'testUserForCreation' )
->willReturn( StatusValue::newGood() );
$mock2 = $this->getMockForAbstractClass( SecondaryAuthenticationProvider::class );
$mock2 = $this->createMock( AbstractSecondaryAuthenticationProvider::class );
$mock2->method( 'getUniqueId' )
->willReturn( 'secondary' );
$mock2->method( 'beginSecondaryAuthentication' )
@ -3360,7 +3342,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
public function testAutoCreateFailOnLogin() {
$username = self::usernameForCreation();
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'primary' );
$mock->method( 'beginPrimaryAuthentication' )
->willReturn( AuthenticationResponse::newPass( $username ) );
@ -3422,7 +3404,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
];
foreach ( $types as $type => $can ) {
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( $type );
$mock->method( 'accountCreationType' )
->willReturn( $type );
@ -3445,7 +3427,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
}
$this->assertNull( $this->request->getSession()->getSecret( 'AuthManager::accountLinkState' ) );
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_LINK );
@ -3480,7 +3462,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
$this->assertEquals( 'Account linking is not possible', $ex->getMessage() );
}
$mock = $this->getMockForAbstractClass( PrimaryAuthenticationProvider::class );
$mock = $this->createMock( AbstractPrimaryAuthenticationProvider::class );
$mock->method( 'getUniqueId' )->willReturn( 'X' );
$mock->method( 'accountCreationType' )
->willReturn( PrimaryAuthenticationProvider::TYPE_LINK );
@ -3533,16 +3515,16 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
foreach ( [ 'pre', 'primary' ] as $key ) {
$class = ucfirst( $key ) . 'AuthenticationProvider';
$mocks[$key] = $this->getMockForAbstractClass(
"MediaWiki\\Auth\\$class", [], "Mock$class"
);
$mocks[$key] = $this->getMockBuilder( "MediaWiki\\Auth\\Abstract$class" )
->setMockClassName( "MockAbstract$class" )
->getMock();
$mocks[$key]->method( 'getUniqueId' )
->willReturn( $key );
for ( $i = 2; $i <= 3; $i++ ) {
$mocks[$key . $i] = $this->getMockForAbstractClass(
"MediaWiki\\Auth\\$class", [], "Mock$class"
);
$mocks[$key . $i] = $this->getMockBuilder( "MediaWiki\\Auth\\Abstract$class" )
->setMockClassName( "MockAbstract$class" )
->getMock();
$mocks[$key . $i]->method( 'getUniqueId' )
->willReturn( $key . $i );
}
@ -3734,7 +3716,7 @@ class AuthManagerTest extends \MediaWikiIntegrationTestCase {
[
$tmp,
new \DomainException(
'MockPrimaryAuthenticationProvider::continuePrimaryAccountLink() returned ABSTAIN'
'MockAbstractPrimaryAuthenticationProvider::continuePrimaryAccountLink() returned ABSTAIN'
)
]
],

View file

@ -2,8 +2,11 @@
namespace MediaWiki\Auth;
use HashConfig;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -18,7 +21,13 @@ class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
$config = new \HashConfig( [
'BlockDisablesLogin' => false
] );
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( false, $providerPriv->blockDisablesLogin );
$provider = new CheckBlocksSecondaryAuthenticationProvider(
@ -28,7 +37,13 @@ class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
$config = new \HashConfig( [
'BlockDisablesLogin' => false
] );
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( true, $providerPriv->blockDisablesLogin );
}
@ -120,9 +135,14 @@ class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
$provider = new CheckBlocksSecondaryAuthenticationProvider(
[ 'blockDisablesLogin' => false ]
);
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setConfig( new \HashConfig() );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
new NullLogger(),
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig(),
$this->createNoOpMock( UserNameUtils::class )
);
$unblockedUser = \User::newFromName( 'UTSysop' );
$blockedUser = $this->getBlockedUser();
@ -160,7 +180,6 @@ class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
];
$block = new DatabaseBlock( $blockOptions );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$scopeVariable = new \Wikimedia\ScopedCallback( [ $block, 'delete' ] );
$user = \User::newFromName( 'UTNormalUser' );
if ( $user->getId() == 0 ) {
@ -174,9 +193,13 @@ class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
$provider = new CheckBlocksSecondaryAuthenticationProvider(
[ 'blockDisablesLogin' => true ]
);
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setConfig( new \HashConfig() );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
new NullLogger(),
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig(),
$this->createNoOpMock( UserNameUtils::class )
);
$ret = $provider->beginSecondaryAuthentication( $user, [] );
$this->assertEquals( AuthenticationResponse::FAIL, $ret->status );

View file

@ -2,9 +2,11 @@
namespace MediaWiki\Auth;
use Config;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -156,7 +158,13 @@ class ConfirmLinkSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
? \StatusValue::newGood()
: \StatusValue::newFatal( 'no' );
} ) );
$provider->setManager( $manager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$manager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertEquals(
AuthenticationResponse::newAbstain(),
@ -255,7 +263,13 @@ class ConfirmLinkSecondaryAuthenticationProviderTest extends \MediaWikiIntegrati
$mwServices->getBlockErrorFormatter(),
$mwServices->getWatchlistManager()
);
$provider->setManager( $manager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$manager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$this->createNoOpMock( UserNameUtils::class )
);
$provider = TestingAccessWrapper::newFromObject( $provider );
$req = new ConfirmLinkAuthenticationRequest( $reqs );

View file

@ -2,10 +2,12 @@
namespace MediaWiki\Auth;
use Config;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -19,14 +21,26 @@ class EmailNotificationSecondaryAuthenticationProviderTest extends \MediaWikiInt
] );
$provider = new EmailNotificationSecondaryAuthenticationProvider();
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$this->assertTrue( $providerPriv->sendConfirmationEmail );
$provider = new EmailNotificationSecondaryAuthenticationProvider( [
'sendConfirmationEmail' => false,
] );
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$this->assertFalse( $providerPriv->sendConfirmationEmail );
}
@ -106,25 +120,49 @@ class EmailNotificationSecondaryAuthenticationProviderTest extends \MediaWikiInt
$provider = new EmailNotificationSecondaryAuthenticationProvider( [
'sendConfirmationEmail' => false,
] );
$provider->setManager( $authManager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$authManager,
$hookContainer,
$this->createNoOpAbstractMock( Config::class ),
$userNameUtils
);
$provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
$provider = new EmailNotificationSecondaryAuthenticationProvider( [
'sendConfirmationEmail' => true,
] );
$provider->setManager( $authManager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$authManager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$userNameUtils
);
$provider->beginSecondaryAccountCreation( $userWithoutEmail, $creator, [] );
$provider->beginSecondaryAccountCreation( $userExpectsConfirmation, $creator, [] );
// test logging of email errors
$logger = $this->getMockForAbstractClass( LoggerInterface::class );
$logger->expects( $this->once() )->method( 'warning' );
$provider->setLogger( $logger );
$provider->init(
$logger,
$authManager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$userNameUtils
);
$provider->beginSecondaryAccountCreation( $userWithEmailError, $creator, [] );
// test disable flag used by other providers
$authManager->setAuthenticationSessionData( 'no-email', true );
$provider->setManager( $authManager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$authManager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$userNameUtils
);
$provider->beginSecondaryAccountCreation( $userNotExpectsConfirmation, $creator, [] );
}
}

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Auth;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -68,10 +69,13 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrati
->will( $this->returnCallback( function () {
return $this->validity;
} ) );
$provider->setConfig( $config );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setManager( $this->manager );
$provider->setHookContainer( $hookContainer );
$provider->init(
new NullLogger(),
$this->manager,
$hookContainer,
$config,
$this->createNoOpMock( UserNameUtils::class )
);
return $provider;
}
@ -81,7 +85,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrati
$userName = $user->getName();
$lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
$provider = new LocalPasswordPrimaryAuthenticationProvider();
$provider = $this->getProvider();
$this->assertSame(
PrimaryAuthenticationProvider::TYPE_CREATE,
@ -93,7 +97,7 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrati
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
$this->assertFalse( $provider->testUserExists( '<invalid>' ) );
$provider = new LocalPasswordPrimaryAuthenticationProvider( [ 'loginOnly' => true ] );
$provider = $this->getProvider( [ 'loginOnly' => true ] );
$this->assertSame(
PrimaryAuthenticationProvider::TYPE_NONE,
@ -151,9 +155,13 @@ class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrati
$this->config->set( 'InvalidPasswordReset', true );
$provider = new LocalPasswordPrimaryAuthenticationProvider();
$provider->setConfig( $this->config );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setManager( $this->manager );
$provider->init(
new NullLogger(),
$this->manager,
$this->createHookContainer(),
$this->config,
$this->createNoOpMock( UserNameUtils::class )
);
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$user = $this->getMutableTestUser()->getUser();

View file

@ -2,10 +2,12 @@
namespace MediaWiki\Auth;
use Config;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Psr\Log\NullLogger;
use Wikimedia\TestingAccessWrapper;
/**
@ -106,7 +108,13 @@ class ResetPasswordSecondaryAuthenticationProviderTest extends \MediaWikiIntegra
$mwServices->getBlockErrorFormatter(),
$mwServices->getWatchlistManager()
);
$provider->setManager( $manager );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$manager,
$this->createHookContainer(),
$this->createNoOpAbstractMock( Config::class ),
$userNameUtils
);
$provider = TestingAccessWrapper::newFromObject( $provider );
$msg = wfMessage( 'foo' );

View file

@ -5,6 +5,7 @@ namespace MediaWiki\Auth;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Psr\Log\NullLogger;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
@ -69,10 +70,14 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
->will( $this->returnCallback( function () {
return $this->validity;
} ) );
$provider->setConfig( $config );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setManager( $this->manager );
$provider->setHookContainer( $hookContainer );
$userNameUtils = $this->getServiceContainer()->getUserNameUtils();
$provider->init(
new NullLogger(),
$this->manager,
$hookContainer,
$config,
$this->createNoOpMock( UserNameUtils::class )
);
return $provider;
}
@ -91,7 +96,7 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
}
public function testBasics() {
$provider = new TemporaryPasswordPrimaryAuthenticationProvider();
$provider = $this->getProvider();
$this->assertSame(
PrimaryAuthenticationProvider::TYPE_CREATE,
@ -118,7 +123,13 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
] );
$p = TestingAccessWrapper::newFromObject( new TemporaryPasswordPrimaryAuthenticationProvider() );
$p->setConfig( $config );
$p->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( false, $p->emailEnabled );
$this->assertSame( 100, $p->newPasswordExpiry );
$this->assertSame( 101, $p->passwordReminderResendTime );
@ -129,7 +140,13 @@ class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiInteg
'passwordReminderResendTime' => 43,
'allowRequiringEmailForResets' => true,
] ) );
$p->setConfig( $config );
$p->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( true, $p->emailEnabled );
$this->assertSame( 42, $p->newPasswordExpiry );
$this->assertSame( 43, $p->passwordReminderResendTime );

View file

@ -2,7 +2,10 @@
namespace MediaWiki\Auth;
use MediaWiki\MediaWikiServices;
use HashConfig;
use MediaWiki\User\UserNameUtils;
use Psr\Log\NullLogger;
use Psr\Log\Test\TestLogger;
use stdClass;
use Wikimedia\TestingAccessWrapper;
@ -15,7 +18,7 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
public function testConstructor() {
$provider = new ThrottlePreAuthenticationProvider();
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$config = new \HashConfig( [
$config = new HashConfig( [
'AccountCreationThrottle' => [ [
'count' => 123,
'seconds' => 86400,
@ -25,7 +28,13 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'seconds' => 300,
] ],
] );
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( [
'accountCreationThrottle' => [ [ 'count' => 123, 'seconds' => 86400 ] ],
'passwordAttemptThrottle' => [ [ 'count' => 5, 'seconds' => 300 ] ]
@ -44,7 +53,7 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
] );
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$config = new \HashConfig( [
$config = new HashConfig( [
'AccountCreationThrottle' => [ [
'count' => 123,
'seconds' => 86400,
@ -54,7 +63,13 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'seconds' => 300,
] ],
] );
$provider->setConfig( $config );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertSame( [
'accountCreationThrottle' => [ [ 'count' => 43, 'seconds' => 10000 ] ],
'passwordAttemptThrottle' => [ [ 'count' => 11, 'seconds' => 100 ] ],
@ -63,10 +78,17 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
$cache = new \HashBagOStuff();
$provider = new ThrottlePreAuthenticationProvider( [ 'cache' => $cache ] );
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$provider->setConfig( new \HashConfig( [
$config = new HashConfig( [
'AccountCreationThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
'PasswordAttemptThrottle' => [ [ 'count' => 1, 'seconds' => 1 ] ],
] ) );
] );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->createNoOpMock( AuthManager::class ),
$this->createHookContainer(),
$config,
$this->createNoOpMock( UserNameUtils::class )
);
$accountCreationThrottle = TestingAccessWrapper::newFromObject(
$providerPriv->accountCreationThrottle );
$this->assertSame( $cache, $accountCreationThrottle->cache );
@ -81,12 +103,16 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'passwordAttemptThrottle' => [],
'cache' => new \HashBagOStuff(),
] );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setConfig( new \HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ) );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
$this->createNoOpMock( NullLogger::class ),
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ),
$this->createNoOpMock( UserNameUtils::class )
);
$this->assertEquals(
\StatusValue::newGood(),
@ -113,13 +139,16 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'accountCreationThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
'cache' => new \HashBagOStuff(),
] );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setConfig( new \HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ) );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->setHookContainer( MediaWikiServices::getInstance()->getHookContainer() );
$provider->init(
new NullLogger(),
$this->getServiceContainer()->getAuthManager(),
$this->getServiceContainer()->getHookContainer(),
new HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ),
$this->createNoOpMock( UserNameUtils::class )
);
$user = \User::newFromName( 'RandomUser' );
$creator = \User::newFromName( $creatorname );
@ -164,12 +193,16 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'passwordAttemptThrottle' => [ [ 'count' => 2, 'seconds' => 86400 ] ],
'cache' => new \HashBagOStuff(),
] );
$provider->setLogger( new \Psr\Log\NullLogger() );
$provider->setConfig( new \HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ) );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
new NullLogger(),
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ),
$this->createNoOpMock( UserNameUtils::class )
);
$req = new UsernameAuthenticationRequest;
$req->username = 'SomeUser';
@ -210,12 +243,16 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'passwordAttemptThrottle' => [],
'cache' => new \HashBagOStuff(),
] );
$provider->setLogger( new \TestLogger );
$provider->setConfig( new \HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ) );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
new TestLogger(),
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ),
$this->createNoOpMock( UserNameUtils::class )
);
$provider->postAuthentication( \User::newFromName( 'SomeUser' ),
AuthenticationResponse::newPass() );
@ -224,12 +261,16 @@ class ThrottlePreAuthenticationProviderTest extends \MediaWikiIntegrationTestCas
'cache' => new \HashBagOStuff(),
] );
$logger = new \TestLogger( true );
$provider->setLogger( $logger );
$provider->setConfig( new \HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ) );
$provider->setManager( MediaWikiServices::getInstance()->getAuthManager() );
$provider->init(
$logger,
$this->getServiceContainer()->getAuthManager(),
$this->createHookContainer(),
new HashConfig( [
'AccountCreationThrottle' => null,
'PasswordAttemptThrottle' => null,
] ),
$this->createNoOpMock( UserNameUtils::class )
);
$provider->postAuthentication( \User::newFromName( 'SomeUser' ),
AuthenticationResponse::newPass() );
$this->assertSame( [

View file

@ -2,8 +2,12 @@
namespace MediaWiki\Tests\Unit\Auth;
use Config;
use MediaWiki\Auth\AbstractAuthenticationProvider;
use MediaWiki\Auth\AuthManager;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\User\UserNameUtils;
use Psr\Log\LoggerInterface;
use Wikimedia\TestingAccessWrapper;
/**
@ -15,18 +19,35 @@ class AbstractAuthenticationProviderTest extends \MediaWikiUnitTestCase {
$provider = $this->getMockForAbstractClass( AbstractAuthenticationProvider::class );
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$obj = $this->getMockForAbstractClass( \Psr\Log\LoggerInterface::class );
// test AbstractAuthenticationProvider::init
$logger = $this->getMockForAbstractClass( LoggerInterface::class );
$authManager = $this->createMock( AuthManager::class );
$hookContainer = $this->createMock( HookContainer::class );
$config = $this->getMockForAbstractClass( Config::class );
$userNameUtils = $this->createNoOpMock( UserNameUtils::class );
$provider->init( $logger, $authManager, $hookContainer, $config, $userNameUtils );
$this->assertSame( $logger, $providerPriv->logger );
$this->assertSame( $authManager, $providerPriv->manager );
$this->assertSame( $hookContainer, $providerPriv->hookContainer );
$this->assertSame( $config, $providerPriv->config );
$this->assertSame( $userNameUtils, $providerPriv->userNameUtils );
// test AbstractAuthenticationProvider::setLogger
$obj = $this->getMockForAbstractClass( LoggerInterface::class );
$provider->setLogger( $obj );
$this->assertSame( $obj, $providerPriv->logger, 'setLogger' );
// test AbstractAuthenticationProvider::setManager
$obj = $this->createMock( AuthManager::class );
$provider->setManager( $obj );
$this->assertSame( $obj, $providerPriv->manager, 'setManager' );
$obj = $this->getMockForAbstractClass( \Config::class );
// test AbstractAuthenticationProvider::setConfig
$obj = $this->getMockForAbstractClass( Config::class );
$provider->setConfig( $obj );
$this->assertSame( $obj, $providerPriv->config, 'setConfig' );
// test AbstractAuthenticationProvider::getUniqueId
$this->assertIsString( $provider->getUniqueId(), 'getUniqueId' );
}
}