Merge "Remove use of BagOStuff TTL constants from unrelated code"
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryAllUsersTest.php
blob2329f98140fe11bd0d0a1eb87a465a360b15f5e5
1 <?php
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;
11 /**
12 * @group API
13 * @group Database
14 * @group medium
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',
32 /** @var User[] */
33 private static $usersAdded = [];
35 protected function setUp(): void {
36 parent::setUp();
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( [
54 'action' => 'query',
55 'list' => 'allusers',
56 'auprefix' => self::USER_PREFIX
57 ] );
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( [
68 'action' => 'query',
69 'list' => 'allusers',
70 'auprefix' => self::USER_PREFIX,
71 'aurights' => 'stashedit',
72 ] );
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( [
83 'action' => 'query',
84 'list' => 'allusers',
85 'auprefix' => self::TEMP_USER_PREFIX,
86 'aurights' => 'stashedit',
87 ] );
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( [
96 'action' => 'query',
97 'list' => 'allusers',
98 'auprefix' => self::USER_PREFIX,
99 'aurights' => 'bot'
100 ] );
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()
112 ->newBlockUser(
114 new UltimateAuthority( $a ),
115 'infinity',
117 [ 'isHideUser' => true ],
119 ->placeBlock();
120 $this->assertStatusGood( $blockStatus );
122 $apiParams = [
123 'action' => 'query',
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' ] ) );
135 $this->assertSame(
137 'userid' => $b->getId(),
138 'name' => $b->getName(),
139 'hidden' => true,
140 ] ],
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(),
151 'hidden' => true,
152 'blockedby' => $a->getName(),
153 'blockreason' => '',
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()
167 ->newBlockUser(
169 new UltimateAuthority( $a ),
170 'infinity',
172 ->placeBlock();
173 $this->assertStatusGood( $blockStatus );
175 $blockStatus = $this->getServiceContainer()->getBlockUserFactory()
176 ->newBlockUser(
178 new UltimateAuthority( $a ),
179 'infinity',
181 [ 'isUserTalkEditBlocked' => true ],
183 ->placeBlock();
184 $this->assertStatusGood( $blockStatus );
186 $result = $this->doApiRequest( [
187 'action' => 'query',
188 'list' => 'allusers',
189 'auprefix' => self::USER_PREFIX,
190 'auprop' => 'blockinfo'
191 ] );
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(),
205 'blockreason' => '',
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(),
217 'blockreason' => '',
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( [
232 'action' => 'query',
233 'list' => 'allusers',
234 'auprefix' => self::USER_PREFIX,
235 'auprop' => 'rights',
236 'aulimit' => 2,
237 ] );
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( [
249 'action' => 'query',
250 'list' => 'allusers',
251 'auexcludetemp' => true,
252 ] );
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( [
268 'action' => 'query',
269 'list' => 'allusers',
270 'auexcludetemp' => true,
271 ] );
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( [
288 'action' => 'query',
289 'list' => 'allusers',
290 'auexcludetemp' => true,
291 ] );
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( [
303 'action' => 'query',
304 'list' => 'allusers',
305 'auexcludenamed' => true,
306 ] );
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( [
318 'action' => 'query',
319 'list' => 'allusers',
320 'auexcludenamed' => true,
321 ] );
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( [
336 'action' => 'query',
337 'list' => 'allusers',
338 'auexcludenamed' => true,
339 ] );
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(
349 array_search(
350 $username,
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 ) {
358 $this->assertFalse(
359 array_search(
360 $username,
361 array_column( $results[0]['query']['allusers'], 'name' )
363 "Failed to assert that '{$username}' is not in the AllUsers API response"