ec6eea99c90bb06913aa3b3aa9c9bf6fb00c449d
[lhc/web/wiklou.git] / tests / phpunit / includes / block / BlockManagerTest.php
1 <?php
2
3 use MediaWiki\Block\BlockManager;
4
5 /**
6 * @group Blocking
7 * @group Database
8 * @coversDefaultClass \MediaWiki\Block\BlockManager
9 */
10 class BlockManagerTest extends MediaWikiTestCase {
11
12 /** @var User */
13 protected $user;
14
15 /** @var int */
16 protected $sysopId;
17
18 protected function setUp() {
19 parent::setUp();
20
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,
29 'wgProxyList' => [],
30 'wgProxyWhitelist' => [],
31 'wgSoftBlockRanges' => [],
32 ];
33 }
34
35 private function getBlockManager( $overrideConfig ) {
36 $blockManagerConfig = array_merge( $this->blockManagerConfig, $overrideConfig );
37 return new BlockManager(
38 $this->user,
39 $this->user->getRequest(),
40 ...array_values( $blockManagerConfig )
41 );
42 }
43
44 /**
45 * @dataProvider provideGetBlockFromCookieValue
46 * @covers ::getBlockFromCookieValue
47 */
48 public function testGetBlockFromCookieValue( $options, $expected ) {
49 $blockManager = $this->getBlockManager( [
50 'wgCookieSetOnAutoblock' => true,
51 'wgCookieSetOnIpBlock' => true,
52 ] );
53
54 $block = new Block( array_merge( [
55 'address' => $options[ 'target' ] ?: $this->user,
56 'by' => $this->sysopId,
57 ], $options[ 'blockOptions' ] ) );
58 $block->insert();
59
60 $class = new ReflectionClass( BlockManager::class );
61 $method = $class->getMethod( 'getBlockFromCookieValue' );
62 $method->setAccessible( true );
63
64 $user = $options[ 'loggedIn' ] ? $this->user : new User();
65 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
66
67 $this->assertSame( $expected, (bool)$method->invoke(
68 $blockManager,
69 $user,
70 $user->getRequest()
71 ) );
72
73 $block->delete();
74 }
75
76 public static function provideGetBlockFromCookieValue() {
77 return [
78 'Autoblocking user block' => [
79 [
80 'target' => '',
81 'loggedIn' => true,
82 'blockOptions' => [
83 'enableAutoblock' => true
84 ],
85 ],
86 true,
87 ],
88 'Non-autoblocking user block' => [
89 [
90 'target' => '',
91 'loggedIn' => true,
92 'blockOptions' => [],
93 ],
94 false,
95 ],
96 'IP block for anonymous user' => [
97 [
98 'target' => '127.0.0.1',
99 'loggedIn' => false,
100 'blockOptions' => [],
101 ],
102 true,
103 ],
104 'IP block for logged in user' => [
105 [
106 'target' => '127.0.0.1',
107 'loggedIn' => true,
108 'blockOptions' => [],
109 ],
110 false,
111 ],
112 'IP range block for anonymous user' => [
113 [
114 'target' => '127.0.0.0/8',
115 'loggedIn' => false,
116 'blockOptions' => [],
117 ],
118 true,
119 ],
120 ];
121 }
122
123 /**
124 * @dataProvider provideIsLocallyBlockedProxy
125 * @covers ::isLocallyBlockedProxy
126 */
127 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
128 $class = new ReflectionClass( BlockManager::class );
129 $method = $class->getMethod( 'isLocallyBlockedProxy' );
130 $method->setAccessible( true );
131
132 $blockManager = $this->getBlockManager( [
133 'wgProxyList' => $proxyList
134 ] );
135
136 $ip = '1.2.3.4';
137 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
138 }
139
140 public static function provideIsLocallyBlockedProxy() {
141 return [
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 ],
146 ];
147 }
148
149 /**
150 * @covers ::isLocallyBlockedProxy
151 */
152 public function testIsLocallyBlockedProxyDeprecated() {
153 $proxy = '1.2.3.4';
154
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)'
158 );
159
160 $class = new ReflectionClass( BlockManager::class );
161 $method = $class->getMethod( 'isLocallyBlockedProxy' );
162 $method->setAccessible( true );
163
164 $blockManager = $this->getBlockManager( [
165 'wgProxyList' => [ $proxy => 'test' ]
166 ] );
167
168 $ip = '1.2.3.4';
169 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
170 }
171
172 /**
173 * @dataProvider provideIsDnsBlacklisted
174 * @covers ::isDnsBlacklisted
175 * @covers ::inDnsBlacklist
176 */
177 public function testIsDnsBlacklisted( $options, $expected ) {
178 $blockManagerConfig = array_merge( $this->blockManagerConfig, [
179 'wgEnableDnsBlacklist' => true,
180 'wgDnsBlacklistUrls' => $options['blacklist'],
181 'wgProxyWhitelist' => $options['whitelist'],
182 ] );
183
184 $blockManager = $this->getMockBuilder( BlockManager::class )
185 ->setConstructorArgs(
186 array_merge( [
187 $this->user,
188 $this->user->getRequest(),
189 ], $blockManagerConfig ) )
190 ->setMethods( [ 'checkHost' ] )
191 ->getMock();
192
193 $blockManager->expects( $this->any() )
194 ->method( 'checkHost' )
195 ->will( $this->returnValueMap( [ [
196 $options['dnsblQuery'],
197 $options['dnsblResponse'],
198 ] ] ) );
199
200 $this->assertSame(
201 $expected,
202 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
203 );
204 }
205
206 public static function provideIsDnsBlacklisted() {
207 $dnsblFound = [ '127.0.0.2' ];
208 $dnsblNotFound = false;
209 return [
210 'IP is blacklisted' => [
211 [
212 'blacklist' => [ 'dnsbl.test' ],
213 'ip' => '127.0.0.1',
214 'dnsblQuery' => '1.0.0.127.dnsbl.test',
215 'dnsblResponse' => $dnsblFound,
216 'whitelist' => [],
217 'checkWhitelist' => false,
218 ],
219 true,
220 ],
221 'IP is blacklisted; blacklist has key' => [
222 [
223 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
224 'ip' => '127.0.0.1',
225 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
226 'dnsblResponse' => $dnsblFound,
227 'whitelist' => [],
228 'checkWhitelist' => false,
229 ],
230 true,
231 ],
232 'IP is blacklisted; blacklist is array' => [
233 [
234 'blacklist' => [ [ 'dnsbl.test' ] ],
235 'ip' => '127.0.0.1',
236 'dnsblQuery' => '1.0.0.127.dnsbl.test',
237 'dnsblResponse' => $dnsblFound,
238 'whitelist' => [],
239 'checkWhitelist' => false,
240 ],
241 true,
242 ],
243 'IP is not blacklisted' => [
244 [
245 'blacklist' => [ 'dnsbl.test' ],
246 'ip' => '1.2.3.4',
247 'dnsblQuery' => '4.3.2.1.dnsbl.test',
248 'dnsblResponse' => $dnsblNotFound,
249 'whitelist' => [],
250 'checkWhitelist' => false,
251 ],
252 false,
253 ],
254 'Blacklist is empty' => [
255 [
256 'blacklist' => [],
257 'ip' => '127.0.0.1',
258 'dnsblQuery' => '1.0.0.127.dnsbl.test',
259 'dnsblResponse' => $dnsblFound,
260 'whitelist' => [],
261 'checkWhitelist' => false,
262 ],
263 false,
264 ],
265 'IP is blacklisted and whitelisted; whitelist is not checked' => [
266 [
267 'blacklist' => [ 'dnsbl.test' ],
268 'ip' => '127.0.0.1',
269 'dnsblQuery' => '1.0.0.127.dnsbl.test',
270 'dnsblResponse' => $dnsblFound,
271 'whitelist' => [ '127.0.0.1' ],
272 'checkWhitelist' => false,
273 ],
274 true,
275 ],
276 'IP is blacklisted and whitelisted; whitelist is checked' => [
277 [
278 'blacklist' => [ 'dnsbl.test' ],
279 'ip' => '127.0.0.1',
280 'dnsblQuery' => '1.0.0.127.dnsbl.test',
281 'dnsblResponse' => $dnsblFound,
282 'whitelist' => [ '127.0.0.1' ],
283 'checkWhitelist' => true,
284 ],
285 false,
286 ],
287 ];
288 }
289 }