Merge "Converted ChangeTags to using the WAN cache"
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
8
9 /** @var Block */
10 private $block;
11 private $madeAt;
12
13 /* variable used to save up the blockID we insert in this test suite */
14 private $blockId;
15
16 protected function setUp() {
17 parent::setUp();
18 $this->setMwGlobals( array(
19 'wgLanguageCode' => 'en',
20 'wgContLang' => Language::factory( 'en' )
21 ) );
22 }
23
24 function addDBData() {
25
26 $user = User::newFromName( 'UTBlockee' );
27 if ( $user->getID() == 0 ) {
28 $user->addToDatabase();
29 $user->setPassword( 'UTBlockeePassword' );
30
31 $user->saveSettings();
32 }
33
34 // Delete the last round's block if it's still there
35 $oldBlock = Block::newFromTarget( 'UTBlockee' );
36 if ( $oldBlock ) {
37 // An old block will prevent our new one from saving.
38 $oldBlock->delete();
39 }
40
41 $this->block = new Block( 'UTBlockee', $user->getID(), 0,
42 'Parce que', 0, false, time() + 100500
43 );
44 $this->madeAt = wfTimestamp( TS_MW );
45
46 $this->block->insert();
47 // save up ID for use in assertion. Since ID is an autoincrement,
48 // its value might change depending on the order the tests are run.
49 // ApiBlockTest insert its own blocks!
50 $newBlockId = $this->block->getId();
51 if ( $newBlockId ) {
52 $this->blockId = $newBlockId;
53 } else {
54 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55 }
56
57 $this->addXffBlocks();
58 }
59
60 /**
61 * debug function : dump the ipblocks table
62 */
63 function dumpBlocks() {
64 $v = $this->db->select( 'ipblocks', '*' );
65 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
66 foreach ( $v as $row ) {
67 print_r( $row );
68 }
69 }
70
71 /**
72 * @covers Block::newFromTarget
73 */
74 public function testINewFromTargetReturnsCorrectBlock() {
75 $this->assertTrue(
76 $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ),
77 "newFromTarget() returns the same block as the one that was made"
78 );
79 }
80
81 /**
82 * @covers Block::newFromID
83 */
84 public function testINewFromIDReturnsCorrectBlock() {
85 $this->assertTrue(
86 $this->block->equals( Block::newFromID( $this->blockId ) ),
87 "newFromID() returns the same block as the one that was made"
88 );
89 }
90
91 /**
92 * per bug 26425
93 */
94 public function testBug26425BlockTimestampDefaultsToTime() {
95 // delta to stop one-off errors when things happen to go over a second mark.
96 $delta = abs( $this->madeAt - $this->block->mTimestamp );
97 $this->assertLessThan(
98 2,
99 $delta,
100 "If no timestamp is specified, the block is recorded as time()"
101 );
102 }
103
104 /**
105 * CheckUser since being changed to use Block::newFromTarget started failing
106 * because the new function didn't accept empty strings like Block::load()
107 * had. Regression bug 29116.
108 *
109 * @dataProvider provideBug29116Data
110 * @covers Block::newFromTarget
111 */
112 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
113 $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
114 $this->assertTrue(
115 $this->block->equals( $block ),
116 "newFromTarget() returns the same block as the one that was made when "
117 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
118 );
119 }
120
121 public static function provideBug29116Data() {
122 return array(
123 array( null ),
124 array( '' ),
125 array( false )
126 );
127 }
128
129 /**
130 * @covers Block::prevents
131 */
132 public function testBlockedUserCanNotCreateAccount() {
133 $username = 'BlockedUserToCreateAccountWith';
134 $u = User::newFromName( $username );
135 $u->setPassword( 'NotRandomPass' );
136 $u->setId( 14146 );
137 $u->addToDatabase();
138 unset( $u );
139
140 // Sanity check
141 $this->assertNull(
142 Block::newFromTarget( $username ),
143 "$username should not be blocked"
144 );
145
146 // Reload user
147 $u = User::newFromName( $username );
148 $this->assertFalse(
149 $u->isBlockedFromCreateAccount(),
150 "Our sandbox user should be able to create account before being blocked"
151 );
152
153 // Foreign perspective (blockee not on current wiki)...
154 $block = new Block(
155 /* $address */ $username,
156 /* $user */ 14146,
157 /* $by */ 0,
158 /* $reason */ 'crosswiki block...',
159 /* $timestamp */ wfTimestampNow(),
160 /* $auto */ false,
161 /* $expiry */ $this->db->getInfinity(),
162 /* anonOnly */ false,
163 /* $createAccount */ true,
164 /* $enableAutoblock */ true,
165 /* $hideName (ipb_deleted) */ true,
166 /* $blockEmail */ true,
167 /* $allowUsertalk */ false,
168 /* $byName */ 'MetaWikiUser'
169 );
170 $block->insert();
171
172 // Reload block from DB
173 $userBlock = Block::newFromTarget( $username );
174 $this->assertTrue(
175 (bool)$block->prevents( 'createaccount' ),
176 "Block object in DB should prevents 'createaccount'"
177 );
178
179 $this->assertInstanceOf(
180 'Block',
181 $userBlock,
182 "'$username' block block object should be existent"
183 );
184
185 // Reload user
186 $u = User::newFromName( $username );
187 $this->assertTrue(
188 (bool)$u->isBlockedFromCreateAccount(),
189 "Our sandbox user '$username' should NOT be able to create account"
190 );
191 }
192
193 /**
194 * @covers Block::insert
195 */
196 public function testCrappyCrossWikiBlocks() {
197 // Delete the last round's block if it's still there
198 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
199 if ( $oldBlock ) {
200 // An old block will prevent our new one from saving.
201 $oldBlock->delete();
202 }
203
204 // Local perspective (blockee on current wiki)...
205 $user = User::newFromName( 'UserOnForeignWiki' );
206 $user->addToDatabase();
207 // Set user ID to match the test value
208 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
209
210 // Foreign perspective (blockee not on current wiki)...
211 $block = new Block(
212 /* $address */ 'UserOnForeignWiki',
213 /* $user */ 14146,
214 /* $by */ 0,
215 /* $reason */ 'crosswiki block...',
216 /* $timestamp */ wfTimestampNow(),
217 /* $auto */ false,
218 /* $expiry */ $this->db->getInfinity(),
219 /* anonOnly */ false,
220 /* $createAccount */ true,
221 /* $enableAutoblock */ true,
222 /* $hideName (ipb_deleted) */ true,
223 /* $blockEmail */ true,
224 /* $allowUsertalk */ false,
225 /* $byName */ 'MetaWikiUser'
226 );
227
228 $res = $block->insert( $this->db );
229 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
230
231 $user = null; // clear
232
233 $block = Block::newFromID( $res['id'] );
234 $this->assertEquals(
235 'UserOnForeignWiki',
236 $block->getTarget()->getName(),
237 'Correct blockee name'
238 );
239 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
240 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
241 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
242 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
243 }
244
245 protected function addXffBlocks() {
246 static $inited = false;
247
248 if ( $inited ) {
249 return;
250 }
251
252 $inited = true;
253
254 $blockList = array(
255 array( 'target' => '70.2.0.0/16',
256 'type' => Block::TYPE_RANGE,
257 'desc' => 'Range Hardblock',
258 'ACDisable' => false,
259 'isHardblock' => true,
260 'isAutoBlocking' => false,
261 ),
262 array( 'target' => '2001:4860:4001::/48',
263 'type' => Block::TYPE_RANGE,
264 'desc' => 'Range6 Hardblock',
265 'ACDisable' => false,
266 'isHardblock' => true,
267 'isAutoBlocking' => false,
268 ),
269 array( 'target' => '60.2.0.0/16',
270 'type' => Block::TYPE_RANGE,
271 'desc' => 'Range Softblock with AC Disabled',
272 'ACDisable' => true,
273 'isHardblock' => false,
274 'isAutoBlocking' => false,
275 ),
276 array( 'target' => '50.2.0.0/16',
277 'type' => Block::TYPE_RANGE,
278 'desc' => 'Range Softblock',
279 'ACDisable' => false,
280 'isHardblock' => false,
281 'isAutoBlocking' => false,
282 ),
283 array( 'target' => '50.1.1.1',
284 'type' => Block::TYPE_IP,
285 'desc' => 'Exact Softblock',
286 'ACDisable' => false,
287 'isHardblock' => false,
288 'isAutoBlocking' => false,
289 ),
290 );
291
292 foreach ( $blockList as $insBlock ) {
293 $target = $insBlock['target'];
294
295 if ( $insBlock['type'] === Block::TYPE_IP ) {
296 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
297 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
298 $target = IP::sanitizeRange( $target );
299 }
300
301 $block = new Block();
302 $block->setTarget( $target );
303 $block->setBlocker( 'testblocker@global' );
304 $block->mReason = $insBlock['desc'];
305 $block->mExpiry = 'infinity';
306 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
307 $block->isHardblock( $insBlock['isHardblock'] );
308 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
309 $block->insert();
310 }
311 }
312
313 public static function providerXff() {
314 return array(
315 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
316 'count' => 2,
317 'result' => 'Range Hardblock'
318 ),
319 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
320 'count' => 2,
321 'result' => 'Range Softblock with AC Disabled'
322 ),
323 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
324 'count' => 2,
325 'result' => 'Exact Softblock'
326 ),
327 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
328 'count' => 3,
329 'result' => 'Exact Softblock'
330 ),
331 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
332 'count' => 2,
333 'result' => 'Range Hardblock'
334 ),
335 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
336 'count' => 2,
337 'result' => 'Range Hardblock'
338 ),
339 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
340 'count' => 2,
341 'result' => 'Range Softblock with AC Disabled'
342 ),
343 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
344 'count' => 2,
345 'result' => 'Exact Softblock'
346 ),
347 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
348 'count' => 1,
349 'result' => 'Range Softblock with AC Disabled'
350 ),
351 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
352 'count' => 2,
353 'result' => 'Range6 Hardblock'
354 ),
355 );
356 }
357
358 /**
359 * @dataProvider providerXff
360 * @covers Block::getBlocksForIPList
361 * @covers Block::chooseBlock
362 */
363 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
364 $list = array_map( 'trim', explode( ',', $xff ) );
365 $xffblocks = Block::getBlocksForIPList( $list, true );
366 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
367 $block = Block::chooseBlock( $xffblocks, $list );
368 $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
369 }
370 }