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