| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Block\DatabaseBlock; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\MainConfigSchema; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\User\User; |
| 10 |
use TestUserRegistry; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ApiUserrightsTest extends ApiTestCase { |
| 20 |
|
| 21 |
protected function setUp(): void { |
| 22 |
parent::setUp(); |
| 23 |
|
| 24 |
$this->overrideConfigValues( [ |
| 25 |
MainConfigNames::AddGroups => [], |
| 26 |
MainConfigNames::RemoveGroups => [], |
| 27 |
] ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected function setPermissions( $add = [], $remove = [] ) { |
| 39 |
$this->setGroupPermissions( 'bureaucrat', 'userrights', false ); |
| 40 |
|
| 41 |
if ( $add ) { |
| 42 |
$this->overrideConfigValue( |
| 43 |
MainConfigNames::AddGroups, |
| 44 |
[ 'bureaucrat' => $add ] + MainConfigSchema::getDefaultValue( MainConfigNames::AddGroups ) |
| 45 |
); |
| 46 |
} |
| 47 |
if ( $remove ) { |
| 48 |
$this->overrideConfigValue( |
| 49 |
MainConfigNames::RemoveGroups, |
| 50 |
[ 'bureaucrat' => $remove ] + MainConfigSchema::getDefaultValue( MainConfigNames::RemoveGroups ) |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
protected function doSuccessfulRightsChange( |
| 70 |
$expectedGroups = 'sysop', array $params = [], ?User $user = null |
| 71 |
) { |
| 72 |
$expectedGroups = (array)$expectedGroups; |
| 73 |
$params['action'] = 'userrights'; |
| 74 |
|
| 75 |
if ( !$user ) { |
| 76 |
$user = $this->getMutableTestUser()->getUser(); |
| 77 |
} |
| 78 |
|
| 79 |
$this->assertTrue( TestUserRegistry::isMutable( $user ), |
| 80 |
'Immutable user passed to doSuccessfulRightsChange!' ); |
| 81 |
|
| 82 |
if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) { |
| 83 |
$params['user'] = $user->getName(); |
| 84 |
} |
| 85 |
if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) { |
| 86 |
$params['add'] = 'sysop'; |
| 87 |
} |
| 88 |
|
| 89 |
$res = $this->doApiRequestWithToken( $params ); |
| 90 |
|
| 91 |
$user->clearInstanceCache(); |
| 92 |
$this->getServiceContainer()->getPermissionManager()->invalidateUsersRightsCache(); |
| 93 |
$this->assertSame( |
| 94 |
$expectedGroups, $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user ) |
| 95 |
); |
| 96 |
|
| 97 |
$this->assertArrayNotHasKey( 'warnings', $res[0] ); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private function doFailedRightsChange( |
| 110 |
$expectedCode, array $params = [], ?User $user = null |
| 111 |
) { |
| 112 |
$params['action'] = 'userrights'; |
| 113 |
$userGroupManager = $this->getServiceContainer()->getUserGroupManager(); |
| 114 |
|
| 115 |
$this->expectApiErrorCode( $expectedCode ); |
| 116 |
|
| 117 |
if ( !$user ) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$user = $this->getMutableTestUser()->getUser(); |
| 123 |
} |
| 124 |
|
| 125 |
$this->assertTrue( TestUserRegistry::isMutable( $user ), |
| 126 |
'Immutable user passed to doFailedRightsChange!' ); |
| 127 |
|
| 128 |
if ( !isset( $params['user'] ) && !isset( $params['userid'] ) ) { |
| 129 |
$params['user'] = $user->getName(); |
| 130 |
} |
| 131 |
if ( !isset( $params['add'] ) && !isset( $params['remove'] ) ) { |
| 132 |
$params['add'] = 'sysop'; |
| 133 |
} |
| 134 |
$expectedGroups = $userGroupManager->getUserGroups( $user ); |
| 135 |
|
| 136 |
try { |
| 137 |
$this->doApiRequestWithToken( $params ); |
| 138 |
} finally { |
| 139 |
$user->clearInstanceCache(); |
| 140 |
$this->assertSame( $expectedGroups, $userGroupManager->getUserGroups( $user ) ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function testAdd() { |
| 145 |
$this->doSuccessfulRightsChange(); |
| 146 |
} |
| 147 |
|
| 148 |
public function testBlockedWithUserrights() { |
| 149 |
$user = $this->getTestSysop()->getUser(); |
| 150 |
|
| 151 |
$block = new DatabaseBlock( [ 'address' => $user, 'by' => $user, ] ); |
| 152 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 153 |
$blockStore->insertBlock( $block ); |
| 154 |
|
| 155 |
$this->doSuccessfulRightsChange(); |
| 156 |
} |
| 157 |
|
| 158 |
public function testBlockedWithoutUserrights() { |
| 159 |
$user = $this->getTestSysop()->getUser(); |
| 160 |
|
| 161 |
$this->setPermissions( true, true ); |
| 162 |
|
| 163 |
$block = new DatabaseBlock( [ 'address' => $user, 'by' => $user ] ); |
| 164 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 165 |
$blockStore->insertBlock( $block ); |
| 166 |
|
| 167 |
$this->doFailedRightsChange( 'blocked' ); |
| 168 |
} |
| 169 |
|
| 170 |
public function testAddMultiple() { |
| 171 |
$this->doSuccessfulRightsChange( |
| 172 |
[ 'bureaucrat', 'sysop' ], |
| 173 |
[ 'add' => 'bureaucrat|sysop' ] |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
public function testTooFewExpiries() { |
| 178 |
$this->doFailedRightsChange( |
| 179 |
'toofewexpiries', |
| 180 |
[ 'add' => 'sysop|bureaucrat|bot', 'expiry' => 'infinity|tomorrow' ] |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
public function testTooManyExpiries() { |
| 185 |
$this->doFailedRightsChange( |
| 186 |
'toofewexpiries', |
| 187 |
[ 'add' => 'sysop|bureaucrat', 'expiry' => 'infinity|tomorrow|never' ] |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
public function testInvalidExpiry() { |
| 192 |
$this->doFailedRightsChange( 'invalidexpiry', [ 'expiry' => 'yummy lollipops!' ] ); |
| 193 |
} |
| 194 |
|
| 195 |
public function testMultipleInvalidExpiries() { |
| 196 |
$this->doFailedRightsChange( |
| 197 |
'invalidexpiry', |
| 198 |
[ 'add' => 'sysop|bureaucrat', 'expiry' => 'foo|bar' ] |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
public function testWithTag() { |
| 203 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 204 |
|
| 205 |
$user = $this->getMutableTestUser()->getUser(); |
| 206 |
|
| 207 |
$this->doSuccessfulRightsChange( 'sysop', [ 'tags' => 'custom tag' ], $user ); |
| 208 |
|
| 209 |
$this->assertSame( |
| 210 |
'custom tag', |
| 211 |
$this->getDb()->newSelectQueryBuilder() |
| 212 |
->select( 'ctd_name' ) |
| 213 |
->from( 'logging' ) |
| 214 |
->join( 'change_tag', null, 'ct_log_id = log_id' ) |
| 215 |
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' ) |
| 216 |
->where( [ 'log_namespace' => NS_USER, 'log_title' => strtr( $user->getName(), ' ', '_' ) ] ) |
| 217 |
->caller( __METHOD__ )->fetchField() ); |
| 218 |
} |
| 219 |
|
| 220 |
public function testWithoutTagPermission() { |
| 221 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 222 |
|
| 223 |
$this->setGroupPermissions( 'user', 'applychangetags', false ); |
| 224 |
|
| 225 |
$this->doFailedRightsChange( |
| 226 |
'tags-apply-no-permission', |
| 227 |
[ 'tags' => 'custom tag' ] |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
public function testNonexistentUser() { |
| 232 |
$this->doFailedRightsChange( |
| 233 |
'nosuchuser', |
| 234 |
[ 'user' => 'Nonexistent user' ] |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
public function testWebToken() { |
| 239 |
$sysop = $this->getTestSysop()->getUser(); |
| 240 |
$user = $this->getMutableTestUser()->getUser(); |
| 241 |
|
| 242 |
$token = $sysop->getEditToken( $user->getName() ); |
| 243 |
|
| 244 |
$res = $this->doApiRequest( [ |
| 245 |
'action' => 'userrights', |
| 246 |
'user' => $user->getName(), |
| 247 |
'add' => 'sysop', |
| 248 |
'token' => $token, |
| 249 |
] ); |
| 250 |
|
| 251 |
$user->clearInstanceCache(); |
| 252 |
$this->assertSame( [ 'sysop' ], $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user ) ); |
| 253 |
|
| 254 |
$this->assertArrayNotHasKey( 'warnings', $res[0] ); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function testAddAndRemoveGroups( |
| 267 |
?array $permissions, array $groupsToChange, array $expectedGroups |
| 268 |
) { |
| 269 |
if ( $permissions !== null ) { |
| 270 |
$this->setPermissions( $permissions[0], $permissions[1] ); |
| 271 |
} |
| 272 |
|
| 273 |
$params = [ |
| 274 |
'add' => implode( '|', $groupsToChange[0] ), |
| 275 |
'remove' => implode( '|', $groupsToChange[1] ), |
| 276 |
]; |
| 277 |
|
| 278 |
|
| 279 |
$user = $this->getMutableTestUser( [ 'bot' ] )->getUser(); |
| 280 |
|
| 281 |
$this->doSuccessfulRightsChange( $expectedGroups, $params, $user ); |
| 282 |
} |
| 283 |
|
| 284 |
public static function addAndRemoveGroupsProvider() { |
| 285 |
return [ |
| 286 |
'Simple add' => [ |
| 287 |
[ [ 'sysop' ], [] ], |
| 288 |
[ [ 'sysop' ], [] ], |
| 289 |
[ 'bot', 'sysop' ] |
| 290 |
], 'Add with only remove permission' => [ |
| 291 |
[ [], [ 'sysop' ] ], |
| 292 |
[ [ 'sysop' ], [] ], |
| 293 |
[ 'bot' ], |
| 294 |
], 'Add with global remove permission' => [ |
| 295 |
[ [], true ], |
| 296 |
[ [ 'sysop' ], [] ], |
| 297 |
[ 'bot' ], |
| 298 |
], 'Simple remove' => [ |
| 299 |
[ [], [ 'bot' ] ], |
| 300 |
[ [], [ 'bot' ] ], |
| 301 |
[], |
| 302 |
], 'Remove with only add permission' => [ |
| 303 |
[ [ 'bot' ], [] ], |
| 304 |
[ [], [ 'bot' ] ], |
| 305 |
[ 'bot' ], |
| 306 |
], 'Remove with global add permission' => [ |
| 307 |
[ true, [] ], |
| 308 |
[ [], [ 'bot' ] ], |
| 309 |
[ 'bot' ], |
| 310 |
], 'Add and remove same new group' => [ |
| 311 |
null, |
| 312 |
[ [ 'sysop' ], [ 'sysop' ] ], |
| 313 |
|
| 314 |
|
| 315 |
[ 'bot', 'sysop' ], |
| 316 |
], 'Add and remove same existing group' => [ |
| 317 |
null, |
| 318 |
[ [ 'bot' ], [ 'bot' ] ], |
| 319 |
|
| 320 |
[ 'bot' ], |
| 321 |
], |
| 322 |
]; |
| 323 |
} |
| 324 |
|
| 325 |
public function testWatched() { |
| 326 |
$user = $this->getMutableTestUser()->getUser(); |
| 327 |
$userPage = Title::makeTitle( NS_USER, $user->getName() ); |
| 328 |
$this->doSuccessfulRightsChange( 'sysop', [ 'watchuser' => true ], $user ); |
| 329 |
$this->assertTrue( $this->getServiceContainer()->getWatchlistManager() |
| 330 |
->isWatched( $this->getTestSysop()->getUser(), $userPage ) ); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public function testInterwikiLookupWithoutPermission() { |
| 338 |
$this->setGroupPermissions( 'sysop', 'userrights-interwiki', false ); |
| 339 |
|
| 340 |
$this->doFailedRightsChange( |
| 341 |
'permissiondenied', |
| 342 |
[ 'user' => 'SomeUser@remotewiki' ] |
| 343 |
); |
| 344 |
} |
| 345 |
} |