3 use MediaWiki\Block\BlockManager
;
4 use MediaWiki\Block\DatabaseBlock
;
5 use MediaWiki\Block\SystemBlock
;
10 * @coversDefaultClass \MediaWiki\Block\BlockManager
12 class BlockManagerTest
extends MediaWikiTestCase
{
20 protected function setUp() {
23 $this->user
= $this->getTestUser()->getUser();
24 $this->sysopId
= $this->getTestSysop()->getUser()->getId();
25 $this->blockManagerConfig
= [
26 'wgApplyIpBlocksToXff' => true,
27 'wgCookieSetOnAutoblock' => true,
28 'wgCookieSetOnIpBlock' => true,
29 'wgDnsBlacklistUrls' => [],
30 'wgEnableDnsBlacklist' => true,
32 'wgProxyWhitelist' => [],
33 'wgSecretKey' => false,
34 'wgSoftBlockRanges' => [],
38 private function getBlockManager( $overrideConfig ) {
39 $blockManagerConfig = array_merge( $this->blockManagerConfig
, $overrideConfig );
40 return new BlockManager(
42 $this->user
->getRequest(),
43 ...array_values( $blockManagerConfig )
48 * @dataProvider provideGetBlockFromCookieValue
49 * @covers ::getBlockFromCookieValue
50 * @covers ::shouldApplyCookieBlock
52 public function testGetBlockFromCookieValue( $options, $expected ) {
53 $blockManager = $this->getBlockManager( [
54 'wgCookieSetOnAutoblock' => true,
55 'wgCookieSetOnIpBlock' => true,
58 $block = new DatabaseBlock( array_merge( [
59 'address' => $options[ 'target' ] ?
: $this->user
,
60 'by' => $this->sysopId
,
61 ], $options[ 'blockOptions' ] ) );
64 $class = new ReflectionClass( BlockManager
::class );
65 $method = $class->getMethod( 'getBlockFromCookieValue' );
66 $method->setAccessible( true );
68 $user = $options[ 'loggedIn' ] ?
$this->user
: new User();
69 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
71 $this->assertSame( $expected, (bool)$method->invoke(
80 public static function provideGetBlockFromCookieValue() {
82 'Autoblocking user block' => [
87 'enableAutoblock' => true
92 'Non-autoblocking user block' => [
100 'IP block for anonymous user' => [
102 'target' => '127.0.0.1',
104 'blockOptions' => [],
108 'IP block for logged in user' => [
110 'target' => '127.0.0.1',
112 'blockOptions' => [],
116 'IP range block for anonymous user' => [
118 'target' => '127.0.0.0/8',
120 'blockOptions' => [],
128 * @dataProvider provideIsLocallyBlockedProxy
129 * @covers ::isLocallyBlockedProxy
131 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
132 $class = new ReflectionClass( BlockManager
::class );
133 $method = $class->getMethod( 'isLocallyBlockedProxy' );
134 $method->setAccessible( true );
136 $blockManager = $this->getBlockManager( [
137 'wgProxyList' => $proxyList
141 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
144 public static function provideIsLocallyBlockedProxy() {
146 'Proxy list is empty' => [ [], false ],
147 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
148 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
149 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
154 * @covers ::isLocallyBlockedProxy
156 public function testIsLocallyBlockedProxyDeprecated() {
159 $this->hideDeprecated(
160 'IP addresses in the keys of $wgProxyList (found the following IP ' .
161 'addresses in keys: ' . $proxy . ', please move them to values)'
164 $class = new ReflectionClass( BlockManager
::class );
165 $method = $class->getMethod( 'isLocallyBlockedProxy' );
166 $method->setAccessible( true );
168 $blockManager = $this->getBlockManager( [
169 'wgProxyList' => [ $proxy => 'test' ]
173 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
177 * @dataProvider provideIsDnsBlacklisted
178 * @covers ::isDnsBlacklisted
179 * @covers ::inDnsBlacklist
181 public function testIsDnsBlacklisted( $options, $expected ) {
182 $blockManagerConfig = array_merge( $this->blockManagerConfig
, [
183 'wgEnableDnsBlacklist' => true,
184 'wgDnsBlacklistUrls' => $options['blacklist'],
185 'wgProxyWhitelist' => $options['whitelist'],
188 $blockManager = $this->getMockBuilder( BlockManager
::class )
189 ->setConstructorArgs(
192 $this->user
->getRequest(),
193 ], $blockManagerConfig ) )
194 ->setMethods( [ 'checkHost' ] )
197 $blockManager->expects( $this->any() )
198 ->method( 'checkHost' )
199 ->will( $this->returnValueMap( [ [
200 $options['dnsblQuery'],
201 $options['dnsblResponse'],
206 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
210 public static function provideIsDnsBlacklisted() {
211 $dnsblFound = [ '127.0.0.2' ];
212 $dnsblNotFound = false;
214 'IP is blacklisted' => [
216 'blacklist' => [ 'dnsbl.test' ],
218 'dnsblQuery' => '1.0.0.127.dnsbl.test',
219 'dnsblResponse' => $dnsblFound,
221 'checkWhitelist' => false,
225 'IP is blacklisted; blacklist has key' => [
227 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
229 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
230 'dnsblResponse' => $dnsblFound,
232 'checkWhitelist' => false,
236 'IP is blacklisted; blacklist is array' => [
238 'blacklist' => [ [ 'dnsbl.test' ] ],
240 'dnsblQuery' => '1.0.0.127.dnsbl.test',
241 'dnsblResponse' => $dnsblFound,
243 'checkWhitelist' => false,
247 'IP is not blacklisted' => [
249 'blacklist' => [ 'dnsbl.test' ],
251 'dnsblQuery' => '4.3.2.1.dnsbl.test',
252 'dnsblResponse' => $dnsblNotFound,
254 'checkWhitelist' => false,
258 'Blacklist is empty' => [
262 'dnsblQuery' => '1.0.0.127.dnsbl.test',
263 'dnsblResponse' => $dnsblFound,
265 'checkWhitelist' => false,
269 'IP is blacklisted and whitelisted; whitelist is not checked' => [
271 'blacklist' => [ 'dnsbl.test' ],
273 'dnsblQuery' => '1.0.0.127.dnsbl.test',
274 'dnsblResponse' => $dnsblFound,
275 'whitelist' => [ '127.0.0.1' ],
276 'checkWhitelist' => false,
280 'IP is blacklisted and whitelisted; whitelist is checked' => [
282 'blacklist' => [ 'dnsbl.test' ],
284 'dnsblQuery' => '1.0.0.127.dnsbl.test',
285 'dnsblResponse' => $dnsblFound,
286 'whitelist' => [ '127.0.0.1' ],
287 'checkWhitelist' => true,
295 * @covers ::getUniqueBlocks
297 public function testGetUniqueBlocks() {
300 $class = new ReflectionClass( BlockManager
::class );
301 $method = $class->getMethod( 'getUniqueBlocks' );
302 $method->setAccessible( true );
304 $blockManager = $this->getBlockManager( [] );
306 $block = $this->getMockBuilder( DatabaseBlock
::class )
307 ->setMethods( [ 'getId' ] )
309 $block->expects( $this->any() )
311 ->willReturn( $blockId );
313 $autoblock = $this->getMockBuilder( DatabaseBlock
::class )
314 ->setMethods( [ 'getParentBlockId', 'getType' ] )
316 $autoblock->expects( $this->any() )
317 ->method( 'getParentBlockId' )
318 ->willReturn( $blockId );
319 $autoblock->expects( $this->any() )
320 ->method( 'getType' )
321 ->willReturn( DatabaseBlock
::TYPE_AUTO
);
323 $blocks = [ $block, $block, $autoblock, new SystemBlock() ];
325 $this->assertSame( 2, count( $method->invoke( $blockManager, $blocks ) ) );
329 * @covers ::trackBlockWithCookie
330 * @dataProvider provideTrackBlockWithCookie
331 * @param bool $expectCookieSet
332 * @param bool $hasCookie
333 * @param bool $isBlocked
335 public function testTrackBlockWithCookie( $expectCookieSet, $hasCookie, $isBlocked ) {
337 $this->setMwGlobals( 'wgCookiePrefix', '' );
339 $request = new FauxRequest();
341 $request->setCookie( 'BlockID', 'the value does not matter' );
345 $block = $this->getMockBuilder( DatabaseBlock
::class )
346 ->setMethods( [ 'getType', 'getId' ] )
348 $block->method( 'getType' )
349 ->willReturn( DatabaseBlock
::TYPE_IP
);
350 $block->method( 'getId' )
351 ->willReturn( $blockID );
356 $user = $this->getMockBuilder( User
::class )
357 ->setMethods( [ 'getBlock', 'getRequest' ] )
359 $user->method( 'getBlock' )
360 ->willReturn( $block );
361 $user->method( 'getRequest' )
362 ->willReturn( $request );
363 /** @var User $user */
365 // Although the block cookie is set via DeferredUpdates, in command line mode updates are
366 // processed immediately
367 $blockManager = $this->getBlockManager( [] );
368 $blockManager->trackBlockWithCookie( $user );
370 /** @var FauxResponse $response */
371 $response = $request->response();
372 $this->assertCount( $expectCookieSet ?
1 : 0, $response->getCookies() );
373 $this->assertEquals( $expectCookieSet ?
$blockID : null, $response->getCookie( 'BlockID' ) );
376 public function provideTrackBlockWithCookie() {
378 // $expectCookieSet, $hasCookie, $isBlocked
379 [ false, false, false ],
380 [ false, true, false ],
381 [ true, false, true ],
382 [ false, true, true ],