3 namespace MediaWiki\Tests\Api\Query
;
5 use MediaWiki\Permissions\UltimateAuthority
;
6 use MediaWiki\Tests\Api\ApiTestCase
;
7 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
8 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
9 use MediaWiki\User\User
;
16 * @covers \MediaWiki\Api\ApiQueryAllUsers
18 class ApiQueryAllUsersTest
extends ApiTestCase
{
19 use MockAuthorityTrait
;
20 use TempUserTestTrait
;
22 private const USER_PREFIX
= 'ApiQueryAllUsersTest ';
24 private const TEMP_USER_PREFIX
= '~';
26 private const TEMP_USER_CONFIG
= [
27 'genPattern' => self
::TEMP_USER_PREFIX
. '$1',
28 'matchPattern' => self
::TEMP_USER_PREFIX
. '$1',
29 'reservedPattern' => self
::TEMP_USER_PREFIX
. '$1',
33 private static $usersAdded = [];
35 protected function setUp(): void
{
38 $this->enableAutoCreateTempUser( self
::TEMP_USER_CONFIG
);
41 public function addDBDataOnce() {
42 $groupManager = $this->getServiceContainer()->getUserGroupManager();
43 $userA = $this->getMutableTestUser( [], self
::USER_PREFIX
. 'A' )->getUser();
44 $userB = $this->getMutableTestUser( [], self
::USER_PREFIX
. 'B' )->getUser();
45 $userC = $this->getMutableTestUser( [], self
::USER_PREFIX
. 'C' )->getUser();
46 $userD = $this->getMutableTestUser( [], self
::TEMP_USER_PREFIX
. 'D' )->getUser();
47 $groupManager->addUserToGroup( $userB, 'bot' );
48 $groupManager->addUserToGroup( $userC, 'bot' );
49 self
::$usersAdded = [ $userA, $userB, $userC, $userD ];
52 public function testPrefix() {
53 $result = $this->doApiRequest( [
56 'auprefix' => self
::USER_PREFIX
59 $this->assertArrayHasKey( 'query', $result[0] );
60 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
61 $this->assertApiResultsHasUser( self
::$usersAdded[0]->getName(), $result );
62 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
63 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
66 public function testImplicitRights() {
67 $result = $this->doApiRequest( [
70 'auprefix' => self
::USER_PREFIX
,
71 'aurights' => 'stashedit',
74 $this->assertArrayHasKey( 'query', $result[0] );
75 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
76 $this->assertApiResultsHasUser( self
::$usersAdded[0]->getName(), $result );
77 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
78 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
81 public function testTempUserImplicitRights() {
82 $result = $this->doApiRequest( [
85 'auprefix' => self
::TEMP_USER_PREFIX
,
86 'aurights' => 'stashedit',
89 $this->assertArrayHasKey( 'query', $result[0] );
90 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
91 $this->assertApiResultsHasUser( self
::$usersAdded[3]->getName(), $result );
94 public function testPermissions() {
95 $result = $this->doApiRequest( [
98 'auprefix' => self
::USER_PREFIX
,
102 $this->assertArrayHasKey( 'query', $result[0] );
103 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
104 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
105 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
108 public function testHiddenUser() {
109 $a = self
::$usersAdded[0];
110 $b = self
::$usersAdded[1];
111 $blockStatus = $this->getServiceContainer()->getBlockUserFactory()
114 new UltimateAuthority( $a ),
117 [ 'isHideUser' => true ],
120 $this->assertStatusGood( $blockStatus );
124 'list' => 'allusers',
125 'auprefix' => self
::USER_PREFIX
. 'B',
128 $result = $this->doApiRequest( $apiParams, null, null,
129 $this->mockRegisteredAuthorityWithPermissions( [] ) );
130 $this->assertSame( [], $result[0]['query']['allusers'] );
132 $result = $this->doApiRequest( $apiParams, null, null,
133 $this->mockRegisteredAuthorityWithPermissions( [ 'hideuser' ] ) );
137 'userid' => $b->getId(),
138 'name' => $b->getName(),
141 $result[0]['query']['allusers']
144 $apiParams['auprop'] = 'blockinfo';
145 $result = $this->doApiRequest( $apiParams, null, null,
146 $this->mockRegisteredAuthorityWithPermissions( [ 'hideuser' ] ) );
147 $this->assertArraySubmapSame(
149 'userid' => $b->getId(),
150 'name' => $b->getName(),
152 'blockedby' => $a->getName(),
154 'blockexpiry' => 'infinite',
155 'blockpartial' => false,
157 $result[0]['query']['allusers'][0]
161 public function testBlockInfo() {
162 $a = self
::$usersAdded[0];
163 $b = self
::$usersAdded[1];
164 $c = self
::$usersAdded[2];
166 $blockStatus = $this->getServiceContainer()->getBlockUserFactory()
169 new UltimateAuthority( $a ),
173 $this->assertStatusGood( $blockStatus );
175 $blockStatus = $this->getServiceContainer()->getBlockUserFactory()
178 new UltimateAuthority( $a ),
181 [ 'isUserTalkEditBlocked' => true ],
184 $this->assertStatusGood( $blockStatus );
186 $result = $this->doApiRequest( [
188 'list' => 'allusers',
189 'auprefix' => self
::USER_PREFIX
,
190 'auprop' => 'blockinfo'
193 $this->assertArraySubmapSame(
195 'userid' => $a->getId(),
196 'name' => $a->getName(),
198 $result[0]['query']['allusers'][0]
200 $this->assertArraySubmapSame(
202 'userid' => $b->getId(),
203 'name' => $b->getName(),
204 'blockedby' => $a->getName(),
206 'blockexpiry' => 'infinite',
207 'blockpartial' => false,
208 'blockowntalk' => false
210 $result[0]['query']['allusers'][1]
212 $this->assertArraySubmapSame(
214 'userid' => $c->getId(),
215 'name' => $c->getName(),
216 'blockedby' => $a->getName(),
218 'blockexpiry' => 'infinite',
219 'blockpartial' => false,
220 'blockowntalk' => true
222 $result[0]['query']['allusers'][2]
226 public function testUserRights() {
227 $userA = self
::$usersAdded[0];
228 $permissionManager = $this->getServiceContainer()->getPermissionManager();
229 $permissionManager->overrideUserRightsForTesting( $userA, [ 'protect' ] );
231 $result = $this->doApiRequest( [
233 'list' => 'allusers',
234 'auprefix' => self
::USER_PREFIX
,
235 'auprop' => 'rights',
239 $this->assertArrayHasKey( 'query', $result[0] );
240 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
241 $this->assertArrayHasKey( 'rights', $result[0]['query']['allusers'][0] );
242 $this->assertArrayHasKey( 'rights', $result[0]['query']['allusers'][1] );
243 $this->assertContains( 'protect', $result[0]['query']['allusers'][0]['rights'] );
244 $this->assertNotContains( 'protect', $result[0]['query']['allusers'][1]['rights'] );
247 public function testNamedOnly() {
248 $result = $this->doApiRequest( [
250 'list' => 'allusers',
251 'auexcludetemp' => true,
254 $this->assertArrayHasKey( 'query', $result[0] );
255 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
256 $this->assertApiResultsHasUser( self
::$usersAdded[0]->getName(), $result );
257 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
258 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
259 $this->assertApiResultsNotHasUser( self
::$usersAdded[3]->getName(), $result );
262 public function testNamedOnlyTempDisabled() {
263 $this->disableAutoCreateTempUser(
264 array_merge( self
::TEMP_USER_CONFIG
, [ 'known' => false ] )
267 $result = $this->doApiRequest( [
269 'list' => 'allusers',
270 'auexcludetemp' => true,
273 $this->assertArrayHasKey( 'query', $result[0] );
274 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
275 $this->assertApiResultsHasUser( self
::$usersAdded[0]->getName(), $result );
276 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
277 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
278 // The temp user from the setup will return as the filter will be disabled.
279 $this->assertApiResultsHasUser( self
::$usersAdded[3]->getName(), $result );
282 public function testNamedOnlyTempKnown() {
283 $this->disableAutoCreateTempUser(
284 array_merge( self
::TEMP_USER_CONFIG
, [ 'enabled' => false, 'known' => true ] )
287 $result = $this->doApiRequest( [
289 'list' => 'allusers',
290 'auexcludetemp' => true,
293 $this->assertArrayHasKey( 'query', $result[0] );
294 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
295 $this->assertApiResultsHasUser( self
::$usersAdded[0]->getName(), $result );
296 $this->assertApiResultsHasUser( self
::$usersAdded[1]->getName(), $result );
297 $this->assertApiResultsHasUser( self
::$usersAdded[2]->getName(), $result );
298 $this->assertApiResultsNotHasUser( self
::$usersAdded[3]->getName(), $result );
301 public function testTempOnly() {
302 $result = $this->doApiRequest( [
304 'list' => 'allusers',
305 'auexcludenamed' => true,
308 $this->assertArrayHasKey( 'query', $result[0] );
309 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
310 $this->assertApiResultsHasUser( self
::$usersAdded[3]->getName(), $result );
311 $this->assertCount( 1, $result[0]['query']['allusers'] );
314 public function testTempOnlyTempDisabled() {
315 $this->disableAutoCreateTempUser();
317 $result = $this->doApiRequest( [
319 'list' => 'allusers',
320 'auexcludenamed' => true,
323 $this->assertArrayHasKey( 'query', $result[0] );
324 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
325 // We'll get some count higher than 1 if the feature is disabled as the
326 // filter gets ignored in that case.
327 $this->assertNotCount( 1, $result[0]['query']['allusers'] );
330 public function testTempOnlyTempKnown() {
331 $this->disableAutoCreateTempUser(
332 array_merge( self
::TEMP_USER_CONFIG
, [ 'enabled' => false, 'known' => true ] )
335 $result = $this->doApiRequest( [
337 'list' => 'allusers',
338 'auexcludenamed' => true,
341 $this->assertArrayHasKey( 'query', $result[0] );
342 $this->assertArrayHasKey( 'allusers', $result[0]['query'] );
343 $this->assertApiResultsHasUser( self
::$usersAdded[3]->getName(), $result );
344 $this->assertCount( 1, $result[0]['query']['allusers'] );
347 private function assertApiResultsHasUser( $username, $results ) {
348 $this->assertNotFalse(
351 array_column( $results[0]['query']['allusers'], 'name' )
353 "Failed to assert that '{$username}' is in the AllUsers API response"
357 private function assertApiResultsNotHasUser( $username, $results ) {
361 array_column( $results[0]['query']['allusers'], 'name' )
363 "Failed to assert that '{$username}' is not in the AllUsers API response"