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