3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\MediaWikiServices
;
11 * @covers ApiUserrights
13 class ApiUserrightsTest
extends ApiTestCase
{
15 protected function setUp() {
17 $this->tablesUsed
= array_merge(
19 [ 'change_tag', 'change_tag_def', 'logging' ]
24 * Unsets $wgGroupPermissions['bureaucrat']['userrights'], and sets
25 * $wgAddGroups['bureaucrat'] and $wgRemoveGroups['bureaucrat'] to the
28 * @param array|bool $add Groups bureaucrats should be allowed to add, true for all
29 * @param array|bool $remove Groups bureaucrats should be allowed to remove, true for all
31 protected function setPermissions( $add = [], $remove = [] ) {
32 $this->setGroupPermissions( 'bureaucrat', 'userrights', false );
35 $this->mergeMwGlobalArrayValue( 'wgAddGroups', [ 'bureaucrat' => $add ] );
38 $this->mergeMwGlobalArrayValue( 'wgRemoveGroups', [ 'bureaucrat' => $remove ] );
43 * Perform an API userrights request that's expected to be successful.
45 * @param array|string $expectedGroups Group(s) that the user is expected
46 * to have after the API request
47 * @param array $params Array to pass to doApiRequestWithToken(). 'action'
48 * => 'userrights' is implicit. If no 'user' or 'userid' is specified,
49 * we add a 'user' parameter. If no 'add' or 'remove' is specified, we
50 * add 'add' => 'sysop'.
51 * @param User|null $user The user that we're modifying. The user must be
52 * mutable, because we're going to change its groups! null means that
53 * we'll make up our own user to modify, and doesn't make sense if 'user'
54 * or 'userid' is specified in $params.
56 protected function doSuccessfulRightsChange(
57 $expectedGroups = 'sysop', array $params = [], User
$user = null
59 $expectedGroups = (array)$expectedGroups;
60 $params['action'] = 'userrights';
63 $user = $this->getMutableTestUser()->getUser();
66 $this->assertTrue( TestUserRegistry
::isMutable( $user ),
67 'Immutable user passed to doSuccessfulRightsChange!' );
69 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
70 $params['user'] = $user->getName();
72 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
73 $params['add'] = 'sysop';
76 $res = $this->doApiRequestWithToken( $params );
78 $user->clearInstanceCache();
79 MediaWikiServices
::getInstance()->getPermissionManager()->invalidateUsersRightsCache();
80 $this->assertSame( $expectedGroups, $user->getGroups() );
82 $this->assertArrayNotHasKey( 'warnings', $res[0] );
86 * Perform an API userrights request that's expected to fail.
88 * @param string $expectedException Expected exception text
89 * @param array $params As for doSuccessfulRightsChange()
90 * @param User|null $user As for doSuccessfulRightsChange(). If there's no
91 * user who will possibly be affected (such as if an invalid username is
92 * provided in $params), pass null.
94 protected function doFailedRightsChange(
95 $expectedException, array $params = [], User
$user = null
97 $params['action'] = 'userrights';
99 $this->setExpectedException( ApiUsageException
::class, $expectedException );
102 // If 'user' or 'userid' is specified and $user was not specified,
103 // the user we're creating now will have nothing to do with the API
104 // request, but that's okay, since we're just testing that it has
106 $user = $this->getMutableTestUser()->getUser();
109 $this->assertTrue( TestUserRegistry
::isMutable( $user ),
110 'Immutable user passed to doFailedRightsChange!' );
112 if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) {
113 $params['user'] = $user->getName();
115 if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) {
116 $params['add'] = 'sysop';
118 $expectedGroups = $user->getGroups();
121 $this->doApiRequestWithToken( $params );
123 $user->clearInstanceCache();
124 $this->assertSame( $expectedGroups, $user->getGroups() );
128 public function testAdd() {
129 $this->doSuccessfulRightsChange();
132 public function testBlockedWithUserrights() {
135 $block = new DatabaseBlock( [ 'address' => $wgUser, 'by' => $wgUser->getId(), ] );
139 $this->doSuccessfulRightsChange();
142 $wgUser->clearInstanceCache();
146 public function testBlockedWithoutUserrights() {
147 $user = $this->getTestSysop()->getUser();
149 $this->setPermissions( true, true );
151 $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user->getId() ] );
155 $this->doFailedRightsChange( 'You have been blocked from editing.' );
158 $user->clearInstanceCache();
162 public function testAddMultiple() {
163 $this->doSuccessfulRightsChange(
164 [ 'bureaucrat', 'sysop' ],
165 [ 'add' => 'bureaucrat|sysop' ]
169 public function testTooFewExpiries() {
170 $this->doFailedRightsChange(
171 '2 expiry timestamps were provided where 3 were needed.',
172 [ 'add' => 'sysop|bureaucrat|bot', 'expiry' => 'infinity|tomorrow' ]
176 public function testTooManyExpiries() {
177 $this->doFailedRightsChange(
178 '3 expiry timestamps were provided where 2 were needed.',
179 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'infinity|tomorrow|never' ]
183 public function testInvalidExpiry() {
184 $this->doFailedRightsChange( 'Invalid expiry time', [ 'expiry' => 'yummy lollipops!' ] );
187 public function testMultipleInvalidExpiries() {
188 $this->doFailedRightsChange(
189 'Invalid expiry time "foo".',
190 [ 'add' => 'sysop|bureaucrat', 'expiry' => 'foo|bar' ]
194 public function testWithTag() {
195 ChangeTags
::defineTag( 'custom tag' );
197 $user = $this->getMutableTestUser()->getUser();
199 $this->doSuccessfulRightsChange( 'sysop', [ 'tags' => 'custom tag' ], $user );
201 $dbr = wfGetDB( DB_REPLICA
);
205 [ 'change_tag', 'logging', 'change_tag_def' ],
208 'ct_log_id = log_id',
209 'log_namespace' => NS_USER
,
210 'log_title' => strtr( $user->getName(), ' ', '_' )
213 [ 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ] ]
218 public function testWithoutTagPermission() {
219 ChangeTags
::defineTag( 'custom tag' );
221 $this->setGroupPermissions( 'user', 'applychangetags', false );
223 $this->doFailedRightsChange(
224 'You do not have permission to apply change tags along with your changes.',
225 [ 'tags' => 'custom tag' ]
229 public function testNonexistentUser() {
230 $this->doFailedRightsChange(
231 'There is no user by the name "Nonexistent user". Check your spelling.',
232 [ 'user' => 'Nonexistent user' ]
236 public function testWebToken() {
237 $sysop = $this->getTestSysop()->getUser();
238 $user = $this->getMutableTestUser()->getUser();
240 $token = $sysop->getEditToken( $user->getName() );
242 $res = $this->doApiRequest( [
243 'action' => 'userrights',
244 'user' => $user->getName(),
249 $user->clearInstanceCache();
250 $this->assertSame( [ 'sysop' ], $user->getGroups() );
252 $this->assertArrayNotHasKey( 'warnings', $res[0] );
256 * Helper for testCanProcessExpiries that returns a mock ApiUserrights that either can or cannot
257 * process expiries. Although the regular page can process expiries, we use a mock here to
258 * ensure that it's the result of canProcessExpiries() that makes a difference, and not some
259 * error in the way we construct the mock.
261 * @param bool $canProcessExpiries
263 private function getMockForProcessingExpiries( $canProcessExpiries ) {
264 $sysop = $this->getTestSysop()->getUser();
265 $user = $this->getMutableTestUser()->getUser();
267 $token = $sysop->getEditToken( 'userrights' );
269 $main = new ApiMain( new FauxRequest( [
270 'action' => 'userrights',
271 'user' => $user->getName(),
276 $mockUserRightsPage = $this->getMockBuilder( UserrightsPage
::class )
277 ->setMethods( [ 'canProcessExpiries' ] )
279 $mockUserRightsPage->method( 'canProcessExpiries' )->willReturn( $canProcessExpiries );
281 $mockApi = $this->getMockBuilder( ApiUserrights
::class )
282 ->setConstructorArgs( [ $main, 'userrights' ] )
283 ->setMethods( [ 'getUserRightsPage' ] )
285 $mockApi->method( 'getUserRightsPage' )->willReturn( $mockUserRightsPage );
290 public function testCanProcessExpiries() {
291 $mock1 = $this->getMockForProcessingExpiries( true );
292 $this->assertArrayHasKey( 'expiry', $mock1->getAllowedParams() );
294 $mock2 = $this->getMockForProcessingExpiries( false );
295 $this->assertArrayNotHasKey( 'expiry', $mock2->getAllowedParams() );
299 * Tests adding and removing various groups with various permissions.
301 * @dataProvider addAndRemoveGroupsProvider
302 * @param array|null $permissions [ [ $wgAddGroups, $wgRemoveGroups ] ] or null for 'userrights'
303 * to be set in $wgGroupPermissions
304 * @param array $groupsToChange [ [ groups to add ], [ groups to remove ] ]
305 * @param array $expectedGroups Array of expected groups
307 public function testAddAndRemoveGroups(
308 array $permissions = null, array $groupsToChange, array $expectedGroups
310 if ( $permissions !== null ) {
311 $this->setPermissions( $permissions[0], $permissions[1] );
315 'add' => implode( '|', $groupsToChange[0] ),
316 'remove' => implode( '|', $groupsToChange[1] ),
319 // We'll take a bot so we have a group to remove
320 $user = $this->getMutableTestUser( [ 'bot' ] )->getUser();
322 $this->doSuccessfulRightsChange( $expectedGroups, $params, $user );
325 public function addAndRemoveGroupsProvider() {
331 ], 'Add with only remove permission' => [
335 ], 'Add with global remove permission' => [
339 ], 'Simple remove' => [
343 ], 'Remove with only add permission' => [
347 ], 'Remove with global add permission' => [
351 ], 'Add and remove same new group' => [
353 [ [ 'sysop' ], [ 'sysop' ] ],
354 // The userrights code does removals before adds, so it doesn't remove the sysop
355 // group here and only adds it.
357 ], 'Add and remove same existing group' => [
359 [ [ 'bot' ], [ 'bot' ] ],
360 // But here it first removes the existing group and then re-adds it.