wiki.techinc.nl/tests/phpunit/includes/api/query/ApiQueryAllUsersTest.php
Tim Starling d6727856fd 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-11-29 13:31:42 +11:00

191 lines
5.4 KiB
PHP

<?php
use MediaWiki\Permissions\UltimateAuthority;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWiki\User\User;
/**
* @group API
* @group Database
* @group medium
*
* @covers ApiQueryAllUsers
*/
class ApiQueryAllUsersTest extends ApiTestCase {
use MockAuthorityTrait;
private const USER_PREFIX = 'ApiQueryAllUsersTest ';
public function addDBDataOnce() {
$groupManager = $this->getServiceContainer()->getUserGroupManager();
User::createNew( self::USER_PREFIX . 'A' );
$userB = User::createNew( self::USER_PREFIX . 'B' );
$groupManager->addUserToGroup( $userB, 'bot' );
$userC = User::createNew( self::USER_PREFIX . 'C' );
$groupManager->addUserToGroup( $userC, 'bot' );
}
public function testPrefix() {
$result = $this->doApiRequest( [
'action' => 'query',
'list' => 'allusers',
'auprefix' => self::USER_PREFIX
] );
$this->assertArrayHasKey( 'query', $result[0] );
$this->assertArrayHasKey( 'allusers', $result[0]['query'] );
$this->assertContains( self::USER_PREFIX . 'A', $result[0]['query']['allusers'][0] );
$this->assertContains( self::USER_PREFIX . 'B', $result[0]['query']['allusers'][1] );
$this->assertContains( self::USER_PREFIX . 'C', $result[0]['query']['allusers'][2] );
}
public function testImplicitRights() {
$result = $this->doApiRequest( [
'action' => 'query',
'list' => 'allusers',
'auprefix' => self::USER_PREFIX,
'aurights' => 'stashedit'
] );
$this->assertArrayHasKey( 'query', $result[0] );
$this->assertArrayHasKey( 'allusers', $result[0]['query'] );
$this->assertContains( self::USER_PREFIX . 'A', $result[0]['query']['allusers'][0] );
$this->assertContains( self::USER_PREFIX . 'B', $result[0]['query']['allusers'][1] );
$this->assertContains( self::USER_PREFIX . 'C', $result[0]['query']['allusers'][2] );
}
public function testPermissions() {
$result = $this->doApiRequest( [
'action' => 'query',
'list' => 'allusers',
'auprefix' => self::USER_PREFIX,
'aurights' => 'bot'
] );
$this->assertArrayHasKey( 'query', $result[0] );
$this->assertArrayHasKey( 'allusers', $result[0]['query'] );
$this->assertContains( self::USER_PREFIX . 'B', $result[0]['query']['allusers'][0] );
$this->assertContains( self::USER_PREFIX . 'C', $result[0]['query']['allusers'][1] );
}
public function testHiddenUser() {
$userFactory = $this->getServiceContainer()->getUserFactory();
$a = $userFactory->newFromName( self::USER_PREFIX . 'A' );
$b = $userFactory->newFromName( self::USER_PREFIX . 'B' );
$blockStatus = $this->getServiceContainer()->getBlockUserFactory()
->newBlockUser(
$b,
new UltimateAuthority( $a ),
'infinity',
'',
[ 'isHideUser' => true ],
)
->placeBlock();
$this->assertStatusGood( $blockStatus );
$apiParams = [
'action' => 'query',
'list' => 'allusers',
'auprefix' => self::USER_PREFIX . 'B',
];
$result = $this->doApiRequest( $apiParams, null, null,
$this->mockRegisteredAuthorityWithPermissions( [] ) );
$this->assertSame( [], $result[0]['query']['allusers'] );
$result = $this->doApiRequest( $apiParams, null, null,
$this->mockRegisteredAuthorityWithPermissions( [ 'hideuser' ] ) );
$this->assertSame(
[ [
'userid' => $b->getId(),
'name' => $b->getName(),
'hidden' => true,
] ],
$result[0]['query']['allusers']
);
$apiParams['auprop'] = 'blockinfo';
$result = $this->doApiRequest( $apiParams, null, null,
$this->mockRegisteredAuthorityWithPermissions( [ 'hideuser' ] ) );
$this->assertArraySubmapSame(
[
'userid' => $b->getId(),
'name' => $b->getName(),
'hidden' => true,
'blockedby' => 'ApiQueryAllUsersTest A',
'blockreason' => '',
'blockexpiry' => 'infinite',
'blockpartial' => false,
],
$result[0]['query']['allusers'][0]
);
}
public function testBlockInfo() {
$userFactory = $this->getServiceContainer()->getUserFactory();
$a = $userFactory->newFromName( self::USER_PREFIX . 'A' );
$b = $userFactory->newFromName( self::USER_PREFIX . 'B' );
$c = $userFactory->newFromName( self::USER_PREFIX . 'C' );
$blockStatus = $this->getServiceContainer()->getBlockUserFactory()
->newBlockUser(
$b,
new UltimateAuthority( $a ),
'infinity',
)
->placeBlock();
$this->assertStatusGood( $blockStatus );
$blockStatus = $this->getServiceContainer()->getBlockUserFactory()
->newBlockUser(
$c,
new UltimateAuthority( $a ),
'infinity',
'',
[ 'isUserTalkEditBlocked' => true ],
)
->placeBlock();
$this->assertStatusGood( $blockStatus );
$result = $this->doApiRequest( [
'action' => 'query',
'list' => 'allusers',
'auprefix' => self::USER_PREFIX,
'auprop' => 'blockinfo'
] );
$this->assertArraySubmapSame(
[
'userid' => $a->getId(),
'name' => $a->getName(),
],
$result[0]['query']['allusers'][0]
);
$this->assertArraySubmapSame(
[
'userid' => $b->getId(),
'name' => $b->getName(),
'blockedby' => 'ApiQueryAllUsersTest A',
'blockreason' => '',
'blockexpiry' => 'infinite',
'blockpartial' => false,
'blockowntalk' => false
],
$result[0]['query']['allusers'][1]
);
$this->assertArraySubmapSame(
[
'userid' => $c->getId(),
'name' => $c->getName(),
'blockedby' => 'ApiQueryAllUsersTest A',
'blockreason' => '',
'blockexpiry' => 'infinite',
'blockpartial' => false,
'blockowntalk' => true
],
$result[0]['query']['allusers'][2]
);
}
}