Why: Temporary accounts introduced as part of IP Masking are supposed to expire 1 year after their registration. Accounts can have multiple registration timestamps when in a wiki-farm setup, depending on which wiki we consult. To implement the account expiration logic, we need to know the global (first) registration timestamp. Similar to CentralIdLookup, a concept of a registration date provider is introduced. Unlike IDs, users can have multiple kinds of registration dates (Wikimedia recognizes local and global, but third parties can have different needs). For that reason, any number of registration providers can be registered at any given time; caller determines which one is requested. The default is `local`, which is the only provider that is guaranteed to exist. What: * Add UserRegistrationLookup * Add UserRegistrationProviders config variable Bug: T344694 Change-Id: If9fa12a392064dd504590a861a175e3604a34fab
79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Tests\User\Registration;
|
|
|
|
use InvalidArgumentException;
|
|
use MediaWiki\Config\ServiceOptions;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\User\Registration\IUserRegistrationProvider;
|
|
use MediaWiki\User\Registration\LocalUserRegistrationProvider;
|
|
use MediaWiki\User\Registration\UserRegistrationLookup;
|
|
use MediaWiki\User\UserIdentityValue;
|
|
use MediaWikiUnitTestCase;
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
|
|
|
/**
|
|
* @covers \MediaWiki\User\Registration\UserRegistrationLookup
|
|
*/
|
|
class UserRegistrationLookupTest extends MediaWikiUnitTestCase {
|
|
|
|
public function testIsRegistered() {
|
|
$lookup = new UserRegistrationLookup(
|
|
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [
|
|
MainConfigNames::UserRegistrationProviders => [
|
|
'local' => [
|
|
'class' => LocalUserRegistrationProvider::class
|
|
],
|
|
'foo' => [
|
|
'class' => 'FooUserRegistrationLookup'
|
|
],
|
|
]
|
|
] ),
|
|
$this->createNoOpMock( ObjectFactory::class )
|
|
);
|
|
|
|
$this->assertTrue( $lookup->isRegistered( 'local' ) );
|
|
$this->assertTrue( $lookup->isRegistered( 'foo' ) );
|
|
$this->assertFalse( $lookup->isRegistered( 'bar' ) );
|
|
}
|
|
|
|
public function testGetRegistration() {
|
|
$userIdentity = new UserIdentityValue( 123, 'Admin' );
|
|
$userRegistrationProviderMock = $this->createMock( IUserRegistrationProvider::class );
|
|
$userRegistrationProviderMock->expects( $this->once() )
|
|
->method( 'fetchRegistration' )
|
|
->with( $userIdentity )
|
|
->willReturn( '20200101000000' );
|
|
$objectFactoryMock = $this->createMock( ObjectFactory::class );
|
|
$objectFactoryMock->expects( $this->once() )
|
|
->method( 'createObject' )
|
|
->with( [ 'class' => LocalUserRegistrationProvider::class ] )
|
|
->willReturn( $userRegistrationProviderMock );
|
|
|
|
$lookup = new UserRegistrationLookup(
|
|
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [
|
|
MainConfigNames::UserRegistrationProviders => [
|
|
'local' => [
|
|
'class' => LocalUserRegistrationProvider::class
|
|
],
|
|
]
|
|
] ),
|
|
$objectFactoryMock
|
|
);
|
|
|
|
$this->assertSame( '20200101000000', $lookup->getRegistration( $userIdentity ) );
|
|
}
|
|
|
|
public function testGetRegistrationFails() {
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
$userIdentity = new UserIdentityValue( 123, 'Admin' );
|
|
$lookup = new UserRegistrationLookup(
|
|
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [
|
|
MainConfigNames::UserRegistrationProviders => []
|
|
] ),
|
|
$this->createNoOpMock( ObjectFactory::class )
|
|
);
|
|
$lookup->getRegistration( $userIdentity, 'invalid' );
|
|
}
|
|
}
|