mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-24 06:03:49 +00:00
Merge "Improve coverage of parser-related classes"
This commit is contained in:
commit
e35ab7c3e7
|
@ -25,7 +25,7 @@ class CheckSyntax extends ApiBase {
|
|||
$warnings = [];
|
||||
foreach ( $result->getWarnings() as $warning ) {
|
||||
$warnings[] = [
|
||||
'message' => $warning->getMessageObj()->text(),
|
||||
'message' => $this->msg( $warning->getMessageObj() )->text(),
|
||||
'character' => $warning->getPosition()
|
||||
];
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ class CheckSyntax extends ApiBase {
|
|||
'@phan-var AFPUserVisibleException $excep';
|
||||
$r = [
|
||||
'status' => 'error',
|
||||
'message' => $excep->getMessageObj()->text(),
|
||||
'message' => $this->msg( $excep->getMessageObj() )->text(),
|
||||
'character' => $excep->getPosition(),
|
||||
];
|
||||
}
|
||||
|
|
|
@ -462,7 +462,7 @@ class FilterRunner {
|
|||
$origExtraTime = LazyVariableComputer::$profilingExtraTime;
|
||||
|
||||
$this->parser->setFilter( $filterName );
|
||||
$result = $this->parser->checkConditions( $filter->getRules(), true, $filterName )->getResult();
|
||||
$result = $this->parser->checkConditions( $filter->getRules(), $filterName )->getResult();
|
||||
|
||||
$actualExtra = LazyVariableComputer::$profilingExtraTime - $origExtraTime;
|
||||
$timeTaken = 1000 * ( microtime( true ) - $startTime - $actualExtra );
|
||||
|
|
|
@ -120,7 +120,7 @@ class FilterValidator {
|
|||
if ( $syntaxStatus->getResult() !== true ) {
|
||||
$excep = $syntaxStatus->getException();
|
||||
$errMsg = $excep instanceof AFPUserVisibleException
|
||||
? $excep->getMessageObj()->text()
|
||||
? $excep->getMessageObj()
|
||||
: $excep->getMessage();
|
||||
$ret->error( 'abusefilter-edit-badsyntax', $errMsg );
|
||||
}
|
||||
|
|
|
@ -37,25 +37,18 @@ class AFPUserVisibleException extends AFPException {
|
|||
}
|
||||
|
||||
/**
|
||||
* Change the message of the exception to a localized version
|
||||
*/
|
||||
public function setLocalizedMessage() {
|
||||
$this->message = $this->getMessageObj()->text();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error message in English for use in logs
|
||||
* Returns the error message for use in logs
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMessageForLogs() {
|
||||
return $this->getMessageObj()->inLanguage( 'en' )->useDatabase( false )->text();
|
||||
public function getMessageForLogs() : string {
|
||||
return "ID: {$this->mExceptionID}; position: {$this->mPosition}; params: " . implode( $this->mParams );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Message
|
||||
*/
|
||||
public function getMessageObj() {
|
||||
public function getMessageObj() : Message {
|
||||
// Give grep a chance to find the usages:
|
||||
// abusefilter-exception-unexpectedatend, abusefilter-exception-expectednotfound
|
||||
// abusefilter-exception-unrecognisedkeyword, abusefilter-exception-unexpectedtoken
|
||||
|
@ -68,9 +61,9 @@ class AFPUserVisibleException extends AFPException {
|
|||
// abusefilter-exception-invalidiprange, abusefilter-exception-disabledvar
|
||||
// abusefilter-exception-variablevariable, abusefilter-exception-toomanyargs
|
||||
// abusefilter-exception-negativeoffset
|
||||
return wfMessage(
|
||||
return new Message(
|
||||
'abusefilter-exception-' . $this->mExceptionID,
|
||||
$this->mPosition, ...$this->mParams
|
||||
array_merge( [ $this->mPosition ], $this->mParams )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -282,28 +282,21 @@ class AbuseFilterParser extends AFPTransitionBase {
|
|||
* be used to determine whether this method should throw.
|
||||
*
|
||||
* @param string $conds
|
||||
* @param bool $ignoreError
|
||||
* @param string|null $filter The ID of the filter being parsed
|
||||
* @return ParserStatus
|
||||
* @throws AFPException
|
||||
*/
|
||||
public function checkConditions( string $conds, $ignoreError = true, $filter = null ) : ParserStatus {
|
||||
public function checkConditions( string $conds, $filter = null ) : ParserStatus {
|
||||
$result = $this->parseDetailed( $conds );
|
||||
$excep = $result->getException();
|
||||
if ( $excep !== null ) {
|
||||
if ( $excep instanceof AFPUserVisibleException ) {
|
||||
$msg = $excep->getMessageForLogs();
|
||||
$excep->setLocalizedMessage();
|
||||
} else {
|
||||
$msg = $excep->getMessage();
|
||||
}
|
||||
|
||||
$extraInfo = $filter !== null ? " for filter $filter" : '';
|
||||
$this->logger->warning( "AbuseFilter parser error$extraInfo: $msg" );
|
||||
|
||||
if ( !$ignoreError ) {
|
||||
throw $excep;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
|
|
@ -11,12 +11,12 @@ class UserVisibleWarning extends AFPUserVisibleException {
|
|||
/**
|
||||
* @return Message
|
||||
*/
|
||||
public function getMessageObj() {
|
||||
public function getMessageObj() : Message {
|
||||
// Give grep a chance to find the usages:
|
||||
// abusefilter-warning-match-empty-regex
|
||||
return wfMessage(
|
||||
return new Message(
|
||||
'abusefilter-warning-' . $this->mExceptionID,
|
||||
$this->mPosition, ...$this->mParams
|
||||
array_merge( [ $this->mPosition ], $this->mParams )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ class FilterValidatorTest extends MediaWikiUnitTestCase {
|
|||
$excMsg = $this->getMockMessage( $excText );
|
||||
$excep = $this->createMock( AFPUserVisibleException::class );
|
||||
$excep->method( 'getMessageObj' )->willReturn( $excMsg );
|
||||
yield 'invalid, user error' => [ false, $excep, 'abusefilter-edit-badsyntax', [ $excText ] ];
|
||||
yield 'invalid, user error' => [ false, $excep, 'abusefilter-edit-badsyntax', [ $excMsg ] ];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
35
tests/phpunit/unit/Parser/AFPUserVisibleExceptionTest.php
Normal file
35
tests/phpunit/unit/Parser/AFPUserVisibleExceptionTest.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit\Parser;
|
||||
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\AFPUserVisibleException;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
||||
/**
|
||||
* @group Test
|
||||
* @group AbuseFilter
|
||||
* @group AbuseFilterParser
|
||||
*
|
||||
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Parser\AFPUserVisibleException
|
||||
*/
|
||||
class AFPUserVisibleExceptionTest extends MediaWikiUnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::getPosition
|
||||
* @covers ::getMessageForLogs
|
||||
* @covers ::getMessageObj
|
||||
*/
|
||||
public function testGetters() {
|
||||
$excID = 'abusefilter-foo';
|
||||
$position = 42;
|
||||
$params = [ 'foo' ];
|
||||
$exc = new AFPUserVisibleException( $excID, $position, $params );
|
||||
$this->assertSame( $position, $exc->getPosition(), 'position' );
|
||||
$this->assertStringContainsString( $excID, $exc->getMessageForLogs(), 'ID in log message' );
|
||||
$this->assertStringContainsString( $position, $exc->getMessageForLogs(), 'position in logs message' );
|
||||
$message = $exc->getMessageObj();
|
||||
$this->assertSame( 'abusefilter-exception-' . $excID, $message->getKey(), 'msg key' );
|
||||
$this->assertArrayEquals( array_merge( [ $position ], $params ), $message->getParams(), 'msg params' );
|
||||
}
|
||||
}
|
48
tests/phpunit/unit/Parser/ParserFactoryTest.php
Normal file
48
tests/phpunit/unit/Parser/ParserFactoryTest.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit\Parser;
|
||||
|
||||
use BagOStuff;
|
||||
use Language;
|
||||
use MediaWiki\Extension\AbuseFilter\KeywordsManager;
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterCachingParser;
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterParser;
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\ParserFactory;
|
||||
use MediaWiki\Extension\AbuseFilter\Variables\VariablesManager;
|
||||
use MediaWikiUnitTestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* @group Test
|
||||
* @group AbuseFilter
|
||||
* @group AbuseFilterParser
|
||||
*
|
||||
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Parser\ParserFactory
|
||||
*/
|
||||
class ParserFactoryTest extends MediaWikiUnitTestCase {
|
||||
|
||||
public function provideParserClass() : array {
|
||||
return [
|
||||
'old' => [ 'AbuseFilterParser', AbuseFilterParser::class ],
|
||||
'new' => [ 'AbuseFilterCachingParser', AbuseFilterCachingParser::class ]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::newParser
|
||||
* @dataProvider provideParserClass
|
||||
*/
|
||||
public function testNewParser( string $classOption, string $expClass ) {
|
||||
$factory = new ParserFactory(
|
||||
$this->createMock( Language::class ),
|
||||
$this->createMock( BagOStuff::class ),
|
||||
new NullLogger(),
|
||||
$this->createMock( KeywordsManager::class ),
|
||||
$this->createMock( VariablesManager::class ),
|
||||
$classOption,
|
||||
1000
|
||||
);
|
||||
$this->assertInstanceOf( $expClass, $factory->newParser() );
|
||||
}
|
||||
}
|
37
tests/phpunit/unit/Parser/ParserStatusTest.php
Normal file
37
tests/phpunit/unit/Parser/ParserStatusTest.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit\Parser;
|
||||
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\AFPException;
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\ParserStatus;
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\UserVisibleWarning;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
||||
/**
|
||||
* @group Test
|
||||
* @group AbuseFilter
|
||||
* @group AbuseFilterParser
|
||||
*
|
||||
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Parser\ParserStatus
|
||||
*/
|
||||
class ParserStatusTest extends MediaWikiUnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::__construct
|
||||
* @covers ::getResult
|
||||
* @covers ::getWarmCache
|
||||
* @covers ::getException
|
||||
* @covers ::getWarnings
|
||||
*/
|
||||
public function testGetters() {
|
||||
$result = true;
|
||||
$warm = false;
|
||||
$exc = new AFPException();
|
||||
$warnings = [ new UserVisibleWarning( 'foo', 1, [] ) ];
|
||||
$status = new ParserStatus( $result, $warm, $exc, $warnings );
|
||||
$this->assertSame( $result, $status->getResult() );
|
||||
$this->assertSame( $warm, $status->getWarmCache() );
|
||||
$this->assertSame( $exc, $status->getException() );
|
||||
$this->assertSame( $warnings, $status->getWarnings() );
|
||||
}
|
||||
}
|
28
tests/phpunit/unit/Parser/UserVisibleWarningTest.php
Normal file
28
tests/phpunit/unit/Parser/UserVisibleWarningTest.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit\Parser;
|
||||
|
||||
use MediaWiki\Extension\AbuseFilter\Parser\UserVisibleWarning;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
||||
/**
|
||||
* @group Test
|
||||
* @group AbuseFilter
|
||||
* @group AbuseFilterParser
|
||||
*
|
||||
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Parser\UserVisibleWarning
|
||||
*/
|
||||
class UserVisibleWarningTest extends MediaWikiUnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::getMessageObj
|
||||
*/
|
||||
public function testGetMessageObj() {
|
||||
$excID = 'abusefilter-foo';
|
||||
$position = 42;
|
||||
$params = [ 'foo' ];
|
||||
$message = ( new UserVisibleWarning( $excID, $position, $params ) )->getMessageObj();
|
||||
$this->assertSame( 'abusefilter-warning-' . $excID, $message->getKey(), 'msg key' );
|
||||
$this->assertArrayEquals( array_merge( [ $position ], $params ), $message->getParams(), 'msg params' );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue