Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User;
4
5 use CannotCreateActorException;
6 use InvalidArgumentException;
7 use MediaWiki\DAO\WikiAwareEntity;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
11 use MediaWiki\User\ActorStore;
12 use MediaWiki\User\User;
13 use MediaWiki\User\UserIdentity;
14 use MediaWiki\User\UserIdentityValue;
15 use MediaWiki\User\UserSelectQueryBuilder;
16 use stdClass;
17 use Wikimedia\Assert\PreconditionException;
18 use Wikimedia\Rdbms\IDatabase;
19 use Wikimedia\Rdbms\IDBAccessObject;
20
21 /**
22 * @covers \MediaWiki\User\ActorStore
23 * @group Database
24 */
25 class ActorStoreTest extends ActorStoreTestBase {
26 use TempUserTestTrait;
27
28 public static function provideGetActorById() {
29 yield 'getActorById, registered' => [
30 42, // $argument
31 new UserIdentityValue( 24, 'TestUser' ), // $expected
32 ];
33 yield 'getActorById, anon' => [
34 43, // $argument
35 new UserIdentityValue( 0, self::IP ), // $expected
36 ];
37 yield 'getActorById, non-existent' => [
38 4321231, // $argument
39 null, // $expected
40 ];
41 yield 'getActorById, zero' => [
42 0, // $argument
43 null, // $expected
44 ];
45 }
46
47 /**
48 * @dataProvider provideGetActorById
49 */
50 public function testGetActorById( $argument, ?UserIdentity $expected ) {
51 $actor = $this->getStore()->getActorById( $argument, $this->getDb() );
52 if ( $expected ) {
53 $this->assertNotNull( $actor );
54 $this->assertSameActors( $expected, $actor );
55
56 // test caching
57 $cachedActor = $this->getStore()->getActorById( $argument, $this->getDb() );
58 $this->assertSame( $actor, $cachedActor );
59 } else {
60 $this->assertNull( $actor );
61 }
62 }
63
64 public static function provideGetActorByMethods() {
65 yield 'getUserIdentityByName, registered' => [
66 'getUserIdentityByName', // $method
67 'TestUser', // $argument
68 new UserIdentityValue( 24, 'TestUser' ), // $expected
69 ];
70 yield 'getUserIdentityByName, non-existent' => [
71 'getUserIdentityByName', // $method
72 'TestUser_I_Do_Not_Exist', // $argument
73 null, // $expected
74 ];
75 yield 'getUserIdentityByName, anon' => [
76 'getUserIdentityByName', // $method
77 self::IP, // $argument
78 new UserIdentityValue( 0, self::IP ), // $expected
79 ];
80 yield 'getUserIdentityByName, anon, non-canonicalized IP' => [
81 'getUserIdentityByName', // $method
82 strtolower( self::IP ), // $argument
83 new UserIdentityValue( 0, self::IP ), // $expected
84 ];
85 yield 'getUserIdentityByName, user name 0' => [
86 'getUserIdentityByName', // $method
87 '0', // $argument
88 new UserIdentityValue( 26, '0' ), // $expected
89 ];
90 yield 'getUserIdentityByName, empty' => [
91 'getUserIdentityByName', // $method
92 '', // $argument
93 null, // $expected
94 ];
95 yield 'getUserIdentityByName, ip range' => [
96 'getUserIdentityByName', // $method
97 '1.1.1.1/1', // $argument
98 null, // $expected
99 ];
100 yield 'getUserIdentityByUserId, registered' => [
101 'getUserIdentityByUserId', // $method
102 24, // $argument
103 new UserIdentityValue( 24, 'TestUser' ), // $expected
104 ];
105 yield 'getUserIdentityByUserId, non-exitent' => [
106 'getUserIdentityByUserId', // $method
107 2412312, // $argument
108 null, // $expected
109 ];
110 yield 'getUserIdentityByUserId, zero' => [
111 'getUserIdentityByUserId', // $method
112 0, // $argument
113 null, // $expected
114 ];
115 }
116
117 /**
118 * @dataProvider provideGetActorByMethods
119 */
120 public function testGetActorByMethods( string $method, $argument, ?UserIdentity $expected ) {
121 $actor = $this->getStore()->$method( $argument );
122 if ( $expected ) {
123 $this->assertNotNull( $actor );
124 $this->assertSameActors( $expected, $actor );
125
126 // test caching
127 $cachedActor = $this->getStore()->$method( $argument );
128 $this->assertSame( $actor, $cachedActor );
129 } else {
130 $this->assertNull( $actor );
131 }
132 }
133
134 public static function provideUserIdentityValues() {
135 yield [ new UserIdentityValue( 24, 'TestUser' ) ];
136 yield [ new UserIdentityValue( 0, self::IP ) ];
137 }
138
139 /**
140 * @dataProvider provideUserIdentityValues
141 * @param UserIdentity $expected
142 */
143 public function testSequentialCacheRetrieval( UserIdentity $expected ) {
144 if ( $expected->getId() === 0 ) {
145 $this->disableAutoCreateTempUser();
146 }
147 // ensure UserIdentity is cached
148 $actorId = $this->getStore()->findActorId( $expected, $this->getDb() );
149
150 $cachedActorId = $this->getStore()->findActorId( $expected, $this->getDb() );
151 $this->assertSame( $actorId, $cachedActorId );
152
153 $cachedActorId = $this->getStore()->acquireActorId( $expected, $this->getDb() );
154 $this->assertSame( $actorId, $cachedActorId );
155
156 $cached = $this->getStore()->getActorById( $actorId, $this->getDb() );
157 $this->assertNotNull( $cached );
158 $this->assertSameActors( $expected, $cached );
159
160 $cached = $this->getStore()->getUserIdentityByName( $expected->getName() );
161 $this->assertNotNull( $cached );
162 $this->assertSameActors( $expected, $cached );
163
164 $userId = $expected->getId();
165 if ( $userId ) {
166 $cached = $this->getStore()->getUserIdentityByUserId( $userId );
167 $this->assertNotNull( $cached );
168 $this->assertSameActors( $expected, $cached );
169 }
170 }
171
172 public function testGetActorByIdRealUser() {
173 $user = $this->getTestUser()->getUser();
174 $actor = $this->getStore()->getActorById( $user->getActorId(), $this->getDb() );
175 $this->assertSameActors( $user, $actor );
176 }
177
178 public function testgetUserIdentityByNameRealUser() {
179 $user = $this->getTestUser()->getUser();
180 $actor = $this->getStore()->getUserIdentityByName( $user->getName() );
181 $this->assertSameActors( $user, $actor );
182 }
183
184 public function testGetUserIdentityByUserIdRealUser() {
185 $user = $this->getTestUser()->getUser();
186 $actor = $this->getStore()->getUserIdentityByUserId( $user->getId() );
187 $this->assertSameActors( $user, $actor );
188 }
189
190 public static function provideNewActorFromRow() {
191 yield 'full row' => [
192 UserIdentity::LOCAL, // $wikiId
193 (object)[ 'actor_id' => 42, 'actor_name' => 'TestUser', 'actor_user' => 24 ], // $row
194 new UserIdentityValue( 24, 'TestUser' ), // $expected
195 ];
196 yield 'full row, strings' => [
197 UserIdentity::LOCAL, // $wikiId
198 (object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => '24' ], // $row
199 new UserIdentityValue( 24, 'TestUser' ), // $expected
200 ];
201 yield 'null user' => [
202 UserIdentity::LOCAL, // $wikiId
203 (object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => null ], // $row
204 new UserIdentityValue( 0, 'TestUser' ), // $expected
205 ];
206 yield 'zero user' => [
207 UserIdentity::LOCAL, // $wikiId
208 (object)[ 'actor_id' => '42', 'actor_name' => 'TestUser', 'actor_user' => 0 ], // $row
209 new UserIdentityValue( 0, 'TestUser' ), // $expected
210 ];
211 yield 'cross-wiki' => [
212 'acmewiki', // $wikiId
213 (object)[ 'actor_id' => 42, 'actor_name' => 'TestUser', 'actor_user' => 24 ], // $row
214 new UserIdentityValue( 24, 'TestUser', 'acmewiki' ), // $expected
215 ];
216 yield 'user name 0' => [
217 UserIdentity::LOCAL, // $wikiId
218 (object)[ 'actor_id' => '46', 'actor_name' => '0', 'actor_user' => 26 ], // $row
219 new UserIdentityValue( 26, '0' ), // $expected
220 ];
221 }
222
223 /**
224 * @dataProvider provideNewActorFromRow
225 */
226 public function testNewActorFromRow( $wikiId, stdClass $row, UserIdentity $expected ) {
227 $actor = $this->getStore( $wikiId )->newActorFromRow( $row );
228 $this->assertSameActors( $expected, $actor, $wikiId );
229 }
230
231 public static function provideNewActorFromRow_exception() {
232 yield 'null actor' => [
233 (object)[ 'actor_id' => '0', 'actor_name' => 'TestUser', 'actor_user' => 0 ], // $row
234 ];
235 yield 'zero actor' => [
236 (object)[ 'actor_id' => null, 'actor_name' => 'TestUser', 'actor_user' => 0 ], // $row
237 ];
238 yield 'empty name' => [
239 (object)[ 'actor_id' => '10', 'actor_name' => '', 'actor_user' => 15 ], // $row
240 ];
241 }
242
243 /**
244 * @dataProvider provideNewActorFromRow_exception
245 */
246 public function testNewActorFromRow_exception( stdClass $row ) {
247 $this->expectException( InvalidArgumentException::class );
248 $this->getStore()->newActorFromRow( $row );
249 }
250
251 public static function provideNewActorFromRowFields() {
252 yield 'full row' => [
253 UserIdentity::LOCAL, // $wikiId
254 42, // $actorId
255 'TestUser', // $name
256 24, // $userId
257 new UserIdentityValue( 24, 'TestUser' ), // $expected
258 ];
259 yield 'full row, strings' => [
260 UserIdentity::LOCAL, // $wikiId
261 '42', // $actorId
262 'TestUser', // $name
263 '24', // $userId
264 new UserIdentityValue( 24, 'TestUser' ), // $expected
265 ];
266 yield 'null user' => [
267 UserIdentity::LOCAL, // $wikiId
268 42, // $actorId
269 'TestUser', // $name
270 null, // $userId
271 new UserIdentityValue( 0, 'TestUser' ), // $expected
272 ];
273 yield 'zero user' => [
274 UserIdentity::LOCAL, // $wikiId
275 42, // $actorId
276 'TestUser', // $name
277 0, // $userId
278 new UserIdentityValue( 0, 'TestUser' ), // $expected
279 ];
280 yield 'user name 0' => [
281 UserIdentity::LOCAL, // $wikiId
282 46, // $actorId
283 '0', // $name
284 26, // $userId
285 new UserIdentityValue( 26, '0' ), // $expected
286 ];
287 yield 'cross-wiki' => [
288 'acmewiki', // $wikiId
289 42, // $actorId
290 'TestUser', // $name
291 0, // $userId
292 new UserIdentityValue( 0, 'TestUser', 'acmewiki' ), // $expected
293 ];
294 }
295
296 /**
297 * @dataProvider provideNewActorFromRowFields
298 */
299 public function testNewActorFromRowFields( $wikiId, $actorId, $name, $userId, UserIdentity $expected ) {
300 $actor = $this->getStore( $wikiId )->newActorFromRowFields( $userId, $name, $actorId );
301 $this->assertSameActors( $expected, $actor, $wikiId );
302 }
303
304 public static function provideNewActorFromRowFields_exception() {
305 yield 'empty name' => [
306 42, // $actorId
307 '', // $name
308 24, // $userId
309 ];
310 yield 'null name' => [
311 42, // $actorId
312 null, // $name
313 24, // $userId
314 ];
315 yield 'null actor' => [
316 null, // $actorId
317 'TestUser', // $name
318 null, // $userId
319 ];
320 yield 'zero actor' => [
321 0, // $actorId
322 'TestUser', // $name
323 null, // $userId
324 ];
325 }
326
327 /**
328 * @dataProvider provideNewActorFromRowFields_exception
329 */
330 public function testNewActorFromRowFields_exception( $actorId, $name, $userId ) {
331 $this->expectException( InvalidArgumentException::class );
332 $this->getStore()->newActorFromRowFields( $userId, $name, $actorId );
333 }
334
335 public static function provideFindActorId() {
336 yield 'anon, local' => [
337 static function () {
338 return new UserIdentityValue( 0, self::IP );
339 }, // $actorCallback
340 43, // $expected
341 ];
342 yield 'anon, non-canonical, local' => [
343 static function () {
344 return new UserIdentityValue( 0, strtolower( self::IP ) );
345 }, // $actorCallback
346 43, // $expected
347 ];
348 yield 'registered, local' => [
349 static function () {
350 return new UserIdentityValue( 24, 'TestUser' );
351 }, // $actorCallback
352 42, // $expected
353 ];
354 yield 'registered, zero user name' => [
355 static function () {
356 return new UserIdentityValue( 26, '0' );
357 }, // $actorCallback
358 46, // $expected
359 ];
360 yield 'anon, non-existent, local' => [
361 static function () {
362 return new UserIdentityValue( 0, '127.1.2.3' );
363 }, // $actorCallback
364 null, // $expected
365 ];
366 yield 'registered, non-existent, local' => [
367 static function () {
368 return new UserIdentityValue( 51, 'DoNotExist' );
369 }, // $actorCallback
370 null, // $expected
371 ];
372 yield 'external, local' => [
373 static function () {
374 return new UserIdentityValue( 0, 'acme>TestUser' );
375 }, // $actorCallback
376 45, // $expected
377 ];
378 yield 'anon User, local' => [
379 static function ( MediaWikiServices $serviceContainer ) {
380 return $serviceContainer->getUserFactory()->newAnonymous( self::IP );
381 }, // $actorCallback
382 43, // $expected
383 ];
384 yield 'anon User, non-canonical, local' => [
385 static function ( MediaWikiServices $serviceContainer ) {
386 return $serviceContainer->getUserFactory()->newAnonymous( strtolower( self::IP ) );
387 }, // $actorCallback
388 43, // $expected
389 ];
390 yield 'anon User, non-existent, local' => [
391 static function ( MediaWikiServices $serviceContainer ) {
392 return $serviceContainer->getUserFactory()->newAnonymous( '127.1.2.3' );
393 }, // $actorCallback
394 null, // $expected
395 ];
396 yield 'anon, foreign' => [
397 static function () {
398 return new UserIdentityValue( 0, self::IP, 'acmewiki' );
399 }, // $actorCallback
400 43, // $expected
401 'acmewiki', // $wikiId
402 ];
403 yield 'registered, foreign' => [
404 static function () {
405 return new UserIdentityValue( 24, 'TestUser', 'acmewiki' );
406 }, // $actorCallback
407 42, // $expected
408 'acmewiki', // $wikiId
409 ];
410 }
411
412 /**
413 * @dataProvider provideFindActorId
414 */
415 public function testFindActorId( callable $actorCallback, $expected, $wikiId = WikiAwareEntity::LOCAL ) {
416 $actor = $actorCallback( $this->getServiceContainer() );
417 if ( $wikiId ) {
418 $this->executeWithForeignStore(
419 $wikiId,
420 function ( ActorStore $store ) use ( $expected, $actor ) {
421 $this->assertSame( $expected, $store->findActorId( $actor, $this->getDb() ) );
422
423 if ( $actor instanceof User ) {
424 $this->assertSame( $expected ?: 0, $actor->getActorId() );
425 }
426 }
427 );
428 } else {
429 $this->assertSame( $expected, $this->getStore()->findActorId( $actor, $this->getDb() ) );
430
431 if ( $actor instanceof User ) {
432 $this->assertSame( $expected ?: 0, $actor->getActorId() );
433 }
434 }
435 }
436
437 public function testFindActorId_wikiMismatch() {
438 $this->markTestSkipped();
439 $this->expectException( PreconditionException::class );
440 $this->getStore()->findActorId(
441 new UserIdentityValue( 0, self::IP, 'acmewiki' ),
442 $this->getDb()
443 );
444 }
445
446 public static function provideFindActorIdByName() {
447 yield 'anon' => [
448 self::IP, // $actorCallback
449 43, // $expected
450 ];
451 yield 'anon, non-canonical' => [
452 strtolower( self::IP ), // $actorCallback
453 43, // $expected
454 ];
455 yield 'registered' => [
456 'TestUser', // $actorCallback
457 42, // $expected
458 ];
459 yield 'registered, unnormalized' => [
460 'testUser', // $actorCallback
461 42, // $expected
462 ];
463 yield 'registered, 0 user name' => [
464 '0', // $actorCallback
465 46, // $expected
466 ];
467 yield 'external, local' => [
468 'acme>TestUser',
469 45, // $expected
470 ];
471 yield 'anon, non-existent' => [
472 '127.1.2.3', // $actorCallback
473 null, // $expected
474 ];
475 yield 'registered, non-existent' => [
476 'DoNotExist', // $actorCallback
477 null, // $expected
478 ];
479 yield 'invalid' => [
480 '#', // $actorCallback
481 null, // $expected
482 ];
483 }
484
485 /**
486 * @dataProvider provideFindActorIdByName
487 */
488 public function testFindActorIdByName( $name, $expected ) {
489 $this->assertSame( $expected, $this->getStore()->findActorIdByName( $name, $this->getDb() ) );
490 }
491
492 public static function provideAcquireActorId() {
493 yield 'anon' => [ static function () {
494 return new UserIdentityValue( 0, '127.3.2.1' );
495 } ];
496 yield 'registered' => [ static function () {
497 return new UserIdentityValue( 15, 'MyUser' );
498 } ];
499 yield 'User object' => [ static function ( $serviceContainer ) {
500 return $serviceContainer->getUserFactory()->newAnonymous( '127.4.3.2' );
501 } ];
502 }
503
504 /**
505 * @dataProvider provideAcquireActorId
506 */
507 public function testAcquireActorId( callable $userCallback ) {
508 /** @var UserIdentity $user */
509 $user = $userCallback( $this->getServiceContainer() );
510 if ( $user->getId() === 0 ) {
511 $this->disableAutoCreateTempUser();
512 }
513 $actorId = $this->getStore()->acquireActorId( $user, $this->getDb() );
514 $this->assertTrue( $actorId > 0 );
515
516 if ( $user instanceof User ) {
517 $this->assertSame( $actorId, $user->getActorId() );
518 }
519 }
520
521 public static function provideAcquireActorId_foreign() {
522 yield 'anon' => [
523 'userCallback' => static function () {
524 return new UserIdentityValue( 0, '127.3.2.1', 'acmewiki' );
525 },
526 'method' => 'acquireActorId'
527 ];
528 yield 'registered' => [
529 'userCallback' => static function () {
530 return new UserIdentityValue( 15, 'MyUser', 'acmewiki' );
531 },
532 'method' => 'acquireActorId'
533 ];
534 yield 'anon, new' => [
535 'userCallback' => static function () {
536 return new UserIdentityValue( 0, '128.9.8.7', 'acmewiki' );
537 },
538 'method' => 'createNewActor'
539 ];
540 yield 'registered, new' => [
541 'userCallback' => static function () {
542 return new UserIdentityValue( 15, 'New MyUser', 'acmewiki' );
543 },
544 'method' => 'createNewActor'
545 ];
546 }
547
548 /**
549 * @dataProvider provideAcquireActorId_foreign
550 */
551 public function testAcquireActorId_foreign( callable $userCallback, string $method ) {
552 /** @var UserIdentity $user */
553 $user = $userCallback( $this->getServiceContainer() );
554 if ( $user->getId( 'acmewiki' ) === 0 ) {
555 $this->disableAutoCreateTempUser();
556 }
557 $this->executeWithForeignStore(
558 'acmewiki',
559 function ( ActorStore $store ) use ( $user, $method ) {
560 $actorId = $store->$method( $user, $this->getDb() );
561 $this->assertTrue( $actorId > 0 );
562 if ( $user instanceof User ) {
563 $this->assertSame( $actorId, $user->getActorId() );
564 }
565 }
566 );
567 }
568
569 /**
570 * @dataProvider provideAcquireActorId_foreign
571 */
572 public function testAcquireActorId_foreign_withDBConnection( callable $userCallback, string $method ) {
573 /** @var UserIdentity $user */
574 $user = $userCallback( $this->getServiceContainer() );
575 if ( $user->getId( 'acmewiki' ) === 0 ) {
576 $this->disableAutoCreateTempUser();
577 }
578 $this->executeWithForeignStore(
579 'acmewiki',
580 function ( ActorStore $store, IDatabase $dbw ) use ( $user, $method ) {
581 $actorId = $store->$method( $user, $dbw );
582 $this->assertTrue( $actorId > 0 );
583 if ( $user instanceof User ) {
584 $this->assertSame( $actorId, $user->getActorId() );
585 }
586 }
587 );
588 }
589
590 public static function provideAcquireActorId_canNotCreate() {
591 yield 'usable name' => [
592 'actor' => new UserIdentityValue( 0, 'MyFancyUser' ),
593 'method' => 'acquireActorId'
594 ];
595 yield 'empty name' => [
596 'actor' => new UserIdentityValue( 15, '' ),
597 'method' => 'acquireActorId'
598 ];
599 yield 'usable name, new' => [
600 'actor' => new UserIdentityValue( 0, 'MyFancyUser' ),
601 'method' => 'createNewActor'
602 ];
603 yield 'empty name, new' => [
604 'actor' => new UserIdentityValue( 15, '' ),
605 'method' => 'createNewActor'
606 ];
607 yield 'usable name, replace' => [
608 'actor' => new UserIdentityValue( 0, 'MyFancyUser' ),
609 'method' => 'acquireSystemActorId'
610 ];
611 yield 'empty name, replace' => [
612 'actor' => new UserIdentityValue( 15, '' ),
613 'method' => 'acquireSystemActorId'
614 ];
615 yield 'existing non-anon, replace' => [
616 'actor' => new UserIdentityValue( 25, 'TestUser' ),
617 'method' => 'acquireSystemActorId'
618 ];
619 }
620
621 /**
622 * @dataProvider provideAcquireActorId_canNotCreate
623 */
624 public function testAcquireActorId_canNotCreate( UserIdentityValue $actor, string $method ) {
625 // We rely on DB to protect against duplicates when inserting new actor
626 $this->setNullLogger( 'rdbms' );
627 $this->expectException( CannotCreateActorException::class );
628 $this->getStore()->$method( $actor, $this->getDb() );
629 }
630
631 public function testAcquireNowActorId_existing() {
632 // We rely on DB to protect against duplicates when inserting new actor
633 $this->setNullLogger( 'rdbms' );
634 $this->expectException( CannotCreateActorException::class );
635 $this->getStore()->createNewActor( new UserIdentityValue( 24, 'TestUser' ), $this->getDb() );
636 }
637
638 public function testAcquireActorId_autocreateTempIP() {
639 $this->enableAutoCreateTempUser();
640 $this->expectException( CannotCreateActorException::class );
641 $this->getStore()->createNewActor( new UserIdentityValue( 0, '127.3.2.1' ), $this->getDb() );
642 }
643
644 public function testAcquireActorId_autocreateTempIPallowed() {
645 $this->enableAutoCreateTempUser();
646 $actorId = $this->getStoreForImport()->createNewActor( new UserIdentityValue( 0, '127.3.2.1' ), $this->getDb() );
647 $this->assertTrue( $actorId > 0 );
648 }
649
650 public static function provideAcquireActorId_existing() {
651 yield 'anon' => [
652 new UserIdentityValue( 0, self::IP ), // $actor
653 43, // $expected
654 ];
655 yield 'registered' => [
656 new UserIdentityValue( 24, 'TestUser' ), // $actor
657 42, // $expected
658 ];
659 yield 'registered, 0 user name' => [
660 new UserIdentityValue( 26, '0' ), // $actor
661 46, // $expected
662 ];
663 }
664
665 /**
666 * @dataProvider provideAcquireActorId_existing
667 */
668 public function testAcquireActorId_existing( UserIdentityValue $actor, int $expected ) {
669 if ( $actor->getId() === 0 ) {
670 $this->disableAutoCreateTempUser();
671 }
672 $this->assertSame( $expected, $this->getStore()->acquireActorId( $actor, $this->getDb() ) );
673 }
674
675 public function testAcquireActorId_domain_mismatch() {
676 $this->expectException( InvalidArgumentException::class );
677 $this->getStore( 'fancywiki' )->acquireActorId(
678 new UserIdentityValue( 15, 'Test', 'fancywiki' ),
679 $this->getDb()
680 );
681 }
682
683 public function testcreateNewActor_domain_mismatch() {
684 $this->expectException( InvalidArgumentException::class );
685 $this->getStore( 'fancywiki' )->createNewActor(
686 new UserIdentityValue( 15, 'Test', 'fancywiki' ),
687 $this->getDb()
688 );
689 }
690
691 public function testAcquireSystemActorId_domain_mismatch() {
692 $this->expectException( InvalidArgumentException::class );
693 $this->getStore( 'fancywiki' )->acquireSystemActorId(
694 new UserIdentityValue( 15, 'Test', 'fancywiki' ),
695 $this->getDb()
696 );
697 }
698
699 public function testAcquireActorId_wikiMismatch() {
700 $this->markTestSkipped();
701 $this->expectException( PreconditionException::class );
702 $this->getStore()->acquireActorId(
703 new UserIdentityValue( 0, self::IP, 'acmewiki' ),
704 $this->getDb()
705 );
706 }
707
708 public function testAcquireActorId_clearCacheOnRollback() {
709 $this->disableAutoCreateTempUser();
710 $rolledBackActor = new UserIdentityValue( 0, '127.0.0.10' );
711 $store = $this->getStore();
712 $this->getDb()->startAtomic( __METHOD__ );
713 $rolledBackActorId = $store->acquireActorId( $rolledBackActor, $this->getDb() );
714 $this->assertTrue( $rolledBackActorId > 0 );
715 $foundActorId = $store->findActorId( $rolledBackActor, $this->getDb() );
716 $this->assertSame( $rolledBackActorId, $foundActorId );
717 $this->getDb()->rollback( __METHOD__ );
718
719 // Insert some other user identity using another store
720 // so that we take over the rolled back actor ID.
721 $anotherActor = new UserIdentityValue( 0, '127.0.0.11' );
722 $anotherActorId = $this->getStore()->acquireActorId( $anotherActor, $this->getDb() );
723
724 // Make sure no actor ID associated with rolled back actor.
725 $foundActorIdAfterRollback = $store->findActorId( $rolledBackActor, $this->getDb() );
726 $this->assertNull( $foundActorIdAfterRollback );
727
728 // Make sure we can acquire new actor ID for the rolled back actor
729 $newActorId = $store->acquireActorId( $rolledBackActor, $this->getDb() );
730 $this->assertTrue( $newActorId > 0 );
731 $this->assertNotSame( $newActorId, $rolledBackActorId );
732
733 // Make sure we find correct actor by rolled back actor ID
734 $this->assertSameActors( $anotherActor, $store->getActorById( $anotherActorId, $this->getDb() ) );
735 }
736
737 public function testUserRenameUpdatesActor() {
738 $user = $this->getMutableTestUser()->getUser();
739 $oldName = $user->getName();
740
741 $store = $this->getStore();
742 $actorId = $store->findActorId( $user, $this->getDb() );
743 $this->assertTrue( $actorId > 0 );
744
745 $user->setName( 'NewName' );
746 $user->saveSettings();
747
748 $this->assertSameActors( $user, $store->getActorById( $actorId, $this->getDb() ) );
749 $this->assertSameActors( $user, $store->getUserIdentityByName( 'NewName' ) );
750 $this->assertSameActors( $user, $store->getUserIdentityByUserId( $user->getId() ) );
751 $this->assertNull( $store->getUserIdentityByName( $oldName ) );
752 }
753
754 public function testAcquireSystemActorId() {
755 $this->disableAutoCreateTempUser();
756 $store = $this->getStore();
757 $originalActor = new UserIdentityValue( 0, '129.0.0.1' );
758 $actorId = $store->createNewActor( $originalActor, $this->getDb() );
759 $this->assertTrue( $actorId > 0, 'Acquired new actor ID' );
760
761 $updatedActor = new UserIdentityValue( 56789, '129.0.0.1' );
762 $this->assertSame( $actorId, $store->acquireSystemActorId( $updatedActor, $this->getDb() ) );
763 $this->assertSame( 56789, $store->getActorById( $actorId, $this->getDb() )->getId() );
764 // Try with another store to verify not just cache was updated
765 $this->assertSame( 56789, $this->getStore()->getActorById( $actorId, $this->getDb() )->getId() );
766 }
767
768 public function testAcquireSystemActorId_replaceReserved() {
769 $this->overrideConfigValue(
770 MainConfigNames::ReservedUsernames,
771 [ 'RESERVED' ]
772 );
773 $store = $this->getStore();
774 $originalActor = new UserIdentityValue( 0, 'RESERVED' );
775 $actorId = $store->createNewActor( $originalActor, $this->getDb() );
776 $this->assertTrue( $actorId > 0, 'Acquired new actor ID' );
777
778 $updatedActor = new UserIdentityValue( 80, 'RESERVED' );
779 $this->assertSame( $actorId, $store->acquireSystemActorId( $updatedActor, $this->getDb() ) );
780 $this->assertSame( 80, $store->getActorById( $actorId, $this->getDb() )->getId() );
781 // Try with another store to verify not just cache was updated
782 $this->assertSame( 80, $this->getStore()->getActorById( $actorId, $this->getDb() )->getId() );
783 }
784
785 public function testAcquireSystemActorId_assignsNew() {
786 $store = $this->getStore();
787
788 $newActor = new UserIdentityValue( 456789, 'Foo' );
789 $newActorId = $store->acquireSystemActorId( $newActor, $this->getDb() );
790 $this->assertTrue( $newActorId > 0 );
791 $this->assertSame( 456789, $store->getActorById( $newActorId, $this->getDb() )->getId() );
792 // Try with another store to verify not just cache was updated
793 $this->assertSame( 456789, $this->getStore()->getActorById( $newActorId, $this->getDb() )->getId() );
794 }
795
796 public function testDelete() {
797 $store = $this->getStore();
798 $actor = new UserIdentityValue( 9999, 'DeleteTest' );
799 $actorId = $store->acquireActorId( $actor, $this->getDb() );
800 $this->assertTrue( $store->deleteActor( $actor, $this->getDb() ) );
801
802 $this->assertNull( $store->findActorId( $actor, $this->getDb() ) );
803 $this->assertNull( $store->getUserIdentityByUserId( $actor->getId() ) );
804 $this->assertNull( $store->getUserIdentityByName( $actor->getName() ) );
805 $this->assertNull( $store->getActorById( $actorId, $this->getDb() ) );
806 }
807
808 public function testDeleteDoesNotExist() {
809 $this->assertFalse(
810 $this->getStore()->deleteActor( new UserIdentityValue( 9998, 'DoesNotExist' ), $this->getDb() )
811 );
812 }
813
814 public function testGetUnknownActor() {
815 $store = $this->getStore();
816 $unknownActor = $store->getUnknownActor();
817 $this->assertInstanceOf( UserIdentity::class, $unknownActor );
818 $this->assertSame( ActorStore::UNKNOWN_USER_NAME, $unknownActor->getName() );
819 $this->assertSameActors( $unknownActor, $store->getUnknownActor() );
820 }
821
822 public static function provideNormalizeUserName() {
823 yield [ strtolower( self::IP ), self::IP ];
824 yield [ 'acme>test', 'acme>test' ];
825 yield [ 'test_this', 'Test this' ];
826 yield [ 'foo#bar', null ];
827 yield [ 'foo|bar', null ];
828 yield [ '_', null ];
829 yield [ 'test', 'Test' ];
830 yield [ '', null ];
831 yield [ '0', '0' ];
832 }
833
834 /**
835 * @dataProvider provideNormalizeUserName
836 */
837 public function testNormalizeUserName( $name, $expected ) {
838 $store = $this->getStore();
839 $this->assertSame( $expected, $store->normalizeUserName( $name ) );
840 }
841
842 public function testNewSelectQueryBuilderWithoutDB() {
843 $store = $this->getStore();
844 $queryBuilder = $store->newSelectQueryBuilder();
845 $this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder );
846 }
847
848 public function testNewSelectQueryBuilderWithDB() {
849 $store = $this->getStore();
850 $queryBuilder = $store->newSelectQueryBuilder( $this->getDb() );
851 $this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder );
852 }
853
854 public function testNewSelectQueryBuilderWithQueryFlags() {
855 $store = $this->getStore();
856 $queryBuilder = $store->newSelectQueryBuilder( IDBAccessObject::READ_NORMAL );
857 $this->assertInstanceOf( UserSelectQueryBuilder::class, $queryBuilder );
858 }
859 }