| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User\Registration; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\User\Registration\IUserRegistrationProvider; |
| 9 |
use MediaWiki\User\Registration\LocalUserRegistrationProvider; |
| 10 |
use MediaWiki\User\Registration\UserRegistrationLookup; |
| 11 |
use MediaWiki\User\UserIdentityValue; |
| 12 |
use MediaWikiUnitTestCase; |
| 13 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class UserRegistrationLookupTest extends MediaWikiUnitTestCase { |
| 19 |
|
| 20 |
public function testIsRegistered() { |
| 21 |
$lookup = new UserRegistrationLookup( |
| 22 |
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [ |
| 23 |
MainConfigNames::UserRegistrationProviders => [ |
| 24 |
'local' => [ |
| 25 |
'class' => LocalUserRegistrationProvider::class |
| 26 |
], |
| 27 |
'foo' => [ |
| 28 |
'class' => 'FooUserRegistrationLookup' |
| 29 |
], |
| 30 |
] |
| 31 |
] ), |
| 32 |
$this->createNoOpMock( ObjectFactory::class ) |
| 33 |
); |
| 34 |
|
| 35 |
$this->assertTrue( $lookup->isRegistered( 'local' ) ); |
| 36 |
$this->assertTrue( $lookup->isRegistered( 'foo' ) ); |
| 37 |
$this->assertFalse( $lookup->isRegistered( 'bar' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
public function testGetRegistration() { |
| 41 |
$userIdentity = new UserIdentityValue( 123, 'Admin' ); |
| 42 |
$userRegistrationProviderMock = $this->createMock( IUserRegistrationProvider::class ); |
| 43 |
$userRegistrationProviderMock->expects( $this->once() ) |
| 44 |
->method( 'fetchRegistration' ) |
| 45 |
->with( $userIdentity ) |
| 46 |
->willReturn( '20200101000000' ); |
| 47 |
$objectFactoryMock = $this->createMock( ObjectFactory::class ); |
| 48 |
$objectFactoryMock->expects( $this->once() ) |
| 49 |
->method( 'createObject' ) |
| 50 |
->with( [ 'class' => LocalUserRegistrationProvider::class ] ) |
| 51 |
->willReturn( $userRegistrationProviderMock ); |
| 52 |
|
| 53 |
$lookup = new UserRegistrationLookup( |
| 54 |
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [ |
| 55 |
MainConfigNames::UserRegistrationProviders => [ |
| 56 |
'local' => [ |
| 57 |
'class' => LocalUserRegistrationProvider::class |
| 58 |
], |
| 59 |
] |
| 60 |
] ), |
| 61 |
$objectFactoryMock |
| 62 |
); |
| 63 |
|
| 64 |
$this->assertSame( '20200101000000', $lookup->getRegistration( $userIdentity ) ); |
| 65 |
} |
| 66 |
|
| 67 |
public function testGetRegistrationFails() { |
| 68 |
$this->expectException( InvalidArgumentException::class ); |
| 69 |
|
| 70 |
$userIdentity = new UserIdentityValue( 123, 'Admin' ); |
| 71 |
$lookup = new UserRegistrationLookup( |
| 72 |
new ServiceOptions( UserRegistrationLookup::CONSTRUCTOR_OPTIONS, [ |
| 73 |
MainConfigNames::UserRegistrationProviders => [] |
| 74 |
] ), |
| 75 |
$this->createNoOpMock( ObjectFactory::class ) |
| 76 |
); |
| 77 |
$lookup->getRegistration( $userIdentity, 'invalid' ); |
| 78 |
} |
| 79 |
} |