| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\User; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use LogEntryBase; |
| 25 |
use LogicException; |
| 26 |
use MediaWiki\Block\DatabaseBlock; |
| 27 |
use MediaWiki\Config\ServiceOptions; |
| 28 |
use MediaWiki\Config\SiteConfiguration; |
| 29 |
use MediaWiki\Context\RequestContext; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\Permissions\SimpleAuthority; |
| 32 |
use MediaWiki\Request\WebRequest; |
| 33 |
use MediaWiki\Session\PHPSessionHandler; |
| 34 |
use MediaWiki\Session\SessionManager; |
| 35 |
use MediaWiki\User\TempUser\RealTempUserConfig; |
| 36 |
use MediaWiki\User\User; |
| 37 |
use MediaWiki\User\UserEditTracker; |
| 38 |
use MediaWiki\User\UserGroupManager; |
| 39 |
use MediaWiki\User\UserIdentity; |
| 40 |
use MediaWiki\User\UserIdentityValue; |
| 41 |
use MediaWiki\Utils\MWTimestamp; |
| 42 |
use MediaWikiIntegrationTestCase; |
| 43 |
use PHPUnit\Framework\MockObject\MockObject; |
| 44 |
use PHPUnit\Framework\MockObject\Rule\InvokedCount; |
| 45 |
use TestLogger; |
| 46 |
use Wikimedia\Assert\PreconditionException; |
| 47 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class UserGroupManagerTest extends MediaWikiIntegrationTestCase { |
| 54 |
|
| 55 |
private const GROUP = 'user_group_manager_test_group'; |
| 56 |
|
| 57 |
|
| 58 |
private $expiryTime; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private function getManager( |
| 67 |
array $configOverrides = [], |
| 68 |
?UserEditTracker $userEditTrackerOverride = null, |
| 69 |
?callable $callback = null |
| 70 |
): UserGroupManager { |
| 71 |
$services = $this->getServiceContainer(); |
| 72 |
return new UserGroupManager( |
| 73 |
new ServiceOptions( |
| 74 |
UserGroupManager::CONSTRUCTOR_OPTIONS, |
| 75 |
$configOverrides, |
| 76 |
[ |
| 77 |
MainConfigNames::AddGroups => [], |
| 78 |
MainConfigNames::AutoConfirmAge => 0, |
| 79 |
MainConfigNames::AutoConfirmCount => 0, |
| 80 |
MainConfigNames::Autopromote => [ |
| 81 |
'autoconfirmed' => [ APCOND_EDITCOUNT, 0 ] |
| 82 |
], |
| 83 |
MainConfigNames::AutopromoteOnce => [], |
| 84 |
MainConfigNames::GroupPermissions => [ |
| 85 |
self::GROUP => [ |
| 86 |
'runtest' => true, |
| 87 |
] |
| 88 |
], |
| 89 |
MainConfigNames::GroupsAddToSelf => [], |
| 90 |
MainConfigNames::GroupsRemoveFromSelf => [], |
| 91 |
MainConfigNames::ImplicitGroups => [ '*', 'user', 'autoconfirmed' ], |
| 92 |
MainConfigNames::RemoveGroups => [], |
| 93 |
MainConfigNames::RevokePermissions => [], |
| 94 |
], |
| 95 |
$services->getMainConfig() |
| 96 |
), |
| 97 |
$services->getReadOnlyMode(), |
| 98 |
$services->getDBLoadBalancerFactory(), |
| 99 |
$services->getHookContainer(), |
| 100 |
$userEditTrackerOverride ?? $services->getUserEditTracker(), |
| 101 |
$services->getGroupPermissionsLookup(), |
| 102 |
$services->getJobQueueGroup(), |
| 103 |
new TestLogger(), |
| 104 |
new RealTempUserConfig( [ |
| 105 |
'enabled' => true, |
| 106 |
'expireAfterDays' => null, |
| 107 |
'actions' => [ 'edit' ], |
| 108 |
'serialProvider' => [ 'type' => 'local' ], |
| 109 |
'serialMapping' => [ 'type' => 'plain-numeric' ], |
| 110 |
'matchPattern' => '*Unregistered $1', |
| 111 |
'genPattern' => '*Unregistered $1' |
| 112 |
] ), |
| 113 |
$callback ? [ $callback ] : [] |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
protected function setUp(): void { |
| 118 |
parent::setUp(); |
| 119 |
|
| 120 |
$this->expiryTime = wfTimestamp( TS_MW, time() + 100500 ); |
| 121 |
$this->clearHooks(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private function countPromise( $invokedCount ) { |
| 130 |
$mockHandler = $this->getMockBuilder( \stdClass::class ) |
| 131 |
->addMethods( [ '__invoke' ] ) |
| 132 |
->getMock(); |
| 133 |
$mockHandler->expects( $invokedCount ) |
| 134 |
->method( '__invoke' ); |
| 135 |
return $mockHandler; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private function assertMembership( |
| 145 |
UserGroupManager $manager, |
| 146 |
UserIdentity $user, |
| 147 |
string $group, |
| 148 |
?string $expiry = null |
| 149 |
) { |
| 150 |
$this->assertContains( $group, $manager->getUserGroups( $user ) ); |
| 151 |
$memberships = $manager->getUserGroupMemberships( $user ); |
| 152 |
$this->assertArrayHasKey( $group, $memberships ); |
| 153 |
$membership = $memberships[$group]; |
| 154 |
$this->assertSame( $group, $membership->getGroup() ); |
| 155 |
$this->assertSame( $user->getId(), $membership->getUserId() ); |
| 156 |
$this->assertSame( $expiry, $membership->getExpiry() ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function testNewGroupMembershipFromRow() { |
| 163 |
$row = new \stdClass(); |
| 164 |
$row->ug_user = '1'; |
| 165 |
$row->ug_group = __METHOD__; |
| 166 |
$row->ug_expiry = null; |
| 167 |
$membership = $this->getManager()->newGroupMembershipFromRow( $row ); |
| 168 |
$this->assertSame( 1, $membership->getUserId() ); |
| 169 |
$this->assertSame( __METHOD__, $membership->getGroup() ); |
| 170 |
$this->assertNull( $membership->getExpiry() ); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function testNewGroupMembershipFromRowExpiring() { |
| 177 |
$row = new \stdClass(); |
| 178 |
$row->ug_user = '1'; |
| 179 |
$row->ug_group = __METHOD__; |
| 180 |
$row->ug_expiry = $this->expiryTime; |
| 181 |
$membership = $this->getManager()->newGroupMembershipFromRow( $row ); |
| 182 |
$this->assertSame( 1, $membership->getUserId() ); |
| 183 |
$this->assertSame( __METHOD__, $membership->getGroup() ); |
| 184 |
$this->assertSame( $this->expiryTime, $membership->getExpiry() ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function testGetImplicitGroups() { |
| 191 |
$manager = $this->getManager(); |
| 192 |
$user = $this->getTestUser( 'unittesters' )->getUser(); |
| 193 |
$this->assertArrayEquals( |
| 194 |
[ '*', 'user', 'autoconfirmed' ], |
| 195 |
$manager->getUserImplicitGroups( $user ) |
| 196 |
); |
| 197 |
|
| 198 |
$user = $this->getTestUser( [ 'bureaucrat', 'test' ] )->getUser(); |
| 199 |
$this->assertArrayEquals( |
| 200 |
[ '*', 'user', 'autoconfirmed' ], |
| 201 |
$manager->getUserImplicitGroups( $user ) |
| 202 |
); |
| 203 |
|
| 204 |
$this->assertTrue( |
| 205 |
$manager->addUserToGroup( $user, self::GROUP ), |
| 206 |
'added user to group' |
| 207 |
); |
| 208 |
$this->assertArrayEquals( |
| 209 |
[ '*', 'user', 'autoconfirmed' ], |
| 210 |
$manager->getUserImplicitGroups( $user ) |
| 211 |
); |
| 212 |
|
| 213 |
$user = User::newFromName( 'UTUser1' ); |
| 214 |
$this->assertSame( [ '*' ], $manager->getUserImplicitGroups( $user ) ); |
| 215 |
|
| 216 |
$manager = $this->getManager( [ MainConfigNames::Autopromote => [ |
| 217 |
'dummy' => APCOND_EMAILCONFIRMED |
| 218 |
] ] ); |
| 219 |
$user = $this->getTestUser()->getUser(); |
| 220 |
$this->assertArrayEquals( |
| 221 |
[ '*', 'user' ], |
| 222 |
$manager->getUserImplicitGroups( $user ) |
| 223 |
); |
| 224 |
$this->assertArrayEquals( |
| 225 |
[ '*', 'user' ], |
| 226 |
$manager->getUserEffectiveGroups( $user ) |
| 227 |
); |
| 228 |
$user->confirmEmail(); |
| 229 |
$this->assertArrayEquals( |
| 230 |
[ '*', 'user', 'dummy' ], |
| 231 |
$manager->getUserImplicitGroups( $user, IDBAccessObject::READ_NORMAL, true ) |
| 232 |
); |
| 233 |
$this->assertArrayEquals( |
| 234 |
[ '*', 'user', 'dummy' ], |
| 235 |
$manager->getUserEffectiveGroups( $user ) |
| 236 |
); |
| 237 |
|
| 238 |
$user = $this->getTestUser( [ 'dummy' ] )->getUser(); |
| 239 |
$user->confirmEmail(); |
| 240 |
$this->assertArrayEquals( |
| 241 |
[ '*', 'user', 'dummy' ], |
| 242 |
$manager->getUserImplicitGroups( $user ) |
| 243 |
); |
| 244 |
|
| 245 |
$user = new User; |
| 246 |
$user->setName( '*Unregistered 1234' ); |
| 247 |
$this->assertArrayEquals( |
| 248 |
[ '*', 'temp' ], |
| 249 |
$manager->getUserImplicitGroups( $user ) |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
public static function provideGetEffectiveGroups() { |
| 254 |
yield [ [], [ '*', 'user', 'autoconfirmed' ] ]; |
| 255 |
yield [ [ 'bureaucrat', 'test' ], [ '*', 'user', 'autoconfirmed', 'bureaucrat', 'test' ] ]; |
| 256 |
yield [ [ 'autoconfirmed', 'test' ], [ '*', 'user', 'autoconfirmed', 'test' ] ]; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function testGetEffectiveGroups( $userGroups, $effectiveGroups ) { |
| 264 |
$manager = $this->getManager(); |
| 265 |
$user = $this->getTestUser( $userGroups )->getUser(); |
| 266 |
$this->assertArrayEquals( $effectiveGroups, $manager->getUserEffectiveGroups( $user ) ); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public function testGetEffectiveGroupsHook() { |
| 273 |
$manager = $this->getManager(); |
| 274 |
$user = $this->getTestUser()->getUser(); |
| 275 |
$this->setTemporaryHook( |
| 276 |
'UserEffectiveGroups', |
| 277 |
function ( UserIdentity $hookUser, array &$groups ) use ( $user ) { |
| 278 |
$this->assertTrue( $hookUser->equals( $user ) ); |
| 279 |
$groups[] = 'from_hook'; |
| 280 |
} |
| 281 |
); |
| 282 |
$this->assertContains( 'from_hook', $manager->getUserEffectiveGroups( $user ) ); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public function testAddUserToGroup() { |
| 291 |
$manager = $this->getManager(); |
| 292 |
$user = $this->getMutableTestUser()->getUser(); |
| 293 |
|
| 294 |
$result = $manager->addUserToGroup( $user, self::GROUP ); |
| 295 |
$this->assertTrue( $result ); |
| 296 |
$this->assertMembership( $manager, $user, self::GROUP ); |
| 297 |
$manager->clearCache( $user ); |
| 298 |
$this->assertMembership( $manager, $user, self::GROUP ); |
| 299 |
|
| 300 |
|
| 301 |
$result = $manager->addUserToGroup( $user, self::GROUP, $this->expiryTime ); |
| 302 |
$this->assertFalse( $result ); |
| 303 |
|
| 304 |
|
| 305 |
$result = $manager->addUserToGroup( $user, self::GROUP, $this->expiryTime, true ); |
| 306 |
$this->assertTrue( $result ); |
| 307 |
$this->assertMembership( $manager, $user, self::GROUP, $this->expiryTime ); |
| 308 |
$manager->clearCache( $user ); |
| 309 |
$this->assertMembership( $manager, $user, self::GROUP, $this->expiryTime ); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function testAddUserToGroupReadonly() { |
| 316 |
$user = $this->getTestUser()->getUser(); |
| 317 |
$this->getServiceContainer()->getReadOnlyMode()->setReason( 'TEST' ); |
| 318 |
$manager = $this->getManager(); |
| 319 |
$this->assertFalse( $manager->addUserToGroup( $user, 'test' ) ); |
| 320 |
$this->assertNotContains( 'test', $manager->getUserGroups( $user ) ); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function testAddUserToGroupAnon() { |
| 327 |
$manager = $this->getManager(); |
| 328 |
$anon = new UserIdentityValue( 0, 'Anon' ); |
| 329 |
$this->expectException( InvalidArgumentException::class ); |
| 330 |
$manager->addUserToGroup( $anon, 'test' ); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function testAddUserToGroupHookAbort() { |
| 337 |
$manager = $this->getManager(); |
| 338 |
$user = $this->getTestUser()->getUser(); |
| 339 |
$originalGroups = $manager->getUserGroups( $user ); |
| 340 |
$this->setTemporaryHook( |
| 341 |
'UserAddGroup', |
| 342 |
function ( UserIdentity $hookUser ) use ( $user ) { |
| 343 |
$this->assertTrue( $hookUser->equals( $user ) ); |
| 344 |
return false; |
| 345 |
} |
| 346 |
); |
| 347 |
$this->assertFalse( $manager->addUserToGroup( $user, 'test_group' ) ); |
| 348 |
$this->assertArrayEquals( $originalGroups, $manager->getUserGroups( $user ) ); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function testAddUserToGroupHookModify() { |
| 355 |
$manager = $this->getManager(); |
| 356 |
$user = $this->getTestUser()->getUser(); |
| 357 |
$this->setTemporaryHook( |
| 358 |
'UserAddGroup', |
| 359 |
function ( UserIdentity $hookUser, &$group, &$hookExp ) use ( $user ) { |
| 360 |
$this->assertTrue( $hookUser->equals( $user ) ); |
| 361 |
$this->assertSame( self::GROUP, $group ); |
| 362 |
$this->assertSame( $this->expiryTime, $hookExp ); |
| 363 |
$group = 'from_hook'; |
| 364 |
$hookExp = null; |
| 365 |
return true; |
| 366 |
} |
| 367 |
); |
| 368 |
$this->assertTrue( $manager->addUserToGroup( $user, self::GROUP, $this->expiryTime ) ); |
| 369 |
$this->assertContains( 'from_hook', $manager->getUserGroups( $user ) ); |
| 370 |
$this->assertNotContains( self::GROUP, $manager->getUserGroups( $user ) ); |
| 371 |
$this->assertNull( $manager->getUserGroupMemberships( $user )['from_hook']->getExpiry() ); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public function testAddUserToMultipleGroups() { |
| 378 |
$manager = $this->getManager(); |
| 379 |
$user = $this->getMutableTestUser()->getUser(); |
| 380 |
|
| 381 |
$manager->addUserToMultipleGroups( $user, [ self::GROUP, self::GROUP . '1' ] ); |
| 382 |
$this->assertMembership( $manager, $user, self::GROUP ); |
| 383 |
$this->assertMembership( $manager, $user, self::GROUP . '1' ); |
| 384 |
|
| 385 |
$anon = new UserIdentityValue( 0, 'Anon' ); |
| 386 |
$this->expectException( InvalidArgumentException::class ); |
| 387 |
$manager->addUserToMultipleGroups( $anon, [ self::GROUP, self::GROUP . '1' ] ); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public function testGetUserGroupMembershipsForAnon() { |
| 394 |
$manager = $this->getManager(); |
| 395 |
$anon = new UserIdentityValue( 0, 'Anon' ); |
| 396 |
|
| 397 |
$this->assertSame( [], $manager->getUserGroupMemberships( $anon ) ); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public function testGetUserFormerGroupsForAnon() { |
| 404 |
$manager = $this->getManager(); |
| 405 |
$anon = new UserIdentityValue( 0, 'Anon' ); |
| 406 |
|
| 407 |
$this->assertSame( [], $manager->getUserFormerGroups( $anon ) ); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function testRemoveUserFromGroup() { |
| 417 |
$manager = $this->getManager(); |
| 418 |
$user = $this->getMutableTestUser( [ self::GROUP ] )->getUser(); |
| 419 |
$this->assertMembership( $manager, $user, self::GROUP ); |
| 420 |
|
| 421 |
$result = $manager->removeUserFromGroup( $user, self::GROUP ); |
| 422 |
$this->assertTrue( $result ); |
| 423 |
$this->assertNotContains( self::GROUP, |
| 424 |
$manager->getUserGroups( $user ) ); |
| 425 |
$this->assertArrayNotHasKey( self::GROUP, |
| 426 |
$manager->getUserGroupMemberships( $user ) ); |
| 427 |
$this->assertContains( self::GROUP, |
| 428 |
$manager->getUserFormerGroups( $user ) ); |
| 429 |
$manager->clearCache( $user ); |
| 430 |
$this->assertNotContains( self::GROUP, |
| 431 |
$manager->getUserGroups( $user ) ); |
| 432 |
$this->assertArrayNotHasKey( self::GROUP, |
| 433 |
$manager->getUserGroupMemberships( $user ) ); |
| 434 |
$this->assertContains( self::GROUP, |
| 435 |
$manager->getUserFormerGroups( $user ) ); |
| 436 |
$this->assertContains( self::GROUP, |
| 437 |
$manager->getUserFormerGroups( $user ) ); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public function testRemoveUserToGroupHookAbort() { |
| 444 |
$manager = $this->getManager(); |
| 445 |
$user = $this->getTestUser( [ self::GROUP ] )->getUser(); |
| 446 |
$originalGroups = $manager->getUserGroups( $user ); |
| 447 |
$this->setTemporaryHook( |
| 448 |
'UserRemoveGroup', |
| 449 |
function ( UserIdentity $hookUser ) use ( $user ) { |
| 450 |
$this->assertTrue( $hookUser->equals( $user ) ); |
| 451 |
return false; |
| 452 |
} |
| 453 |
); |
| 454 |
$this->assertFalse( $manager->removeUserFromGroup( $user, self::GROUP ) ); |
| 455 |
$this->assertArrayEquals( $originalGroups, $manager->getUserGroups( $user ) ); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function testRemoveUserFromGroupHookModify() { |
| 462 |
$manager = $this->getManager(); |
| 463 |
$user = $this->getTestUser( [ self::GROUP, 'from_hook' ] )->getUser(); |
| 464 |
$this->setTemporaryHook( |
| 465 |
'UserRemoveGroup', |
| 466 |
function ( UserIdentity $hookUser, &$group ) use ( $user ) { |
| 467 |
$this->assertTrue( $hookUser->equals( $user ) ); |
| 468 |
$this->assertSame( self::GROUP, $group ); |
| 469 |
$group = 'from_hook'; |
| 470 |
return true; |
| 471 |
} |
| 472 |
); |
| 473 |
$this->assertTrue( $manager->removeUserFromGroup( $user, self::GROUP ) ); |
| 474 |
$this->assertNotContains( 'from_hook', $manager->getUserGroups( $user ) ); |
| 475 |
$this->assertContains( self::GROUP, $manager->getUserGroups( $user ) ); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public function testRemoveUserFromGroupReadOnly() { |
| 482 |
$user = $this->getTestUser( [ 'test' ] )->getUser(); |
| 483 |
$this->getServiceContainer()->getReadOnlyMode()->setReason( 'TEST' ); |
| 484 |
$manager = $this->getManager(); |
| 485 |
$this->assertFalse( $manager->removeUserFromGroup( $user, 'test' ) ); |
| 486 |
$this->assertContains( 'test', $manager->getUserGroups( $user ) ); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function testRemoveUserFromGroupAnon() { |
| 493 |
$manager = $this->getManager(); |
| 494 |
$anon = new UserIdentityValue( 0, 'Anon' ); |
| 495 |
$this->expectException( InvalidArgumentException::class ); |
| 496 |
$manager->removeUserFromGroup( $anon, 'test' ); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function testRemoveUserFromGroupCallback() { |
| 503 |
$user = $this->getTestUser( [ 'test' ] )->getUser(); |
| 504 |
$calledCount = 0; |
| 505 |
$callback = function ( UserIdentity $callbackUser ) use ( $user, &$calledCount ) { |
| 506 |
$this->assertTrue( $callbackUser->equals( $user ) ); |
| 507 |
$calledCount += 1; |
| 508 |
}; |
| 509 |
$manager = $this->getManager( [], null, $callback ); |
| 510 |
$this->assertTrue( $manager->removeUserFromGroup( $user, 'test' ) ); |
| 511 |
$this->assertNotContains( 'test', $manager->getUserGroups( $user ) ); |
| 512 |
$this->assertSame( 1, $calledCount ); |
| 513 |
$this->assertFalse( $manager->removeUserFromGroup( $user, 'test' ) ); |
| 514 |
$this->assertSame( 1, $calledCount ); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function testPurgeExpired() { |
| 521 |
$manager = $this->getManager(); |
| 522 |
$user = $this->getTestUser()->getUser(); |
| 523 |
$expiryInPast = wfTimestamp( TS_MW, time() - 100500 ); |
| 524 |
$this->assertTrue( |
| 525 |
$manager->addUserToGroup( $user, 'expired', $expiryInPast ), |
| 526 |
'can add expired group' |
| 527 |
); |
| 528 |
$manager->purgeExpired(); |
| 529 |
$this->assertNotContains( 'expired', $manager->getUserGroups( $user ) ); |
| 530 |
$this->assertArrayNotHasKey( 'expired', $manager->getUserGroupMemberships( $user ) ); |
| 531 |
$this->assertContains( 'expired', $manager->getUserFormerGroups( $user ) ); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
public function testPurgeExpiredReadOnly() { |
| 538 |
$this->getServiceContainer()->getReadOnlyMode()->setReason( 'TEST' ); |
| 539 |
$manager = $this->getManager(); |
| 540 |
$this->assertFalse( $manager->purgeExpired() ); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public function testGetAllGroups() { |
| 547 |
$manager = $this->getManager( [ |
| 548 |
MainConfigNames::GroupPermissions => [ |
| 549 |
__METHOD__ => [ 'test' => true ], |
| 550 |
'implicit' => [ 'test' => true ] |
| 551 |
], |
| 552 |
MainConfigNames::RevokePermissions => [ |
| 553 |
'revoked' => [ 'test' => true ] |
| 554 |
], |
| 555 |
MainConfigNames::ImplicitGroups => [ 'implicit' ] |
| 556 |
] ); |
| 557 |
$this->assertArrayEquals( [ __METHOD__, 'revoked' ], $manager->listAllGroups() ); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public function testGetAllImplicitGroups() { |
| 564 |
$manager = $this->getManager( [ MainConfigNames::ImplicitGroups => [ __METHOD__ ] ] ); |
| 565 |
$this->assertArrayEquals( [ __METHOD__ ], $manager->listAllImplicitGroups() ); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public function testLoadGroupMembershipsFromArray() { |
| 572 |
$manager = $this->getManager(); |
| 573 |
$user = $this->getTestUser()->getUser(); |
| 574 |
$row = new \stdClass(); |
| 575 |
$row->ug_user = $user->getId(); |
| 576 |
$row->ug_group = 'test'; |
| 577 |
$row->ug_expiry = null; |
| 578 |
$manager->loadGroupMembershipsFromArray( $user, [ $row ], IDBAccessObject::READ_NORMAL ); |
| 579 |
$memberships = $manager->getUserGroupMemberships( $user ); |
| 580 |
$this->assertCount( 1, $memberships ); |
| 581 |
$this->assertArrayHasKey( 'test', $memberships ); |
| 582 |
$this->assertSame( $user->getId(), $memberships['test']->getUserId() ); |
| 583 |
$this->assertSame( 'test', $memberships['test']->getGroup() ); |
| 584 |
} |
| 585 |
|
| 586 |
public function provideGetUserAutopromoteEmailConfirmed() { |
| 587 |
$successUserMock = $this->createNoOpMock( |
| 588 |
User::class, [ 'getEmail', 'getEmailAuthenticationTimestamp', 'isTemp', 'assertWiki' ] |
| 589 |
); |
| 590 |
$successUserMock->method( 'assertWiki' )->willReturn( true ); |
| 591 |
$successUserMock->expects( $this->once() ) |
| 592 |
->method( 'getEmail' ) |
| 593 |
->willReturn( '[email protected]' ); |
| 594 |
$successUserMock->expects( $this->once() ) |
| 595 |
->method( 'getEmailAuthenticationTimestamp' ) |
| 596 |
->willReturn( wfTimestampNow() ); |
| 597 |
yield 'Successful autopromote' => [ |
| 598 |
true, $successUserMock, [ 'test_autoconfirmed' ] |
| 599 |
]; |
| 600 |
$emailAuthMock = $this->createNoOpMock( User::class, [ 'getEmail', 'isTemp', 'assertWiki' ] ); |
| 601 |
$emailAuthMock->method( 'assertWiki' )->willReturn( true ); |
| 602 |
$emailAuthMock->expects( $this->once() ) |
| 603 |
->method( 'getEmail' ) |
| 604 |
->willReturn( '[email protected]' ); |
| 605 |
yield 'wgEmailAuthentication is false' => [ |
| 606 |
false, $emailAuthMock, [ 'test_autoconfirmed' ] |
| 607 |
]; |
| 608 |
$invalidEmailMock = $this->createNoOpMock( User::class, [ 'getEmail', 'isTemp', 'assertWiki' ] ); |
| 609 |
$invalidEmailMock->method( 'assertWiki' )->willReturn( true ); |
| 610 |
$invalidEmailMock |
| 611 |
->expects( $this->once() ) |
| 612 |
->method( 'getEmail' ) |
| 613 |
->willReturn( 'INVALID!' ); |
| 614 |
yield 'Invalid email' => [ |
| 615 |
true, $invalidEmailMock, [] |
| 616 |
]; |
| 617 |
$nullTimestampMock = $this->createNoOpMock( |
| 618 |
User::class, [ 'getEmail', 'getEmailAuthenticationTimestamp', 'isTemp', 'assertWiki' ] |
| 619 |
); |
| 620 |
$nullTimestampMock->method( 'assertWiki' )->willReturn( true ); |
| 621 |
$nullTimestampMock->expects( $this->once() ) |
| 622 |
->method( 'getEmail' ) |
| 623 |
->willReturn( '[email protected]' ); |
| 624 |
$nullTimestampMock->expects( $this->once() ) |
| 625 |
->method( 'getEmailAuthenticationTimestamp' ) |
| 626 |
->willReturn( null ); |
| 627 |
yield 'Invalid email auth timestamp' => [ |
| 628 |
true, $nullTimestampMock, [] |
| 629 |
]; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
public function testGetUserAutopromoteEmailConfirmed( |
| 641 |
bool $emailAuthentication, |
| 642 |
User $user, |
| 643 |
array $expected |
| 644 |
) { |
| 645 |
$manager = $this->getManager( [ |
| 646 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => [ APCOND_EMAILCONFIRMED ] ], |
| 647 |
MainConfigNames::EmailAuthentication => $emailAuthentication |
| 648 |
] ); |
| 649 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 650 |
} |
| 651 |
|
| 652 |
public static function provideGetUserAutopromoteEditCount() { |
| 653 |
yield 'Successful promote' => [ |
| 654 |
[ APCOND_EDITCOUNT, 5 ], true, 10, [ 'test_autoconfirmed' ] |
| 655 |
]; |
| 656 |
yield 'Required edit count negative' => [ |
| 657 |
[ APCOND_EDITCOUNT, -1 ], true, 10, [ 'test_autoconfirmed' ] |
| 658 |
]; |
| 659 |
yield 'No edit count, use AutoConfirmCount = 11' => [ |
| 660 |
[ APCOND_EDITCOUNT ], true, 10, [] |
| 661 |
]; |
| 662 |
yield 'Null edit count, use AutoConfirmCount = 11' => [ |
| 663 |
[ APCOND_EDITCOUNT, null ], true, 13, [ 'test_autoconfirmed' ] |
| 664 |
]; |
| 665 |
yield 'Anon' => [ |
| 666 |
[ APCOND_EDITCOUNT, 5 ], false, 100, [] |
| 667 |
]; |
| 668 |
yield 'Not enough edits' => [ |
| 669 |
[ APCOND_EDITCOUNT, 100 ], true, 10, [] |
| 670 |
]; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
public function testGetUserAutopromoteEditCount( |
| 679 |
array $requiredCond, |
| 680 |
bool $userRegistered, |
| 681 |
int $userEditCount, |
| 682 |
array $expected |
| 683 |
) { |
| 684 |
$userEditTrackerMock = $this->createNoOpMock( |
| 685 |
UserEditTracker::class, |
| 686 |
[ 'getUserEditCount' ] |
| 687 |
); |
| 688 |
if ( $userRegistered ) { |
| 689 |
$user = $this->getTestUser()->getUser(); |
| 690 |
$userEditTrackerMock->method( 'getUserEditCount' ) |
| 691 |
->with( $user ) |
| 692 |
->willReturn( $userEditCount ); |
| 693 |
} else { |
| 694 |
$user = User::newFromName( 'UTUser1' ); |
| 695 |
} |
| 696 |
$manager = $this->getManager( |
| 697 |
[ |
| 698 |
MainConfigNames::AutoConfirmCount => 11, |
| 699 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => $requiredCond ] |
| 700 |
], |
| 701 |
$userEditTrackerMock |
| 702 |
); |
| 703 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 704 |
} |
| 705 |
|
| 706 |
public static function provideGetUserAutopromoteAge() { |
| 707 |
yield 'Successful promote' => [ |
| 708 |
[ APCOND_AGE, 1000 ], |
| 709 |
MWTimestamp::convert( TS_MW, time() - 1000000 ), |
| 710 |
[ 'test_autoconfirmed' ] |
| 711 |
]; |
| 712 |
yield 'Not old enough' => [ |
| 713 |
[ APCOND_AGE, 10000000 ], MWTimestamp::now(), [] |
| 714 |
]; |
| 715 |
yield 'Not old enough, using AutoConfirmAge via unset' => [ |
| 716 |
[ APCOND_AGE ], MWTimestamp::now(), [] |
| 717 |
]; |
| 718 |
yield 'Not old enough, using AutoConfirmAge via null' => [ |
| 719 |
[ APCOND_AGE, null ], MWTimestamp::now(), [] |
| 720 |
]; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
public function testGetUserAutopromoteAge( |
| 732 |
array $requiredCondition, |
| 733 |
string $registrationTs, |
| 734 |
array $expected |
| 735 |
) { |
| 736 |
$manager = $this->getManager( [ |
| 737 |
MainConfigNames::AutoConfirmAge => 10000000, |
| 738 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => $requiredCondition ] |
| 739 |
] ); |
| 740 |
$user = $this->createNoOpMock( User::class, [ 'getRegistration', 'isTemp', 'assertWiki' ] ); |
| 741 |
$user->method( 'assertWiki' )->willReturn( true ); |
| 742 |
$user->method( 'getRegistration' ) |
| 743 |
->willReturn( $registrationTs ); |
| 744 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 745 |
} |
| 746 |
|
| 747 |
public static function provideGetUserAutopromoteEditAge() { |
| 748 |
yield 'Successful promote' => [ |
| 749 |
[ APCOND_AGE_FROM_EDIT, 1000 ], |
| 750 |
MWTimestamp::convert( TS_MW, time() - 1000000 ), |
| 751 |
[ 'test_autoconfirmed' ] |
| 752 |
]; |
| 753 |
yield 'Not old enough' => [ |
| 754 |
[ APCOND_AGE_FROM_EDIT, 10000000 ], MWTimestamp::now(), [] |
| 755 |
]; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public function testGetUserAutopromoteEditAge( |
| 767 |
array $requiredCondition, |
| 768 |
string $firstEditTs, |
| 769 |
array $expected |
| 770 |
) { |
| 771 |
$user = $this->getTestUser()->getUser(); |
| 772 |
$mockUserEditTracker = $this->createNoOpMock( UserEditTracker::class, [ 'getFirstEditTimestamp' ] ); |
| 773 |
$mockUserEditTracker->expects( $this->once() ) |
| 774 |
->method( 'getFirstEditTimestamp' ) |
| 775 |
->with( $user ) |
| 776 |
->willReturn( $firstEditTs ); |
| 777 |
$manager = $this->getManager( [ |
| 778 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => $requiredCondition ] |
| 779 |
], $mockUserEditTracker ); |
| 780 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 781 |
} |
| 782 |
|
| 783 |
public static function provideGetUserAutopromoteGroups() { |
| 784 |
yield 'Successful promote' => [ |
| 785 |
[ 'group1', 'group2' ], [ 'group1', 'group2' ], [ 'test_autoconfirmed' ] |
| 786 |
]; |
| 787 |
yield 'Not enough groups to promote' => [ |
| 788 |
[ 'group1', 'group2' ], [ 'group1' ], [] |
| 789 |
]; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public function testGetUserAutopromoteGroups( |
| 798 |
array $requiredGroups, |
| 799 |
array $userGroups, |
| 800 |
array $expected |
| 801 |
) { |
| 802 |
$user = $this->getTestUser( $userGroups )->getUser(); |
| 803 |
$manager = $this->getManager( [ |
| 804 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => array_merge( [ APCOND_INGROUPS ], $requiredGroups ) ] |
| 805 |
] ); |
| 806 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 807 |
} |
| 808 |
|
| 809 |
public static function provideGetUserAutopromoteIP() { |
| 810 |
yield 'Individual ip, success' => [ |
| 811 |
[ APCOND_ISIP, '123.123.123.123' ], '123.123.123.123', [ 'test_autoconfirmed' ] |
| 812 |
]; |
| 813 |
yield 'Individual ip, failed' => [ |
| 814 |
[ APCOND_ISIP, '123.123.123.123' ], '124.124.124.124', [] |
| 815 |
]; |
| 816 |
yield 'Range ip, success' => [ |
| 817 |
[ APCOND_IPINRANGE, '123.123.123.1/24' ], '123.123.123.123', [ 'test_autoconfirmed' ] |
| 818 |
]; |
| 819 |
yield 'Range ip, failed' => [ |
| 820 |
[ APCOND_IPINRANGE, '123.123.123.1/24' ], '124.124.124.124', [] |
| 821 |
]; |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
public function testGetUserAutopromoteIP( |
| 833 |
array $condition, |
| 834 |
string $userIp, |
| 835 |
array $expected |
| 836 |
) { |
| 837 |
$manager = $this->getManager( [ |
| 838 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => $condition ] |
| 839 |
] ); |
| 840 |
$requestMock = $this->createNoOpMock( WebRequest::class, [ 'getIP' ] ); |
| 841 |
$requestMock->expects( $this->once() ) |
| 842 |
->method( 'getIP' ) |
| 843 |
->willReturn( $userIp ); |
| 844 |
$user = $this->createNoOpMock( User::class, [ 'getRequest', 'isTemp', 'assertWiki' ] ); |
| 845 |
$user->method( 'assertWiki' )->willReturn( true ); |
| 846 |
$user->expects( $this->once() ) |
| 847 |
->method( 'getRequest' ) |
| 848 |
->willReturn( $requestMock ); |
| 849 |
$this->assertArrayEquals( $expected, $manager->getUserAutopromoteGroups( $user ) ); |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
public function testGetUserAutopromoteGroupsHook() { |
| 856 |
$manager = $this->getManager( [ MainConfigNames::Autopromote => [] ] ); |
| 857 |
$user = $this->getTestUser()->getUser(); |
| 858 |
$this->setTemporaryHook( |
| 859 |
'GetAutoPromoteGroups', |
| 860 |
function ( User $hookUser, array &$promote ) use ( $user ){ |
| 861 |
$this->assertTrue( $user->equals( $hookUser ) ); |
| 862 |
$this->assertSame( [], $promote ); |
| 863 |
$promote[] = 'from_hook'; |
| 864 |
} |
| 865 |
); |
| 866 |
$this->assertArrayEquals( [ 'from_hook' ], $manager->getUserAutopromoteGroups( $user ) ); |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public function testGetUserAutopromoteComplexCondition() { |
| 874 |
$manager = $this->getManager( [ |
| 875 |
MainConfigNames::Autopromote => [ |
| 876 |
'test_autoconfirmed' => [ '&', |
| 877 |
[ APCOND_INGROUPS, 'group1' ], |
| 878 |
[ '!', [ APCOND_INGROUPS, 'group2' ] ], |
| 879 |
[ '^', [ APCOND_INGROUPS, 'group3' ], [ APCOND_INGROUPS, 'group4' ] ], |
| 880 |
[ '|', [ APCOND_INGROUPS, 'group5' ], [ APCOND_INGROUPS, 'group6' ] ] |
| 881 |
] |
| 882 |
] |
| 883 |
] ); |
| 884 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( |
| 885 |
$this->getTestUser( [ 'group1' ] )->getUser() ) |
| 886 |
); |
| 887 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( |
| 888 |
$this->getTestUser( [ 'group1', 'group2' ] )->getUser() ) |
| 889 |
); |
| 890 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( |
| 891 |
$this->getTestUser( [ 'group1', 'group3', 'group4' ] )->getUser() ) |
| 892 |
); |
| 893 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( |
| 894 |
$this->getTestUser( [ 'group1', 'group3' ] )->getUser() ) |
| 895 |
); |
| 896 |
$this->assertArrayEquals( |
| 897 |
[ 'test_autoconfirmed' ], |
| 898 |
$manager->getUserAutopromoteGroups( $this->getTestUser( [ 'group1', 'group3', 'group5' ] )->getUser() ) |
| 899 |
); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
public function testGetUserAutopromoteBot() { |
| 907 |
$manager = $this->getManager( [ |
| 908 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => [ APCOND_ISBOT ] ] |
| 909 |
] ); |
| 910 |
$notBot = $this->getTestUser()->getUser(); |
| 911 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( $notBot ) ); |
| 912 |
$bot = $this->getTestUser( [ 'bot' ] )->getUser(); |
| 913 |
$this->assertArrayEquals( [ 'test_autoconfirmed' ], |
| 914 |
$manager->getUserAutopromoteGroups( $bot ) ); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
public function testGetUserAutopromoteBlocked() { |
| 922 |
$manager = $this->getManager( [ |
| 923 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => [ APCOND_BLOCKED ] ] |
| 924 |
] ); |
| 925 |
$nonBlockedUser = $this->getTestUser()->getUser(); |
| 926 |
$this->assertSame( [], $manager->getUserAutopromoteGroups( $nonBlockedUser ) ); |
| 927 |
$blockedUser = $this->getTestUser( [ 'blocked' ] )->getUser(); |
| 928 |
$block = new DatabaseBlock(); |
| 929 |
$block->setTarget( $blockedUser ); |
| 930 |
$block->setBlocker( $this->getTestSysop()->getUser() ); |
| 931 |
$block->isSitewide( true ); |
| 932 |
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); |
| 933 |
$this->assertArrayEquals( [ 'test_autoconfirmed' ], |
| 934 |
$manager->getUserAutopromoteGroups( $blockedUser ) ); |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
public function testGetUserAutopromoteBlockedDoesNotRecurse() { |
| 942 |
|
| 943 |
if ( !PHPSessionHandler::isInstalled() ) { |
| 944 |
PHPSessionHandler::install( |
| 945 |
SessionManager::singleton() |
| 946 |
); |
| 947 |
} |
| 948 |
$oldSessionId = session_id(); |
| 949 |
|
| 950 |
$context = RequestContext::getMain(); |
| 951 |
|
| 952 |
$oInfo = $context->exportSession(); |
| 953 |
|
| 954 |
$user = User::newFromName( 'UnitTestContextUser' ); |
| 955 |
$user->addToDatabase(); |
| 956 |
|
| 957 |
$sinfo = [ |
| 958 |
'sessionId' => 'd612ee607c87e749ef14da4983a702cd', |
| 959 |
'userId' => $user->getId(), |
| 960 |
'ip' => '192.0.2.0', |
| 961 |
'headers' => [ |
| 962 |
'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0' |
| 963 |
] |
| 964 |
]; |
| 965 |
$this->overrideConfigValue( |
| 966 |
MainConfigNames::Autopromote, |
| 967 |
[ 'test_autoconfirmed' => [ '&', APCOND_BLOCKED ] ] |
| 968 |
); |
| 969 |
|
| 970 |
$sc = RequestContext::importScopedSession( $sinfo ); |
| 971 |
$info = $context->exportSession(); |
| 972 |
|
| 973 |
$this->assertNull( $user->getBlock() ); |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
public function testGetUserAutopromoteBlockedDoesNotRecurseWithHook() { |
| 981 |
$this->overrideConfigValue( |
| 982 |
MainConfigNames::Autopromote, |
| 983 |
[ 'test_autoconfirmed' => [ '&', APCOND_BLOCKED ] ] |
| 984 |
); |
| 985 |
|
| 986 |
|
| 987 |
if ( !PHPSessionHandler::isInstalled() ) { |
| 988 |
PHPSessionHandler::install( |
| 989 |
SessionManager::singleton() |
| 990 |
); |
| 991 |
} |
| 992 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 993 |
$permissionManager->invalidateUsersRightsCache(); |
| 994 |
|
| 995 |
$oldSessionId = session_id(); |
| 996 |
|
| 997 |
$context = RequestContext::getMain(); |
| 998 |
|
| 999 |
$oInfo = $context->exportSession(); |
| 1000 |
|
| 1001 |
$user = User::newFromName( 'UnitTestContextUser' ); |
| 1002 |
$user->addToDatabase(); |
| 1003 |
|
| 1004 |
$sinfo = [ |
| 1005 |
'sessionId' => 'd612ee607c87e749ef14da4983a702cd', |
| 1006 |
'userId' => $user->getId(), |
| 1007 |
'ip' => '192.0.2.0', |
| 1008 |
'headers' => [ |
| 1009 |
'USER-AGENT' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0' |
| 1010 |
] |
| 1011 |
]; |
| 1012 |
|
| 1013 |
$onGetUserBlockCalled = false; |
| 1014 |
$this->setTemporaryHook( |
| 1015 |
'GetUserBlock', |
| 1016 |
static function ( $user, $ip, &$block ) use ( $permissionManager, &$onGetUserBlockCalled ) { |
| 1017 |
$onGetUserBlockCalled = true; |
| 1018 |
|
| 1019 |
try { |
| 1020 |
if ( $permissionManager->userHasAnyRight( $user, 'ipblock-exempt', 'globalblock-exempt' ) ) { |
| 1021 |
return true; |
| 1022 |
} |
| 1023 |
} catch ( LogicException $e ) { |
| 1024 |
|
| 1025 |
|
| 1026 |
if ( !str_starts_with( $e->getMessage(), "Unexpected recursion!" ) ) { |
| 1027 |
throw $e; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
return true; |
| 1032 |
} |
| 1033 |
); |
| 1034 |
|
| 1035 |
|
| 1036 |
$sc = RequestContext::importScopedSession( $sinfo ); |
| 1037 |
$info = $context->exportSession(); |
| 1038 |
|
| 1039 |
$this->assertNull( $user->getBlock() ); |
| 1040 |
|
| 1041 |
$this->assertTrue( |
| 1042 |
$onGetUserBlockCalled, |
| 1043 |
'Check that HookRunner::onGetUserBlock was called' |
| 1044 |
); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
public function testGetUserAutopromoteInvalid() { |
| 1051 |
$manager = $this->getManager( [ |
| 1052 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => [ 999 ] ] |
| 1053 |
] ); |
| 1054 |
$user = $this->getTestUser()->getUser(); |
| 1055 |
$this->expectException( InvalidArgumentException::class ); |
| 1056 |
$manager->getUserAutopromoteGroups( $user ); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
public function testGetUserAutopromoteConditionHook() { |
| 1064 |
$user = $this->getTestUser()->getUser(); |
| 1065 |
$this->setTemporaryHook( |
| 1066 |
'AutopromoteCondition', |
| 1067 |
function ( $type, array $arg, User $hookUser, &$result ) use ( $user ){ |
| 1068 |
$this->assertTrue( $user->equals( $hookUser ) ); |
| 1069 |
$this->assertSame( 999, $type ); |
| 1070 |
$this->assertSame( 'ARGUMENT', $arg[0] ); |
| 1071 |
$result = true; |
| 1072 |
} |
| 1073 |
); |
| 1074 |
$manager = $this->getManager( [ |
| 1075 |
MainConfigNames::Autopromote => [ 'test_autoconfirmed' => [ 999, 'ARGUMENT' ] ] |
| 1076 |
] ); |
| 1077 |
$this->assertArrayEquals( [ 'test_autoconfirmed' ], $manager->getUserAutopromoteGroups( $user ) ); |
| 1078 |
} |
| 1079 |
|
| 1080 |
public static function provideGetUserAutopromoteOnce() { |
| 1081 |
yield 'Events are not matching' => [ |
| 1082 |
[ 'NOT_EVENT' => [ 'autopromoteonce' => [ APCOND_EDITCOUNT, 0 ] ] ], [], [], [] |
| 1083 |
]; |
| 1084 |
yield 'Empty config' => [ |
| 1085 |
[ 'EVENT' => [] ], [], [], [] |
| 1086 |
]; |
| 1087 |
yield 'Simple case, not user groups, not former groups' => [ |
| 1088 |
[ 'EVENT' => [ 'autopromoteonce' => [ APCOND_EDITCOUNT, 0 ] ] ], [], [], [ 'autopromoteonce' ] |
| 1089 |
]; |
| 1090 |
yield 'User already in the group' => [ |
| 1091 |
[ 'EVENT' => [ 'autopromoteonce' => [ APCOND_EDITCOUNT, 0 ] ] ], [], [ 'autopromoteonce' ], [] |
| 1092 |
]; |
| 1093 |
yield 'User used to be in the group' => [ |
| 1094 |
[ 'EVENT' => [ 'autopromoteonce' => [ APCOND_EDITCOUNT, 0 ] ] ], [ 'autopromoteonce' ], [], [] |
| 1095 |
]; |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
public function testGetUserAutopromoteOnce( |
| 1107 |
array $config, |
| 1108 |
array $formerGroups, |
| 1109 |
array $userGroups, |
| 1110 |
array $expected |
| 1111 |
) { |
| 1112 |
$manager = $this->getManager( [ MainConfigNames::AutopromoteOnce => $config ] ); |
| 1113 |
$user = $this->getTestUser()->getUser(); |
| 1114 |
$manager->addUserToMultipleGroups( $user, $userGroups ); |
| 1115 |
foreach ( $formerGroups as $formerGroup ) { |
| 1116 |
$manager->addUserToGroup( $user, $formerGroup ); |
| 1117 |
$manager->removeUserFromGroup( $user, $formerGroup ); |
| 1118 |
} |
| 1119 |
$this->assertArrayEquals( $userGroups, $manager->getUserGroups( $user ), |
| 1120 |
false, 'user groups are correct ' ); |
| 1121 |
$this->assertArrayEquals( $formerGroups, $manager->getUserFormerGroups( $user ), |
| 1122 |
false, 'user former groups are correct ' ); |
| 1123 |
$this->assertArrayEquals( |
| 1124 |
$expected, |
| 1125 |
$manager->getUserAutopromoteOnceGroups( $user, 'EVENT' ) |
| 1126 |
); |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
public function testAddUserToAutopromoteOnceGroupsForeignDomain() { |
| 1133 |
$siteConfig = new SiteConfiguration(); |
| 1134 |
$siteConfig->wikis = [ 'TEST_DOMAIN' ]; |
| 1135 |
$this->setMwGlobals( 'wgConf', $siteConfig ); |
| 1136 |
|
| 1137 |
$this->overrideConfigValue( MainConfigNames::LocalDatabases, [ 'TEST_DOMAIN' ] ); |
| 1138 |
|
| 1139 |
$manager = $this->getServiceContainer() |
| 1140 |
->getUserGroupManagerFactory() |
| 1141 |
->getUserGroupManager( 'TEST_DOMAIN' ); |
| 1142 |
$user = $this->getTestUser()->getUser(); |
| 1143 |
$this->expectException( PreconditionException::class ); |
| 1144 |
$this->assertSame( [], $manager->addUserToAutopromoteOnceGroups( $user, 'TEST' ) ); |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
public function testAddUserToAutopromoteOnceGroupsAnon() { |
| 1151 |
$manager = $this->getManager(); |
| 1152 |
$anon = new UserIdentityValue( 0, 'TEST' ); |
| 1153 |
$this->assertSame( [], $manager->addUserToAutopromoteOnceGroups( $anon, 'TEST' ) ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
public function testAddUserToAutopromoteOnceGroupsReadOnly() { |
| 1160 |
$manager = $this->getManager(); |
| 1161 |
$user = $this->getTestUser()->getUser(); |
| 1162 |
$this->getServiceContainer()->getReadOnlyMode()->setReason( 'TEST' ); |
| 1163 |
$this->assertSame( [], $manager->addUserToAutopromoteOnceGroups( $user, 'TEST' ) ); |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
public function testAddUserToAutopromoteOnceGroupsNoGroups() { |
| 1170 |
$manager = $this->getManager(); |
| 1171 |
$user = $this->getTestUser()->getUser(); |
| 1172 |
$this->assertSame( [], $manager->addUserToAutopromoteOnceGroups( $user, 'TEST' ) ); |
| 1173 |
} |
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
public function testAddUserToAutopromoteOnceGroupsSuccess() { |
| 1179 |
$user = $this->getTestUser()->getUser(); |
| 1180 |
$manager = $this->getManager( [ |
| 1181 |
MainConfigNames::AutopromoteOnce => [ 'EVENT' => [ 'autopromoteonce' => [ APCOND_EDITCOUNT, 0 ] ] ] |
| 1182 |
] ); |
| 1183 |
$this->assertNotContains( 'autopromoteonce', $manager->getUserGroups( $user ) ); |
| 1184 |
$hookCalled = false; |
| 1185 |
$this->setTemporaryHook( |
| 1186 |
'UserGroupsChanged', |
| 1187 |
function ( User $hookUser, array $added, array $removed ) use ( $user, &$hookCalled ) { |
| 1188 |
$this->assertTrue( $user->equals( $hookUser ) ); |
| 1189 |
$this->assertArrayEquals( [ 'autopromoteonce' ], $added ); |
| 1190 |
$this->assertSame( [], $removed ); |
| 1191 |
$hookCalled = true; |
| 1192 |
} |
| 1193 |
); |
| 1194 |
$manager->addUserToAutopromoteOnceGroups( $user, 'EVENT' ); |
| 1195 |
$this->assertContains( 'autopromoteonce', $manager->getUserGroups( $user ) ); |
| 1196 |
$this->assertTrue( $hookCalled ); |
| 1197 |
$this->newSelectQueryBuilder() |
| 1198 |
->select( [ 'log_type', 'log_action', 'log_params' ] ) |
| 1199 |
->from( 'logging' ) |
| 1200 |
->where( [ 'log_type' => 'rights' ] ) |
| 1201 |
->assertResultSet( [ [ 'rights', |
| 1202 |
'autopromote', |
| 1203 |
LogEntryBase::makeParamBlob( [ |
| 1204 |
'4::oldgroups' => [], |
| 1205 |
'5::newgroups' => [ 'autopromoteonce' ], |
| 1206 |
] ) |
| 1207 |
] ] ); |
| 1208 |
} |
| 1209 |
|
| 1210 |
private const CHANGEABLE_GROUPS_TEST_CONFIG = [ |
| 1211 |
MainConfigNames::GroupPermissions => [], |
| 1212 |
MainConfigNames::AddGroups => [ |
| 1213 |
'sysop' => [ 'rollback' ], |
| 1214 |
'bureaucrat' => [ 'sysop', 'bureaucrat' ], |
| 1215 |
], |
| 1216 |
MainConfigNames::RemoveGroups => [ |
| 1217 |
'sysop' => [ 'rollback' ], |
| 1218 |
'bureaucrat' => [ 'sysop' ], |
| 1219 |
], |
| 1220 |
MainConfigNames::GroupsAddToSelf => [ |
| 1221 |
'sysop' => [ 'flood' ], |
| 1222 |
], |
| 1223 |
MainConfigNames::GroupsRemoveFromSelf => [ |
| 1224 |
'flood' => [ 'flood' ], |
| 1225 |
], |
| 1226 |
]; |
| 1227 |
|
| 1228 |
private function assertGroupsEquals( array $expected, array $actual ) { |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
$this->assertArrayEquals( $expected['add'], $actual['add'], 'Add must match' ); |
| 1233 |
$this->assertArrayEquals( $expected['remove'], $actual['remove'], 'Remove must match' ); |
| 1234 |
$this->assertArrayEquals( $expected['add-self'], $actual['add-self'], 'Add-self must match' ); |
| 1235 |
$this->assertArrayEquals( $expected['remove-self'], $actual['remove-self'], 'Remove-self must match' ); |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
public function testChangeableGroups() { |
| 1242 |
$manager = $this->getManager( self::CHANGEABLE_GROUPS_TEST_CONFIG ); |
| 1243 |
$allGroups = $manager->listAllGroups(); |
| 1244 |
|
| 1245 |
$user = $this->getTestUser()->getUser(); |
| 1246 |
$changeableGroups = $manager->getGroupsChangeableBy( new SimpleAuthority( $user, [ 'userrights' ] ) ); |
| 1247 |
$this->assertGroupsEquals( |
| 1248 |
[ |
| 1249 |
'add' => $allGroups, |
| 1250 |
'remove' => $allGroups, |
| 1251 |
'add-self' => [], |
| 1252 |
'remove-self' => [], |
| 1253 |
], |
| 1254 |
$changeableGroups |
| 1255 |
); |
| 1256 |
|
| 1257 |
$user = $this->getTestUser( [ 'bureaucrat', 'sysop' ] )->getUser(); |
| 1258 |
$changeableGroups = $manager->getGroupsChangeableBy( new SimpleAuthority( $user, [] ) ); |
| 1259 |
$this->assertGroupsEquals( |
| 1260 |
[ |
| 1261 |
'add' => [ 'sysop', 'bureaucrat', 'rollback' ], |
| 1262 |
'remove' => [ 'sysop', 'rollback' ], |
| 1263 |
'add-self' => [ 'flood' ], |
| 1264 |
'remove-self' => [], |
| 1265 |
], |
| 1266 |
$changeableGroups |
| 1267 |
); |
| 1268 |
|
| 1269 |
$user = $this->getTestUser( [ 'flood' ] )->getUser(); |
| 1270 |
$changeableGroups = $manager->getGroupsChangeableBy( new SimpleAuthority( $user, [] ) ); |
| 1271 |
$this->assertGroupsEquals( |
| 1272 |
[ |
| 1273 |
'add' => [], |
| 1274 |
'remove' => [], |
| 1275 |
'add-self' => [], |
| 1276 |
'remove-self' => [ 'flood' ], |
| 1277 |
], |
| 1278 |
$changeableGroups |
| 1279 |
); |
| 1280 |
} |
| 1281 |
|
| 1282 |
public static function provideChangeableByGroup() { |
| 1283 |
yield 'sysop' => [ 'sysop', [ |
| 1284 |
'add' => [ 'rollback' ], |
| 1285 |
'remove' => [ 'rollback' ], |
| 1286 |
'add-self' => [ 'flood' ], |
| 1287 |
'remove-self' => [], |
| 1288 |
] ]; |
| 1289 |
yield 'flood' => [ 'flood', [ |
| 1290 |
'add' => [], |
| 1291 |
'remove' => [], |
| 1292 |
'add-self' => [], |
| 1293 |
'remove-self' => [ 'flood' ], |
| 1294 |
] ]; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
public function testChangeableByGroup( string $group, array $expected ) { |
| 1304 |
$manager = $this->getManager( self::CHANGEABLE_GROUPS_TEST_CONFIG ); |
| 1305 |
$this->assertGroupsEquals( $expected, $manager->getGroupsChangeableByGroup( $group ) ); |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
public function testGetUserPrivilegedGroups() { |
| 1312 |
$this->overrideConfigValue( MainConfigNames::PrivilegedGroups, [ 'sysop', 'interface-admin', 'bar', 'baz' ] ); |
| 1313 |
$makeHook = function ( $invocationCount, User $userToMatch, array $groupsToAdd ) { |
| 1314 |
return function ( $u, &$groups ) use ( $userToMatch, $invocationCount, $groupsToAdd ) { |
| 1315 |
$invocationCount(); |
| 1316 |
$this->assertTrue( $userToMatch->equals( $u ) ); |
| 1317 |
$groups = array_merge( $groups, $groupsToAdd ); |
| 1318 |
}; |
| 1319 |
}; |
| 1320 |
|
| 1321 |
$manager = $this->getManager(); |
| 1322 |
|
| 1323 |
$user = new User; |
| 1324 |
$user->setName( '*Unregistered 1234' ); |
| 1325 |
|
| 1326 |
$this->assertArrayEquals( |
| 1327 |
[], |
| 1328 |
$manager->getUserPrivilegedGroups( $user ) |
| 1329 |
); |
| 1330 |
|
| 1331 |
$user = $this->getTestUser( [ 'sysop', 'bot', 'interface-admin' ] )->getUser(); |
| 1332 |
|
| 1333 |
$this->setTemporaryHook( 'UserPrivilegedGroups', |
| 1334 |
$makeHook( $this->countPromise( $this->once() ), $user, [ 'foo' ] ) ); |
| 1335 |
$this->setTemporaryHook( 'UserEffectiveGroups', |
| 1336 |
$makeHook( $this->countPromise( $this->once() ), $user, [ 'bar', 'boom' ] ) ); |
| 1337 |
$this->assertArrayEquals( |
| 1338 |
[ 'sysop', 'interface-admin', 'foo', 'bar' ], |
| 1339 |
$manager->getUserPrivilegedGroups( $user ) |
| 1340 |
); |
| 1341 |
$this->assertArrayEquals( |
| 1342 |
[ 'sysop', 'interface-admin', 'foo', 'bar' ], |
| 1343 |
$manager->getUserPrivilegedGroups( $user ) |
| 1344 |
); |
| 1345 |
|
| 1346 |
$this->setTemporaryHook( 'UserPrivilegedGroups', |
| 1347 |
$makeHook( $this->countPromise( $this->once() ), $user, [ 'baz' ] ) ); |
| 1348 |
$this->setTemporaryHook( 'UserEffectiveGroups', |
| 1349 |
$makeHook( $this->countPromise( $this->once() ), $user, [ 'baz' ] ) ); |
| 1350 |
$this->assertArrayEquals( |
| 1351 |
[ 'sysop', 'interface-admin', 'foo', 'bar' ], |
| 1352 |
$manager->getUserPrivilegedGroups( $user ) |
| 1353 |
); |
| 1354 |
$this->assertArrayEquals( |
| 1355 |
[ 'sysop', 'interface-admin', 'baz' ], |
| 1356 |
$manager->getUserPrivilegedGroups( $user, IDBAccessObject::READ_NORMAL, true ) |
| 1357 |
); |
| 1358 |
$this->assertArrayEquals( |
| 1359 |
[ 'sysop', 'interface-admin', 'baz' ], |
| 1360 |
$manager->getUserPrivilegedGroups( $user ) |
| 1361 |
); |
| 1362 |
|
| 1363 |
$this->setTemporaryHook( 'UserPrivilegedGroups', static function () { |
| 1364 |
} ); |
| 1365 |
$this->setTemporaryHook( 'UserEffectiveGroups', static function () { |
| 1366 |
} ); |
| 1367 |
$user = $this->getTestUser( [] )->getUser(); |
| 1368 |
$this->assertArrayEquals( |
| 1369 |
[], |
| 1370 |
$manager->getUserPrivilegedGroups( $user, IDBAccessObject::READ_NORMAL, true ) |
| 1371 |
); |
| 1372 |
} |
| 1373 |
} |