mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 06:03:49 +00:00
62376f437e
Bug: T275482 Change-Id: Ie9fd52005ca1eb264dc791a2f87d7308a9e8810e
146 lines
4.7 KiB
PHP
146 lines
4.7 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Block\BlockUser;
|
|
use MediaWiki\Block\BlockUserFactory;
|
|
use MediaWiki\Block\DatabaseBlock;
|
|
use MediaWiki\Block\DatabaseBlockStore;
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Block;
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters;
|
|
use MediaWiki\Extension\AbuseFilter\FilterUser;
|
|
use MediaWiki\User\UserIdentity;
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
/**
|
|
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\Block
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Consequences\Consequence\BlockingConsequence
|
|
* @todo Make this a unit test once T266409 is resolved
|
|
*/
|
|
class AbuseFilterBlockTest extends MediaWikiIntegrationTestCase {
|
|
use ConsequenceGetMessageTestTrait;
|
|
|
|
private function getMsgLocalizer() : MessageLocalizer {
|
|
$ml = $this->createMock( MessageLocalizer::class );
|
|
$ml->method( 'msg' )->willReturnCallback( function ( $k, $p ) {
|
|
return $this->getMockMessage( $k, $p );
|
|
} );
|
|
return $ml;
|
|
}
|
|
|
|
private function getFilterUser() : FilterUser {
|
|
$filterUser = $this->createMock( FilterUser::class );
|
|
$filterUser->method( 'getUser' )
|
|
->willReturn( new UserIdentityValue( 2, 'FilterUser', 3 ) );
|
|
return $filterUser;
|
|
}
|
|
|
|
public function provideExecute() : iterable {
|
|
foreach ( [ true, false ] as $result ) {
|
|
$resStr = wfBoolToStr( $result );
|
|
yield "IPv4, $resStr" => [ new UserIdentityValue( 0, '1.2.3.4', 42 ), $result ];
|
|
yield "IPv6, $resStr" => [
|
|
// random IP from https://en.wikipedia.org/w/index.php?title=IPv6&oldid=989727833
|
|
new UserIdentityValue( 0, '2001:0db8:0000:0000:0000:ff00:0042:8329', 42 ),
|
|
$result
|
|
];
|
|
yield "Registered, $resStr" => [ new UserIdentityValue( 3, 'Some random user', 42 ), $result ];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideExecute
|
|
* @covers ::__construct
|
|
* @covers ::execute
|
|
*/
|
|
public function testExecute( UserIdentity $target, bool $result ) {
|
|
$expiry = '1 day';
|
|
$params = $this->provideGetMessageParameters( $target )->current()[0];
|
|
$blockUser = $this->createMock( BlockUser::class );
|
|
$blockUser->expects( $this->once() )
|
|
->method( 'placeBlockUnsafe' )
|
|
->willReturn( $result ? Status::newGood() : Status::newFatal( 'error' ) );
|
|
$blockUserFactory = $this->createMock( BlockUserFactory::class );
|
|
$blockUserFactory->expects( $this->once() )
|
|
->method( 'newBlockUser' )
|
|
->with(
|
|
$target->getName(),
|
|
$this->anything(),
|
|
$expiry,
|
|
$this->anything(),
|
|
$this->anything()
|
|
)
|
|
->willReturn( $blockUser );
|
|
|
|
$block = new Block(
|
|
$params,
|
|
$expiry,
|
|
$preventsTalkEdit = true,
|
|
$blockUserFactory,
|
|
$this->createMock( DatabaseBlockStore::class ),
|
|
function () {
|
|
return null;
|
|
},
|
|
$this->getFilterUser(),
|
|
$this->getMsgLocalizer()
|
|
);
|
|
$this->assertSame( $result, $block->execute() );
|
|
}
|
|
|
|
/**
|
|
* @covers ::getMessage
|
|
* @dataProvider provideGetMessageParameters
|
|
*/
|
|
public function testGetMessage( Parameters $params ) {
|
|
$block = new Block(
|
|
$params,
|
|
'0',
|
|
false,
|
|
$this->createMock( BlockUserFactory::class ),
|
|
$this->createMock( DatabaseBlockStore::class ),
|
|
function () {
|
|
return null;
|
|
},
|
|
$this->createMock( FilterUser::class ),
|
|
$this->getMsgLocalizer()
|
|
);
|
|
$this->doTestGetMessage( $block, $params, 'abusefilter-blocked-display' );
|
|
}
|
|
|
|
public function provideRevert() {
|
|
yield 'no block to revert' => [ null, null, false ];
|
|
|
|
$randomUser = new UserIdentityValue( 1234, 'Some other user', 3456 );
|
|
yield 'not blocked by AF user' => [ new DatabaseBlock( [ 'by' => $randomUser->getId() ] ), null, false ];
|
|
|
|
$blockByFilter = new DatabaseBlock( [ 'by' => $this->getFilterUser()->getUser()->getId() ] );
|
|
$failBlockStore = $this->createMock( DatabaseBlockStore::class );
|
|
$failBlockStore->expects( $this->once() )->method( 'deleteBlock' )->willReturn( false );
|
|
yield 'cannot delete block' => [ $blockByFilter, $failBlockStore, false ];
|
|
|
|
$succeedBlockStore = $this->createMock( DatabaseBlockStore::class );
|
|
$succeedBlockStore->expects( $this->once() )->method( 'deleteBlock' )->willReturn( true );
|
|
yield 'succeed' => [ $blockByFilter, $succeedBlockStore, true ];
|
|
}
|
|
|
|
/**
|
|
* @covers ::revert
|
|
* @dataProvider provideRevert
|
|
*/
|
|
public function testRevert( ?DatabaseBlock $block, ?DatabaseBlockStore $blockStore, bool $expected ) {
|
|
$params = $this->createMock( Parameters::class );
|
|
$params->method( 'getUser' )->willReturn( new UserIdentityValue( 1, 'Foobar', 2 ) );
|
|
$block = new Block(
|
|
$params,
|
|
'0',
|
|
false,
|
|
$this->createMock( BlockUserFactory::class ),
|
|
$blockStore ?? $this->createMock( DatabaseBlockStore::class ),
|
|
function () use ( $block ) {
|
|
return $block;
|
|
},
|
|
$this->getFilterUser(),
|
|
$this->getMsgLocalizer()
|
|
);
|
|
$this->assertSame( $expected, $block->revert( [], $this->createMock( UserIdentity::class ), '' ) );
|
|
}
|
|
}
|