mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-23 13:46:48 +00:00
Restore unit tests for CachingParser and fix it
Added cachingParser back to *all* the parser tests, fixed a couple of differences with the normal parser, and added a couple of tests so that any cachingParser-related file has 100% coverage. Also move the remaining get_matches tests inside parserTests, and specify the parser used in case of failure. This also adds a new base class for parser-related tests with a couple of util methods. Bug: T201193 Change-Id: I980aec3481a52ecc35f1811a366014a5581a7cdb
This commit is contained in:
parent
1cb80be0ad
commit
39fc7c12af
|
@ -152,7 +152,8 @@
|
|||
"ApiAbuseFilterUnblockAutopromote": "includes/api/ApiAbuseFilterUnblockAutopromote.php",
|
||||
"ApiAbuseFilterCheckMatch": "includes/api/ApiAbuseFilterCheckMatch.php",
|
||||
"NormalizeThrottleParameters": "maintenance/normalizeThrottleParameters.php",
|
||||
"AbuseFilterConsequencesTest": "tests/phpunit/AbuseFilterConsequencesTest.php"
|
||||
"AbuseFilterConsequencesTest": "tests/phpunit/AbuseFilterConsequencesTest.php",
|
||||
"AbuseFilterParserTestCase": "tests/phpunit/AbuseFilterParserTestCase.php"
|
||||
},
|
||||
"ResourceModules": {
|
||||
"ext.abuseFilter": {
|
||||
|
|
|
@ -533,7 +533,7 @@ class AbuseFilter {
|
|||
}
|
||||
|
||||
try {
|
||||
$result = $parser->parse( $conds, self::$condCount );
|
||||
$result = $parser->parse( $conds );
|
||||
} catch ( Exception $excep ) {
|
||||
$result = false;
|
||||
|
||||
|
|
|
@ -102,11 +102,16 @@ class AFPTreeNode {
|
|||
|
||||
/**
|
||||
* @return string
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function toDebugString() {
|
||||
return implode( "\n", $this->toDebugStringInner() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
private function toDebugStringInner() {
|
||||
if ( $this->type === self::ATOM ) {
|
||||
return [ "ATOM({$this->children->type} {$this->children->value})" ];
|
||||
|
|
|
@ -108,7 +108,9 @@ class AFPTreeParser {
|
|||
$this->move();
|
||||
$position = $this->mPos;
|
||||
|
||||
if ( $this->mCur->type === AFPToken::TNONE ) {
|
||||
if ( $this->mCur->type === AFPToken::TNONE ||
|
||||
( $this->mCur->type === AFPToken::TBRACE && $this->mCur->value == ')' )
|
||||
) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,9 @@ class AbuseFilterCachingParser extends AbuseFilterParser {
|
|||
}
|
||||
// Fallthrough intended
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new AFPException( "Unknown token provided in the ATOM node" );
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
case AFPTreeNode::ARRAY_DEFINITION:
|
||||
$items = array_map( [ $this, 'evalNode' ], $node->children );
|
||||
|
@ -133,7 +135,9 @@ class AbuseFilterCachingParser extends AbuseFilterParser {
|
|||
}
|
||||
|
||||
if ( count( self::$funcCache ) > 1000 ) {
|
||||
// @codeCoverageIgnoreStart
|
||||
self::$funcCache = [];
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
@ -203,7 +207,9 @@ class AbuseFilterCachingParser extends AbuseFilterParser {
|
|||
case '-':
|
||||
return AFPData::sub( $leftOperand, $rightOperand );
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new AFPException( "Unknown sum-related operator: {$op}" );
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
case AFPTreeNode::COMPARE:
|
||||
|
@ -242,6 +248,9 @@ class AbuseFilterCachingParser extends AbuseFilterParser {
|
|||
case AFPTreeNode::INDEX_ASSIGNMENT:
|
||||
list( $varName, $offset, $value ) = $node->children;
|
||||
|
||||
if ( !$this->mVars->varIsSet( $varName ) ) {
|
||||
throw new AFPUserVisibleException( 'unrecognisedvar', $node->position, [ $varName ] );
|
||||
}
|
||||
$array = $this->mVars->getVar( $varName );
|
||||
if ( $array->type !== AFPData::DARRAY ) {
|
||||
throw new AFPUserVisibleException( 'notarray', $node->position, [] );
|
||||
|
@ -281,7 +290,9 @@ class AbuseFilterCachingParser extends AbuseFilterParser {
|
|||
|
||||
return $lastValue;
|
||||
default:
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new AFPException( "Unknown node type passed: {$node->type}" );
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -747,7 +747,9 @@ class AbuseFilterParser {
|
|||
}
|
||||
|
||||
if ( count( self::$funcCache ) > 1000 ) {
|
||||
// @codeCoverageIgnoreStart
|
||||
self::$funcCache = [];
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
} else {
|
||||
$this->doLevelAtom( $result );
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
+1 + -1 === 0 &
|
||||
-1 + +2 === 1 &
|
||||
-0 === 0 &
|
||||
(1 + 1 === 2) &
|
||||
(1.5 + 1.5 === 3.0) &
|
||||
(2.5 + 1 === 3.5) &
|
||||
|
@ -24,4 +27,4 @@
|
|||
(2 ** 4 === 16) &
|
||||
(2.5 ** 2 === 6.25) &
|
||||
(2.5 ** 0 === 1.0) &
|
||||
(1000 ** 0 === 1)
|
||||
(1000 ** 0 === 1)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/* More complete tests for get_matches are in AbuseFilterParserTest.php */
|
||||
a := get_matches('I am a (dog|cat)', 'What did you say?');
|
||||
get_matches('I am a (dog|cat)', 'What did you say?') === [ false, false ] &
|
||||
get_matches('The (truth|pineapple) is (?:rarely)? pure and (nee*v(ah|er) sh?imple)', 'The truth is rarely pure and never simple, Wilde said') == ['The truth is rarely pure and never simple', 'truth', 'never simple', 'er'] &
|
||||
a === [false, false]
|
||||
get_matches('You say (.*) \(and I say (.*)\)\.', 'You say hello (and I say goodbye).') === [ 'You say hello (and I say goodbye).', 'hello', 'goodbye' ] &
|
||||
get_matches('I(?: am)? the ((walrus|egg man).*)\!', 'I am the egg man, I am the walrus !') === [ 'I am the egg man, I am the walrus !', 'egg man, I am the walrus ', 'egg man' ] &
|
||||
get_matches('this (does) not match', 'foo bar') === [ false, false ]
|
|
@ -1,10 +1,18 @@
|
|||
/* Examples from [[mw:Extension:AbuseFilter/Rules format#Lists]] */
|
||||
/* Examples from [[mw:Extension:AbuseFilter/Rules format#Arrays]] */
|
||||
|
||||
a_array := [ 5, 6, 7, 10];
|
||||
a_array[0] == 5 &
|
||||
length(a_array) == 4 &
|
||||
string(a_array) == "5\n6\n7\n10\n" &
|
||||
5 in a_array == true &
|
||||
'5' in a_array == true &
|
||||
'5\n6' in a_array == true &
|
||||
1 in a_array == true
|
||||
my_array := [ 5, 6, 7, 10];
|
||||
my_array[0] == 5 &
|
||||
length(my_array) == 4 &
|
||||
int( my_array ) === 4 &
|
||||
float( my_array ) === 4.0 &
|
||||
string(my_array) == "5\n6\n7\n10\n" &
|
||||
5 in my_array == true &
|
||||
'5' in my_array == true &
|
||||
'5\n6' in my_array == true &
|
||||
1 in my_array == true & (
|
||||
my_array[] := 57;
|
||||
my_array === [ 5, 6, 7, 10, 57 ]
|
||||
) & (
|
||||
my_array[2] := 42;
|
||||
my_array === [ 5, 6, 42, 10, 57 ]
|
||||
)
|
||||
|
|
|
@ -31,44 +31,7 @@
|
|||
* @covers AFPException
|
||||
* @covers AbuseFilterParser
|
||||
*/
|
||||
class AFPDataTest extends MediaWikiTestCase {
|
||||
/**
|
||||
* @return AbuseFilterParser
|
||||
*/
|
||||
public static function getParser() {
|
||||
/** @var AbuseFilterParser|null The parser used for tests */
|
||||
static $parser = null;
|
||||
if ( !$parser ) {
|
||||
$parser = new AbuseFilterParser();
|
||||
} else {
|
||||
$parser->resetState();
|
||||
}
|
||||
return $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base method for testing exceptions
|
||||
*
|
||||
* @param string $excep Identifier of the exception (e.g. 'unexpectedtoken')
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
*/
|
||||
private function exceptionTest( $excep, $expr, $caller ) {
|
||||
$parser = self::getParser();
|
||||
try {
|
||||
$parser->parse( $expr );
|
||||
} catch ( AFPUserVisibleException $e ) {
|
||||
$this->assertEquals(
|
||||
$excep,
|
||||
$e->mExceptionID,
|
||||
"Exception $excep not thrown in AFPData::$caller"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fail( "Exception $excep not thrown in AFPData::$caller" );
|
||||
}
|
||||
|
||||
class AFPDataTest extends AbuseFilterParserTestCase {
|
||||
/**
|
||||
* Test the 'regexfailure' exception
|
||||
*
|
||||
|
|
|
@ -39,43 +39,14 @@
|
|||
* @covers AbuseFilterVariableHolder
|
||||
* @covers AFComputedVariable
|
||||
*/
|
||||
class AbuseFilterParserTest extends MediaWikiTestCase {
|
||||
/**
|
||||
* @return AbuseFilterParser
|
||||
*/
|
||||
public static function getParser() {
|
||||
/** @var AbuseFilterParser */
|
||||
static $parser = null;
|
||||
if ( !$parser ) {
|
||||
$parser = new AbuseFilterParser();
|
||||
} else {
|
||||
$parser->resetState();
|
||||
}
|
||||
return $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbuseFilterParser[]
|
||||
*/
|
||||
public static function getParsers() {
|
||||
static $parsers = null;
|
||||
if ( !$parsers ) {
|
||||
$parsers = [
|
||||
new AbuseFilterParser()
|
||||
// @ToDo: Here we should also instantiate an AbuseFilterCachingParser as we'll have
|
||||
// fixed its problems (T156095). Right now it may break otherwise working tests (see T201193)
|
||||
];
|
||||
}
|
||||
return $parsers;
|
||||
}
|
||||
|
||||
class AbuseFilterParserTest extends AbuseFilterParserTestCase {
|
||||
/**
|
||||
* @param string $rule The rule to parse
|
||||
* @dataProvider readTests
|
||||
*/
|
||||
public function testParser( $rule ) {
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$this->assertTrue( $parser->parse( $rule ) );
|
||||
$this->assertTrue( $parser->parse( $rule ), 'Parser used: ' . get_class( $parser ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -182,12 +153,16 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
* @dataProvider condCountCases
|
||||
*/
|
||||
public function testCondCount( $rule, $expected ) {
|
||||
$parser = self::getParser();
|
||||
$countBefore = AbuseFilter::$condCount;
|
||||
$parser->parse( $rule );
|
||||
$countAfter = AbuseFilter::$condCount;
|
||||
$actual = $countAfter - $countBefore;
|
||||
$this->assertEquals( $expected, $actual, 'Condition count for ' . $rule );
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$parserClass = get_class( $parser );
|
||||
$countBefore = AbuseFilter::$condCount;
|
||||
$parser->parse( $rule );
|
||||
$countAfter = AbuseFilter::$condCount;
|
||||
$actual = $countAfter - $countBefore;
|
||||
$this->assertEquals( $expected, $actual, "Wrong condition count for $rule with $parserClass" );
|
||||
// Reset cache or it would compromise conditions count
|
||||
$parser::$funcCache = [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -216,85 +191,9 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
public function testArrayShortcircuit() {
|
||||
$code = 'a := [false, false]; b := [false, false]; c := 42; d := [0,1];' .
|
||||
'a[0] != false & b[1] != false & (b[5**2/(5*(4+1))] !== a[43-c] | a[d[0]] === b[d[c-41]])';
|
||||
$this->assertFalse( self::getParser()->parse( $code ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure get_matches function captures returns expected output.
|
||||
* @param string $needle Regex to pass to get_matches.
|
||||
* @param string $haystack String to run regex against.
|
||||
* @param string[] $expected The expected values of the matched groups.
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
* @dataProvider getMatchesCases
|
||||
*/
|
||||
public function testGetMatches( $needle, $haystack, $expected ) {
|
||||
$parser = self::getParser();
|
||||
$afpData = $parser->intEval( "get_matches('$needle', '$haystack')" )->data;
|
||||
|
||||
// Extract matches from AFPData.
|
||||
$matches = array_map( function ( $afpDatum ) {
|
||||
return $afpDatum->data;
|
||||
}, $afpData );
|
||||
|
||||
$this->assertEquals( $expected, $matches );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for get_matches method.
|
||||
* @return array
|
||||
*/
|
||||
public function getMatchesCases() {
|
||||
return [
|
||||
[
|
||||
'You say (.*) \(and I say (.*)\)\.',
|
||||
'You say hello (and I say goodbye).',
|
||||
[
|
||||
'You say hello (and I say goodbye).',
|
||||
'hello',
|
||||
'goodbye',
|
||||
],
|
||||
],
|
||||
[
|
||||
'I(?: am)? the ((walrus|egg man).*)\!',
|
||||
'I am the egg man, I am the walrus !',
|
||||
[
|
||||
'I am the egg man, I am the walrus !',
|
||||
'egg man, I am the walrus ',
|
||||
'egg man',
|
||||
],
|
||||
],
|
||||
[
|
||||
'this (does) not match',
|
||||
'foo bar',
|
||||
[
|
||||
false,
|
||||
false,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Base method for testing exceptions
|
||||
*
|
||||
* @param string $excep Identifier of the exception (e.g. 'unexpectedtoken')
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
*/
|
||||
private function exceptionTest( $excep, $expr, $caller ) {
|
||||
$parser = self::getParser();
|
||||
try {
|
||||
$parser->parse( $expr );
|
||||
} catch ( AFPUserVisibleException $e ) {
|
||||
$this->assertEquals(
|
||||
$excep,
|
||||
$e->mExceptionID,
|
||||
"Exception $excep not thrown in AbuseFilterParser::$caller"
|
||||
);
|
||||
return;
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$this->assertFalse( $parser->parse( $code ), 'Parser: ' . get_class( $parser ) );
|
||||
}
|
||||
|
||||
$this->fail( "Exception $excep not thrown in AbuseFilterParser::$caller" );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -302,13 +201,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelSet
|
||||
* @covers AbuseFilterParser::doLevelConditions
|
||||
* @covers AbuseFilterParser::doLevelBraces
|
||||
* @covers AbuseFilterParser::doLevelFunction
|
||||
* @covers AbuseFilterParser::doLevelAtom
|
||||
* @covers AbuseFilterParser::skipOverBraces
|
||||
* @covers AbuseFilterParser::doLevelArrayElements
|
||||
* @dataProvider expectedNotFound
|
||||
*/
|
||||
public function testExpectedNotFoundException( $expr, $caller ) {
|
||||
|
@ -343,7 +235,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelEntry
|
||||
* @dataProvider unexpectedAtEnd
|
||||
*/
|
||||
public function testUnexpectedAtEndException( $expr, $caller ) {
|
||||
|
@ -368,8 +259,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelSet
|
||||
* @covers AbuseFilterParser::getVarValue
|
||||
* @dataProvider unrecognisedVar
|
||||
*/
|
||||
public function testUnrecognisedVarException( $expr, $caller ) {
|
||||
|
@ -395,8 +284,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelSet
|
||||
* @covers AbuseFilterParser::doLevelArrayElements
|
||||
* @dataProvider notArray
|
||||
*/
|
||||
public function testNotArrayException( $expr, $caller ) {
|
||||
|
@ -414,6 +301,8 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
return [
|
||||
[ 'a := 5; a[1] = 5', 'doLevelSet' ],
|
||||
[ 'a := 1; 3 = a[5]', 'doLevelArrayElements' ],
|
||||
[ 'a := 2; a[] := 2', '[different callers]' ],
|
||||
[ 'a := 3; a[3] := 5', '[different callers]' ]
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -422,8 +311,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelSet
|
||||
* @covers AbuseFilterParser::doLevelArrayElements
|
||||
* @dataProvider outOfBounds
|
||||
*/
|
||||
public function testOutOfBoundsException( $expr, $caller ) {
|
||||
|
@ -441,6 +328,7 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
return [
|
||||
[ 'a := [2]; a[5] = 9', 'doLevelSet' ],
|
||||
[ 'a := [1,2,3]; 3 = a[5]', 'doLevelArrayElements' ],
|
||||
[ 'a := [1]; a[15] := 5', '[different callers]' ]
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -449,7 +337,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelAtom
|
||||
* @dataProvider unrecognisedKeyword
|
||||
*/
|
||||
public function testUnrecognisedKeywordException( $expr, $caller ) {
|
||||
|
@ -474,7 +361,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::doLevelAtom
|
||||
* @dataProvider unexpectedToken
|
||||
*/
|
||||
public function testUnexpectedTokenException( $expr, $caller ) {
|
||||
|
@ -499,7 +385,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::getVarValue
|
||||
* @dataProvider disabledVar
|
||||
*/
|
||||
public function testDisabledVarException( $expr, $caller ) {
|
||||
|
@ -524,7 +409,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::setUserVariable
|
||||
* @dataProvider overrideBuiltin
|
||||
*/
|
||||
public function testOverrideBuiltinException( $expr, $caller ) {
|
||||
|
@ -549,8 +433,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::funcRCount
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
* @dataProvider regexFailure
|
||||
*/
|
||||
public function testRegexFailureException( $expr, $caller ) {
|
||||
|
@ -576,7 +458,6 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
*
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
* @covers AbuseFilterParser::funcIPInRange
|
||||
* @dataProvider invalidIPRange
|
||||
*/
|
||||
public function testInvalidIPRangeException( $expr, $caller ) {
|
||||
|
@ -601,33 +482,16 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
* without 0 params. They should throw a 'noparams' exception.
|
||||
*
|
||||
* @param string $func The function to test
|
||||
* @covers AbuseFilterParser::checkEnoughArguments
|
||||
* @covers AbuseFilterParser::funcLc
|
||||
* @covers AbuseFilterParser::funcUc
|
||||
* @covers AbuseFilterParser::funcLen
|
||||
* @covers AbuseFilterParser::funcSpecialRatio
|
||||
* @covers AbuseFilterParser::funcCount
|
||||
* @covers AbuseFilterParser::funcRCount
|
||||
* @covers AbuseFilterParser::funcCCNorm
|
||||
* @covers AbuseFilterParser::funcSanitize
|
||||
* @covers AbuseFilterParser::funcRMSpecials
|
||||
* @covers AbuseFilterParser::funcRMWhitespace
|
||||
* @covers AbuseFilterParser::funcRMDoubles
|
||||
* @covers AbuseFilterParser::funcNorm
|
||||
* @covers AbuseFilterParser::funcStrRegexEscape
|
||||
* @covers AbuseFilterParser::castString
|
||||
* @covers AbuseFilterParser::castInt
|
||||
* @covers AbuseFilterParser::castFloat
|
||||
* @covers AbuseFilterParser::castBool
|
||||
* @dataProvider oneParamFuncs
|
||||
*/
|
||||
public function testNoParamsException( $func ) {
|
||||
$parser = self::getParser();
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
'No parameters given to function'
|
||||
);
|
||||
$parser->parse( "$func()" );
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
'No parameters given to function'
|
||||
);
|
||||
$parser->parse( "$func()" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -664,31 +528,21 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
* They should throw a 'notenoughargs' exception.
|
||||
*
|
||||
* @param string $func The function to test
|
||||
* @covers AbuseFilterParser::checkEnoughArguments
|
||||
* @covers AbuseFilterParser::funcGetMatches
|
||||
* @covers AbuseFilterParser::funcIPInRange
|
||||
* @covers AbuseFilterParser::funcContainsAny
|
||||
* @covers AbuseFilterParser::funcContainsAll
|
||||
* @covers AbuseFilterParser::funcCCNormContainsAny
|
||||
* @covers AbuseFilterParser::funcCCNormContainsAll
|
||||
* @covers AbuseFilterParser::funcEqualsToAny
|
||||
* @covers AbuseFilterParser::funcSubstr
|
||||
* @covers AbuseFilterParser::funcStrPos
|
||||
* @covers AbuseFilterParser::funcSetVar
|
||||
* @dataProvider twoParamsFuncs
|
||||
*/
|
||||
public function testNotEnoughArgsExceptionTwo( $func ) {
|
||||
$parser = self::getParser();
|
||||
// Nevermind if the argument can't be string since we check the amount
|
||||
// of parameters before anything else.
|
||||
$code = "$func('foo')";
|
||||
$length = strlen( $code );
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
"Not enough arguments to function $func called at character $length.\n" .
|
||||
'Expected 2 arguments, got 1'
|
||||
);
|
||||
$parser->parse( $code );
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
// Nevermind if the argument can't be string since we check the amount
|
||||
// of parameters before anything else.
|
||||
$code = "$func('foo')";
|
||||
$length = strlen( $code );
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
"Not enough arguments to function $func called at character $length.\n" .
|
||||
'Expected 2 arguments, got 1'
|
||||
);
|
||||
$parser->parse( $code );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -717,20 +571,19 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
* They should throw a 'notenoughargs' exception.
|
||||
*
|
||||
* @param string $func The function to test
|
||||
* @covers AbuseFilterParser::checkEnoughArguments
|
||||
* @covers AbuseFilterParser::funcStrReplace
|
||||
* @dataProvider threeParamsFuncs
|
||||
*/
|
||||
public function testNotEnoughArgsExceptionThree( $func ) {
|
||||
$parser = self::getParser();
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
"Not enough arguments to function $func called at character 25.\n" .
|
||||
'Expected 3 arguments, got 2'
|
||||
);
|
||||
// Nevermind if the argument can't be string since we check the amount
|
||||
// of parameters before anything else.
|
||||
$parser->parse( "$func('foo', 'bar')" );
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$this->setExpectedException(
|
||||
AFPUserVisibleException::class,
|
||||
"Not enough arguments to function $func called at character 25.\n" .
|
||||
'Expected 3 arguments, got 2'
|
||||
);
|
||||
// Nevermind if the argument can't be string since we check the amount
|
||||
// of parameters before anything else.
|
||||
$parser->parse( "$func('foo', 'bar')" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -757,24 +610,26 @@ class AbuseFilterParserTest extends MediaWikiTestCase {
|
|||
$loggerMock->setCollect( true );
|
||||
$this->setLogger( 'AbuseFilter', $loggerMock );
|
||||
|
||||
$parser = self::getParser();
|
||||
$actual = $parser->parse( "$old === $new" );
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$pname = get_class( $parser );
|
||||
$actual = $parser->parse( "$old === $new" );
|
||||
|
||||
$loggerBuffer = $loggerMock->getBuffer();
|
||||
// Check that the use has been logged
|
||||
$found = false;
|
||||
foreach ( $loggerBuffer as $entry ) {
|
||||
$check = preg_match( '/AbuseFilter: deprecated variable/', $entry[1] );
|
||||
if ( $check ) {
|
||||
$found = true;
|
||||
break;
|
||||
$loggerBuffer = $loggerMock->getBuffer();
|
||||
// Check that the use has been logged
|
||||
$found = false;
|
||||
foreach ( $loggerBuffer as $entry ) {
|
||||
$check = preg_match( '/AbuseFilter: deprecated variable/', $entry[1] );
|
||||
if ( $check ) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !$found ) {
|
||||
$this->fail( "The use of the deprecated variable $old was not logged. Parser: $pname" );
|
||||
}
|
||||
}
|
||||
if ( !$found ) {
|
||||
$this->fail( "The use of the deprecated variable $old was not logged." );
|
||||
}
|
||||
|
||||
$this->assertTrue( $actual );
|
||||
$this->assertTrue( $actual, "Parser: $pname" );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
75
tests/phpunit/AbuseFilterParserTestCase.php
Normal file
75
tests/phpunit/AbuseFilterParserTestCase.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*
|
||||
* @license GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helper for parser-related tests
|
||||
*/
|
||||
abstract class AbuseFilterParserTestCase extends MediaWikiTestCase {
|
||||
/**
|
||||
* @return AbuseFilterParser[]
|
||||
*/
|
||||
public static function getParsers() {
|
||||
static $parsers = null;
|
||||
if ( !$parsers ) {
|
||||
$parsers = [
|
||||
new AbuseFilterParser(),
|
||||
// The following line can be removed if some tests fail only due to CachingParser:
|
||||
// it is currently broken (T156095) and it could produce unwanted failures (T201193)
|
||||
// @ToDo: This comment can be safely removed (making sure that a CachingParser is
|
||||
// instantiated below) as T156095 will be resolved.
|
||||
new AbuseFilterCachingParser()
|
||||
];
|
||||
} else {
|
||||
// Reset so that already executed tests don't influence new ones
|
||||
$parsers[0]->resetState();
|
||||
$parsers[1]->resetState();
|
||||
}
|
||||
return $parsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base method for testing exceptions
|
||||
*
|
||||
* @param string $excep Identifier of the exception (e.g. 'unexpectedtoken')
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown, if available
|
||||
* The method may be different in Parser and CachingParser, but this parameter is
|
||||
* just used for debugging purposes.
|
||||
*/
|
||||
protected function exceptionTest( $excep, $expr, $caller ) {
|
||||
foreach ( self::getParsers() as $parser ) {
|
||||
$pname = get_class( $parser );
|
||||
try {
|
||||
$parser->parse( $expr );
|
||||
} catch ( AFPUserVisibleException $e ) {
|
||||
$this->assertEquals(
|
||||
$excep,
|
||||
$e->mExceptionID,
|
||||
"Exception $excep not thrown in $caller. Parser: $pname."
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->fail( "Exception $excep not thrown in $caller. Parser: $pname." );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,43 +30,7 @@
|
|||
* @covers AFPUserVisibleException
|
||||
* @covers AFPException
|
||||
*/
|
||||
class AbuseFilterTokenizerTest extends MediaWikiTestCase {
|
||||
/**
|
||||
* @return AbuseFilterParser
|
||||
*/
|
||||
public static function getParser() {
|
||||
static $parser = null;
|
||||
if ( !$parser ) {
|
||||
$parser = new AbuseFilterParser();
|
||||
} else {
|
||||
$parser->resetState();
|
||||
}
|
||||
return $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base method for testing exceptions
|
||||
*
|
||||
* @param string $excep Identifier of the exception (e.g. 'unexpectedtoken')
|
||||
* @param string $expr The expression to test
|
||||
* @param string $caller The function where the exception is thrown
|
||||
*/
|
||||
private function exceptionTest( $excep, $expr, $caller ) {
|
||||
$parser = self::getParser();
|
||||
try {
|
||||
$parser->parse( $expr );
|
||||
} catch ( AFPUserVisibleException $e ) {
|
||||
$this->assertEquals(
|
||||
$excep,
|
||||
$e->mExceptionID,
|
||||
"Exception $excep not thrown in AbuseFilterTokenizer::$caller"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fail( "Exception $excep not thrown in AbuseFilterTokenizer::$caller" );
|
||||
}
|
||||
|
||||
class AbuseFilterTokenizerTest extends AbuseFilterParserTestCase {
|
||||
/**
|
||||
* Test the 'unclosedcomment' exception
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue