wiki.techinc.nl/tests/phpunit/includes/api/query/ApiQueryBlockInfoTraitTest.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

109 lines
3 KiB
PHP

<?php
use MediaWiki\MainConfigNames;
use MediaWiki\Tests\MockDatabase;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* @covers ApiQueryBlockInfoTrait
*/
class ApiQueryBlockInfoTraitTest extends MediaWikiIntegrationTestCase {
use MockAuthorityTrait;
public function testUsesApiBlockInfoTrait() {
$this->assertTrue( method_exists( ApiQueryBlockInfoTrait::class, 'getBlockDetails' ),
'ApiQueryBlockInfoTrait::getBlockDetails exists' );
}
/**
* @dataProvider provideAddDeletedUserFilter
*/
public function testAddDeletedUserFilter( $schema, $isAllowed, $expect ) {
$this->overrideConfigValue( MainConfigNames::BlockTargetMigrationStage, $schema );
// Fake timestamp to show up in the queries
$reset = ConvertibleTimestamp::setFakeTime( '20190101000000' );
$authority = $this->mockRegisteredAuthorityWithPermissions(
$isAllowed ? [ 'hideuser' ] : [] );
$db = new MockDatabase;
$queryBuilder = $db->newSelectQueryBuilder()
->from( 'table' );
$mock = $this->getMockBuilder( ApiQueryBase::class )
->disableOriginalConstructor()
->onlyMethods( [
'getQueryBuilder',
'getDB',
'getAuthority'
] )
->getMockForAbstractClass();
$mock->method( 'getQueryBuilder' )->willReturn( $queryBuilder );
$mock->method( 'getDB' )->willReturn( new MockDatabase );
$mock->method( 'getAuthority' )->willReturn( $authority );
TestingAccessWrapper::newFromObject( $mock )->addDeletedUserFilter();
$data = $queryBuilder->getQueryInfo();
$this->assertSame( $expect, $data );
}
public static function provideAddDeletedUserFilter() {
return [
'old unauthorized' => [
SCHEMA_COMPAT_OLD,
false,
[
'tables' => [ 'table' ],
'fields' => [ 'hu_deleted' => '1=0' ],
'conds' => [
'NOT EXISTS (SELECT 1 FROM "ipblocks" ' .
'WHERE (ipb_user=user_id) AND ipb_deleted = 1 )' ],
'options' => [],
'join_conds' => [],
],
],
'old authorized' => [
SCHEMA_COMPAT_OLD,
true,
[
'tables' => [ 'table' ],
'fields' => [ 'hu_deleted' => 'EXISTS (SELECT 1 FROM "ipblocks" ' .
'WHERE (ipb_user=user_id) AND ipb_deleted = 1 )' ],
'conds' => [],
'options' => [],
'join_conds' => []
],
],
'new unauthorized' => [
SCHEMA_COMPAT_NEW,
false,
[
'tables' => [ 'table' ],
'fields' => [ 'hu_deleted' => '1=0' ],
'conds' => [ 'NOT EXISTS (SELECT 1 FROM "block_target" ' .
'JOIN "block" ON ((bl_target=bt_id)) ' .
'WHERE (bt_user=user_id) AND bl_deleted = 1 )' ],
'options' => [],
'join_conds' => [],
],
],
'new authorized' => [
SCHEMA_COMPAT_NEW,
true,
[
'tables' => [ 'table' ],
'fields' => [ 'hu_deleted' => 'EXISTS (SELECT 1 FROM "block_target" ' .
'JOIN "block" ON ((bl_target=bt_id)) ' .
'WHERE (bt_user=user_id) AND bl_deleted = 1 )' ],
'conds' => [],
'options' => [],
'join_conds' => []
],
],
];
}
}