summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/AbuseFilter/tests/phpunit/AbuseFilterParserTest.php
blob: d7560e92d8a38486a33a8e3595d3bf47f42d0fd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?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
 *
 * @license GPL-2.0-or-later
 * @author Marius Hoch < hoo@online.de >
 */

/**
 * @group Test
 * @group AbuseFilter
 *
 * @covers AbuseFilterCachingParser
 * @covers AbuseFilterParser
 * @covers AbuseFilterTokenizer
 */
class AbuseFilterParserTest extends MediaWikiTestCase {
	/**
	 * @return AbuseFilterParser
	 */
	static function getParser() {
		static $parser = null;
		if ( !$parser ) {
			$parser = new AbuseFilterParser();
		}
		return $parser;
	}

	/**
	 * @return AbuseFilterParser[]
	 */
	static function getParsers() {
		static $parsers = null;
		if ( !$parsers ) {
			$parsers = [
				new AbuseFilterParser(),
				new AbuseFilterCachingParser()
			];
		}
		return $parsers;
	}

	/**
	 * @dataProvider readTests
	 */
	public function testParser( $testName, $rule, $expected ) {
		foreach ( self::getParsers() as $parser ) {
			$actual = $parser->parse( $rule );
			$this->assertEquals( $expected, $actual, 'Running parser test ' . $testName );
		}
	}

	/**
	 * @return array
	 */
	public function readTests() {
		$tests = [];
		$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[] = [
				basename( $testName ),
				$rule,
				$result
			];
		}

		return $tests;
	}

	/**
	 * Ensure that AbuseFilterTokenizer::OPERATOR_RE matches the contents
	 * and order of AbuseFilterTokenizer::$operators.
	 */
	public function testOperatorRe() {
		$operatorRe = '/(' . implode( '|', array_map( function ( $op ) {
			return preg_quote( $op, '/' );
		}, AbuseFilterTokenizer::$operators ) ) . ')/A';
		$this->assertEquals( $operatorRe, AbuseFilterTokenizer::OPERATOR_RE );
	}

	/**
	 * Ensure that AbuseFilterTokenizer::RADIX_RE matches the contents
	 * and order of AbuseFilterTokenizer::$bases.
	 */
	public function testRadixRe() {
		$baseClass = implode( '', array_keys( AbuseFilterTokenizer::$bases ) );
		$radixRe = "/([0-9A-Fa-f]+(?:\.\d*)?|\.\d+)([$baseClass])?/Au";
		$this->assertEquals( $radixRe, AbuseFilterTokenizer::RADIX_RE );
	}

	/**
	 * Ensure the number of conditions counted for given expressions is right.
	 *
	 * @dataProvider condCountCases
	 */
	public function testCondCount( $rule, $expected ) {
		$parser = self::getParser();
		// Set some variables for convenience writing test cases
		$parser->setVars( array_combine( range( 'a', 'f' ), range( 'a', 'f' ) ) );
		$countBefore = AbuseFilter::$condCount;
		$parser->parse( $rule );
		$countAfter = AbuseFilter::$condCount;
		$actual = $countAfter - $countBefore;
		$this->assertEquals( $expected, $actual, 'Condition count for ' . $rule );
	}

	/**
	 * Data provider for testCondCount method.
	 * @return array
	 */
	public function condCountCases() {
		return [
			[ '(((a == b)))', 1 ],
			[ 'contains_any(a, b, c)', 1 ],
			[ 'a == b == c', 2 ],
			[ 'a in b + c in d + e in f', 3 ],
			[ 'true', 0 ],
			[ 'a == a | c == d', 1 ],
			[ 'a == b & c == d', 1 ],
		];
	}

	/**
	 * get_matches should throw an exception with an invalid number of arguments.
	 * @expectedException AFPUserVisibleException
	 * @covers AbuseFilterParser::funcGetMatches
	 */
	public function testGetMatchesInvalidArgs() {
		$parser = self::getParser();
		$parser->parse( "get_matches('')" );
	}

	/**
	 * get_matches should throw an exception when given an invalid regular expression.
	 * @expectedException AFPUserVisibleException
	 * @covers AbuseFilterParser::funcGetMatches
	 */
	public function testGetMatchesInvalidRegex() {
		$parser = self::getParser();
		$parser->parse( "get_matches('this (should fail')" );
	}

	/**
	 * Ensure get_matches function captures returns expected output.
	 * @param string $needle Regex to pass to get_matches.
	 * @param string $haystack String to run regex against.
	 * @param string[] $expected The expected values of the matched groups.
	 * @covers AbuseFilterParser::funcGetMatches
	 * @dataProvider getMatchesCases
	 */
	public function testGetMatches( $needle, $haystack, $expected ) {
		$parser = self::getParser();
		$afpData = $parser->intEval( "get_matches('$needle', '$haystack')" )->data;

		// Extract matches from AFPData.
		$matches = array_map( function ( $afpDatum ) {
			return $afpDatum->data;
		}, $afpData );

		$this->assertEquals( $expected, $matches );
	}

	/**
	 * Data provider for get_matches method.
	 * @return array
	 */
	public function getMatchesCases() {
		return [
			[
				'You say (.*) \(and I say (.*)\)\.',
				'You say hello (and I say goodbye).',
				[
					'You say hello (and I say goodbye).',
					'hello',
					'goodbye',
				],
			],
			[
				'I(?: am)? the ((walrus|egg man).*)\!',
				'I am the egg man, I am the walrus !',
				[
					'I am the egg man, I am the walrus !',
					'egg man, I am the walrus ',
					'egg man',
				],
			],
			[
				'this (does) not match',
				'foo bar',
				[
					false,
					false,
				],
			],
		];
	}
}