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