2021-02-10 21:56:38 +00:00
|
|
|
<?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';
|
|
|
|
|
|
|
|
|
|
public function addDBData() {
|
2021-04-07 00:50:30 +00:00
|
|
|
$actors = [
|
|
|
|
|
'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' ],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ( $actors as $description => $row ) {
|
2024-06-13 16:53:25 +00:00
|
|
|
$this->getDb()->newInsertQueryBuilder()
|
2024-04-14 19:33:50 +00:00
|
|
|
->insertInto( 'actor' )
|
|
|
|
|
->ignore()
|
|
|
|
|
->row( $row )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->execute();
|
2024-06-13 16:53:25 +00:00
|
|
|
$this->assertSame( 1, $this->getDb()->affectedRows(), "Must create {$description} actor" );
|
2021-04-07 00:50:30 +00:00
|
|
|
}
|
2021-02-10 21:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function getStore( $wikiId = UserIdentity::LOCAL ): ActorStore {
|
2021-02-10 21:56:38 +00:00
|
|
|
return $this->getServiceContainer()->getActorStoreFactory()->getActorStore( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 20:43:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param string|false $wikiId
|
|
|
|
|
* @return ActorStore
|
|
|
|
|
*/
|
|
|
|
|
protected function getStoreForImport( $wikiId = UserIdentity::LOCAL ): ActorStore {
|
|
|
|
|
return $this->getServiceContainer()->getActorStoreFactory()->getActorStoreForImport( $wikiId );
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 21:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* 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() ] );
|
2024-04-17 12:47:05 +00:00
|
|
|
$foreignDB = $foreignLB->getConnection( DB_PRIMARY );
|
2021-02-10 21:56:38 +00:00
|
|
|
|
|
|
|
|
$store = new ActorStore(
|
|
|
|
|
$dbLoadBalancer,
|
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
2023-08-02 13:27:41 +00:00
|
|
|
$this->getServiceContainer()->getTempUserConfig(),
|
2021-02-10 21:56:38 +00:00
|
|
|
new NullLogger(),
|
Support new block schema
Support migration stages when reading and writing blocks.
I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.
However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.
HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.
When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.
Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-10-27 03:34:10 +00:00
|
|
|
$this->getServiceContainer()->getHideUserUtils(),
|
2021-02-10 21:56:38 +00:00
|
|
|
$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 );
|
2021-02-23 15:40:12 +00:00
|
|
|
$this->assertSame( $expected->getId( $wikiId ), $actor->getId( $wikiId ) );
|
2021-02-10 21:56:38 +00:00
|
|
|
$this->assertSame( $expected->getName(), $actor->getName() );
|
|
|
|
|
$this->assertSame( $expected->getWikiId(), $actor->getWikiId() );
|
|
|
|
|
}
|
|
|
|
|
}
|