Merge "Improve rollback tests setup by extracting repeating logic to HistoryPage...
[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 }
24
25 private function getBlockManager( $overrideConfig ) {
26 $blockManagerConfig = array_merge( [
27 'wgApplyIpBlocksToXff' => true,
28 'wgCookieSetOnAutoblock' => true,
29 'wgCookieSetOnIpBlock' => true,
30 'wgDnsBlacklistUrls' => [],
31 'wgEnableDnsBlacklist' => true,
32 'wgProxyList' => [],
33 'wgProxyWhitelist' => [],
34 'wgSoftBlockRanges' => [],
35 ], $overrideConfig );
36 return new BlockManager(
37 $this->user,
38 $this->user->getRequest(),
39 ...array_values( $blockManagerConfig )
40 );
41 }
42
43 /**
44 * @dataProvider provideGetBlockFromCookieValue
45 * @covers ::getBlockFromCookieValue
46 */
47 public function testGetBlockFromCookieValue( $options, $expected ) {
48 $blockManager = $this->getBlockManager( [
49 'wgCookieSetOnAutoblock' => true,
50 'wgCookieSetOnIpBlock' => true,
51 ] );
52
53 $block = new Block( array_merge( [
54 'address' => $options[ 'target' ] ?: $this->user,
55 'by' => $this->sysopId,
56 ], $options[ 'blockOptions' ] ) );
57 $block->insert();
58
59 $class = new ReflectionClass( BlockManager::class );
60 $method = $class->getMethod( 'getBlockFromCookieValue' );
61 $method->setAccessible( true );
62
63 $user = $options[ 'loggedIn' ] ? $this->user : new User();
64 $user->getRequest()->setCookie( 'BlockID', $block->getCookieValue() );
65
66 $this->assertSame( $expected, (bool)$method->invoke(
67 $blockManager,
68 $user,
69 $user->getRequest()
70 ) );
71
72 $block->delete();
73 }
74
75 public static function provideGetBlockFromCookieValue() {
76 return [
77 'Autoblocking user block' => [
78 [
79 'target' => '',
80 'loggedIn' => true,
81 'blockOptions' => [
82 'enableAutoblock' => true
83 ],
84 ],
85 true,
86 ],
87 'Non-autoblocking user block' => [
88 [
89 'target' => '',
90 'loggedIn' => true,
91 'blockOptions' => [],
92 ],
93 false,
94 ],
95 'IP block for anonymous user' => [
96 [
97 'target' => '127.0.0.1',
98 'loggedIn' => false,
99 'blockOptions' => [],
100 ],
101 true,
102 ],
103 'IP block for logged in user' => [
104 [
105 'target' => '127.0.0.1',
106 'loggedIn' => true,
107 'blockOptions' => [],
108 ],
109 false,
110 ],
111 'IP range block for anonymous user' => [
112 [
113 'target' => '127.0.0.0/8',
114 'loggedIn' => false,
115 'blockOptions' => [],
116 ],
117 true,
118 ],
119 ];
120 }
121
122 /**
123 * @dataProvider provideIsLocallyBlockedProxy
124 * @covers ::isLocallyBlockedProxy
125 */
126 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
127 $class = new ReflectionClass( BlockManager::class );
128 $method = $class->getMethod( 'isLocallyBlockedProxy' );
129 $method->setAccessible( true );
130
131 $blockManager = $this->getBlockManager( [
132 'wgProxyList' => $proxyList
133 ] );
134
135 $ip = '1.2.3.4';
136 $this->assertSame( $expected, $method->invoke( $blockManager, $ip ) );
137 }
138
139 public static function provideIsLocallyBlockedProxy() {
140 return [
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 ],
145 ];
146 }
147
148 /**
149 * @covers ::isLocallyBlockedProxy
150 */
151 public function testIsLocallyBlockedProxyDeprecated() {
152 $proxy = '1.2.3.4';
153
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)'
157 );
158
159 $class = new ReflectionClass( BlockManager::class );
160 $method = $class->getMethod( 'isLocallyBlockedProxy' );
161 $method->setAccessible( true );
162
163 $blockManager = $this->getBlockManager( [
164 'wgProxyList' => [ $proxy => 'test' ]
165 ] );
166
167 $ip = '1.2.3.4';
168 $this->assertSame( true, $method->invoke( $blockManager, $ip ) );
169 }
170
171 /**
172 * @dataProvider provideIsDnsBlacklisted
173 * @covers ::isDnsBlacklisted
174 * @covers ::inDnsBlacklist
175 */
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' ] : [],
181 ] );
182
183 $ip = '127.0.0.1';
184 $this->assertSame(
185 $expected,
186 $blockManager->isDnsBlacklisted( $ip, $options[ 'check' ] )
187 );
188 }
189
190 public static function provideIsDnsBlacklisted() {
191 return [
192 'IP is blacklisted' => [
193 [
194 'inBlacklist' => true,
195 'inWhitelist' => false,
196 'check' => false,
197 ],
198 true,
199 ],
200 'IP is not blacklisted' => [
201 [
202 'inBlacklist' => false,
203 'inWhitelist' => false,
204 'check' => false,
205 ],
206 false,
207 ],
208 'IP is blacklisted and whitelisted; whitelist is checked' => [
209 [
210 'inBlacklist' => true,
211 'inWhitelist' => true,
212 'check' => false,
213 ],
214 true,
215 ],
216 'IP is blacklisted and whitelisted; whitelist is not checked' => [
217 [
218 'inBlacklist' => true,
219 'inWhitelist' => true,
220 'check' => true,
221 ],
222 false,
223 ],
224 ];
225 }
226 }