2021-01-06 13:07:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Consequence\HookAborterConsequence;
|
|
|
|
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters;
|
2021-01-07 20:10:52 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\ExistingFilter;
|
2021-01-06 13:07:02 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method MockObject createMock($class)
|
|
|
|
* @method assertSame($x,$y,$msg='')
|
|
|
|
* @method assertStringMatchesFormat($fmt,$str,$msg='')
|
|
|
|
*/
|
|
|
|
trait ConsequenceGetMessageTestTrait {
|
|
|
|
/**
|
|
|
|
* @param UserIdentity|string|null $user Test name when used as data provider, a UserIdentity can be passed when
|
|
|
|
* called explicitly
|
|
|
|
* @return Generator
|
|
|
|
*/
|
|
|
|
public function provideGetMessageParameters( $user = null ) : Generator {
|
|
|
|
$user = $user instanceof UserIdentity
|
|
|
|
? $user
|
|
|
|
: new UserIdentityValue( 1, 'getMessage test user', 2 );
|
2021-01-07 20:10:52 +00:00
|
|
|
$localFilter = $this->createMock( ExistingFilter::class );
|
|
|
|
$localFilter->method( 'getID' )->willReturn( 1 );
|
|
|
|
$localFilter->method( 'getName' )->willReturn( 'Local filter' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$localParams = new Parameters(
|
|
|
|
$localFilter,
|
|
|
|
false,
|
|
|
|
$user,
|
|
|
|
$this->createMock( LinkTarget::class ),
|
|
|
|
'edit'
|
|
|
|
);
|
|
|
|
yield 'local filter' => [ $localParams ];
|
|
|
|
|
2021-01-07 20:10:52 +00:00
|
|
|
$globalFilter = $this->createMock( ExistingFilter::class );
|
|
|
|
$globalFilter->method( 'getID' )->willReturn( 3 );
|
|
|
|
$globalFilter->method( 'getName' )->willReturn( 'Global filter' );
|
2021-01-06 13:07:02 +00:00
|
|
|
$globalParams = new Parameters(
|
|
|
|
$globalFilter,
|
|
|
|
true,
|
|
|
|
$user,
|
|
|
|
$this->createMock( LinkTarget::class ),
|
|
|
|
'edit'
|
|
|
|
);
|
|
|
|
yield 'global filter' => [ $globalParams ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param HookAborterConsequence $consequence
|
|
|
|
* @param Parameters $params
|
|
|
|
* @param string $msg
|
|
|
|
*/
|
|
|
|
protected function doTestGetMessage(
|
|
|
|
HookAborterConsequence $consequence,
|
|
|
|
Parameters $params,
|
|
|
|
string $msg
|
|
|
|
) : void {
|
|
|
|
$actualMsg = $consequence->getMessage();
|
|
|
|
$this->assertSame( $msg, $actualMsg[0], 'message' );
|
|
|
|
$this->assertSame( $params->getFilter()->getName(), $actualMsg[1], 'name' );
|
|
|
|
$format = $params->getIsGlobalFilter() ? 'global-%d' : '%d';
|
|
|
|
$this->assertStringMatchesFormat( $format, $actualMsg[2], 'global name' );
|
|
|
|
}
|
|
|
|
}
|