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();
25 private function getBlockManager( $overrideConfig ) {
26 $blockManagerConfig = array_merge( [
27 'wgApplyIpBlocksToXff' => true,
28 'wgCookieSetOnAutoblock' => true,
29 'wgCookieSetOnIpBlock' => true,
30 'wgDnsBlacklistUrls' => [],
31 'wgEnableDnsBlacklist' => true,
33 'wgProxyWhitelist' => [],
34 'wgSoftBlockRanges' => [],
36 return new BlockManager(
38 $this->user
->getRequest(),
39 ...array_values( $blockManagerConfig )
44 * @dataProvider provideGetBlockFromCookieValue
45 * @covers ::getBlockFromCookieValue
47 public function testGetBlockFromCookieValue( $options, $expected ) {
48 $blockManager = $this->getBlockManager( [
49 'wgCookieSetOnAutoblock' => true,
50 'wgCookieSetOnIpBlock' => true,
53 $block = new Block( array_merge( [
54 'address' => $options[ 'target' ] ?
: $this->user
,
55 'by' => $this->sysopId
,
56 ], $options[ 'blockOptions' ] ) );
59 $class = new ReflectionClass( BlockManager
::class );
60 $method = $class->getMethod( 'getBlockFromCookieValue' );
61 $method->setAccessible( true );
63 $user = $options[ 'loggedIn' ] ?
$this->user
: new User();
64 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
66 $this->assertSame( $expected, (bool)$method->invoke(
75 public static function provideGetBlockFromCookieValue() {
77 'Autoblocking user block' => [
82 'enableAutoblock' => true
87 'Non-autoblocking user block' => [
95 'IP block for anonymous user' => [
97 'target' => '127.0.0.1',
103 'IP block for logged in user' => [
105 'target' => '127.0.0.1',
107 'blockOptions' => [],
111 'IP range block for anonymous user' => [
113 'target' => '127.0.0.0/8',
115 'blockOptions' => [],
123 * @dataProvider provideIsLocallyBlockedProxy
124 * @covers ::isLocallyBlockedProxy
126 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
127 $class = new ReflectionClass( BlockManager
::class );
128 $method = $class->getMethod( 'isLocallyBlockedProxy' );
129 $method->setAccessible( true );
131 $blockManager = $this->getBlockManager( [
132 'wgProxyList' => $proxyList
136 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
139 public static function provideIsLocallyBlockedProxy() {
141 'Proxy list is empty' => [ [], false ],
142 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
143 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
144 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
149 * @covers ::isLocallyBlockedProxy
151 public function testIsLocallyBlockedProxyDeprecated() {
154 $this->hideDeprecated(
155 'IP addresses in the keys of $wgProxyList (found the following IP ' .
156 'addresses in keys: ' . $proxy . ', please move them to values)'
159 $class = new ReflectionClass( BlockManager
::class );
160 $method = $class->getMethod( 'isLocallyBlockedProxy' );
161 $method->setAccessible( true );
163 $blockManager = $this->getBlockManager( [
164 'wgProxyList' => [ $proxy => 'test' ]
168 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
172 * @dataProvider provideIsDnsBlacklisted
173 * @covers ::isDnsBlacklisted
174 * @covers ::inDnsBlacklist
176 public function testIsDnsBlacklisted( $options, $expected ) {
177 $blockManager = $this->getBlockManager( [
178 'wgEnableDnsBlacklist' => true,
179 'wgDnsBlacklistUrls' => $options[ 'inBlacklist' ] ?
[ 'local.wmftest.net' ] : [],
180 'wgProxyWhitelist' => $options[ 'inWhitelist' ] ?
[ '127.0.0.1' ] : [],
186 $blockManager->isDnsBlacklisted( $ip, $options[ 'check' ] )
190 public static function provideIsDnsBlacklisted() {
192 'IP is blacklisted' => [
194 'inBlacklist' => true,
195 'inWhitelist' => false,
200 'IP is not blacklisted' => [
202 'inBlacklist' => false,
203 'inWhitelist' => false,
208 'IP is blacklisted and whitelisted; whitelist is checked' => [
210 'inBlacklist' => true,
211 'inWhitelist' => true,
216 'IP is blacklisted and whitelisted; whitelist is not checked' => [
218 'inBlacklist' => true,
219 'inWhitelist' => true,