Cover some API modules by tests

Change-Id: Icc57e260b3b06a58fc05f304d6e63dc40f970fe9
This commit is contained in:
Matěj Suchánek 2021-02-02 12:16:40 +01:00
parent 8c9f35146d
commit a0fcfbcc32
6 changed files with 499 additions and 0 deletions

View file

@ -152,6 +152,7 @@
"TestAutoloadClasses": {
"AbuseFilterConsequencesTest": "tests/phpunit/AbuseFilterConsequencesTest.php",
"MediaWiki\\Extension\\AbuseFilter\\Tests\\Unit\\Parser\\ParserTestCase": "tests/phpunit/unit/Parser/ParserTestCase.php",
"MediaWiki\\Extension\\AbuseFilter\\Tests\\Integration\\Api\\AbuseFilterApiTestTrait": "tests/phpunit/integration/Api/AbuseFilterApiTestTrait.php",
"AbuseFilterUploadTestTrait": "tests/phpunit/AbuseFilterUploadTestTrait.php",
"AbuseFilterCreateAccountTestTrait": "tests/phpunit/AbuseFilterCreateAccountTestTrait.php",
"AbuseFilterRowsAndFiltersTestTrait": "tests/phpunit/AbuseFilterRowsAndFiltersTestTrait.php",

View file

@ -0,0 +1,28 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration\Api;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
/**
* This trait contains helper methods for Api integration tests.
*/
trait AbuseFilterApiTestTrait {
/**
* @param AbuseFilterParser|null $parser
* @return ParserFactory
*/
protected function getParserFactory( AbuseFilterParser $parser = null ) : ParserFactory {
$factory = $this->createMock( ParserFactory::class );
if ( $parser !== null ) {
$factory->expects( $this->atLeastOnce() )
->method( 'newParser' )
->willReturn( $parser );
} else {
$factory->expects( $this->never() )->method( 'newParser' );
}
return $factory;
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration\Api;
use ApiTestCase;
use FormatJson;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Parser\ParserStatus;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Api\CheckMatch
* @group medium
*/
class CheckMatchTest extends ApiTestCase {
use AbuseFilterApiTestTrait;
/**
* @covers ::execute
*/
public function testExecute_noPermissions() {
$this->setExpectedApiException( 'apierror-abusefilter-canttest', 'permissiondenied' );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory() );
$this->doApiRequest( [
'action' => 'abusefiltercheckmatch',
'filter' => 'sampleFilter',
'vars' => FormatJson::encode( [] ),
], null, null, self::getTestUser()->getUser() );
}
public function provideExecuteOk() {
return [
'matched' => [ true ],
'no match' => [ false ],
];
}
/**
* @dataProvider provideExecuteOk
* @covers ::execute
*/
public function testExecute_Ok( bool $expected ) {
$filter = 'sampleFilter';
$checkStatus = new ParserStatus( true, false, null, [] );
$resultStatus = new ParserStatus( $expected, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->expects( $this->once() )
->method( 'checkSyntax' )->with( $filter )
->willReturn( $checkStatus );
$parser->expects( $this->once() )
->method( 'checkConditions' )->with( $filter )
->willReturn( $resultStatus );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefiltercheckmatch',
'filter' => $filter,
'vars' => FormatJson::encode( [] ),
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[
'abusefiltercheckmatch' => [
'result' => $expected
]
],
$result[0],
false,
true
);
}
/**
* @covers ::execute
*/
public function testExecute_error() {
$this->setExpectedApiException( 'apierror-abusefilter-badsyntax', 'badsyntax' );
$filter = 'sampleFilter';
$status = new ParserStatus( false, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->expects( $this->once() )
->method( 'checkSyntax' )->with( $filter )
->willReturn( $status );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$this->doApiRequest( [
'action' => 'abusefiltercheckmatch',
'filter' => $filter,
'vars' => FormatJson::encode( [] ),
], null, null, self::getTestSysop()->getUser() );
}
}

View file

@ -0,0 +1,139 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration\Api;
use ApiTestCase;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
use MediaWiki\Extension\AbuseFilter\Parser\AFPUserVisibleException;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Parser\ParserStatus;
use MediaWiki\Extension\AbuseFilter\Parser\UserVisibleWarning;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Api\CheckSyntax
* @group medium
*/
class CheckSyntaxTest extends ApiTestCase {
use AbuseFilterApiTestTrait;
/**
* @covers ::execute
*/
public function testExecute_noPermissions() {
$this->setExpectedApiException( 'apierror-abusefilter-cantcheck', 'permissiondenied' );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory() );
$this->doApiRequest( [
'action' => 'abusefilterchecksyntax',
'filter' => 'sampleFilter',
], null, null, self::getTestUser()->getUser() );
}
/**
* @covers ::execute
*/
public function testExecute_Ok() {
$input = 'sampleFilter';
$status = new ParserStatus( true, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $input )
->willReturn( $status );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefilterchecksyntax',
'filter' => $input,
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[ 'abusefilterchecksyntax' => [ 'status' => 'ok' ] ],
$result[0],
false,
true
);
}
/**
* @covers ::execute
*/
public function testExecute_OkAndWarnings() {
$input = 'sampleFilter';
$warnings = [
new UserVisibleWarning( 'exception-1', 3, [] ),
new UserVisibleWarning( 'exception-2', 8, [ 'param' ] ),
];
$status = new ParserStatus( true, false, null, $warnings );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $input )
->willReturn( $status );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefilterchecksyntax',
'filter' => $input,
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[
'abusefilterchecksyntax' => [
'status' => 'ok',
'warnings' => [
[
'message' => wfMessage(
'abusefilter-warning-exception-1',
3
)->text(),
'character' => 3,
],
[
'message' => wfMessage(
'abusefilter-warning-exception-2',
8,
'param'
)->text(),
'character' => 8,
],
]
]
],
$result[0],
false,
true
);
}
/**
* @covers ::execute
*/
public function testExecute_error() {
$input = 'sampleFilter';
$exception = new AFPUserVisibleException( 'error-id', 4, [] );
$status = new ParserStatus( false, false, $exception, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $input )
->willReturn( $status );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefilterchecksyntax',
'filter' => $input,
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[
'abusefilterchecksyntax' => [
'status' => 'error',
'message' => wfMessage(
'abusefilter-exception-error-id',
4
)->text(),
'character' => 4
]
],
$result[0],
false,
true
);
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration\Api;
use ApiTestCase;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
use MediaWiki\Extension\AbuseFilter\Parser\ParserStatus;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Api\EvalExpression
* @group medium
*/
class EvalExpressionTest extends ApiTestCase {
use AbuseFilterApiTestTrait;
/**
* @covers ::execute
*/
public function testExecute_noPermissions() {
$this->setExpectedApiException( 'apierror-abusefilter-canteval', 'permissiondenied' );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory() );
$this->doApiRequest( [
'action' => 'abusefilterevalexpression',
'expression' => 'sampleExpression',
], null, null, self::getTestUser()->getUser() );
}
/**
* @covers ::execute
*/
public function testExecute_error() {
$this->setExpectedApiException( 'abusefilter-tools-syntax-error' );
$expression = 'sampleExpression';
$status = new ParserStatus( false, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $expression )
->willReturn( $status );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$this->doApiRequest( [
'action' => 'abusefilterevalexpression',
'expression' => $expression,
], null, null, self::getTestSysop()->getUser() );
}
/**
* @covers ::execute
*/
public function testExecute_Ok() {
$expression = 'sampleExpression';
$status = new ParserStatus( true, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $expression )
->willReturn( $status );
$parser->expects( $this->once() )->method( 'evaluateExpression' )
->willReturn( 'output' );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefilterevalexpression',
'expression' => $expression,
'prettyprint' => false,
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[
'abusefilterevalexpression' => [
'result' => "'output'"
]
],
$result[0],
false,
true
);
}
/**
* @covers ::execute
*/
public function testExecute_OkAndPrettyPrint() {
$expression = 'sampleExpression';
$status = new ParserStatus( true, false, null, [] );
$parser = $this->createMock( AbuseFilterParser::class );
$parser->method( 'checkSyntax' )->with( $expression )
->willReturn( $status );
$parser->expects( $this->once() )->method( 'evaluateExpression' )
->willReturn( [ 'value1', 2 ] );
$this->setService( ParserFactory::SERVICE_NAME, $this->getParserFactory( $parser ) );
$result = $this->doApiRequest( [
'action' => 'abusefilterevalexpression',
'expression' => $expression,
'prettyprint' => true,
], null, null, self::getTestSysop()->getUser() );
$this->assertArrayEquals(
[
'abusefilterevalexpression' => [
'result' => "[\n\t0 => 'value1',\n\t1 => 2\n]"
]
],
$result[0],
false,
true
);
}
}

