| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\User; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Block\HideUserUtils; |
| 7 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 8 |
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock; |
| 9 |
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper; |
| 10 |
use MediaWiki\User\User; |
| 11 |
use MediaWiki\User\UserNamePrefixSearch; |
| 12 |
use MediaWiki\User\UserNameUtils; |
| 13 |
use MediaWikiUnitTestCase; |
| 14 |
use Wikimedia\Rdbms\Database; |
| 15 |
use Wikimedia\Rdbms\Expression; |
| 16 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 17 |
use Wikimedia\Rdbms\IExpression; |
| 18 |
use Wikimedia\Rdbms\LikeValue; |
| 19 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class UserNamePrefixSearchTest extends MediaWikiUnitTestCase { |
| 26 |
use DummyServicesTrait; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function testSearch( int $audienceType, $prefix, int $limit, int $offset, array $result ) { |
| 37 |
$userNameUtils = $this->getDummyUserNameUtils(); |
| 38 |
|
| 39 |
if ( $audienceType === 1 ) { |
| 40 |
|
| 41 |
$audience = UserNamePrefixSearch::AUDIENCE_PUBLIC; |
| 42 |
$excludeHidden = true; |
| 43 |
} else { |
| 44 |
if ( $audienceType === 2 ) { |
| 45 |
|
| 46 |
$hasHideuser = false; |
| 47 |
$excludeHidden = true; |
| 48 |
} else { |
| 49 |
|
| 50 |
$hasHideuser = true; |
| 51 |
$excludeHidden = false; |
| 52 |
} |
| 53 |
|
| 54 |
$audience = $this->createMock( User::class ); |
| 55 |
$audience->method( 'isAllowed' ) |
| 56 |
->with( 'hideuser' ) |
| 57 |
->willReturn( $hasHideuser ); |
| 58 |
} |
| 59 |
|
| 60 |
$platform = new SQLPlatformTestHelper( new AddQuoterMock() ); |
| 61 |
|
| 62 |
$database = $this->createMock( Database::class ); |
| 63 |
$database->expects( $this->once() ) |
| 64 |
->method( 'anyString' ) |
| 65 |
->willReturn( 'anyStringGoesHere' ); |
| 66 |
$args = [ 'user_name', IExpression::LIKE, new LikeValue( $prefix, 'anyStringGoesHere' ) ]; |
| 67 |
$database->expects( $this->once() ) |
| 68 |
->method( 'expr' ) |
| 69 |
->with( ...$args ) |
| 70 |
->willReturn( new Expression( ...$args ) ); |
| 71 |
$database->expects( $this->any() ) |
| 72 |
->method( 'selectSQLText' ) |
| 73 |
->willReturnCallback( |
| 74 |
static function ( $table, $vars, $conds, $fname, $options, $join_conds ) |
| 75 |
use ( $platform ) { |
| 76 |
return $platform->selectSQLText( |
| 77 |
$table, $vars, $conds, $fname, $options, $join_conds ); |
| 78 |
} |
| 79 |
); |
| 80 |
|
| 81 |
|
| 82 |
$tables = [ 'user' ]; |
| 83 |
$conds = [ new Expression( ...$args ) ]; |
| 84 |
$joinConds = []; |
| 85 |
if ( $excludeHidden ) { |
| 86 |
$conds[] = '(SELECT 1 FROM block_target hu_block_target ' . |
| 87 |
'JOIN block hu_block ON ((hu_block.bl_target=hu_block_target.bt_id)) ' . |
| 88 |
'WHERE (hu_block_target.bt_user=user_id) AND hu_block.bl_deleted = 1 ) IS NULL'; |
| 89 |
} |
| 90 |
$options = [ |
| 91 |
'LIMIT' => $limit, |
| 92 |
'ORDER BY' => [ 'user_name' ], |
| 93 |
'OFFSET' => $offset |
| 94 |
]; |
| 95 |
$database->expects( $this->once() ) |
| 96 |
->method( 'selectFieldValues' ) |
| 97 |
->with( |
| 98 |
$tables, |
| 99 |
'user_name', |
| 100 |
$conds, |
| 101 |
'MediaWiki\User\UserNamePrefixSearch::search', |
| 102 |
$options, |
| 103 |
$joinConds |
| 104 |
) |
| 105 |
->willReturn( $result ); |
| 106 |
$database->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $database ) ); |
| 107 |
|
| 108 |
$dbProvider = $this->createMock( IConnectionProvider::class ); |
| 109 |
$dbProvider->expects( $this->once() ) |
| 110 |
->method( 'getReplicaDatabase' ) |
| 111 |
->willReturn( $database ); |
| 112 |
|
| 113 |
$hideUserUtils = new HideUserUtils(); |
| 114 |
|
| 115 |
$userNamePrefixSearch = new UserNamePrefixSearch( |
| 116 |
$dbProvider, |
| 117 |
$userNameUtils, |
| 118 |
$hideUserUtils |
| 119 |
); |
| 120 |
$res = $userNamePrefixSearch->search( |
| 121 |
$audience, |
| 122 |
$prefix, |
| 123 |
$limit, |
| 124 |
$offset |
| 125 |
); |
| 126 |
$this->assertSame( $result, $res ); |
| 127 |
} |
| 128 |
|
| 129 |
public static function provideTestSearch() { |
| 130 |
|
| 131 |
return [ |
| 132 |
'public' => [ |
| 133 |
1, |
| 134 |
'', |
| 135 |
10, |
| 136 |
0, |
| 137 |
[ 'public result goes here' ] |
| 138 |
], |
| 139 |
'user without hideuser rights' => [ |
| 140 |
2, |
| 141 |
'Prefix', |
| 142 |
10, |
| 143 |
5, |
| 144 |
[ 'public result goes here, since user cannot see anything hidden' ] |
| 145 |
], |
| 146 |
'user with hideuser rights' => [ |
| 147 |
3, |
| 148 |
'AnotherPrefix', |
| 149 |
15, |
| 150 |
2, |
| 151 |
[ |
| 152 |
'result that is public', |
| 153 |
'also a result that is private' |
| 154 |
] |
| 155 |
], |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
public function testSearchInvalidAudience() { |
| 160 |
$userNamePrefixSearch = new UserNamePrefixSearch( |
| 161 |
$this->createMock( IConnectionProvider::class ), |
| 162 |
$this->createMock( UserNameUtils::class ), |
| 163 |
new HideUserUtils() |
| 164 |
); |
| 165 |
|
| 166 |
$this->expectException( InvalidArgumentException::class ); |
| 167 |
$this->expectExceptionMessage( '$audience must be AUDIENCE_PUBLIC or an Authority object' ); |
| 168 |
$userNamePrefixSearch->search( |
| 169 |
'ThisIsTheInvalidAudience', |
| 170 |
'', |
| 171 |
1, |
| 172 |
0 |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
} |