Merge "Simplify FilterEvaluator::getUsedVars using ::checkSyntax"

This commit is contained in:
jenkins-bot 2024-07-08 12:42:18 +00:00 committed by Gerrit Code Review
commit 98eab47d9b
2 changed files with 12 additions and 20 deletions

View file

@ -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 );
}

View file

@ -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 ) );
}
}