Debian Codegraph @

tests/phpunit/integration/includes/user/UserSelectQueryBuilderTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User;
4
5 use MediaWiki\Request\FauxRequest;
6 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
7 use MediaWiki\User\UserIdentityValue;
8 use MediaWiki\User\UserSelectQueryBuilder;
9 use Wikimedia\Rdbms\SelectQueryBuilder;
10
11 /**
12 * @group Database
13 * @covers \MediaWiki\User\UserSelectQueryBuilder
14 * @coversDefaultClass \MediaWiki\User\UserSelectQueryBuilder
15 */
16 class UserSelectQueryBuilderTest extends ActorStoreTestBase {
17
18 use TempUserTestTrait;
19
20 public static function provideFetchUserIdentitiesByNamePrefix() {
21 yield 'nothing found' => [
22 'z_z_Z_Z_z_Z_z_z', // $prefix
23 [ 'limit' => 100 ], // $options
24 [], // $expected
25 ];
26 yield 'default parameters' => [
27 'Test', // $prefix
28 [ 'limit' => 100 ], // $options
29 [
30 new UserIdentityValue( 24, 'TestUser' ),
31 new UserIdentityValue( 25, 'TestUser1' ),
32 ], // $expected
33 ];
34 yield 'limited' => [
35 'Test', // $prefix
36 [ 'limit' => 1 ], // $options
37 [
38 new UserIdentityValue( 24, 'TestUser' ),
39 ], // $expected
40 ];
41 yield 'sorted' => [
42 'Test', // $prefix
43 [
44 'sort' => UserSelectQueryBuilder::SORT_DESC,
45 'limit' => 100,
46 ], // $options
47 [
48 new UserIdentityValue( 25, 'TestUser1' ),
49 new UserIdentityValue( 24, 'TestUser' ),
50 ], // $expected
51 ];
52 }
53
54 /**
55 * @dataProvider provideFetchUserIdentitiesByNamePrefix
56 */
57 public function testFetchUserIdentitiesByNamePrefix( string $prefix, array $options, array $expected ) {
58 $queryBuilder = $this->getStore()
59 ->newSelectQueryBuilder()
60 ->limit( $options['limit'] )
61 ->whereUserNamePrefix( $prefix )
62 ->caller( __METHOD__ )
63 ->orderByName( $options['sort'] ?? SelectQueryBuilder::SORT_ASC );
64 $actors = iterator_to_array( $queryBuilder->fetchUserIdentities() );
65 $this->assertSameSize( $expected, $actors );
66 foreach ( $expected as $idx => $expectedActor ) {
67 $this->assertSameActors( $expectedActor, $actors[$idx] );
68 }
69 }
70
71 public static function provideFetchUserIdentitiesByUserIds() {
72 yield 'default parameters' => [
73 [ 24, 25 ], // ids
74 [], // $options
75 [
76 new UserIdentityValue( 24, 'TestUser' ),
77 new UserIdentityValue( 25, 'TestUser1' ),
78 ], // $expected
79 ];
80 yield 'sorted' => [
81 [ 24, 25 ], // ids
82 [ 'sort' => UserSelectQueryBuilder::SORT_DESC ], // $options
83 [
84 new UserIdentityValue( 25, 'TestUser1' ),
85 new UserIdentityValue( 24, 'TestUser' ),
86 ], // $expected
87 ];
88 }
89
90 /**
91 * @dataProvider provideFetchUserIdentitiesByUserIds
92 */
93 public function testFetchUserIdentitiesByUserIds( array $ids, array $options, array $expected ) {
94 $actors = iterator_to_array(
95 $this->getStore()
96 ->newSelectQueryBuilder()
97 ->whereUserIds( $ids )
98 ->caller( __METHOD__ )
99 ->orderByUserId( $options['sort'] ?? SelectQueryBuilder::SORT_ASC )
100 ->fetchUserIdentities()
101 );
102 $this->assertSameSize( $expected, $actors );
103 foreach ( $expected as $idx => $expectedActor ) {
104 $this->assertSameActors( $expectedActor, $actors[$idx] );
105 }
106 }
107
108 public static function provideFetchUserIdentitiesByNames() {
109 yield 'default parameters' => [
110 [ 'TestUser', 'TestUser1' ], // $names
111 [], // $options
112 [
113 new UserIdentityValue( 24, 'TestUser' ),
114 new UserIdentityValue( 25, 'TestUser1' ),
115 ], // $expected
116 ];
117 yield 'sorted' => [
118 [ 'TestUser', 'TestUser1' ], // $names
119 [ 'sort' => UserSelectQueryBuilder::SORT_DESC ], // $options
120 [
121 new UserIdentityValue( 25, 'TestUser1' ),
122 new UserIdentityValue( 24, 'TestUser' ),
123 ], // $expected
124 ];
125 yield 'with IPs' => [
126 [ self::IP ], // $names
127 [], // $options
128 [
129 new UserIdentityValue( 0, self::IP ),
130 ], // $expected
131 ];
132 yield 'with IPs, normalization' => [
133 [ strtolower( self::IP ), self::IP ], // $names
134 [], // $options
135 [
136 new UserIdentityValue( 0, self::IP ),
137 ], // $expected
138 ];
139 }
140
141 /**
142 * @dataProvider provideFetchUserIdentitiesByNames
143 */
144 public function testFetchUserIdentitiesByNames( array $names, array $options, array $expected ) {
145 $actors = iterator_to_array(
146 $this->getStore()
147 ->newSelectQueryBuilder()
148 ->whereUserNames( $names )
149 ->caller( __METHOD__ )
150 ->orderByUserId( $options['sort'] ?? SelectQueryBuilder::SORT_ASC )
151 ->fetchUserIdentities()
152 );
153 $this->assertSameSize( $expected, $actors );
154 foreach ( $expected as $idx => $expectedActor ) {
155 $this->assertSameActors( $expectedActor, $actors[$idx] );
156 }
157 }
158
159 /**
160 * @covers ::fetchUserIdentity
161 */
162 public function testFetchUserIdentity() {
163 $this->assertSameActors(
164 new UserIdentityValue( 24, 'TestUser' ),
165 $this->getStore()
166 ->newSelectQueryBuilder()
167 ->whereUserIds( 24 )
168 ->fetchUserIdentity()
169 );
170 }
171
172 /**
173 * @covers ::fetchUserNames
174 */
175 public function testFetchUserNames() {
176 $this->assertArrayEquals(
177 [ 'TestUser', 'TestUser1' ],
178 $this->getStore()
179 ->newSelectQueryBuilder()
180 ->conds( [ 'actor_id' => [ 42, 44 ] ] )
181 ->fetchUserNames()
182 );
183 }
184
185 /**
186 * @covers ::registered
187 */
188 public function testRegistered() {
189 $actors = iterator_to_array(
190 $this->getStore()
191 ->newSelectQueryBuilder()
192 ->conds( [ 'actor_id' => [ 42, 43 ] ] )
193 ->registered()
194 ->fetchUserIdentities()
195 );
196 $this->assertCount( 1, $actors );
197 $this->assertSameActors(
198 new UserIdentityValue( 24, 'TestUser' ),
199 $actors[0]
200 );
201 }
202
203 /**
204 * @covers ::anon
205 */
206 public function testAnon() {
207 $actors = iterator_to_array(
208 $this->getStore()
209 ->newSelectQueryBuilder()
210 ->limit( 100 )
211 ->whereUserNamePrefix( '' )
212 ->anon()
213 ->fetchUserIdentities()
214 );
215 $this->assertCount( 2, $actors );
216 $this->assertSameActors(
217 new UserIdentityValue( 0, self::IP ),
218 $actors[0]
219 );
220 }
221
222 /**
223 * @covers ::anon
224 * @covers ::named
225 * @dataProvider provideNamedAndTempMethodNames
226 */
227 public function testNamedAndTempWhenTempUserAutoCreateDisabled( $methodName ) {
228 $this->enableAutoCreateTempUser();
229 // Add a temporary accounts for the test
230 $tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
231 ->create( null, new FauxRequest() );
232 $this->assertStatusOK( $tempUserCreateStatus );
233 $tempUser = $tempUserCreateStatus->getUser();
234
235 $this->resetServices();
236 $this->disableAutoCreateTempUser();
237
238 $actors = iterator_to_array(
239 $this->getStore()
240 ->newSelectQueryBuilder()
241 ->limit( 100 )
242 ->whereUserNamePrefix( '' )
243 ->$methodName()
244 ->fetchUserIdentities()
245 );
246 if ( $methodName === 'temp' ) {
247 $this->assertCount( 0, $actors );
248 } else {
249 // With temp users disabled, the temp user is treated as a normal user and included.
250 $this->assertArrayEquals(
251 array_merge(
252 array_map( fn ( $userRow ) => $userRow['actor_user'], self::TEST_USERS ),
253 [ $tempUser->getId() ]
254 ),
255 array_map( fn ( $actor ) => $actor->getId(), $actors )
256 );
257 }
258 }
259
260 public static function provideNamedAndTempMethodNames() {
261 return [
262 '::temp' => [ 'temp' ],
263 '::named' => [ 'named' ],
264 ];
265 }
266
267 /**
268 * @covers ::named
269 */
270 public function testNamed() {
271 $this->enableAutoCreateTempUser();
272 // Add a temporary accounts for the test
273 $tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
274 ->create( null, new FauxRequest() );
275 $this->assertStatusOK( $tempUserCreateStatus );
276 $tempUser = $tempUserCreateStatus->getUser();
277 $actors = iterator_to_array(
278 $this->getStore()
279 ->newSelectQueryBuilder()
280 ->limit( 100 )
281 ->whereUserNamePrefix( '' )
282 ->named()
283 ->fetchUserIdentities()
284 );
285 // The temp user should not appear in the results list.
286 $this->assertCount( 5, $actors );
287 foreach ( $actors as $actor ) {
288 $this->assertNotSame( $tempUser->getId(), $actor->getId() );
289 }
290 }
291
292 /**
293 * @covers ::temp
294 */
295 public function testTemp() {
296 $this->enableAutoCreateTempUser();
297 // Add a temporary accounts for the test
298 $tempUserCreateStatus = $this->getServiceContainer()->getTempUserCreator()
299 ->create( null, new FauxRequest() );
300 $this->assertStatusOK( $tempUserCreateStatus );
301 $tempUser = $tempUserCreateStatus->getUser();
302 $actors = iterator_to_array(
303 $this->getStore()
304 ->newSelectQueryBuilder()
305 ->limit( 100 )
306 ->whereUserNamePrefix( '' )
307 ->temp()
308 ->fetchUserIdentities()
309 );
310 // The temp user should be the only user identity returned.
311 $this->assertCount( 1, $actors );
312 $this->assertSameActors( $tempUser, $actors[0] );
313 }
314
315 /**
316 * @covers ::hidden
317 */
318 public function testHidden() {
319 $hiddenUser = $this->getMutableTestUser()->getUserIdentity();
320 $normalUser = $this->getMutableTestUser()->getUserIdentity();
321 $this->getServiceContainer()->getBlockUserFactory()->newBlockUser(
322 $hiddenUser,
323 $this->getTestSysop()->getUser(),
324 'infinity',
325 'Test',
326 [
327 'isHideUser' => true
328 ]
329 )->placeBlockUnsafe( true );
330
331 // hidden set to true
332 $actors = iterator_to_array(
333 $this->getStore()
334 ->newSelectQueryBuilder()
335 ->limit( 100 )
336 ->whereUserIds( [
337 $hiddenUser->getId(),
338 $normalUser->getId()
339 ] )
340 ->hidden( true )
341 ->fetchUserIdentities()
342 );
343 $this->assertCount( 1, $actors );
344 $this->assertSameActors(
345 $hiddenUser,
346 $actors[0]
347 );
348
349 // hidden set to false
350 $actors = iterator_to_array(
351 $this->getStore()
352 ->newSelectQueryBuilder()
353 ->limit( 100 )
354 ->whereUserIds( [
355 $hiddenUser->getId(),
356 $normalUser->getId()
357 ] )
358 ->hidden( false )
359 ->fetchUserIdentities()
360 );
361 $this->assertCount( 1, $actors );
362 $this->assertSameActors(
363 $normalUser,
364 $actors[0]
365 );
366
367 // hidden not set
368 $usernames = $this->getStore()
369 ->newSelectQueryBuilder()
370 ->limit( 100 )
371 ->whereUserIds( [
372 $hiddenUser->getId(),
373 $normalUser->getId()
374 ] )
375 ->fetchUserNames();
376 $this->assertCount( 2, $usernames );
377 $this->assertArrayEquals(
378 [ $normalUser->getName(), $hiddenUser->getName() ],
379 $usernames
380 );
381 }
382 }