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