2021-01-09 13:40:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\Handlers\SchemaChangesHandler;
|
2024-05-04 08:14:01 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2021-01-09 13:40:10 +00:00
|
|
|
use MediaWiki\User\UserGroupManager;
|
|
|
|
|
|
|
|
/**
|
2023-07-31 01:09:16 +00:00
|
|
|
* @group Database
|
2024-04-17 00:44:40 +00:00
|
|
|
* @covers \MediaWiki\Extension\AbuseFilter\Hooks\Handlers\SchemaChangesHandler
|
2021-01-09 13:40:10 +00:00
|
|
|
* @todo Make this a unit test once User::newSystemUser is moved to a service
|
|
|
|
*/
|
|
|
|
class SchemaChangesHandlerTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
public function testConstruct() {
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
SchemaChangesHandler::class,
|
|
|
|
new SchemaChangesHandler(
|
|
|
|
$this->createMock( MessageLocalizer::class ),
|
2024-05-04 08:14:01 +00:00
|
|
|
$this->createMock( UserGroupManager::class ),
|
|
|
|
$this->createMock( UserFactory::class )
|
2021-01-09 13:40:10 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-15 14:08:15 +00:00
|
|
|
public function testCreateAbuseFilterUser_invalidUserName() {
|
2021-01-09 13:40:10 +00:00
|
|
|
$noRowUpdater = $this->createMock( DatabaseUpdater::class );
|
|
|
|
$noRowUpdater->method( 'updateRowExists' )->willReturn( false );
|
|
|
|
$invalidML = $this->createMock( MessageLocalizer::class );
|
|
|
|
$invalidML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( '' ) );
|
2024-05-04 08:14:01 +00:00
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )->willReturn( null );
|
|
|
|
$handler = new SchemaChangesHandler(
|
|
|
|
$invalidML,
|
|
|
|
$this->createNoOpMock( UserGroupManager::class ),
|
|
|
|
$userFactory
|
|
|
|
);
|
2023-06-15 14:08:15 +00:00
|
|
|
$this->assertFalse( $handler->createAbuseFilterUser( $noRowUpdater ) );
|
|
|
|
}
|
2021-01-09 13:40:10 +00:00
|
|
|
|
2023-06-15 14:08:15 +00:00
|
|
|
public function testCreateAbuseFilterUser_alreadyCreated() {
|
2021-01-09 13:40:10 +00:00
|
|
|
$rowExistsUpdater = $this->createMock( DatabaseUpdater::class );
|
|
|
|
$rowExistsUpdater->method( 'updateRowExists' )->willReturn( true );
|
|
|
|
$validML = $this->createMock( MessageLocalizer::class );
|
|
|
|
$validML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( 'Foo' ) );
|
2024-05-04 08:14:01 +00:00
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )->willReturn( $this->createMock( User::class ) );
|
|
|
|
$handler = new SchemaChangesHandler(
|
|
|
|
$validML,
|
|
|
|
$this->createNoOpMock( UserGroupManager::class ),
|
|
|
|
$userFactory
|
|
|
|
);
|
2023-06-15 14:08:15 +00:00
|
|
|
$this->assertFalse( $handler->createAbuseFilterUser( $rowExistsUpdater ) );
|
2021-01-09 13:40:10 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:08:15 +00:00
|
|
|
public function testCreateAbuseFilterUser_success() {
|
|
|
|
$noRowUpdater = $this->createMock( DatabaseUpdater::class );
|
|
|
|
$noRowUpdater->method( 'updateRowExists' )->willReturn( false );
|
|
|
|
$validML = $this->createMock( MessageLocalizer::class );
|
|
|
|
$validML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( 'Foo' ) );
|
|
|
|
$ugm = $this->createMock( UserGroupManager::class );
|
|
|
|
$ugm->expects( $this->once() )->method( 'addUserToGroup' );
|
2024-05-04 08:14:01 +00:00
|
|
|
$userFactory = $this->createMock( UserFactory::class );
|
|
|
|
$userFactory->method( 'newFromName' )->willReturn( $this->createMock( User::class ) );
|
|
|
|
$okHandler = new SchemaChangesHandler( $validML, $ugm, $userFactory );
|
2023-06-15 14:08:15 +00:00
|
|
|
$this->assertTrue( $okHandler->createAbuseFilterUser( $noRowUpdater ) );
|
2021-01-09 13:40:10 +00:00
|
|
|
}
|
2023-06-15 14:08:15 +00:00
|
|
|
|
2021-01-09 13:40:10 +00:00
|
|
|
}
|