From bf180e0490f0c72dfda4c2e2a445f9dae9122678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= Date: Fri, 5 Jul 2024 11:30:33 +0200 Subject: [PATCH] Simplify FilterEvaluator::getUsedVars using ::checkSyntax Alternative approach to fixing the regression proposed by Daimona in I78d3a2cd7bada962d7ef9b0f2c39d898bf8987ce. Bug: T368203 Change-Id: I637367c3b3850f7988d890379fef7f4753159953 --- includes/Parser/FilterEvaluator.php | 11 ++--------- tests/phpunit/unit/Parser/ParserTest.php | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 20 deletions(-) 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 ) ); } }