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
|
<?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\Hooks\AbuseFilterHookRunner;
use MediaWiki\Extension\AbuseFilter\KeywordsManager;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Helper for parser-related tests
*/
abstract class AbuseFilterParserTestCase extends MediaWikiUnitTestCase {
/**
* @return AbuseFilterParser[]
*/
protected function getParsers() {
// We're not interested in caching or logging; tests should call respectively setCache
// and setLogger if they want to test any of those.
$contLang = $this->getLanguageMock();
$cache = new EmptyBagOStuff();
$logger = new \Psr\Log\NullLogger();
$keywordsManager = new KeywordsManager( $this->createMock( AbuseFilterHookRunner::class ) );
$parser = new AbuseFilterParser( $contLang, $cache, $logger, $keywordsManager );
$parser->toggleConditionLimit( false );
$cachingParser = new AbuseFilterCachingParser( $contLang, $cache, $logger, $keywordsManager );
$cachingParser->toggleConditionLimit( false );
return [ $parser, $cachingParser ];
}
/**
* @param string $excep
* @param string $expr
* @param string $caller
* @param bool $skippedBlock Whether we're testing code inside a short-circuited block
*/
private function exceptionTestInternal( $excep, $expr, $caller, $skippedBlock ) {
foreach ( $this->getParsers() as $parser ) {
$pname = get_class( $parser );
$msg = "Exception $excep not thrown in $caller";
if ( $skippedBlock ) {
$msg .= " inside a short-circuited block";
}
$msg .= ". Parser: $pname.";
try {
if ( $skippedBlock ) {
// Skipped blocks are, well, skipped when actually parsing.
$parser->checkSyntaxThrow( $expr );
} else {
$parser->parse( $expr );
}
} catch ( AFPUserVisibleException $e ) {
$this->assertEquals( $excep, $e->mExceptionID, $msg . " Got instead:\n$e" );
continue;
}
$this->fail( $msg );
}
}
/**
* Base method for testing exceptions
*
* @param string $excep Identifier of the exception (e.g. 'unexpectedtoken')
* @param string $expr The expression to test
* @param string $caller The function where the exception is thrown, if available
* The method may be different in Parser and CachingParser, but this parameter is
* just used for debugging purposes.
*/
protected function exceptionTest( $excep, $expr, $caller ) {
$this->exceptionTestInternal( $excep, $expr, $caller, false );
}
/**
* Same as self::exceptionTest, but wraps the given code in a block that will be short-circuited.
* Note that this is executed using Parser::checkSyntax, as errors inside a skipped branch won't
* ever be reported at runtime.
*
* @param string $excep
* @param string $expr
* @param string $caller
*/
protected function exceptionTestInSkippedBlock( $excep, $expr, $caller ) {
$expr = "false & ( $expr )";
$this->exceptionTestInternal( $excep, $expr, $caller, true );
}
/**
* Get a mock of LanguageEn with only the methods we need in the parser
*
* @return Language|MockObject
*/
protected function getLanguageMock() {
$lang = $this->getMockBuilder( LanguageEn::class )
->disableOriginalConstructor()
->getMock();
$lang->expects( $this->any() )
->method( 'uc' )
->willReturnCallback( function ( $x ) {
return mb_strtoupper( $x );
} );
$lang->expects( $this->any() )
->method( 'lc' )
->willReturnCallback( function ( $x ) {
return mb_strtolower( $x );
} );
return $lang;
}
}
|