mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-12-12 06:05:45 +00:00
a5b68cf46d
Why: * When CheckUser asks the AbuseFilter extension for modifications to rows inserted into the CheckUser tables, the AbuseFilter extension attempts to get the Filter user via User::newSystemUser * User::newSystemUser can deadlock if multiple requests to create the system user are being made at once. * The CheckUserHander does not need to create the abuse filter system and instead only needs to know if a given $user is the equal to the FilterUser. * As such the FilterUser service needs to provide a way to check if a given $user is equal without creating the FilterUser. What: * Add FilterUser::isUserSameAs which returns a boolean value indicating whether the Abuse Filter system user is the equal to a given UserIdentity in the same way that UserIdentity::equals is implemented. * Refactor ::getUser to get the username for the filter user in a separate method, so that the ::isUserSameAs method can also use this method. Name this new method ::getFilterUserName. * Add a test for the FilterUser service to ensure consistent test coverage * Convert the @covers and @coversDefaultClass annotations to be a @covers for the class. This is because PHPUnit recommends this in https://docs.phpunit.de/en/9.6/annotations.html#appendixes-annotations-covers-tables-annotations Bug: T356275 Bug: T346967 Change-Id: I8a101781bb47612deabb0f2a06a398ac13e860e6
76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterUser;
|
|
use Psr\Log\NullLogger;
|
|
|
|
/**
|
|
* @group Test
|
|
* @group AbuseFilter
|
|
* @group Database
|
|
* @covers \MediaWiki\Extension\AbuseFilter\FilterUser
|
|
* @todo Make a unit test once DI is possible for User::newSystemUser
|
|
*/
|
|
class AbuseFilterFilterUserTest extends MediaWikiIntegrationTestCase {
|
|
public function testGetUser() {
|
|
$name = 'AbuseFilter blocker user';
|
|
$ml = $this->createMock( MessageLocalizer::class );
|
|
$ml->method( 'msg' )->willReturn( $this->getMockMessage( $name ) );
|
|
$ugm = $this->getServiceContainer()->getUserGroupManager();
|
|
$filterUser = new FilterUser(
|
|
$ml,
|
|
$ugm,
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
new NullLogger()
|
|
);
|
|
|
|
$actual = $filterUser->getUserIdentity();
|
|
$this->assertSame( $name, $actual->getName(), 'name' );
|
|
$this->assertContains( 'sysop', $ugm->getUserGroups( $actual ), 'sysop' );
|
|
$this->assertTrue( $filterUser->getAuthority()->isAllowed( 'block' ) );
|
|
}
|
|
|
|
public function testGetUser_invalidName() {
|
|
$name = 'Foobar filter user';
|
|
$ml = $this->createMock( MessageLocalizer::class );
|
|
$msg = $this->createMock( Message::class );
|
|
$msg->method( 'inContentLanguage' )->willReturn( $this->getMockMessage( '' ) );
|
|
$msg->method( 'inLanguage' )->willReturn( $this->getMockMessage( $name ) );
|
|
$ml->method( 'msg' )->willReturn( $msg );
|
|
$ugm = $this->getServiceContainer()->getUserGroupManager();
|
|
$logger = new TestLogger();
|
|
$logger->setCollect( true );
|
|
$filterUser = new FilterUser(
|
|
$ml,
|
|
$ugm,
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
$logger
|
|
);
|
|
|
|
$actual = $filterUser->getUserIdentity();
|
|
$this->assertSame( $name, $actual->getName(), 'name' );
|
|
$found = false;
|
|
foreach ( $logger->getBuffer() as $msg ) {
|
|
if ( strpos( $msg[1], 'MediaWiki:abusefilter-blocker' ) !== false ) {
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
$this->assertTrue( $found, 'Invalid name not logged' );
|
|
}
|
|
|
|
public function testIsUserSameAs() {
|
|
$ml = $this->createMock( MessageLocalizer::class );
|
|
$ml->method( 'msg' )->willReturn( $this->getMockMessage( 'AbuseFilter blocker user' ) );
|
|
$ugm = $this->getServiceContainer()->getUserGroupManager();
|
|
$filterUser = new FilterUser(
|
|
$ml,
|
|
$ugm,
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
new NullLogger()
|
|
);
|
|
|
|
$this->assertTrue( $filterUser->isSameUserAs( $filterUser->getUserIdentity() ) );
|
|
$this->assertFalse( $filterUser->isSameUserAs( $this->getTestUser()->getUser() ) );
|
|
}
|
|
}
|