Merge "Propagate the favicon information to getInfo()"
[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 * CheckUser since being changed to use Block::newFromTarget started failing
96 * because the new function didn't accept empty strings like Block::load()
97 * had. Regression bug 29116.
98 *
99 * @dataProvider provideBug29116Data
100 * @covers Block::newFromTarget
101 */
102 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
103 $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
104 $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 ) );
105 }
106
107 public static function provideBug29116Data() {
108 return array(
109 array( null ),
110 array( '' ),
111 array( false )
112 );
113 }
114
115 /**
116 * @covers Block::prevents
117 */
118 public function testBlockedUserCanNotCreateAccount() {
119 $username = 'BlockedUserToCreateAccountWith';
120 $u = User::newFromName( $username );
121 $u->setPassword( 'NotRandomPass' );
122 $u->addToDatabase();
123 unset( $u );
124
125 // Sanity check
126 $this->assertNull(
127 Block::newFromTarget( $username ),
128 "$username should not be blocked"
129 );
130
131 // Reload user
132 $u = User::newFromName( $username );
133 $this->assertFalse(
134 $u->isBlockedFromCreateAccount(),
135 "Our sandbox user should be able to create account before being blocked"
136 );
137
138 // Foreign perspective (blockee not on current wiki)...
139 $block = new Block(
140 /* $address */ $username,
141 /* $user */ 14146,
142 /* $by */ 0,
143 /* $reason */ 'crosswiki block...',
144 /* $timestamp */ wfTimestampNow(),
145 /* $auto */ false,
146 /* $expiry */ $this->db->getInfinity(),
147 /* anonOnly */ false,
148 /* $createAccount */ true,
149 /* $enableAutoblock */ true,
150 /* $hideName (ipb_deleted) */ true,
151 /* $blockEmail */ true,
152 /* $allowUsertalk */ false,
153 /* $byName */ 'MetaWikiUser'
154 );
155 $block->insert();
156
157 // Reload block from DB
158 $userBlock = Block::newFromTarget( $username );
159 $this->assertTrue(
160 (bool)$block->prevents( 'createaccount' ),
161 "Block object in DB should prevents 'createaccount'"
162 );
163
164 $this->assertInstanceOf(
165 'Block',
166 $userBlock,
167 "'$username' block block object should be existent"
168 );
169
170 // Reload user
171 $u = User::newFromName( $username );
172 $this->assertTrue(
173 (bool)$u->isBlockedFromCreateAccount(),
174 "Our sandbox user '$username' should NOT be able to create account"
175 );
176 }
177
178 /**
179 * @covers Block::insert
180 */
181 public function testCrappyCrossWikiBlocks() {
182 // Delete the last round's block if it's still there
183 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
184 if ( $oldBlock ) {
185 // An old block will prevent our new one from saving.
186 $oldBlock->delete();
187 }
188
189 // Foreign perspective (blockee not on current wiki)...
190 $block = new Block(
191 /* $address */ 'UserOnForeignWiki',
192 /* $user */ 14146,
193 /* $by */ 0,
194 /* $reason */ 'crosswiki block...',
195 /* $timestamp */ wfTimestampNow(),
196 /* $auto */ false,
197 /* $expiry */ $this->db->getInfinity(),
198 /* anonOnly */ false,
199 /* $createAccount */ true,
200 /* $enableAutoblock */ true,
201 /* $hideName (ipb_deleted) */ true,
202 /* $blockEmail */ true,
203 /* $allowUsertalk */ false,
204 /* $byName */ 'MetaWikiUser'
205 );
206
207 $res = $block->insert( $this->db );
208 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
209
210 // Local perspective (blockee on current wiki)...
211 $user = User::newFromName( 'UserOnForeignWiki' );
212 $user->addToDatabase();
213 // Set user ID to match the test value
214 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
215 $user = null; // clear
216
217 $block = Block::newFromID( $res['id'] );
218 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
219 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
220 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
221 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
222 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
223 }
224
225 protected function addXffBlocks() {
226 static $inited = false;
227
228 if ( $inited ) {
229 return;
230 }
231
232 $inited = true;
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
293 public static function providerXff() {
294 return array(
295 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
296 'count' => 2,
297 'result' => 'Range Hardblock'
298 ),
299 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
300 'count' => 2,
301 'result' => 'Range Softblock with AC Disabled'
302 ),
303 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
304 'count' => 2,
305 'result' => 'Exact Softblock'
306 ),
307 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
308 'count' => 3,
309 'result' => 'Exact Softblock'
310 ),
311 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
312 'count' => 2,
313 'result' => 'Range Hardblock'
314 ),
315 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
316 'count' => 2,
317 'result' => 'Range Hardblock'
318 ),
319 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
320 'count' => 2,
321 'result' => 'Range Softblock with AC Disabled'
322 ),
323 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
324 'count' => 2,
325 'result' => 'Exact Softblock'
326 ),
327 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
328 'count' => 1,
329 'result' => 'Range Softblock with AC Disabled'
330 ),
331 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
332 'count' => 2,
333 'result' => 'Range6 Hardblock'
334 ),
335 );
336 }
337
338 /**
339 * @dataProvider providerXff
340 * @covers Block::getBlocksForIPList
341 * @covers Block::chooseBlock
342 */
343 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
344 $list = array_map( 'trim', explode( ',', $xff ) );
345 $xffblocks = Block::getBlocksForIPList( $list, true );
346 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
347 $block = Block::chooseBlock( $xffblocks, $list );
348 $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
349 }
350 }