| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User\Registration; |
| 4 |
|
| 5 |
use MediaWiki\Config\ConfigException; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\User\Registration\IUserRegistrationProvider; |
| 8 |
use MediaWiki\User\Registration\UserRegistrationLookup; |
| 9 |
use MediaWiki\User\UserIdentity; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class UserRegistrationLookupIntegrationTest extends MediaWikiIntegrationTestCase { |
| 17 |
public function testLocalRequired() { |
| 18 |
$this->expectException( ConfigException::class ); |
| 19 |
|
| 20 |
$this->overrideConfigValue( MainConfigNames::UserRegistrationProviders, [] ); |
| 21 |
$this->assertInstanceOf( |
| 22 |
UserRegistrationLookup::class, |
| 23 |
$this->getServiceContainer()->getUserRegistrationLookup() |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
public function testLocal() { |
| 28 |
$user = $this->getMutableTestUser()->getUser(); |
| 29 |
$dbw = $this->getDb(); |
| 30 |
$dbw->newUpdateQueryBuilder() |
| 31 |
->update( 'user' ) |
| 32 |
->set( [ 'user_registration' => $dbw->timestamp( '20050101000000' ) ] ) |
| 33 |
->where( [ 'user_id' => $user->getId() ] ) |
| 34 |
->caller( __METHOD__ ) |
| 35 |
->execute(); |
| 36 |
|
| 37 |
$this->assertSame( |
| 38 |
'20050101000000', |
| 39 |
$this->getServiceContainer()->getUserRegistrationLookup()->getRegistration( |
| 40 |
$this->getServiceContainer()->getUserFactory()->newFromName( $user->getName() ) |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function testCustom() { |
| 46 |
$providers = $this->getConfVar( MainConfigNames::UserRegistrationProviders ); |
| 47 |
$providers['test-foo'] = [ |
| 48 |
'factory' => static function () { |
| 49 |
return new class implements IUserRegistrationProvider { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function fetchRegistration( UserIdentity $user ) { |
| 54 |
return '20230101000000'; |
| 55 |
} |
| 56 |
}; |
| 57 |
} |
| 58 |
]; |
| 59 |
$this->overrideConfigValue( MainConfigNames::UserRegistrationProviders, $providers ); |
| 60 |
|
| 61 |
$user = $this->getTestUser()->getUser(); |
| 62 |
$this->assertSame( |
| 63 |
'20230101000000', |
| 64 |
$this->getServiceContainer()->getUserRegistrationLookup()->getRegistration( |
| 65 |
$user, |
| 66 |
'test-foo' |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
} |