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