| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User\CentralId; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Block\HideUserUtils; |
| 7 |
use MediaWiki\Config\HashConfig; |
| 8 |
use MediaWiki\Config\ServiceOptions; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 11 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 12 |
use MediaWiki\User\CentralId\CentralIdLookupFactory; |
| 13 |
use MediaWiki\User\CentralId\LocalIdLookup; |
| 14 |
use MediaWiki\User\UserFactory; |
| 15 |
use MediaWiki\User\UserIdentityLookup; |
| 16 |
use MediaWikiUnitTestCase; |
| 17 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class CentralIdLookupFactoryTest extends MediaWikiUnitTestCase { |
| 23 |
use DummyServicesTrait; |
| 24 |
|
| 25 |
|
| 26 |
private $centralLookupMock; |
| 27 |
|
| 28 |
protected function setUp(): void { |
| 29 |
parent::setUp(); |
| 30 |
$this->centralLookupMock = $this->getMockForAbstractClass( CentralIdLookup::class ); |
| 31 |
} |
| 32 |
|
| 33 |
private function makeFactory(): CentralIdLookupFactory { |
| 34 |
$services = [ |
| 35 |
'DBLoadBalancerFactory' => $this->createMock( IConnectionProvider::class ), |
| 36 |
'MainConfig' => new HashConfig( [ |
| 37 |
MainConfigNames::SharedDB => null, |
| 38 |
MainConfigNames::SharedTables => [], |
| 39 |
MainConfigNames::LocalDatabases => [], |
| 40 |
] ), |
| 41 |
'HideUserUtils' => new HideUserUtils() |
| 42 |
]; |
| 43 |
$localIdLookupTest = [ |
| 44 |
'class' => LocalIdLookup::class, |
| 45 |
'services' => [ |
| 46 |
'MainConfig', |
| 47 |
'DBLoadBalancerFactory', |
| 48 |
'HideUserUtils', |
| 49 |
] |
| 50 |
]; |
| 51 |
return new CentralIdLookupFactory( |
| 52 |
new ServiceOptions( |
| 53 |
CentralIdLookupFactory::CONSTRUCTOR_OPTIONS, |
| 54 |
[ |
| 55 |
MainConfigNames::CentralIdLookupProviders => [ |
| 56 |
'local' => $localIdLookupTest, |
| 57 |
'local2' => $localIdLookupTest, |
| 58 |
'mock' => [ 'factory' => function () { |
| 59 |
return $this->centralLookupMock; |
| 60 |
} ] |
| 61 |
], |
| 62 |
MainConfigNames::CentralIdLookupProvider => 'mock', |
| 63 |
] |
| 64 |
), |
| 65 |
$this->getDummyObjectFactory( $services ), |
| 66 |
$this->createNoOpMock( UserIdentityLookup::class ), |
| 67 |
$this->createNoOpMock( UserFactory::class ) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function testCreation() { |
| 76 |
$factory = $this->makeFactory(); |
| 77 |
|
| 78 |
$this->assertSame( $this->centralLookupMock, $factory->getLookup() ); |
| 79 |
$this->assertSame( $this->centralLookupMock, $factory->getLookup( 'mock' ) ); |
| 80 |
$this->assertSame( 'mock', $this->centralLookupMock->getProviderId() ); |
| 81 |
|
| 82 |
$local = $factory->getLookup( 'local' ); |
| 83 |
$this->assertNotSame( $this->centralLookupMock, $local ); |
| 84 |
$this->assertInstanceOf( LocalIdLookup::class, $local ); |
| 85 |
$this->assertSame( $local, $factory->getLookup( 'local' ) ); |
| 86 |
$this->assertSame( 'local', $local->getProviderId() ); |
| 87 |
|
| 88 |
$local2 = $factory->getLookup( 'local2' ); |
| 89 |
$this->assertNotSame( $local, $local2 ); |
| 90 |
$this->assertInstanceOf( LocalIdLookup::class, $local2 ); |
| 91 |
$this->assertSame( 'local2', $local2->getProviderId() ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function testUnconfiguredProvidersThrow() { |
| 98 |
$this->expectException( InvalidArgumentException::class ); |
| 99 |
$factory = $this->makeFactory(); |
| 100 |
$factory->getLookup( 'unconfigured' ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function testGetNonLocal() { |
| 107 |
$factory = $this->makeFactory(); |
| 108 |
$this->assertInstanceOf( CentralIdLookup::class, $factory->getNonLocalLookup() ); |
| 109 |
$this->assertNull( $factory->getNonLocalLookup( 'local' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function testGetProviderIds() { |
| 116 |
$factory = $this->makeFactory(); |
| 117 |
$expected = [ 'local', 'local2', 'mock' ]; |
| 118 |
$this->assertSame( $expected, $factory->getProviderIds() ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function testGetDefaultProviderId() { |
| 125 |
$factory = $this->makeFactory(); |
| 126 |
$this->assertSame( 'mock', $factory->getDefaultProviderId() ); |
| 127 |
} |
| 128 |
} |