View file

@ -0,0 +1,126 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Integration\Api;
use ApiTestCase;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Extension\AbuseFilter\BlockAutopromoteStore;
use MediaWiki\MediaWikiServices;
use User;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Api\UnblockAutopromote
* @group medium
*/
class UnblockAutopromoteTest extends ApiTestCase {
/**
* @covers ::execute
*/
public function testExecute_noPermissions() {
$this->setExpectedApiException( [
'apierror-permissiondenied',
wfMessage( 'action-abusefilter-modify' )
] );
$store = $this->createMock( BlockAutopromoteStore::class );
$store->expects( $this->never() )->method( 'unblockAutopromote' );
$this->setService( BlockAutopromoteStore::SERVICE_NAME, $store );
$this->doApiRequestWithToken( [
'action' => 'abusefilterunblockautopromote',
'user' => 'User'
], null, self::getTestUser()->getUser(), 'csrf' );
}
/**
* @covers ::execute
*/
public function testExecute_invalidUser() {
$invalid = 'invalid#username';
$this->setExpectedApiException( [
'apierror-baduser',
'user',
$invalid
] );
$store = $this->createMock( BlockAutopromoteStore::class );
$store->expects( $this->never() )->method( 'unblockAutopromote' );
$this->setService( BlockAutopromoteStore::SERVICE_NAME, $store );
$this->doApiRequestWithToken( [
'action' => 'abusefilterunblockautopromote',
'user' => $invalid
], null, self::getTestUser()->getUser(), 'csrf' );
}
/**
* @covers ::execute
*/
public function testExecute_blocked() {
$this->setExpectedApiException( 'apierror-blocked', 'blocked' );
$user = self::getTestUser( [ 'sysop' ] )->getUser();
$store = $this->createMock( BlockAutopromoteStore::class );
$store->expects( $this->never() )->method( 'unblockAutopromote' );
$this->setService( BlockAutopromoteStore::SERVICE_NAME, $store );
$block = new DatabaseBlock( [ 'expiry' => '1 day' ] );
$block->setTarget( $user );
$block->setBlocker( self::getTestSysop()->getUser() );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->doApiRequestWithToken( [
'action' => 'abusefilterunblockautopromote',
'user' => 'User'
], null, $user, 'csrf' );
}
/**
* @covers ::execute
*/
public function testExecute_nothingToDo() {
$target = 'User';
$user = self::getTestUser( [ 'sysop' ] )->getUser();
$this->setExpectedApiException( [ 'abusefilter-reautoconfirm-none', $target ] );
$store = $this->createMock( BlockAutopromoteStore::class );
$store->expects( $this->once() )
->method( 'unblockAutopromote' )
->with( $this->isInstanceOf( User::class ), $user, $this->anything() )
->willReturn( false );
$this->setService( BlockAutopromoteStore::SERVICE_NAME, $store );
$this->doApiRequestWithToken( [
'action' => 'abusefilterunblockautopromote',
'user' => $target
], null, $user, 'csrf' );
}
/**
* @covers ::execute
*/
public function testExecute_success() {
$target = 'User';
$user = self::getTestUser( [ 'sysop' ] )->getUser();
$store = $this->createMock( BlockAutopromoteStore::class );
$store->expects( $this->once() )
->method( 'unblockAutopromote' )
->with( $this->isInstanceOf( User::class ), $user, $this->anything() )
->willReturn( true );
$this->setService( BlockAutopromoteStore::SERVICE_NAME, $store );
$result = $this->doApiRequestWithToken( [
'action' => 'abusefilterunblockautopromote',
'user' => $target
], null, $user, 'csrf' );
$this->assertArrayEquals(
[ 'abusefilterunblockautopromote' => [ 'user' => $target ] ],
$result[0],
false,
true
);
}
}