mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-14 09:44:44 +00:00
55ba083b13
This will decouple a bit the huge and chaotic tangle of AF classes. Some boilerplate code for AbuseFilter services is also added with this patch. Note that this requires injecting a KeywordsManager in AbuseFilterVariableHolder, or unit tests would fail. This is still incomplete, and the Manager is only injected in tests, because VariableHolder still has to be refactored. The test for the UpdateVarDumps script had to be updated, because serializing VHs in there was a bad choice. As pointed out in a comment, the test is likely going to break again once we remove the BC code, but I hope that we'll be able to remove the test at that point. Change-Id: I12a656a310adb8c5f75cab63f6db9e121e109717
130 lines
3.8 KiB
PHP
130 lines
3.8 KiB
PHP
<?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
|
|
*/
|
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
|
|
|
/**
|
|
* Tests that require Equivset, separated from the parser unit tests.
|
|
*
|
|
* @group Test
|
|
* @group AbuseFilter
|
|
* @group AbuseFilterParser
|
|
*
|
|
* @covers AbuseFilterCachingParser
|
|
* @covers AFPTreeParser
|
|
* @covers AFPTransitionBase
|
|
* @covers AFPTreeNode
|
|
* @covers AFPSyntaxTree
|
|
* @covers AFPParserState
|
|
* @covers AbuseFilterParser
|
|
* @covers AbuseFilterTokenizer
|
|
* @covers AFPToken
|
|
* @covers AFPData
|
|
*/
|
|
class AbuseFilterParserEquivsetTest extends MediaWikiIntegrationTestCase {
|
|
/**
|
|
* @see AbuseFilterParserTestCase::getParsers() - we cannot reuse that due to inheritance
|
|
* @return AbuseFilterParser[]
|
|
*/
|
|
protected function getParsers() {
|
|
static $parsers = null;
|
|
if ( !$parsers ) {
|
|
// We're not interested in caching or logging; tests should call respectively setCache
|
|
// and setLogger if they want to test any of those.
|
|
$contLang = new LanguageEn();
|
|
$cache = new EmptyBagOStuff();
|
|
$logger = new \Psr\Log\NullLogger();
|
|
$keywordsManager = AbuseFilterServices::getKeywordsManager();
|
|
|
|
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager );
|
|
$parser->toggleConditionLimit( false );
|
|
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager );
|
|
$cachingParser->toggleConditionLimit( false );
|
|
$parsers = [ $parser, $cachingParser ];
|
|
} else {
|
|
// Reset so that already executed tests don't influence new ones
|
|
$parsers[0]->resetState();
|
|
$parsers[0]->clearFuncCache();
|
|
$parsers[1]->resetState();
|
|
$parsers[1]->clearFuncCache();
|
|
}
|
|
return $parsers;
|
|
}
|
|
|
|
/**
|
|
* @param string $rule The rule to parse
|
|
* @dataProvider provideGenericTests
|
|
*/
|
|
public function testGeneric( $rule ) {
|
|
if ( !class_exists( 'Wikimedia\Equivset\Equivset' ) ) {
|
|
$this->markTestSkipped( 'Equivset is not installed' );
|
|
}
|
|
foreach ( $this->getParsers() as $parser ) {
|
|
$this->assertTrue( $parser->parse( $rule ), 'Parser used: ' . get_class( $parser ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return Generator|array
|
|
*/
|
|
public function provideGenericTests() {
|
|
$testPath = __DIR__ . "/../parserTestsEquivset";
|
|
$testFiles = glob( $testPath . "/*.t" );
|
|
|
|
foreach ( $testFiles as $testFile ) {
|
|
$testName = basename( substr( $testFile, 0, -2 ) );
|
|
$rule = trim( file_get_contents( $testFile ) );
|
|
|
|
yield $testName => [ $rule ];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param string $func
|
|
* @see AbuseFilterParserTest::testVariadicFuncsArbitraryArgsAllowed()
|
|
* @dataProvider variadicFuncs
|
|
*/
|
|
public function testVariadicFuncsArbitraryArgsAllowed( $func ) {
|
|
$argsList = str_repeat( ', "arg"', 50 );
|
|
$code = "$func( 'arg' $argsList )";
|
|
foreach ( $this->getParsers() as $parser ) {
|
|
$pname = get_class( $parser );
|
|
try {
|
|
$parser->parse( $code );
|
|
$this->assertTrue( true );
|
|
} catch ( AFPException $e ) {
|
|
$this->fail( "Got exception with parser $pname.\n$e" );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function variadicFuncs() {
|
|
return [
|
|
[ 'ccnorm_contains_any' ],
|
|
[ 'ccnorm_contains_all' ],
|
|
];
|
|
}
|
|
}
|