2020-12-09 15:51:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Degroup;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterUser;
|
2021-01-02 14:01:00 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Variables\VariableHolder;
|
2020-12-09 15:51:59 +00:00
|
|
|
use MediaWiki\User\UserGroupManager;
|
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Degroup
|
|
|
|
* @covers ::__construct
|
2021-01-09 23:26:29 +00:00
|
|
|
* @todo Make this a unit test once ManualLogEntry is servicified (T253717)
|
2020-12-09 15:51:59 +00:00
|
|
|
*/
|
|
|
|
class DegroupTest extends MediaWikiIntegrationTestCase {
|
2021-01-06 13:07:02 +00:00
|
|
|
use ConsequenceGetMessageTestTrait;
|
2020-12-09 15:51:59 +00:00
|
|
|
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getMsgLocalizer(): MessageLocalizer {
|
2021-01-09 23:26:29 +00:00
|
|
|
$ml = $this->createMock( MessageLocalizer::class );
|
|
|
|
$ml->method( 'msg' )->willReturnCallback( function ( $k, $p ) {
|
|
|
|
return $this->getMockMessage( $k, $p );
|
|
|
|
} );
|
|
|
|
return $ml;
|
|
|
|
}
|
|
|
|
|
2021-07-21 18:51:12 +00:00
|
|
|
private function getFilterUser(): FilterUser {
|
2021-01-09 23:26:29 +00:00
|
|
|
// TODO: Can't use mocks until ManualLogEntry is servicified (T253717)
|
|
|
|
return AbuseFilterServices::getFilterUser();
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:51:59 +00:00
|
|
|
/**
|
|
|
|
* @covers ::execute
|
|
|
|
*/
|
|
|
|
public function testExecute() {
|
2021-03-26 10:00:56 +00:00
|
|
|
$user = new UserIdentityValue( 1, 'Degrouped user' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$params = $this->provideGetMessageParameters( $user )->current()[0];
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
$userGroupManager->method( 'listAllImplicitGroups' )
|
|
|
|
->willReturn( [ '*', 'user' ] );
|
|
|
|
$userGroupManager->expects( $this->once() )
|
|
|
|
->method( 'removeUserFromGroup' )
|
|
|
|
->with( $user, 'sysop' );
|
2021-01-09 23:26:29 +00:00
|
|
|
$filterUser = $this->getFilterUser();
|
2020-12-09 15:51:59 +00:00
|
|
|
|
|
|
|
$degroup = new Degroup(
|
|
|
|
$params,
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder::newFromArray( [ 'user_groups' => [ '*', 'user', 'sysop' ] ] ),
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager,
|
2021-01-09 23:26:29 +00:00
|
|
|
$filterUser,
|
|
|
|
$this->getMsgLocalizer()
|
2020-12-09 15:51:59 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue( $degroup->execute() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::execute
|
|
|
|
*/
|
|
|
|
public function testExecute_noGroups() {
|
2021-01-06 13:07:02 +00:00
|
|
|
$params = $this->provideGetMessageParameters()->current()[0];
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
$userGroupManager->method( 'listAllImplicitGroups' )
|
|
|
|
->willReturn( [ '*', 'user' ] );
|
|
|
|
$userGroupManager->expects( $this->never() )
|
|
|
|
->method( 'removeUserFromGroup' );
|
|
|
|
|
|
|
|
$degroup = new Degroup(
|
|
|
|
$params,
|
2021-01-02 14:01:00 +00:00
|
|
|
VariableHolder::newFromArray( [ 'user_groups' => [ '*', 'user' ] ] ),
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager,
|
2021-01-09 23:26:29 +00:00
|
|
|
$this->createMock( FilterUser::class ),
|
|
|
|
$this->getMsgLocalizer()
|
2020-12-09 15:51:59 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $degroup->execute() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::execute
|
|
|
|
*/
|
|
|
|
public function testExecute_variableNotSet() {
|
2021-03-26 10:00:56 +00:00
|
|
|
$user = new UserIdentityValue( 1, 'Degrouped user' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$params = $this->provideGetMessageParameters( $user )->current()[0];
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
$userGroupManager->method( 'listAllImplicitGroups' )
|
|
|
|
->willReturn( [ '*', 'user' ] );
|
|
|
|
$userGroupManager->method( 'getUserEffectiveGroups' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( [ '*', 'user', 'sysop' ] );
|
|
|
|
$userGroupManager->expects( $this->once() )
|
|
|
|
->method( 'removeUserFromGroup' )
|
|
|
|
->with( $user, 'sysop' );
|
2021-01-09 23:26:29 +00:00
|
|
|
$filterUser = $this->getFilterUser();
|
2020-12-09 15:51:59 +00:00
|
|
|
|
|
|
|
$degroup = new Degroup(
|
|
|
|
$params,
|
2021-01-02 14:01:00 +00:00
|
|
|
new VariableHolder(),
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager,
|
2021-01-09 23:26:29 +00:00
|
|
|
$filterUser,
|
|
|
|
$this->getMsgLocalizer()
|
2020-12-09 15:51:59 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue( $degroup->execute() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::execute
|
|
|
|
*/
|
|
|
|
public function testExecute_anonymous() {
|
2021-03-26 10:00:56 +00:00
|
|
|
$user = new UserIdentityValue( 0, 'Anonymous user' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$params = $this->provideGetMessageParameters( $user )->current()[0];
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
$userGroupManager->expects( $this->never() )->method( $this->anything() );
|
|
|
|
$filterUser = $this->createMock( FilterUser::class );
|
|
|
|
$filterUser->expects( $this->never() )->method( $this->anything() );
|
|
|
|
|
|
|
|
$degroup = new Degroup(
|
|
|
|
$params,
|
2021-01-02 14:01:00 +00:00
|
|
|
$this->createMock( VariableHolder::class ),
|
2020-12-09 15:51:59 +00:00
|
|
|
$userGroupManager,
|
2021-01-09 23:26:29 +00:00
|
|
|
$filterUser,
|
|
|
|
$this->getMsgLocalizer()
|
2020-12-09 15:51:59 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $degroup->execute() );
|
|
|
|
}
|
|
|
|
|
2021-07-21 18:51:12 +00:00
|
|
|
public function provideRevert(): array {
|
2020-11-29 15:18:32 +00:00
|
|
|
return [
|
|
|
|
[ true, [ '*', 'user', 'sysop' ] ],
|
|
|
|
[ true, [ '*', 'user', 'canceled', 'sysop' ] ],
|
|
|
|
[ false, [ '*', 'user', 'sysop' ], [ 'sysop' ] ],
|
|
|
|
[ false, [ '*', 'user', 'canceled' ] ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::revert
|
|
|
|
* @dataProvider provideRevert
|
|
|
|
*/
|
|
|
|
public function testRevert( bool $success, array $hadGroups, array $hasGroups = [] ) {
|
2021-03-26 10:00:56 +00:00
|
|
|
$user = new UserIdentityValue( 1, 'Degrouped user' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$params = $this->provideGetMessageParameters( $user )->current()[0];
|
2020-11-29 15:18:32 +00:00
|
|
|
$userGroupManager = $this->createMock( UserGroupManager::class );
|
|
|
|
$userGroupManager->method( 'listAllImplicitGroups' )
|
|
|
|
->willReturn( [ '*', 'user' ] );
|
|
|
|
$userGroupManager->method( 'getUserGroups' )
|
|
|
|
->with( $user )
|
|
|
|
->willReturn( $hasGroups );
|
|
|
|
$userGroupManager->method( 'addUserToGroup' )
|
2021-04-30 18:54:17 +00:00
|
|
|
->willReturnCallback( static function ( $_, $group ) use ( $hasGroups ) {
|
2020-11-29 15:18:32 +00:00
|
|
|
return $group === 'sysop';
|
|
|
|
} );
|
|
|
|
$degroup = new Degroup(
|
|
|
|
$params,
|
2022-06-08 09:59:18 +00:00
|
|
|
VariableHolder::newFromArray( [ 'user_groups' => $hadGroups ] ),
|
2020-11-29 15:18:32 +00:00
|
|
|
$userGroupManager,
|
2021-01-09 23:26:29 +00:00
|
|
|
$this->createMock( FilterUser::class ),
|
|
|
|
$this->getMsgLocalizer()
|
2020-11-29 15:18:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$performer = $this->getTestUser()->getUser();
|
|
|
|
$this->assertSame(
|
|
|
|
$success,
|
2022-06-08 09:59:18 +00:00
|
|
|
$degroup->revert( $performer, 'reason' )
|
2020-11-29 15:18:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-06 13:07:02 +00:00
|
|
|
/**
|
|
|
|
* @covers ::getMessage
|
|
|
|
* @dataProvider provideGetMessageParameters
|
|
|
|
*/
|
|
|
|
public function testGetMessage( Parameters $params ) {
|
|
|
|
$rangeBlock = new Degroup(
|
|
|
|
$params,
|
|
|
|
new VariableHolder(),
|
|
|
|
$this->createMock( UserGroupManager::class ),
|
2021-01-09 23:26:29 +00:00
|
|
|
$this->createMock( FilterUser::class ),
|
|
|
|
$this->getMsgLocalizer()
|
2021-01-06 13:07:02 +00:00
|
|
|
);
|
|
|
|
$this->doTestGetMessage( $rangeBlock, $params, 'abusefilter-degrouped' );
|
|
|
|
}
|
2020-12-09 15:51:59 +00:00
|
|
|
}
|