Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\User;
4
5 use MediaWiki\User\ActorStore;
6 use MediaWiki\User\UserIdentity;
7 use MediaWikiIntegrationTestCase;
8 use Psr\Log\NullLogger;
9 use Wikimedia\Rdbms\ILoadBalancer;
10
11 /**
12 * Base class with utilities for testing database access to actor table.
13 *
14 */
15 abstract class ActorStoreTestBase extends MediaWikiIntegrationTestCase {
16 protected const IP = '2600:1004:B14A:5DDD:3EBE:BBA4:BFBA:F37E';
17 /** The user IDs set in addDBData() */
18 protected const TEST_USERS = [
19 'registered' => [ 'actor_id' => '42', 'actor_user' => '24', 'actor_name' => 'TestUser' ],
20 'anon' => [ 'actor_id' => '43', 'actor_user' => null, 'actor_name' => self::IP ],
21 'another registered' => [ 'actor_id' => '44', 'actor_user' => '25', 'actor_name' => 'TestUser1' ],
22 'external' => [ 'actor_id' => '45', 'actor_user' => null, 'actor_name' => 'acme>TestUser' ],
23 'user name 0' => [ 'actor_id' => '46', 'actor_user' => '26', 'actor_name' => '0' ],
24 ];
25
26 public function addDBData() {
27 foreach ( self::TEST_USERS as $description => $row ) {
28 $this->getDb()->newInsertQueryBuilder()
29 ->insertInto( 'actor' )
30 ->ignore()
31 ->row( $row )
32 ->caller( __METHOD__ )
33 ->execute();
34 $this->assertSame( 1, $this->getDb()->affectedRows(), "Must create {$description} actor" );
35 }
36 }
37
38 /**
39 * @param string|false $wikiId
40 * @return ActorStore
41 */
42 protected function getStore( $wikiId = UserIdentity::LOCAL ): ActorStore {
43 return $this->getServiceContainer()->getActorStoreFactory()->getActorStore( $wikiId );
44 }
45
46 /**
47 * @param string|false $wikiId
48 * @return ActorStore
49 */
50 protected function getStoreForImport( $wikiId = UserIdentity::LOCAL ): ActorStore {
51 return $this->getServiceContainer()->getActorStoreFactory()->getActorStoreForImport( $wikiId );
52 }
53
54 /**
55 * Execute the $callback passing it an ActorStore for $wikiId,
56 * making sure no queries are made to local DB.
57 * @param string|false $wikiId
58 * @param callable $callback ( ActorStore $store, IDatababase $db )
59 */
60 protected function executeWithForeignStore( $wikiId, callable $callback ) {
61 $dbLoadBalancer = $this->getServiceContainer()->getDBLoadBalancer();
62 $dbLoadBalancer->setDomainAliases( [ $wikiId => $dbLoadBalancer->getLocalDomainID() ] );
63
64 $foreignLB = $this->getServiceContainer()
65 ->getDBLoadBalancerFactory()
66 ->getMainLB( $wikiId );
67 $foreignLB->setDomainAliases( [ $wikiId => $dbLoadBalancer->getLocalDomainID() ] );
68 $foreignDB = $foreignLB->getConnection( DB_PRIMARY );
69
70 $store = new ActorStore(
71 $dbLoadBalancer,
72 $this->getServiceContainer()->getUserNameUtils(),
73 $this->getServiceContainer()->getTempUserConfig(),
74 new NullLogger(),
75 $this->getServiceContainer()->getHideUserUtils(),
76 $wikiId
77 );
78
79 // Redefine the DBLoadBalancer service to verify we don't attempt to resolve its IDs via wfGetDB()
80 $localLoadBalancerMock = $this->createNoOpMock( ILoadBalancer::class );
81 try {
82 $this->setService( 'DBLoadBalancer', $localLoadBalancerMock );
83 $callback( $store, $foreignDB );
84 } finally {
85 // Restore the original loadBalancer.
86 $this->setService( 'DBLoadBalancer', $dbLoadBalancer );
87 }
88 }
89
90 /**
91 * Check whether two actors are the same in the context of $wikiId
92 * @param UserIdentity $expected
93 * @param UserIdentity $actor
94 * @param string|false $wikiId
95 */
96 protected function assertSameActors(
97 UserIdentity $expected,
98 UserIdentity $actor,
99 $wikiId = UserIdentity::LOCAL
100 ) {
101 $actor->assertWiki( $wikiId );
102 $this->assertSame( $expected->getId( $wikiId ), $actor->getId( $wikiId ) );
103 $this->assertSame( $expected->getName(), $actor->getName() );
104 $this->assertSame( $expected->getWikiId(), $actor->getWikiId() );
105 }
106 }