| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry; |
| 6 |
use MediaWiki\Extension\AbuseFilter\EchoNotifier; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter; |
| 8 |
use MediaWiki\Extension\AbuseFilter\FilterLookup; |
| 9 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWikiIntegrationTestCase; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class EchoNotifierTest extends MediaWikiIntegrationTestCase { |
| 18 |
|
| 19 |
private const USER_IDS = [ |
| 20 |
'1' => 1, |
| 21 |
'2' => 42, |
| 22 |
]; |
| 23 |
|
| 24 |
private function getFilterLookup( ?int $userID = null ): FilterLookup { |
| 25 |
$lookup = $this->createMock( FilterLookup::class ); |
| 26 |
$lookup->method( 'getFilter' ) |
| 27 |
->willReturnCallback( function ( $filter, $global ) use ( $userID ) { |
| 28 |
$userID ??= self::USER_IDS[ $global ? "global-$filter" : $filter ] ?? 0; |
| 29 |
$filterObj = $this->createMock( ExistingFilter::class ); |
| 30 |
$filterObj->method( 'getUserID' )->willReturn( $userID ); |
| 31 |
return $filterObj; |
| 32 |
} ); |
| 33 |
return $lookup; |
| 34 |
} |
| 35 |
|
| 36 |
public static function provideDataForEvent(): array { |
| 37 |
return [ |
| 38 |
[ true, 1, 1 ], |
| 39 |
[ true, 2, 42 ], |
| 40 |
[ false, 1, 1 ], |
| 41 |
[ false, 2, 42 ], |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testGetDataForEvent( bool $loaded, int $filter, int $userID ) { |
| 49 |
$expectedThrottledActions = []; |
| 50 |
$notifier = new EchoNotifier( |
| 51 |
$this->getFilterLookup(), |
| 52 |
$this->createMock( ConsequencesRegistry::class ), |
| 53 |
$loaded |
| 54 |
); |
| 55 |
[ |
| 56 |
'type' => $type, |
| 57 |
'title' => $title, |
| 58 |
'extra' => $extra |
| 59 |
] = $notifier->getDataForEvent( $filter ); |
| 60 |
|
| 61 |
$this->assertSame( EchoNotifier::EVENT_TYPE, $type ); |
| 62 |
$this->assertInstanceOf( Title::class, $title ); |
| 63 |
$this->assertSame( -1, $title->getNamespace() ); |
| 64 |
[ , $subpage ] = explode( '/', $title->getText(), 2 ); |
| 65 |
$this->assertSame( (string)$filter, $subpage ); |
| 66 |
$this->assertSame( [ 'user' => $userID, 'throttled-actions' => $expectedThrottledActions ], $extra ); |
| 67 |
} |
| 68 |
|
| 69 |
public function testNotifyForFilter() { |
| 70 |
$this->markTestSkippedIfExtensionNotLoaded( 'Echo' ); |
| 71 |
|
| 72 |
$user = $this->getTestUser()->getUserIdentity(); |
| 73 |
$notifier = new EchoNotifier( |
| 74 |
$this->getFilterLookup( $user->getId() ), |
| 75 |
$this->createMock( ConsequencesRegistry::class ), |
| 76 |
true |
| 77 |
); |
| 78 |
$this->assertInstanceOf( Event::class, $notifier->notifyForFilter( 1 ) ); |
| 79 |
} |
| 80 |
|
| 81 |
public function testNotifyForFilter_EchoNotLoaded() { |
| 82 |
$lookup = $this->createMock( FilterLookup::class ); |
| 83 |
$lookup->expects( $this->never() )->method( $this->anything() ); |
| 84 |
$notifier = new EchoNotifier( |
| 85 |
$lookup, |
| 86 |
$this->createMock( ConsequencesRegistry::class ), |
| 87 |
false |
| 88 |
); |
| 89 |
$this->assertFalse( $notifier->notifyForFilter( 1 ) ); |
| 90 |
} |
| 91 |
|
| 92 |
} |