Merge "Added DatabaseBase::startAtomic and endAtomic"
[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( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
76 }
77
78 /**
79 * @covers Block::newFromID
80 */
81 public function testINewFromIDReturnsCorrectBlock() {
82 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
83 }
84
85 /**
86 * per bug 26425
87 */
88 public function testBug26425BlockTimestampDefaultsToTime() {
89 // delta to stop one-off errors when things happen to go over a second mark.
90 $delta = abs( $this->madeAt - $this->block->mTimestamp );
91 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
92 }
93
94 /**
95 * This is the method previously used to load block info in CheckUser etc
96 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
97 *
98 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
99 *
100 * @dataProvider provideBug29116Data
101 * @covers Block::load
102 */
103 public function testBug29116LoadWithEmptyIp( $vagueTarget ) {
104 $this->hideDeprecated( 'Block::load' );
105
106 $uid = User::idFromName( 'UTBlockee' );
107 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' );
108
109 $block = new Block();
110 $ok = $block->load( $vagueTarget, $uid );
111 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
112
113 $this->assertTrue( $this->block->equals( $block ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
114 }
115
116 /**
117 * CheckUser since being changed to use Block::newFromTarget started failing
118 * because the new function didn't accept empty strings like Block::load()
119 * had. Regression bug 29116.
120 *
121 * @dataProvider provideBug29116Data
122 * @covers Block::newFromTarget
123 */
124 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
125 $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
126 $this->assertTrue( $this->block->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) );
127 }
128
129 public static function provideBug29116Data() {
130 return array(
131 array( null ),
132 array( '' ),
133 array( false )
134 );
135 }
136
137 /**
138 * @covers Block::prevents
139 */
140 public function testBlockedUserCanNotCreateAccount() {
141 $username = 'BlockedUserToCreateAccountWith';
142 $u = User::newFromName( $username );
143 $u->setPassword( 'NotRandomPass' );
144 $u->addToDatabase();
145 unset( $u );
146
147 // Sanity check
148 $this->assertNull(
149 Block::newFromTarget( $username ),
150 "$username should not be blocked"
151 );
152
153 // Reload user
154 $u = User::newFromName( $username );
155 $this->assertFalse(
156 $u->isBlockedFromCreateAccount(),
157 "Our sandbox user should be able to create account before being blocked"
158 );
159
160 // Foreign perspective (blockee not on current wiki)...
161 $block = new Block(
162 /* $address */ $username,
163 /* $user */ 14146,
164 /* $by */ 0,
165 /* $reason */ 'crosswiki block...',
166 /* $timestamp */ wfTimestampNow(),
167 /* $auto */ false,
168 /* $expiry */ $this->db->getInfinity(),
169 /* anonOnly */ false,
170 /* $createAccount */ true,
171 /* $enableAutoblock */ true,
172 /* $hideName (ipb_deleted) */ true,
173 /* $blockEmail */ true,
174 /* $allowUsertalk */ false,
175 /* $byName */ 'MetaWikiUser'
176 );
177 $block->insert();
178
179 // Reload block from DB
180 $userBlock = Block::newFromTarget( $username );
181 $this->assertTrue(
182 (bool)$block->prevents( 'createaccount' ),
183 "Block object in DB should prevents 'createaccount'"
184 );
185
186 $this->assertInstanceOf(
187 'Block',
188 $userBlock,
189 "'$username' block block object should be existent"
190 );
191
192 // Reload user
193 $u = User::newFromName( $username );
194 $this->assertTrue(
195 (bool)$u->isBlockedFromCreateAccount(),
196 "Our sandbox user '$username' should NOT be able to create account"
197 );
198 }
199
200 /**
201 * @covers Block::insert
202 */
203 public function testCrappyCrossWikiBlocks() {
204 // Delete the last round's block if it's still there
205 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
206 if ( $oldBlock ) {
207 // An old block will prevent our new one from saving.
208 $oldBlock->delete();
209 }
210
211 // Foreign perspective (blockee not on current wiki)...
212 $block = new Block(
213 /* $address */ 'UserOnForeignWiki',
214 /* $user */ 14146,
215 /* $by */ 0,
216 /* $reason */ 'crosswiki block...',
217 /* $timestamp */ wfTimestampNow(),
218 /* $auto */ false,
219 /* $expiry */ $this->db->getInfinity(),
220 /* anonOnly */ false,
221 /* $createAccount */ true,
222 /* $enableAutoblock */ true,
223 /* $hideName (ipb_deleted) */ true,
224 /* $blockEmail */ true,
225 /* $allowUsertalk */ false,
226 /* $byName */ 'MetaWikiUser'
227 );
228
229 $res = $block->insert( $this->db );
230 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
231
232 // Local perspective (blockee on current wiki)...
233 $user = User::newFromName( 'UserOnForeignWiki' );
234 $user->addToDatabase();
235 // Set user ID to match the test value
236 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
237 $user = null; // clear
238
239 $block = Block::newFromID( $res['id'] );
240 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
241 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
242 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
243 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
244 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
245 }
246
247 protected function addXffBlocks() {
248 static $inited = false;
249
250 if ( $inited ) {
251 return;
252 }
253
254 $inited = true;
255
256 $blockList = array(
257 array( 'target' => '70.2.0.0/16',
258 'type' => Block::TYPE_RANGE,
259 'desc' => 'Range Hardblock',
260 'ACDisable' => false,
261 'isHardblock' => true,
262 'isAutoBlocking' => false,
263 ),
264 array( 'target' => '2001:4860:4001::/48',
265 'type' => Block::TYPE_RANGE,
266 'desc' => 'Range6 Hardblock',
267 'ACDisable' => false,
268 'isHardblock' => true,
269 'isAutoBlocking' => false,
270 ),
271 array( 'target' => '60.2.0.0/16',
272 'type' => Block::TYPE_RANGE,
273 'desc' => 'Range Softblock with AC Disabled',
274 'ACDisable' => true,
275 'isHardblock' => false,
276 'isAutoBlocking' => false,
277 ),
278 array( 'target' => '50.2.0.0/16',
279 'type' => Block::TYPE_RANGE,
280 'desc' => 'Range Softblock',
281 'ACDisable' => false,
282 'isHardblock' => false,
283 'isAutoBlocking' => false,
284 ),
285 array( 'target' => '50.1.1.1',
286 'type' => Block::TYPE_IP,
287 'desc' => 'Exact Softblock',
288 'ACDisable' => false,
289 'isHardblock' => false,
290 'isAutoBlocking' => false,
291 ),
292 );
293
294 foreach ( $blockList as $insBlock ) {
295 $target = $insBlock['target'];
296
297 if ( $insBlock['type'] === Block::TYPE_IP ) {
298 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
299 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
300 $target = IP::sanitizeRange( $target );
301 }
302
303 $block = new Block();
304 $block->setTarget( $target );
305 $block->setBlocker( 'testblocker@global' );
306 $block->mReason = $insBlock['desc'];
307 $block->mExpiry = 'infinity';
308 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
309 $block->isHardblock( $insBlock['isHardblock'] );
310 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
311 $block->insert();
312 }
313 }
314
315 public static function providerXff() {
316 return array(
317 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
318 'count' => 2,
319 'result' => 'Range Hardblock'
320 ),
321 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
322 'count' => 2,
323 'result' => 'Range Softblock with AC Disabled'
324 ),
325 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
326 'count' => 2,
327 'result' => 'Exact Softblock'
328 ),
329 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
330 'count' => 3,
331 'result' => 'Exact Softblock'
332 ),
333 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
334 'count' => 2,
335 'result' => 'Range Hardblock'
336 ),
337 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
338 'count' => 2,
339 'result' => 'Range Hardblock'
340 ),
341 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
342 'count' => 2,
343 'result' => 'Range Softblock with AC Disabled'
344 ),
345 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
346 'count' => 2,
347 'result' => 'Exact Softblock'
348 ),
349 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
350 'count' => 1,
351 'result' => 'Range Softblock with AC Disabled'
352 ),
353 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
354 'count' => 2,
355 'result' => 'Range6 Hardblock'
356 ),
357 );
358 }
359
360 /**
361 * @dataProvider providerXff
362 * @covers Block::getBlocksForIPList
363 * @covers Block::chooseBlock
364 */
365 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
366 $list = array_map( 'trim', explode( ',', $xff ) );
367 $xffblocks = Block::getBlocksForIPList( $list, true );
368 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
369 $block = Block::chooseBlock( $xffblocks, $list );
370 $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
371 }
372 }