2020-10-25 15:30:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Extension\AbuseFilter\FilterUser;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group Test
|
|
|
|
* @group AbuseFilter
|
2023-07-31 01:09:16 +00:00
|
|
|
* @group Database
|
2024-01-31 17:50:53 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\FilterUser
|
2020-10-25 15:30:43 +00:00
|
|
|
* @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 ) );
|
2024-01-31 17:50:53 +00:00
|
|
|
$ugm = $this->getServiceContainer()->getUserGroupManager();
|
|
|
|
$filterUser = new FilterUser(
|
|
|
|
$ml,
|
|
|
|
$ugm,
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
new NullLogger()
|
|
|
|
);
|
2020-10-25 15:30:43 +00:00
|
|
|
|
2022-03-04 18:04:08 +00:00
|
|
|
$actual = $filterUser->getUserIdentity();
|
2020-10-25 15:30:43 +00:00
|
|
|
$this->assertSame( $name, $actual->getName(), 'name' );
|
|
|
|
$this->assertContains( 'sysop', $ugm->getUserGroups( $actual ), 'sysop' );
|
2022-07-13 17:27:19 +00:00
|
|
|
$this->assertTrue( $filterUser->getAuthority()->isAllowed( 'block' ) );
|
2020-10-25 15:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
2024-01-31 17:50:53 +00:00
|
|
|
$ugm = $this->getServiceContainer()->getUserGroupManager();
|
2020-10-25 15:30:43 +00:00
|
|
|
$logger = new TestLogger();
|
|
|
|
$logger->setCollect( true );
|
2024-01-31 17:50:53 +00:00
|
|
|
$filterUser = new FilterUser(
|
|
|
|
$ml,
|
|
|
|
$ugm,
|
|
|
|
$this->getServiceContainer()->getUserNameUtils(),
|
|
|
|
$logger
|
|
|
|
);
|
2020-10-25 15:30:43 +00:00
|
|
|
|
2022-03-04 18:04:08 +00:00
|
|
|
$actual = $filterUser->getUserIdentity();
|
2020-10-25 15:30:43 +00:00
|
|
|
$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' );
|
|
|
|
}
|
2024-01-31 17:50:53 +00:00
|
|
|
|
|
|
|
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() ) );
|
|
|
|
}
|
2020-10-25 15:30:43 +00:00
|
|
|
}
|