| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Extension\AbuseFilter\Hooks\Handlers\SchemaChangesHandler; |
| 4 |
use MediaWiki\Installer\DatabaseUpdater; |
| 5 |
use MediaWiki\User\User; |
| 6 |
use MediaWiki\User\UserFactory; |
| 7 |
use MediaWiki\User\UserGroupManager; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class SchemaChangesHandlerTest extends MediaWikiIntegrationTestCase { |
| 15 |
|
| 16 |
public function testConstruct() { |
| 17 |
$this->assertInstanceOf( |
| 18 |
SchemaChangesHandler::class, |
| 19 |
new SchemaChangesHandler( |
| 20 |
$this->createMock( MessageLocalizer::class ), |
| 21 |
$this->createMock( UserGroupManager::class ), |
| 22 |
$this->createMock( UserFactory::class ) |
| 23 |
) |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
public function testCreateAbuseFilterUser_invalidUserName() { |
| 28 |
$noRowUpdater = $this->createMock( DatabaseUpdater::class ); |
| 29 |
$noRowUpdater->method( 'updateRowExists' )->willReturn( false ); |
| 30 |
$invalidML = $this->createMock( MessageLocalizer::class ); |
| 31 |
$invalidML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( '' ) ); |
| 32 |
$userFactory = $this->createMock( UserFactory::class ); |
| 33 |
$userFactory->method( 'newFromName' )->willReturn( null ); |
| 34 |
$handler = new SchemaChangesHandler( |
| 35 |
$invalidML, |
| 36 |
$this->createNoOpMock( UserGroupManager::class ), |
| 37 |
$userFactory |
| 38 |
); |
| 39 |
$this->assertFalse( $handler->createAbuseFilterUser( $noRowUpdater ) ); |
| 40 |
} |
| 41 |
|
| 42 |
public function testCreateAbuseFilterUser_alreadyCreated() { |
| 43 |
$rowExistsUpdater = $this->createMock( DatabaseUpdater::class ); |
| 44 |
$rowExistsUpdater->method( 'updateRowExists' )->willReturn( true ); |
| 45 |
$validML = $this->createMock( MessageLocalizer::class ); |
| 46 |
$validML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( 'Foo' ) ); |
| 47 |
$userFactory = $this->createMock( UserFactory::class ); |
| 48 |
$userFactory->method( 'newFromName' )->willReturn( $this->createMock( User::class ) ); |
| 49 |
$handler = new SchemaChangesHandler( |
| 50 |
$validML, |
| 51 |
$this->createNoOpMock( UserGroupManager::class ), |
| 52 |
$userFactory |
| 53 |
); |
| 54 |
$this->assertFalse( $handler->createAbuseFilterUser( $rowExistsUpdater ) ); |
| 55 |
} |
| 56 |
|
| 57 |
public function testCreateAbuseFilterUser_success() { |
| 58 |
$noRowUpdater = $this->createMock( DatabaseUpdater::class ); |
| 59 |
$noRowUpdater->method( 'updateRowExists' )->willReturn( false ); |
| 60 |
$validML = $this->createMock( MessageLocalizer::class ); |
| 61 |
$validML->method( 'msg' )->with( 'abusefilter-blocker' )->willReturn( $this->getMockMessage( 'Foo' ) ); |
| 62 |
$ugm = $this->createMock( UserGroupManager::class ); |
| 63 |
$ugm->expects( $this->once() )->method( 'addUserToGroup' ); |
| 64 |
$userFactory = $this->createMock( UserFactory::class ); |
| 65 |
$userFactory->method( 'newFromName' )->willReturn( $this->createMock( User::class ) ); |
| 66 |
$okHandler = new SchemaChangesHandler( $validML, $ugm, $userFactory ); |
| 67 |
$this->assertTrue( $okHandler->createAbuseFilterUser( $noRowUpdater ) ); |
| 68 |
} |
| 69 |
|
| 70 |
} |