mediawiki-extensions-AbuseF.../tests/phpunit/AbuseFilterFilterUserTest.php
Matěj Suchánek 702d77e3ce Create real integration test for variables
For fixing bugs like T65632, T105325, or T264104, we will need
to update code in more than one place at once. To prevent
regressions, create an integration test which tests the whole
pipeline, from the request submission to variable evaluation.
Edits are simulated using action=edit API call because the hook
AbuseFilter uses is run from EditPage.

To increase confidence in test coverage, remove some annotations
from AbuseFilterConsequencesTest or make them less greedy.
Ideally, it would only test consequences.

This patch includes refactoring of AbuseFilterCreateAccountTestTrait
which now only inserts the user into the database if it really
should be created.
It also restores test coverage of some other classes.

Change-Id: I661f4e0e2bcac4770e499708fca4e4e153f31fed
2022-11-26 18:51:38 +01:00

61 lines
2 KiB
PHP

<?php
use MediaWiki\Extension\AbuseFilter\FilterUser;
use MediaWiki\MediaWikiServices;
use Psr\Log\NullLogger;
/**
* @group Test
* @group AbuseFilter
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\FilterUser
* @covers ::__construct()
* @todo Make a unit test once DI is possible for User::newSystemUser
*/
class AbuseFilterFilterUserTest extends MediaWikiIntegrationTestCase {
/**
* @covers ::getUser
* @covers ::getUserIdentity
* @covers ::getAuthority
*/
public function testGetUser() {
$name = 'AbuseFilter blocker user';
$ml = $this->createMock( MessageLocalizer::class );
$ml->method( 'msg' )->willReturn( $this->getMockMessage( $name ) );
$ugm = MediaWikiServices::getInstance()->getUserGroupManager();
$filterUser = new FilterUser( $ml, $ugm, new NullLogger() );
$actual = $filterUser->getUserIdentity();
$this->assertSame( $name, $actual->getName(), 'name' );
$this->assertContains( 'sysop', $ugm->getUserGroups( $actual ), 'sysop' );
$this->assertTrue( $filterUser->getAuthority()->isAllowed( 'block' ) );
}
/**
* @covers ::getUser
* @covers ::getUserIdentity
*/
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 = MediaWikiServices::getInstance()->getUserGroupManager();
$logger = new TestLogger();
$logger->setCollect( true );
$filterUser = new FilterUser( $ml, $ugm, $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' );
}
}