2020-10-02 02:21:23 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\User;
|
|
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
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
|
|
|
use MediaWiki\Block\HideUserUtils;
|
2021-07-27 11:41:13 +00:00
|
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
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
|
|
|
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
|
|
|
|
|
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-10-02 02:21:23 +00:00
|
|
|
use MediaWiki\User\UserNamePrefixSearch;
|
2021-06-28 07:41:34 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2020-10-02 02:21:23 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
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
|
|
|
use Wikimedia\Rdbms\Database;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\Expression;
|
2023-05-15 19:28:29 +00:00
|
|
|
use Wikimedia\Rdbms\IConnectionProvider;
|
2023-11-07 17:30:50 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
|
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
2023-05-15 19:28:29 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2020-10-02 02:21:23 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-08-31 17:38:46 +00:00
|
|
|
* @covers \MediaWiki\User\UserNamePrefixSearch
|
2020-10-02 02:21:23 +00:00
|
|
|
* @author DannyS712
|
|
|
|
|
*/
|
|
|
|
|
class UserNamePrefixSearchTest extends MediaWikiUnitTestCase {
|
2021-07-27 11:41:13 +00:00
|
|
|
use DummyServicesTrait;
|
2020-10-02 02:21:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestSearch
|
|
|
|
|
* @param int $audienceType 1='public', 2=user without `hideuser` rights, 3=user with `hideuser` rights
|
2021-06-28 07:41:34 +00:00
|
|
|
* @param string $prefix
|
2020-10-02 02:21:23 +00:00
|
|
|
* @param int $limit
|
|
|
|
|
* @param int $offset
|
|
|
|
|
* @param array $result
|
|
|
|
|
*/
|
|
|
|
|
public function testSearch( int $audienceType, $prefix, int $limit, int $offset, array $result ) {
|
2021-07-27 11:41:13 +00:00
|
|
|
$userNameUtils = $this->getDummyUserNameUtils();
|
2020-10-02 02:21:23 +00:00
|
|
|
|
|
|
|
|
if ( $audienceType === 1 ) {
|
|
|
|
|
// 'public' audience
|
|
|
|
|
$audience = UserNamePrefixSearch::AUDIENCE_PUBLIC;
|
|
|
|
|
$excludeHidden = true;
|
|
|
|
|
} else {
|
|
|
|
|
if ( $audienceType === 2 ) {
|
|
|
|
|
// no hideuser rights
|
|
|
|
|
$hasHideuser = false;
|
|
|
|
|
$excludeHidden = true;
|
|
|
|
|
} else {
|
|
|
|
|
// 3 - has hideuser rights
|
|
|
|
|
$hasHideuser = true;
|
|
|
|
|
$excludeHidden = false;
|
|
|
|
|
}
|
|
|
|
|
// specific to a user identity
|
|
|
|
|
$audience = $this->createMock( User::class );
|
2021-05-26 05:28:12 +00:00
|
|
|
$audience->method( 'isAllowed' )
|
|
|
|
|
->with( 'hideuser' )
|
2020-10-02 02:21:23 +00:00
|
|
|
->willReturn( $hasHideuser );
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
$platform = new SQLPlatformTestHelper( new AddQuoterMock() );
|
|
|
|
|
|
|
|
|
|
$database = $this->createMock( Database::class );
|
2020-10-02 02:21:23 +00:00
|
|
|
$database->expects( $this->once() )
|
|
|
|
|
->method( 'anyString' )
|
|
|
|
|
->willReturn( 'anyStringGoesHere' );
|
2023-11-07 17:30:50 +00:00
|
|
|
$args = [ 'user_name', IExpression::LIKE, new LikeValue( $prefix, 'anyStringGoesHere' ) ];
|
2020-10-02 02:21:23 +00:00
|
|
|
$database->expects( $this->once() )
|
2023-11-07 17:30:50 +00:00
|
|
|
->method( 'expr' )
|
|
|
|
|
->with( ...$args )
|
|
|
|
|
->willReturn( new Expression( ...$args ) );
|
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
|
|
|
$database->expects( $this->any() )
|
|
|
|
|
->method( 'selectSQLText' )
|
|
|
|
|
->willReturnCallback(
|
|
|
|
|
static function ( $table, $vars, $conds, $fname, $options, $join_conds )
|
|
|
|
|
use ( $platform ) {
|
|
|
|
|
return $platform->selectSQLText(
|
|
|
|
|
$table, $vars, $conds, $fname, $options, $join_conds );
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-10-02 02:21:23 +00:00
|
|
|
|
|
|
|
|
// Query parameters
|
|
|
|
|
$tables = [ 'user' ];
|
2023-11-07 17:30:50 +00:00
|
|
|
$conds = [ new Expression( ...$args ) ];
|
2020-10-02 02:21:23 +00:00
|
|
|
$joinConds = [];
|
|
|
|
|
if ( $excludeHidden ) {
|
2024-04-09 02:49:33 +00:00
|
|
|
$conds[] = '(SELECT 1 FROM block_target hu_block_target ' .
|
|
|
|
|
'JOIN block hu_block ON ((hu_block.bl_target=hu_block_target.bt_id)) ' .
|
|
|
|
|
'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ) IS NULL';
|
2020-10-02 02:21:23 +00:00
|
|
|
}
|
|
|
|
|
$options = [
|
|
|
|
|
'LIMIT' => $limit,
|
2023-05-15 19:28:29 +00:00
|
|
|
'ORDER BY' => [ 'user_name' ],
|
2020-10-02 02:21:23 +00:00
|
|
|
'OFFSET' => $offset
|
|
|
|
|
];
|
|
|
|
|
$database->expects( $this->once() )
|
|
|
|
|
->method( 'selectFieldValues' )
|
|
|
|
|
->with(
|
2021-04-22 07:56:11 +00:00
|
|
|
$tables,
|
|
|
|
|
'user_name',
|
|
|
|
|
$conds,
|
|
|
|
|
'MediaWiki\User\UserNamePrefixSearch::search',
|
|
|
|
|
$options,
|
|
|
|
|
$joinConds
|
2020-10-02 02:21:23 +00:00
|
|
|
)
|
|
|
|
|
->willReturn( $result );
|
2023-09-27 13:22:44 +00:00
|
|
|
$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) );
|
2020-10-02 02:21:23 +00:00
|
|
|
|
2023-05-15 19:28:29 +00:00
|
|
|
$dbProvider = $this->createMock( IConnectionProvider::class );
|
|
|
|
|
$dbProvider->expects( $this->once() )
|
|
|
|
|
->method( 'getReplicaDatabase' )
|
2020-10-02 02:21:23 +00:00
|
|
|
->willReturn( $database );
|
|
|
|
|
|
2024-04-09 03:26:02 +00:00
|
|
|
$hideUserUtils = new HideUserUtils();
|
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
|
|
|
|
2020-10-02 02:21:23 +00:00
|
|
|
$userNamePrefixSearch = new UserNamePrefixSearch(
|
2023-05-15 19:28:29 +00:00
|
|
|
$dbProvider,
|
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
|
|
|
$userNameUtils,
|
|
|
|
|
$hideUserUtils
|
2020-10-02 02:21:23 +00:00
|
|
|
);
|
|
|
|
|
$res = $userNamePrefixSearch->search(
|
|
|
|
|
$audience,
|
|
|
|
|
$prefix,
|
|
|
|
|
$limit,
|
|
|
|
|
$offset
|
|
|
|
|
);
|
|
|
|
|
$this->assertSame( $result, $res );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideTestSearch() {
|
2020-10-02 02:21:23 +00:00
|
|
|
// [ $audienceType, $prefix, $limit, $offset, $result ]
|
|
|
|
|
return [
|
|
|
|
|
'public' => [
|
|
|
|
|
1,
|
|
|
|
|
'',
|
|
|
|
|
10,
|
|
|
|
|
0,
|
|
|
|
|
[ 'public result goes here' ]
|
|
|
|
|
],
|
|
|
|
|
'user without hideuser rights' => [
|
|
|
|
|
2,
|
2021-07-27 11:41:13 +00:00
|
|
|
'Prefix',
|
2020-10-02 02:21:23 +00:00
|
|
|
10,
|
|
|
|
|
5,
|
|
|
|
|
[ 'public result goes here, since user cannot see anything hidden' ]
|
|
|
|
|
],
|
|
|
|
|
'user with hideuser rights' => [
|
|
|
|
|
3,
|
2021-07-27 11:41:13 +00:00
|
|
|
'AnotherPrefix',
|
2020-10-02 02:21:23 +00:00
|
|
|
15,
|
|
|
|
|
2,
|
|
|
|
|
[
|
|
|
|
|
'result that is public',
|
|
|
|
|
'also a result that is private'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSearchInvalidAudience() {
|
|
|
|
|
$userNamePrefixSearch = new UserNamePrefixSearch(
|
2023-05-15 19:28:29 +00:00
|
|
|
$this->createMock( IConnectionProvider::class ),
|
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->createMock( UserNameUtils::class ),
|
2024-04-09 03:26:02 +00:00
|
|
|
new HideUserUtils()
|
2020-10-02 02:21:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2021-05-26 05:28:12 +00:00
|
|
|
$this->expectExceptionMessage( '$audience must be AUDIENCE_PUBLIC or an Authority object' );
|
2020-10-02 02:21:23 +00:00
|
|
|
$userNamePrefixSearch->search(
|
|
|
|
|
'ThisIsTheInvalidAudience',
|
|
|
|
|
'',
|
|
|
|
|
1,
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|