Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / BlockTest.php
blobb248d24ebc5cd7d47c9ea602f95346792f437c59
1 <?php
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
9 /** @var Block */
10 private $block;
11 private $madeAt;
13 /* variable used to save up the blockID we insert in this test suite */
14 private $blockId;
16 protected function setUp() {
17 parent::setUp();
18 $this->setMwGlobals( array(
19 'wgLanguageCode' => 'en',
20 'wgContLang' => Language::factory( 'en' )
21 ) );
24 function addDBData() {
26 $user = User::newFromName( 'UTBlockee' );
27 if ( $user->getID() == 0 ) {
28 $user->addToDatabase();
29 $user->setPassword( 'UTBlockeePassword' );
31 $user->saveSettings();
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();
41 $this->block = new Block( 'UTBlockee', $user->getID(), 0,
42 'Parce que', 0, false, time() + 100500
44 $this->madeAt = wfTimestamp( TS_MW );
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?" );
57 $this->addXffBlocks();
60 /**
61 * debug function : dump the ipblocks table
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 );
71 /**
72 * @covers Block::newFromTarget
74 public function testINewFromTargetReturnsCorrectBlock() {
75 $this->assertTrue(
76 $this->block->equals( Block::newFromTarget( 'UTBlockee' ) ),
77 "newFromTarget() returns the same block as the one that was made"
81 /**
82 * @covers Block::newFromID
84 public function testINewFromIDReturnsCorrectBlock() {
85 $this->assertTrue(
86 $this->block->equals( Block::newFromID( $this->blockId ) ),
87 "newFromID() returns the same block as the one that was made"
91 /**
92 * per bug 26425
94 public function testBug26425BlockTimestampDefaultsToTime() {
95 // delta to stop one-off errors when things happen to go over a second mark.
96 $delta = abs( $this->madeAt - $this->block->mTimestamp );
97 $this->assertLessThan(
99 $delta,
100 "If no timestamp is specified, the block is recorded as time()"
105 * CheckUser since being changed to use Block::newFromTarget started failing
106 * because the new function didn't accept empty strings like Block::load()
107 * had. Regression bug 29116.
109 * @dataProvider provideBug29116Data
110 * @covers Block::newFromTarget
112 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
113 $block = Block::newFromTarget( 'UTBlockee', $vagueTarget );
114 $this->assertTrue(
115 $this->block->equals( $block ),
116 "newFromTarget() returns the same block as the one that was made when "
117 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
121 public static function provideBug29116Data() {
122 return array(
123 array( null ),
124 array( '' ),
125 array( false )
130 * @covers Block::prevents
132 public function testBlockedUserCanNotCreateAccount() {
133 $username = 'BlockedUserToCreateAccountWith';
134 $u = User::newFromName( $username );
135 $u->setPassword( 'NotRandomPass' );
136 $u->addToDatabase();
137 unset( $u );
139 // Sanity check
140 $this->assertNull(
141 Block::newFromTarget( $username ),
142 "$username should not be blocked"
145 // Reload user
146 $u = User::newFromName( $username );
147 $this->assertFalse(
148 $u->isBlockedFromCreateAccount(),
149 "Our sandbox user should be able to create account before being blocked"
152 // Foreign perspective (blockee not on current wiki)...
153 $block = new Block(
154 /* $address */ $username,
155 /* $user */ 14146,
156 /* $by */ 0,
157 /* $reason */ 'crosswiki block...',
158 /* $timestamp */ wfTimestampNow(),
159 /* $auto */ false,
160 /* $expiry */ $this->db->getInfinity(),
161 /* anonOnly */ false,
162 /* $createAccount */ true,
163 /* $enableAutoblock */ true,
164 /* $hideName (ipb_deleted) */ true,
165 /* $blockEmail */ true,
166 /* $allowUsertalk */ false,
167 /* $byName */ 'MetaWikiUser'
169 $block->insert();
171 // Reload block from DB
172 $userBlock = Block::newFromTarget( $username );
173 $this->assertTrue(
174 (bool)$block->prevents( 'createaccount' ),
175 "Block object in DB should prevents 'createaccount'"
178 $this->assertInstanceOf(
179 'Block',
180 $userBlock,
181 "'$username' block block object should be existent"
184 // Reload user
185 $u = User::newFromName( $username );
186 $this->assertTrue(
187 (bool)$u->isBlockedFromCreateAccount(),
188 "Our sandbox user '$username' should NOT be able to create account"
193 * @covers Block::insert
195 public function testCrappyCrossWikiBlocks() {
196 // Delete the last round's block if it's still there
197 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
198 if ( $oldBlock ) {
199 // An old block will prevent our new one from saving.
200 $oldBlock->delete();
203 // Foreign perspective (blockee not on current wiki)...
204 $block = new Block(
205 /* $address */ 'UserOnForeignWiki',
206 /* $user */ 14146,
207 /* $by */ 0,
208 /* $reason */ 'crosswiki block...',
209 /* $timestamp */ wfTimestampNow(),
210 /* $auto */ false,
211 /* $expiry */ $this->db->getInfinity(),
212 /* anonOnly */ false,
213 /* $createAccount */ true,
214 /* $enableAutoblock */ true,
215 /* $hideName (ipb_deleted) */ true,
216 /* $blockEmail */ true,
217 /* $allowUsertalk */ false,
218 /* $byName */ 'MetaWikiUser'
221 $res = $block->insert( $this->db );
222 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
224 // Local perspective (blockee on current wiki)...
225 $user = User::newFromName( 'UserOnForeignWiki' );
226 $user->addToDatabase();
227 // Set user ID to match the test value
228 $this->db->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
229 $user = null; // clear
231 $block = Block::newFromID( $res['id'] );
232 $this->assertEquals(
233 'UserOnForeignWiki',
234 $block->getTarget()->getName(),
235 'Correct blockee name'
237 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
238 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
239 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
240 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
243 protected function addXffBlocks() {
244 static $inited = false;
246 if ( $inited ) {
247 return;
250 $inited = true;
252 $blockList = array(
253 array( 'target' => '70.2.0.0/16',
254 'type' => Block::TYPE_RANGE,
255 'desc' => 'Range Hardblock',
256 'ACDisable' => false,
257 'isHardblock' => true,
258 'isAutoBlocking' => false,
260 array( 'target' => '2001:4860:4001::/48',
261 'type' => Block::TYPE_RANGE,
262 'desc' => 'Range6 Hardblock',
263 'ACDisable' => false,
264 'isHardblock' => true,
265 'isAutoBlocking' => false,
267 array( 'target' => '60.2.0.0/16',
268 'type' => Block::TYPE_RANGE,
269 'desc' => 'Range Softblock with AC Disabled',
270 'ACDisable' => true,
271 'isHardblock' => false,
272 'isAutoBlocking' => false,
274 array( 'target' => '50.2.0.0/16',
275 'type' => Block::TYPE_RANGE,
276 'desc' => 'Range Softblock',
277 'ACDisable' => false,
278 'isHardblock' => false,
279 'isAutoBlocking' => false,
281 array( 'target' => '50.1.1.1',
282 'type' => Block::TYPE_IP,
283 'desc' => 'Exact Softblock',
284 'ACDisable' => false,
285 'isHardblock' => false,
286 'isAutoBlocking' => false,
290 foreach ( $blockList as $insBlock ) {
291 $target = $insBlock['target'];
293 if ( $insBlock['type'] === Block::TYPE_IP ) {
294 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
295 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
296 $target = IP::sanitizeRange( $target );
299 $block = new Block();
300 $block->setTarget( $target );
301 $block->setBlocker( 'testblocker@global' );
302 $block->mReason = $insBlock['desc'];
303 $block->mExpiry = 'infinity';
304 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
305 $block->isHardblock( $insBlock['isHardblock'] );
306 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
307 $block->insert();
311 public static function providerXff() {
312 return array(
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'
317 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
318 'count' => 2,
319 'result' => 'Range Softblock with AC Disabled'
321 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
322 'count' => 2,
323 'result' => 'Exact Softblock'
325 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
326 'count' => 3,
327 'result' => 'Exact Softblock'
329 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
330 'count' => 2,
331 'result' => 'Range Hardblock'
333 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
334 'count' => 2,
335 'result' => 'Range Hardblock'
337 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
338 'count' => 2,
339 'result' => 'Range Softblock with AC Disabled'
341 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
342 'count' => 2,
343 'result' => 'Exact Softblock'
345 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
346 'count' => 1,
347 'result' => 'Range Softblock with AC Disabled'
349 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
350 'count' => 2,
351 'result' => 'Range6 Hardblock'
357 * @dataProvider providerXff
358 * @covers Block::getBlocksForIPList
359 * @covers Block::chooseBlock
361 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
362 $list = array_map( 'trim', explode( ',', $xff ) );
363 $xffblocks = Block::getBlocksForIPList( $list, true );
364 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
365 $block = Block::chooseBlock( $xffblocks, $list );
366 $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );