Debian Codegraph @

tests/phpunit/unit/includes/user/CentralId/CentralIdLookupTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User\CentralId;
4
5 use InvalidArgumentException;
6 use LogicException;
7 use MediaWiki\Permissions\Authority;
8 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
9 use MediaWiki\User\CentralId\CentralIdLookup;
10 use MediaWiki\User\User;
11 use MediaWiki\User\UserFactory;
12 use MediaWiki\User\UserIdentity;
13 use MediaWiki\User\UserIdentityLookup;
14 use MediaWiki\User\UserIdentityValue;
15 use MediaWikiUnitTestCase;
16 use Wikimedia\Rdbms\IDBAccessObject;
17 use Wikimedia\TestingAccessWrapper;
18
19 /**
20 * @covers \MediaWiki\User\CentralId\CentralIdLookup
21 */
22 class CentralIdLookupTest extends MediaWikiUnitTestCase {
23 use MockAuthorityTrait;
24
25 public function testGetProviderId() {
26 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
27 $mock->init( 'this is a test', $this->createNoOpMock( UserIdentityLookup::class ),
28 $this->createNoOpMock( UserFactory::class ) );
29 $this->assertSame( 'this is a test', $mock->getProviderId() );
30 }
31
32 public function testRepeatingInitThrows() {
33 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
34 $mock->init( 'foo', $this->createNoOpMock( UserIdentityLookup::class ),
35 $this->createNoOpMock( UserFactory::class ) );
36 $this->expectException( LogicException::class );
37 $mock->init( 'bar', $this->createNoOpMock( UserIdentityLookup::class ),
38 $this->createNoOpMock( UserFactory::class ) );
39 }
40
41 public function testCheckAudience() {
42 $mock = TestingAccessWrapper::newFromObject(
43 $this->getMockForAbstractClass( CentralIdLookup::class )
44 );
45 $userFactory = $this->createMock( UserFactory::class );
46 $userFactory->method( 'newAnonymous' )->willReturnCallback( function () {
47 $user = $this->createNoOpMock( User::class );
48 $user->mFrom = 'defaults';
49 return $user;
50 } );
51 $mock->init( 'test', $this->createNoOpMock( UserIdentityLookup::class ), $userFactory );
52
53 $authority = $this->mockAnonUltimateAuthority();
54 $this->assertSame( $authority, $mock->checkAudience( $authority ) );
55
56 $authority = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC );
57 $this->assertInstanceOf( Authority::class, $authority );
58 // Should be an empty User object, don't trigger loading that requires integration
59 // if a user is created from "defaults" then its an anonymous default user
60 $this->assertInstanceOf( User::class, $authority );
61 $this->assertSame( 'defaults', $authority->mFrom );
62
63 $this->assertNull( $mock->checkAudience( CentralIdLookup::AUDIENCE_RAW ) );
64
65 try {
66 $mock->checkAudience( 100 );
67 $this->fail( 'Expected exception not thrown' );
68 } catch ( InvalidArgumentException $ex ) {
69 $this->assertSame( 'Invalid audience', $ex->getMessage() );
70 }
71 }
72
73 public function testNameFromCentralId() {
74 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
75 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
76 ->with(
77 [ 15 => null ],
78 CentralIdLookup::AUDIENCE_RAW,
79 IDBAccessObject::READ_LATEST
80 )
81 ->willReturn( [ 15 => 'FooBar' ] );
82
83 $this->assertSame(
84 'FooBar',
85 $mock->nameFromCentralId( 15, CentralIdLookup::AUDIENCE_RAW, IDBAccessObject::READ_LATEST )
86 );
87 }
88
89 /**
90 * @dataProvider provideLocalUserFromCentralId
91 * @param string $name
92 * @param bool $succeeds
93 */
94 public function testLocalUserFromCentralId( $name, $succeeds ) {
95 // UserIdentityLookup expects to be asked for a UserIdentity with the
96 // name $name, and will return one only if its for UTSysop, no other
97 // users are registered
98 $lookupResult = ( $name === 'UTSysop' ? ( new UserIdentityValue( 5, $name ) ) : null );
99 $userIdentityLookup = $this->createMock( UserIdentityLookup::class );
100 $userIdentityLookup->method( 'getUserIdentityByName' )
101 ->with( $name )
102 ->willReturn( $lookupResult );
103 $userFactory = $this->createMock( UserFactory::class );
104
105 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
106 $mock->method( 'isAttached' )
107 ->willReturn( true );
108 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
109 ->with(
110 [ 42 => null ],
111 CentralIdLookup::AUDIENCE_RAW,
112 IDBAccessObject::READ_LATEST
113 )
114 ->willReturn( [ 42 => $name ] );
115
116 $mock->init( 'test', $userIdentityLookup, $userFactory );
117 $user = $mock->localUserFromCentralId(
118 42, CentralIdLookup::AUDIENCE_RAW, IDBAccessObject::READ_LATEST
119 );
120 if ( $succeeds ) {
121 $this->assertInstanceOf( UserIdentity::class, $user );
122 $this->assertSame( $name, $user->getName() );
123 } else {
124 $this->assertNull( $user );
125 }
126
127 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
128 $mock->method( 'isAttached' )
129 ->willReturn( false );
130 $mock->expects( $this->once() )->method( 'lookupCentralIds' )
131 ->with(
132 [ 42 => null ],
133 CentralIdLookup::AUDIENCE_RAW,
134 IDBAccessObject::READ_LATEST
135 )
136 ->willReturn( [ 42 => $name ] );
137 $mock->init( 'test', $userIdentityLookup, $userFactory );
138
139 $this->assertNull(
140 $mock->localUserFromCentralId( 42, CentralIdLookup::AUDIENCE_RAW, IDBAccessObject::READ_LATEST )
141 );
142 }
143
144 public static function provideLocalUserFromCentralId() {
145 return [
146 [ 'UTSysop', true ],
147 [ 'UTDoesNotExist', false ],
148 [ null, false ],
149 [ '', false ],
150 [ '<X>', false ],
151 ];
152 }
153
154 public function testCentralIdFromName() {
155 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
156 $mock->expects( $this->once() )->method( 'lookupUserNames' )
157 ->with(
158 [ 'FooBar' => 0 ],
159 CentralIdLookup::AUDIENCE_RAW,
160 IDBAccessObject::READ_LATEST
161 )
162 ->willReturn( [ 'FooBar' => 23 ] );
163
164 $this->assertSame(
165 23,
166 $mock->centralIdFromName( 'FooBar', CentralIdLookup::AUDIENCE_RAW, IDBAccessObject::READ_LATEST )
167 );
168 }
169
170 public function testCentralIdFromLocalUser() {
171 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
172 $mock->method( 'isAttached' )
173 ->willReturn( true );
174 $mock->expects( $this->once() )->method( 'lookupUserNames' )
175 ->with(
176 [ 'FooBar' => 0 ],
177 CentralIdLookup::AUDIENCE_RAW,
178 IDBAccessObject::READ_LATEST
179 )
180 ->willReturn( [ 'FooBar' => 23 ] );
181
182 $this->assertSame(
183 23,
184 $mock->centralIdFromLocalUser(
185 new UserIdentityValue( 10, 'FooBar' ),
186 CentralIdLookup::AUDIENCE_RAW,
187 IDBAccessObject::READ_LATEST
188 )
189 );
190
191 $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
192 $mock->method( 'isAttached' )
193 ->willReturn( false );
194 $mock->expects( $this->never() )->method( 'lookupUserNames' );
195
196 $this->assertSame(
197 0,
198 $mock->centralIdFromLocalUser(
199 new UserIdentityValue( 10, 'FooBar' ),
200 CentralIdLookup::AUDIENCE_RAW,
201 IDBAccessObject::READ_LATEST
202 )
203 );
204 }
205
206 }