3 use MediaWiki\Block\BlockManager
;
8 * @coversDefaultClass \MediaWiki\Block\BlockManager
10 class BlockManagerTest
extends MediaWikiTestCase
{
18 protected function setUp() {
21 $this->user
= $this->getTestUser()->getUser();
22 $this->sysopId
= $this->getTestSysop()->getUser()->getId();
23 $this->blockManagerConfig
= [
24 'wgApplyIpBlocksToXff' => true,
25 'wgCookieSetOnAutoblock' => true,
26 'wgCookieSetOnIpBlock' => true,
27 'wgDnsBlacklistUrls' => [],
28 'wgEnableDnsBlacklist' => true,
30 'wgProxyWhitelist' => [],
31 'wgSoftBlockRanges' => [],
35 private function getBlockManager( $overrideConfig ) {
36 $blockManagerConfig = array_merge( $this->blockManagerConfig
, $overrideConfig );
37 return new BlockManager(
39 $this->user
->getRequest(),
40 ...array_values( $blockManagerConfig )
45 * @dataProvider provideGetBlockFromCookieValue
46 * @covers ::getBlockFromCookieValue
48 public function testGetBlockFromCookieValue( $options, $expected ) {
49 $blockManager = $this->getBlockManager( [
50 'wgCookieSetOnAutoblock' => true,
51 'wgCookieSetOnIpBlock' => true,
54 $block = new Block( array_merge( [
55 'address' => $options[ 'target' ] ?
: $this->user
,
56 'by' => $this->sysopId
,
57 ], $options[ 'blockOptions' ] ) );
60 $class = new ReflectionClass( BlockManager
::class );
61 $method = $class->getMethod( 'getBlockFromCookieValue' );
62 $method->setAccessible( true );
64 $user = $options[ 'loggedIn' ] ?
$this->user
: new User();
65 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
67 $this->assertSame( $expected, (bool)$method->invoke(
76 public static function provideGetBlockFromCookieValue() {
78 'Autoblocking user block' => [
83 'enableAutoblock' => true
88 'Non-autoblocking user block' => [
96 'IP block for anonymous user' => [
98 'target' => '127.0.0.1',
100 'blockOptions' => [],
104 'IP block for logged in user' => [
106 'target' => '127.0.0.1',
108 'blockOptions' => [],
112 'IP range block for anonymous user' => [
114 'target' => '127.0.0.0/8',
116 'blockOptions' => [],
124 * @dataProvider provideIsLocallyBlockedProxy
125 * @covers ::isLocallyBlockedProxy
127 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
128 $class = new ReflectionClass( BlockManager
::class );
129 $method = $class->getMethod( 'isLocallyBlockedProxy' );
130 $method->setAccessible( true );
132 $blockManager = $this->getBlockManager( [
133 'wgProxyList' => $proxyList
137 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
140 public static function provideIsLocallyBlockedProxy() {
142 'Proxy list is empty' => [ [], false ],
143 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
144 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
145 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
150 * @covers ::isLocallyBlockedProxy
152 public function testIsLocallyBlockedProxyDeprecated() {
155 $this->hideDeprecated(
156 'IP addresses in the keys of $wgProxyList (found the following IP ' .
157 'addresses in keys: ' . $proxy . ', please move them to values)'
160 $class = new ReflectionClass( BlockManager
::class );
161 $method = $class->getMethod( 'isLocallyBlockedProxy' );
162 $method->setAccessible( true );
164 $blockManager = $this->getBlockManager( [
165 'wgProxyList' => [ $proxy => 'test' ]
169 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
173 * @dataProvider provideIsDnsBlacklisted
174 * @covers ::isDnsBlacklisted
175 * @covers ::inDnsBlacklist
177 public function testIsDnsBlacklisted( $options, $expected ) {
178 $blockManagerConfig = array_merge( $this->blockManagerConfig
, [
179 'wgEnableDnsBlacklist' => true,
180 'wgDnsBlacklistUrls' => $options['blacklist'],
181 'wgProxyWhitelist' => $options['whitelist'],
184 $blockManager = $this->getMockBuilder( BlockManager
::class )
185 ->setConstructorArgs(
188 $this->user
->getRequest(),
189 ], $blockManagerConfig ) )
190 ->setMethods( [ 'checkHost' ] )
193 $blockManager->expects( $this->any() )
194 ->method( 'checkHost' )
195 ->will( $this->returnValueMap( [ [
196 $options['dnsblQuery'],
197 $options['dnsblResponse'],
202 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
206 public static function provideIsDnsBlacklisted() {
207 $dnsblFound = [ '127.0.0.2' ];
208 $dnsblNotFound = false;
210 'IP is blacklisted' => [
212 'blacklist' => [ 'dnsbl.test' ],
214 'dnsblQuery' => '1.0.0.127.dnsbl.test',
215 'dnsblResponse' => $dnsblFound,
217 'checkWhitelist' => false,
221 'IP is blacklisted; blacklist has key' => [
223 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
225 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
226 'dnsblResponse' => $dnsblFound,
228 'checkWhitelist' => false,
232 'IP is blacklisted; blacklist is array' => [
234 'blacklist' => [ [ 'dnsbl.test' ] ],
236 'dnsblQuery' => '1.0.0.127.dnsbl.test',
237 'dnsblResponse' => $dnsblFound,
239 'checkWhitelist' => false,
243 'IP is not blacklisted' => [
245 'blacklist' => [ 'dnsbl.test' ],
247 'dnsblQuery' => '4.3.2.1.dnsbl.test',
248 'dnsblResponse' => $dnsblNotFound,
250 'checkWhitelist' => false,
254 'Blacklist is empty' => [
258 'dnsblQuery' => '1.0.0.127.dnsbl.test',
259 'dnsblResponse' => $dnsblFound,
261 'checkWhitelist' => false,
265 'IP is blacklisted and whitelisted; whitelist is not checked' => [
267 'blacklist' => [ 'dnsbl.test' ],
269 'dnsblQuery' => '1.0.0.127.dnsbl.test',
270 'dnsblResponse' => $dnsblFound,
271 'whitelist' => [ '127.0.0.1' ],
272 'checkWhitelist' => false,
276 'IP is blacklisted and whitelisted; whitelist is checked' => [
278 'blacklist' => [ 'dnsbl.test' ],
280 'dnsblQuery' => '1.0.0.127.dnsbl.test',
281 'dnsblResponse' => $dnsblFound,
282 'whitelist' => [ '127.0.0.1' ],
283 'checkWhitelist' => true,