3 use MediaWiki\Block\BlockManager
;
4 use MediaWiki\Block\DatabaseBlock
;
9 * @coversDefaultClass \MediaWiki\Block\BlockManager
11 class BlockManagerTest
extends MediaWikiTestCase
{
19 protected function setUp() {
22 $this->user
= $this->getTestUser()->getUser();
23 $this->sysopId
= $this->getTestSysop()->getUser()->getId();
24 $this->blockManagerConfig
= [
25 'wgApplyIpBlocksToXff' => true,
26 'wgCookieSetOnAutoblock' => true,
27 'wgCookieSetOnIpBlock' => true,
28 'wgDnsBlacklistUrls' => [],
29 'wgEnableDnsBlacklist' => true,
31 'wgProxyWhitelist' => [],
32 'wgSoftBlockRanges' => [],
36 private function getBlockManager( $overrideConfig ) {
37 $blockManagerConfig = array_merge( $this->blockManagerConfig
, $overrideConfig );
38 return new BlockManager(
40 $this->user
->getRequest(),
41 ...array_values( $blockManagerConfig )
46 * @dataProvider provideGetBlockFromCookieValue
47 * @covers ::getBlockFromCookieValue
49 public function testGetBlockFromCookieValue( $options, $expected ) {
50 $blockManager = $this->getBlockManager( [
51 'wgCookieSetOnAutoblock' => true,
52 'wgCookieSetOnIpBlock' => true,
55 $block = new DatabaseBlock( array_merge( [
56 'address' => $options[ 'target' ] ?
: $this->user
,
57 'by' => $this->sysopId
,
58 ], $options[ 'blockOptions' ] ) );
61 $class = new ReflectionClass( BlockManager
::class );
62 $method = $class->getMethod( 'getBlockFromCookieValue' );
63 $method->setAccessible( true );
65 $user = $options[ 'loggedIn' ] ?
$this->user
: new User();
66 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
68 $this->assertSame( $expected, (bool)$method->invoke(
77 public static function provideGetBlockFromCookieValue() {
79 'Autoblocking user block' => [
84 'enableAutoblock' => true
89 'Non-autoblocking user block' => [
97 'IP block for anonymous user' => [
99 'target' => '127.0.0.1',
101 'blockOptions' => [],
105 'IP block for logged in user' => [
107 'target' => '127.0.0.1',
109 'blockOptions' => [],
113 'IP range block for anonymous user' => [
115 'target' => '127.0.0.0/8',
117 'blockOptions' => [],
125 * @dataProvider provideIsLocallyBlockedProxy
126 * @covers ::isLocallyBlockedProxy
128 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
129 $class = new ReflectionClass( BlockManager
::class );
130 $method = $class->getMethod( 'isLocallyBlockedProxy' );
131 $method->setAccessible( true );
133 $blockManager = $this->getBlockManager( [
134 'wgProxyList' => $proxyList
138 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
141 public static function provideIsLocallyBlockedProxy() {
143 'Proxy list is empty' => [ [], false ],
144 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
145 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
146 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
151 * @covers ::isLocallyBlockedProxy
153 public function testIsLocallyBlockedProxyDeprecated() {
156 $this->hideDeprecated(
157 'IP addresses in the keys of $wgProxyList (found the following IP ' .
158 'addresses in keys: ' . $proxy . ', please move them to values)'
161 $class = new ReflectionClass( BlockManager
::class );
162 $method = $class->getMethod( 'isLocallyBlockedProxy' );
163 $method->setAccessible( true );
165 $blockManager = $this->getBlockManager( [
166 'wgProxyList' => [ $proxy => 'test' ]
170 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
174 * @dataProvider provideIsDnsBlacklisted
175 * @covers ::isDnsBlacklisted
176 * @covers ::inDnsBlacklist
178 public function testIsDnsBlacklisted( $options, $expected ) {
179 $blockManagerConfig = array_merge( $this->blockManagerConfig
, [
180 'wgEnableDnsBlacklist' => true,
181 'wgDnsBlacklistUrls' => $options['blacklist'],
182 'wgProxyWhitelist' => $options['whitelist'],
185 $blockManager = $this->getMockBuilder( BlockManager
::class )
186 ->setConstructorArgs(
189 $this->user
->getRequest(),
190 ], $blockManagerConfig ) )
191 ->setMethods( [ 'checkHost' ] )
194 $blockManager->expects( $this->any() )
195 ->method( 'checkHost' )
196 ->will( $this->returnValueMap( [ [
197 $options['dnsblQuery'],
198 $options['dnsblResponse'],
203 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
207 public static function provideIsDnsBlacklisted() {
208 $dnsblFound = [ '127.0.0.2' ];
209 $dnsblNotFound = false;
211 'IP is blacklisted' => [
213 'blacklist' => [ 'dnsbl.test' ],
215 'dnsblQuery' => '1.0.0.127.dnsbl.test',
216 'dnsblResponse' => $dnsblFound,
218 'checkWhitelist' => false,
222 'IP is blacklisted; blacklist has key' => [
224 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
226 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
227 'dnsblResponse' => $dnsblFound,
229 'checkWhitelist' => false,
233 'IP is blacklisted; blacklist is array' => [
235 'blacklist' => [ [ 'dnsbl.test' ] ],
237 'dnsblQuery' => '1.0.0.127.dnsbl.test',
238 'dnsblResponse' => $dnsblFound,
240 'checkWhitelist' => false,
244 'IP is not blacklisted' => [
246 'blacklist' => [ 'dnsbl.test' ],
248 'dnsblQuery' => '4.3.2.1.dnsbl.test',
249 'dnsblResponse' => $dnsblNotFound,
251 'checkWhitelist' => false,
255 'Blacklist is empty' => [
259 'dnsblQuery' => '1.0.0.127.dnsbl.test',
260 'dnsblResponse' => $dnsblFound,
262 'checkWhitelist' => false,
266 'IP is blacklisted and whitelisted; whitelist is not checked' => [
268 'blacklist' => [ 'dnsbl.test' ],
270 'dnsblQuery' => '1.0.0.127.dnsbl.test',
271 'dnsblResponse' => $dnsblFound,
272 'whitelist' => [ '127.0.0.1' ],
273 'checkWhitelist' => false,
277 'IP is blacklisted and whitelisted; whitelist is checked' => [
279 'blacklist' => [ 'dnsbl.test' ],
281 'dnsblQuery' => '1.0.0.127.dnsbl.test',
282 'dnsblResponse' => $dnsblFound,
283 'whitelist' => [ '127.0.0.1' ],
284 'checkWhitelist' => true,