mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/AbuseFilter.git
synced 2024-11-15 02:03:53 +00:00
b388dfab1b
No functional changes. * Don't include $code as part of the return value; it is ignored anyway. * Removed AbuseFilterParser::lastHandledToken and AFPParserState::lastInput, because AbuseFilterParser::nextToken() no longer calls itself recursively. * The regular expression that matches operators is no longer constructed dynamically, but hard-coded into the class. To make sure it does not drift apart from the more legible AbuseFilterParser::$mOps, add a unit test that constructs the regex dynamically as before and compares it to AbuseFilterParser::OPERATOR_RE. * AbuseFilterParser::RADIX_RE ditto. Change-Id: I9c23b60759ed2f4c73a9b480243b16bbce5a208f
100 lines
2.9 KiB
PHP
100 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* Tests for the AbuseFilter parser
|
|
*
|
|
* 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
|
|
*
|
|
* @group Test
|
|
* @group AbuseFilter
|
|
*
|
|
* @licence GNU GPL v2+
|
|
* @author Marius Hoch < hoo@online.de >
|
|
*/
|
|
class AbuseFilterParserTest extends MediaWikiTestCase {
|
|
/**
|
|
* @return AbuseFilterParser
|
|
*/
|
|
static function getParser() {
|
|
static $parser = null;
|
|
if ( !$parser ) {
|
|
$parser = new AbuseFilterParser();
|
|
}
|
|
return $parser;
|
|
}
|
|
|
|
/**
|
|
* @dataProvider readTests
|
|
*/
|
|
public function testParser( $testName, $rule, $expected ) {
|
|
if ( !class_exists( 'AntiSpoof' ) && preg_match( '/(cc)?norm\(/i', $rule ) ) {
|
|
// The norm and ccnorm parser functions aren't working correctly without AntiSpoof
|
|
$this->markTestSkipped( 'Parser test ' . $testName . ' requires the AntiSpoof extension' );
|
|
}
|
|
|
|
$parser = self::getParser();
|
|
$actual = $parser->parse( $rule );
|
|
$this->assertEquals( $expected, $actual, 'Running parser test ' . $testName );
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function readTests() {
|
|
$tests = array();
|
|
$testPath = __DIR__ . "/../parserTests";
|
|
$testFiles = glob( $testPath . "/*.t" );
|
|
|
|
foreach ( $testFiles as $testFile ) {
|
|
$testName = substr( $testFile, 0, -2 );
|
|
|
|
$resultFile = $testName . '.r';
|
|
$rule = trim( file_get_contents( $testFile ) );
|
|
$result = trim( file_get_contents( $resultFile ) ) == 'MATCH';
|
|
|
|
$tests[] = array(
|
|
basename( $testName ),
|
|
$rule,
|
|
$result
|
|
);
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
/**
|
|
* Ensure that AbsueFilterParser::OPERATOR_RE matches the contents
|
|
* and order of AbuseFilterParser::$mOps.
|
|
*/
|
|
public function testOperatorRe() {
|
|
$operatorRe = '/(' . implode( '|', array_map( function ( $op ) {
|
|
return preg_quote( $op, '/' );
|
|
}, AbuseFilterParser::$mOps ) ) . ')/A';
|
|
$this->assertEquals( $operatorRe, AbuseFilterParser::OPERATOR_RE );
|
|
}
|
|
|
|
/**
|
|
* Ensure that AbsueFilterParser::RADIX_RE matches the contents
|
|
* and order of AbuseFilterParser::$mBases.
|
|
*/
|
|
public function testRadixRe() {
|
|
$baseClass = implode( '', array_keys( AbuseFilterParser::$mBases ) );
|
|
$radixRe = "/([0-9A-Fa-f]+(?:\.\d*)?|\.\d+)([$baseClass])?/Au";
|
|
$this->assertEquals( $radixRe, AbuseFilterParser::RADIX_RE );
|
|
}
|
|
}
|