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