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 'wgSecretKey' => false,
33 'wgSoftBlockRanges' => [],
37 private function getBlockManager( $overrideConfig ) {
38 $blockManagerConfig = array_merge( $this->blockManagerConfig
, $overrideConfig );
39 return new BlockManager(
41 $this->user
->getRequest(),
42 ...array_values( $blockManagerConfig )
47 * @dataProvider provideGetBlockFromCookieValue
48 * @covers ::getBlockFromCookieValue
50 public function testGetBlockFromCookieValue( $options, $expected ) {
51 $blockManager = $this->getBlockManager( [
52 'wgCookieSetOnAutoblock' => true,
53 'wgCookieSetOnIpBlock' => true,
56 $block = new DatabaseBlock( array_merge( [
57 'address' => $options[ 'target' ] ?
: $this->user
,
58 'by' => $this->sysopId
,
59 ], $options[ 'blockOptions' ] ) );
62 $class = new ReflectionClass( BlockManager
::class );
63 $method = $class->getMethod( 'getBlockFromCookieValue' );
64 $method->setAccessible( true );
66 $user = $options[ 'loggedIn' ] ?
$this->user
: new User();
67 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
69 $this->assertSame( $expected, (bool)$method->invoke(
78 public static function provideGetBlockFromCookieValue() {
80 'Autoblocking user block' => [
85 'enableAutoblock' => true
90 'Non-autoblocking user block' => [
98 'IP block for anonymous user' => [
100 'target' => '127.0.0.1',
102 'blockOptions' => [],
106 'IP block for logged in user' => [
108 'target' => '127.0.0.1',
110 'blockOptions' => [],
114 'IP range block for anonymous user' => [
116 'target' => '127.0.0.0/8',
118 'blockOptions' => [],
126 * @dataProvider provideIsLocallyBlockedProxy
127 * @covers ::isLocallyBlockedProxy
129 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
130 $class = new ReflectionClass( BlockManager
::class );
131 $method = $class->getMethod( 'isLocallyBlockedProxy' );
132 $method->setAccessible( true );
134 $blockManager = $this->getBlockManager( [
135 'wgProxyList' => $proxyList
139 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
142 public static function provideIsLocallyBlockedProxy() {
144 'Proxy list is empty' => [ [], false ],
145 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
146 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
147 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
152 * @covers ::isLocallyBlockedProxy
154 public function testIsLocallyBlockedProxyDeprecated() {
157 $this->hideDeprecated(
158 'IP addresses in the keys of $wgProxyList (found the following IP ' .
159 'addresses in keys: ' . $proxy . ', please move them to values)'
162 $class = new ReflectionClass( BlockManager
::class );
163 $method = $class->getMethod( 'isLocallyBlockedProxy' );
164 $method->setAccessible( true );
166 $blockManager = $this->getBlockManager( [
167 'wgProxyList' => [ $proxy => 'test' ]
171 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
175 * @dataProvider provideIsDnsBlacklisted
176 * @covers ::isDnsBlacklisted
177 * @covers ::inDnsBlacklist
179 public function testIsDnsBlacklisted( $options, $expected ) {
180 $blockManagerConfig = array_merge( $this->blockManagerConfig
, [
181 'wgEnableDnsBlacklist' => true,
182 'wgDnsBlacklistUrls' => $options['blacklist'],
183 'wgProxyWhitelist' => $options['whitelist'],
186 $blockManager = $this->getMockBuilder( BlockManager
::class )
187 ->setConstructorArgs(
190 $this->user
->getRequest(),
191 ], $blockManagerConfig ) )
192 ->setMethods( [ 'checkHost' ] )
195 $blockManager->expects( $this->any() )
196 ->method( 'checkHost' )
197 ->will( $this->returnValueMap( [ [
198 $options['dnsblQuery'],
199 $options['dnsblResponse'],
204 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
208 public static function provideIsDnsBlacklisted() {
209 $dnsblFound = [ '127.0.0.2' ];
210 $dnsblNotFound = false;
212 'IP is blacklisted' => [
214 'blacklist' => [ 'dnsbl.test' ],
216 'dnsblQuery' => '1.0.0.127.dnsbl.test',
217 'dnsblResponse' => $dnsblFound,
219 'checkWhitelist' => false,
223 'IP is blacklisted; blacklist has key' => [
225 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
227 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
228 'dnsblResponse' => $dnsblFound,
230 'checkWhitelist' => false,
234 'IP is blacklisted; blacklist is array' => [
236 'blacklist' => [ [ 'dnsbl.test' ] ],
238 'dnsblQuery' => '1.0.0.127.dnsbl.test',
239 'dnsblResponse' => $dnsblFound,
241 'checkWhitelist' => false,
245 'IP is not blacklisted' => [
247 'blacklist' => [ 'dnsbl.test' ],
249 'dnsblQuery' => '4.3.2.1.dnsbl.test',
250 'dnsblResponse' => $dnsblNotFound,
252 'checkWhitelist' => false,
256 'Blacklist is empty' => [
260 'dnsblQuery' => '1.0.0.127.dnsbl.test',
261 'dnsblResponse' => $dnsblFound,
263 'checkWhitelist' => false,
267 'IP is blacklisted and whitelisted; whitelist is not checked' => [
269 'blacklist' => [ 'dnsbl.test' ],
271 'dnsblQuery' => '1.0.0.127.dnsbl.test',
272 'dnsblResponse' => $dnsblFound,
273 'whitelist' => [ '127.0.0.1' ],
274 'checkWhitelist' => false,
278 'IP is blacklisted and whitelisted; whitelist is checked' => [
280 'blacklist' => [ 'dnsbl.test' ],
282 'dnsblQuery' => '1.0.0.127.dnsbl.test',
283 'dnsblResponse' => $dnsblFound,
284 'whitelist' => [ '127.0.0.1' ],
285 'checkWhitelist' => true,