diff --git a/includes/Parser/FilterEvaluator.php b/includes/Parser/FilterEvaluator.php index dec3adc13..32e54cda3 100644 --- a/includes/Parser/FilterEvaluator.php +++ b/includes/Parser/FilterEvaluator.php @@ -450,17 +450,10 @@ class FilterEvaluator { * and saved to self::usedVars to be returned to the caller in this function. * * @param string $filter - * @return array + * @return string[] */ public function getUsedVars( string $filter ): array { - $this->resetState(); - $cachedAllowShort = $this->mAllowShort; - try { - $this->mAllowShort = false; - $this->evaluateExpression( $filter ); - } finally { - $this->mAllowShort = $cachedAllowShort; - } + $this->checkSyntax( $filter ); return array_unique( $this->usedVars ); } diff --git a/tests/phpunit/unit/Parser/ParserTest.php b/tests/phpunit/unit/Parser/ParserTest.php index f7ed30e99..fd99e9a3c 100644 --- a/tests/phpunit/unit/Parser/ParserTest.php +++ b/tests/phpunit/unit/Parser/ParserTest.php @@ -1163,20 +1163,19 @@ class ParserTest extends ParserTestCase { $this->assertSame( $vars, $wrapper->mVariables ); } + public static function provideGetUsedVars() { + yield [ [ 'action', 'user_name' ], 'action = "edit" & ip_in_range( user_name, "1.2.3.4" )' ]; + yield [ [ 'action', 'user_name' ], 'action = "edit" & ip_in_range( USER_NAME, "1.2.3.4" )' ]; + yield [ [ 'added_lines' ], "added_lines[0] !== ''" ]; + yield [ [ 'old_size', 'new_size' ], 'old_size > 0 & new_size / old_size < 0.5' ]; + } + /** * @covers \MediaWiki\Extension\AbuseFilter\Parser\FilterEvaluator + * @dataProvider provideGetUsedVars */ - public function testGetUsedVars() { + public function testGetUsedVars( $expected, $conditions ) { $parser = $this->getParser(); - /** @var FilterEvaluator $wrapper */ - $wrapper = TestingAccessWrapper::newFromObject( $parser ); - $this->assertSame( - [ 'action', 'user_name' ], - $wrapper->getUsedVars( 'action = "edit" & ip_in_range( user_name, "1.2.3.4" )' ) - ); - $this->assertSame( - [ 'action', 'user_name' ], - $wrapper->getUsedVars( 'action = "edit" & ip_in_range( USER_NAME, "1.2.3.4" )' ) - ); + $this->assertSame( $expected, $parser->getUsedVars( $conditions ) ); } }