Merge "Protected function UploadBase->validateName changed to public"
[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
56 /**
57 * debug function : dump the ipblocks table
58 */
59 function dumpBlocks() {
60 $v = $this->db->query( 'SELECT * FROM unittest_ipblocks' );
61 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
62 foreach ( $v as $row ) {
63 print_r( $row );
64 }
65 }
66
67 function testInitializerFunctionsReturnCorrectBlock() {
68 // $this->dumpBlocks();
69
70 $this->assertTrue( $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
71
72 $this->assertTrue( $this->block->equals( Block::newFromID( $this->blockId ) ), "newFromID() returns the same block as the one that was made" );
73
74 }
75
76 /**
77 * per bug 26425
78 */
79 function testBug26425BlockTimestampDefaultsToTime() {
80 // delta to stop one-off errors when things happen to go over a second mark.
81 $delta = abs( $this->madeAt - $this->block->mTimestamp );
82 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
83
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
135 // Sanity check
136 $this->assertNull(
137 Block::newFromTarget( $username ),
138 "$username should not be blocked"
139 );
140
141 // Reload user
142 $u = User::newFromName( $username );
143 $this->assertFalse(
144 $u->isBlockedFromCreateAccount(),
145 "Our sandbox user should be able to create account before being blocked"
146 );
147
148 // Foreign perspective (blockee not on current wiki)...
149 $block = new Block(
150 /* $address */ $username,
151 /* $user */ 14146,
152 /* $by */ 0,
153 /* $reason */ 'crosswiki block...',
154 /* $timestamp */ wfTimestampNow(),
155 /* $auto */ false,
156 /* $expiry */ $this->db->getInfinity(),
157 /* anonOnly */ false,
158 /* $createAccount */ true,
159 /* $enableAutoblock */ true,
160 /* $hideName (ipb_deleted) */ true,
161 /* $blockEmail */ true,
162 /* $allowUsertalk */ false,
163 /* $byName */ 'MetaWikiUser'
164 );
165 $block->insert();
166
167 // Reload block from DB
168 $userBlock = Block::newFromTarget( $username );
169 $this->assertTrue(
170 (bool)$block->prevents( 'createaccount' ),
171 "Block object in DB should prevents 'createaccount'"
172 );
173
174 $this->assertInstanceOf(
175 'Block',
176 $userBlock,
177 "'$username' block block object should be existent"
178 );
179
180 // Reload user
181 $u = User::newFromName( $username );
182 $this->assertTrue(
183 (bool)$u->isBlockedFromCreateAccount(),
184 "Our sandbox user '$username' should NOT be able to create account"
185 );
186 }
187
188 function testCrappyCrossWikiBlocks() {
189 // Delete the last round's block if it's still there
190 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
191 if ( $oldBlock ) {
192 // An old block will prevent our new one from saving.
193 $oldBlock->delete();
194 }
195
196 // Foreign perspective (blockee not on current wiki)...
197 $block = new Block(
198 /* $address */ 'UserOnForeignWiki',
199 /* $user */ 14146,
200 /* $by */ 0,
201 /* $reason */ 'crosswiki block...',
202 /* $timestamp */ wfTimestampNow(),
203 /* $auto */ false,
204 /* $expiry */ $this->db->getInfinity(),
205 /* anonOnly */ false,
206 /* $createAccount */ true,
207 /* $enableAutoblock */ true,
208 /* $hideName (ipb_deleted) */ true,
209 /* $blockEmail */ true,
210 /* $allowUsertalk */ false,
211 /* $byName */ 'MetaWikiUser'
212 );
213
214 $res = $block->insert( $this->db );
215 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
216
217 // Local perspective (blockee on current wiki)...
218 $user = User::newFromName( 'UserOnForeignWiki' );
219 $user->addToDatabase();
220 // Set user ID to match the test value
221 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
222 $user = null; // clear
223
224 $block = Block::newFromID( $res['id'] );
225 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
226 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
227 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
228 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
229 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
230 }
231
232 function testBlocksOnXff() {
233
234 $blockList = array(
235 array( 'target' => '70.2.0.0/16',
236 'type' => Block::TYPE_RANGE,
237 'desc' => 'Range Hardblock',
238 'ACDisable' => false,
239 'isHardblock' => true,
240 'isAutoBlocking' => false,
241 ),
242 array( 'target' => '2001:4860:4001::/48',
243 'type' => Block::TYPE_RANGE,
244 'desc' => 'Range6 Hardblock',
245 'ACDisable' => false,
246 'isHardblock' => true,
247 'isAutoBlocking' => false,
248 ),
249 array( 'target' => '60.2.0.0/16',
250 'type' => Block::TYPE_RANGE,
251 'desc' => 'Range Softblock with AC Disabled',
252 'ACDisable' => true,
253 'isHardblock' => false,
254 'isAutoBlocking' => false,
255 ),
256 array( 'target' => '50.2.0.0/16',
257 'type' => Block::TYPE_RANGE,
258 'desc' => 'Range Softblock',
259 'ACDisable' => false,
260 'isHardblock' => false,
261 'isAutoBlocking' => false,
262 ),
263 array( 'target' => '50.1.1.1',
264 'type' => Block::TYPE_IP,
265 'desc' => 'Exact Softblock',
266 'ACDisable' => false,
267 'isHardblock' => false,
268 'isAutoBlocking' => false,
269 ),
270 );
271
272 foreach ( $blockList as $insBlock ) {
273 $target = $insBlock['target'];
274
275 if ( $insBlock['type'] === Block::TYPE_IP ) {
276 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
277 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
278 $target = IP::sanitizeRange( $target );
279 }
280
281 $block = new Block();
282 $block->setTarget( $target );
283 $block->setBlocker( 'testblocker@global' );
284 $block->mReason = $insBlock['desc'];
285 $block->mExpiry = 'infinity';
286 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
287 $block->isHardblock( $insBlock['isHardblock'] );
288 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
289 $block->insert();
290 }
291
292 $xffHeaders = array(
293 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
294 'count' => 2,
295 'result' => 'Range Hardblock'
296 ),
297 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
298 'count' => 2,
299 'result' => 'Range Softblock with AC Disabled'
300 ),
301 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
302 'count' => 2,
303 'result' => 'Exact Softblock'
304 ),
305 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
306 'count' => 3,
307 'result' => 'Exact Softblock'
308 ),
309 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
310 'count' => 2,
311 'result' => 'Range Hardblock'
312 ),
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' => '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, 50.1.1.1, 60.2.1.1, 2.3.4.5',
322 'count' => 2,
323 'result' => 'Exact Softblock'
324 ),
325 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
326 'count' => 1,
327 'result' => 'Range Softblock with AC Disabled'
328 ),
329 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
330 'count' => 2,
331 'result' => 'Range6 Hardblock'
332 ),
333 );
334
335 foreach ( $xffHeaders as $test ) {
336 $list = array_map( 'trim', explode( ',', $test['xff'] ) );
337 $xffblocks = Block::getBlocksForIPList( $list, true );
338 $this->assertEquals( $test['count'], count( $xffblocks ), 'Number of blocks for ' . $test['xff'] );
339 $block = Block::chooseBlock( $xffblocks, $list );
340 $this->assertEquals( $test['result'], $block->mReason, 'Correct block type for XFF header ' . $test['xff'] );
341 }
342
343 }
344 }