3 use MediaWiki\Block\BlockRestriction
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
10 class BlockTest
extends MediaWikiLangTestCase
{
15 private function getUserForBlocking() {
16 $testUser = $this->getMutableTestUser();
17 $user = $testUser->getUser();
18 $user->addToDatabase();
19 TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
20 $user->saveSettings();
30 private function addBlockForUser( User
$user ) {
31 // Delete the last round's block if it's still there
32 $oldBlock = Block
::newFromTarget( $user->getName() );
34 // An old block will prevent our new one from saving.
39 'address' => $user->getName(),
40 'user' => $user->getId(),
41 'by' => $this->getTestSysop()->getUser()->getId(),
42 'reason' => 'Parce que',
43 'expiry' => time() +
100500,
45 $block = new Block( $blockOptions );
48 // save up ID for use in assertion. Since ID is an autoincrement,
49 // its value might change depending on the order the tests are run.
50 // ApiBlockTest insert its own blocks!
51 if ( !$block->getId() ) {
52 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55 $this->addXffBlocks();
61 * @covers Block::newFromTarget
63 public function testINewFromTargetReturnsCorrectBlock() {
64 $user = $this->getUserForBlocking();
65 $block = $this->addBlockForUser( $user );
68 $block->equals( Block
::newFromTarget( $user->getName() ) ),
69 "newFromTarget() returns the same block as the one that was made"
74 * @covers Block::newFromID
76 public function testINewFromIDReturnsCorrectBlock() {
77 $user = $this->getUserForBlocking();
78 $block = $this->addBlockForUser( $user );
81 $block->equals( Block
::newFromID( $block->getId() ) ),
82 "newFromID() returns the same block as the one that was made"
88 * @covers Block::__construct
90 public function testT28425BlockTimestampDefaultsToTime() {
91 $user = $this->getUserForBlocking();
92 $block = $this->addBlockForUser( $user );
93 $madeAt = wfTimestamp( TS_MW
);
95 // delta to stop one-off errors when things happen to go over a second mark.
96 $delta = abs( $madeAt - $block->mTimestamp
);
97 $this->assertLessThan(
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 T31116.
109 * @dataProvider provideT31116Data
110 * @covers Block::newFromTarget
112 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
113 $user = $this->getUserForBlocking();
114 $initialBlock = $this->addBlockForUser( $user );
115 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
118 $initialBlock->equals( $block ),
119 "newFromTarget() returns the same block as the one that was made when "
120 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
124 public static function provideT31116Data() {
133 * @covers Block::prevents
135 public function testBlockedUserCanNotCreateAccount() {
136 $username = 'BlockedUserToCreateAccountWith';
137 $u = User
::newFromName( $username );
139 $userId = $u->getId();
140 $this->assertNotEquals( 0, $userId, 'sanity' );
141 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
146 Block
::newFromTarget( $username ),
147 "$username should not be blocked"
151 $u = User
::newFromName( $username );
153 $u->isBlockedFromCreateAccount(),
154 "Our sandbox user should be able to create account before being blocked"
157 // Foreign perspective (blockee not on current wiki)...
159 'address' => $username,
161 'reason' => 'crosswiki block...',
162 'timestamp' => wfTimestampNow(),
163 'expiry' => $this->db
->getInfinity(),
164 'createAccount' => true,
165 'enableAutoblock' => true,
167 'blockEmail' => true,
168 'byText' => 'm>MetaWikiUser',
170 $block = new Block( $blockOptions );
173 // Reload block from DB
174 $userBlock = Block
::newFromTarget( $username );
176 (bool)$block->prevents( 'createaccount' ),
177 "Block object in DB should prevents 'createaccount'"
180 $this->assertInstanceOf(
183 "'$username' block block object should be existent"
187 $u = User
::newFromName( $username );
189 (bool)$u->isBlockedFromCreateAccount(),
190 "Our sandbox user '$username' should NOT be able to create account"
195 * @covers Block::insert
197 public function testCrappyCrossWikiBlocks() {
198 // Delete the last round's block if it's still there
199 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
201 // An old block will prevent our new one from saving.
205 // Local perspective (blockee on current wiki)...
206 $user = User
::newFromName( 'UserOnForeignWiki' );
207 $user->addToDatabase();
208 $userId = $user->getId();
209 $this->assertNotEquals( 0, $userId, 'sanity' );
211 // Foreign perspective (blockee not on current wiki)...
213 'address' => 'UserOnForeignWiki',
214 'user' => $user->getId(),
215 'reason' => 'crosswiki block...',
216 'timestamp' => wfTimestampNow(),
217 'expiry' => $this->db
->getInfinity(),
218 'createAccount' => true,
219 'enableAutoblock' => true,
221 'blockEmail' => true,
222 'byText' => 'Meta>MetaWikiUser',
224 $block = new Block( $blockOptions );
226 $res = $block->insert( $this->db
);
227 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
229 $user = null; // clear
231 $block = Block
::newFromID( $res['id'] );
234 $block->getTarget()->getName(),
235 'Correct blockee name'
237 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
238 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
239 'Correct blocker name' );
240 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
241 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
244 protected function addXffBlocks() {
245 static $inited = false;
254 [ 'target' => '70.2.0.0/16',
255 'type' => Block
::TYPE_RANGE
,
256 'desc' => 'Range Hardblock',
257 'ACDisable' => false,
258 'isHardblock' => true,
259 'isAutoBlocking' => false,
261 [ 'target' => '2001:4860:4001::/48',
262 'type' => Block
::TYPE_RANGE
,
263 'desc' => 'Range6 Hardblock',
264 'ACDisable' => false,
265 'isHardblock' => true,
266 'isAutoBlocking' => false,
268 [ 'target' => '60.2.0.0/16',
269 'type' => Block
::TYPE_RANGE
,
270 'desc' => 'Range Softblock with AC Disabled',
272 'isHardblock' => false,
273 'isAutoBlocking' => false,
275 [ 'target' => '50.2.0.0/16',
276 'type' => Block
::TYPE_RANGE
,
277 'desc' => 'Range Softblock',
278 'ACDisable' => false,
279 'isHardblock' => false,
280 'isAutoBlocking' => false,
282 [ 'target' => '50.1.1.1',
283 'type' => Block
::TYPE_IP
,
284 'desc' => 'Exact Softblock',
285 'ACDisable' => false,
286 'isHardblock' => false,
287 'isAutoBlocking' => false,
291 $blocker = $this->getTestUser()->getUser();
292 foreach ( $blockList as $insBlock ) {
293 $target = $insBlock['target'];
295 if ( $insBlock['type'] === Block
::TYPE_IP
) {
296 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
297 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
298 $target = IP
::sanitizeRange( $target );
301 $block = new Block();
302 $block->setTarget( $target );
303 $block->setBlocker( $blocker );
304 $block->mReason
= $insBlock['desc'];
305 $block->mExpiry
= 'infinity';
306 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
307 $block->isHardblock( $insBlock['isHardblock'] );
308 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
313 public static function providerXff() {
315 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
317 'result' => 'Range Hardblock'
319 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
321 'result' => 'Range Softblock with AC Disabled'
323 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
325 'result' => 'Exact Softblock'
327 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
329 'result' => 'Exact Softblock'
331 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
333 'result' => 'Range Hardblock'
335 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
337 'result' => 'Range Hardblock'
339 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
341 'result' => 'Range Softblock with AC Disabled'
343 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
345 'result' => 'Exact Softblock'
347 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
349 'result' => 'Range Softblock with AC Disabled'
351 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
353 'result' => 'Range6 Hardblock'
359 * @dataProvider providerXff
360 * @covers Block::getBlocksForIPList
361 * @covers Block::chooseBlock
363 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
364 $user = $this->getUserForBlocking();
365 $this->addBlockForUser( $user );
367 $list = array_map( 'trim', explode( ',', $xff ) );
368 $xffblocks = Block
::getBlocksForIPList( $list, true );
369 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
370 $block = Block
::chooseBlock( $xffblocks, $list );
371 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );
375 * @covers Block::__construct
377 public function testDeprecatedConstructor() {
378 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
379 $username = 'UnthinkablySecretRandomUsername';
380 $reason = 'being irrational';
383 $u = User
::newFromName( $username );
384 if ( $u->getId() == 0 ) {
386 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
390 # Make sure the user isn't blocked
392 Block
::newFromTarget( $username ),
393 "$username should not be blocked"
398 /* address */ $username,
400 /* by */ $this->getTestSysop()->getUser()->getId(),
401 /* reason */ $reason,
410 $block->getTarget()->getName(),
412 "Target should be set properly"
415 # Check supplied parameter
419 "Reason should be non-default"
422 # Check default parameter
424 (bool)$block->prevents( 'createaccount' ),
425 "Account creation should not be blocked by default"
430 * @covers Block::getSystemBlockType
431 * @covers Block::insert
432 * @covers Block::doAutoblock
434 public function testSystemBlocks() {
435 $user = $this->getUserForBlocking();
436 $this->addBlockForUser( $user );
439 'address' => $user->getName(),
440 'reason' => 'test system block',
441 'timestamp' => wfTimestampNow(),
442 'expiry' => $this->db
->getInfinity(),
443 'byText' => 'MediaWiki default',
444 'systemBlock' => 'test',
445 'enableAutoblock' => true,
447 $block = new Block( $blockOptions );
449 $this->assertSame( 'test', $block->getSystemBlockType() );
453 $this->fail( 'Expected exception not thrown' );
454 } catch ( MWException
$ex ) {
455 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
459 $block->doAutoblock( '192.0.2.2' );
460 $this->fail( 'Expected exception not thrown' );
461 } catch ( MWException
$ex ) {
462 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
467 * @covers Block::newFromRow
469 public function testNewFromRow() {
470 $badActor = $this->getTestUser()->getUser();
471 $sysop = $this->getTestSysop()->getUser();
473 $block = new Block( [
474 'address' => $badActor->getName(),
475 'user' => $badActor->getId(),
476 'by' => $sysop->getId(),
477 'expiry' => 'infinity',
481 $blockQuery = Block
::getQueryInfo();
482 $row = $this->db
->select(
483 $blockQuery['tables'],
484 $blockQuery['fields'],
486 'ipb_id' => $block->getId(),
493 $block = Block
::newFromRow( $row );
494 $this->assertInstanceOf( Block
::class, $block );
495 $this->assertEquals( $block->getBy(), $sysop->getId() );
496 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
501 * @covers Block::equals
503 public function testEquals() {
504 $block = new Block();
506 $this->assertTrue( $block->equals( $block ) );
508 $partial = new Block( [
511 $this->assertFalse( $block->equals( $partial ) );
515 * @covers Block::isSitewide
517 public function testIsSitewide() {
518 $block = new Block();
519 $this->assertTrue( $block->isSitewide() );
521 $block = new Block( [
524 $this->assertTrue( $block->isSitewide() );
526 $block = new Block( [
529 $this->assertFalse( $block->isSitewide() );
531 $block = new Block( [
534 $block->isSitewide( true );
535 $this->assertTrue( $block->isSitewide() );
539 * @covers Block::getRestrictions
540 * @covers Block::setRestrictions
542 public function testRestrictions() {
543 $block = new Block();
545 new PageRestriction( 0, 1 )
547 $block->setRestrictions( $restrictions );
549 $this->assertSame( $restrictions, $block->getRestrictions() );
553 * @covers Block::getRestrictions
554 * @covers Block::insert
556 public function testRestrictionsFromDatabase() {
557 $badActor = $this->getTestUser()->getUser();
558 $sysop = $this->getTestSysop()->getUser();
560 $block = new Block( [
561 'address' => $badActor->getName(),
562 'user' => $badActor->getId(),
563 'by' => $sysop->getId(),
564 'expiry' => 'infinity',
566 $page = $this->getExistingTestPage( 'Foo' );
567 $restriction = new PageRestriction( 0, $page->getId() );
568 $block->setRestrictions( [ $restriction ] );
571 // Refresh the block from the database.
572 $block = Block
::newFromID( $block->getId() );
573 $restrictions = $block->getRestrictions();
574 $this->assertCount( 1, $restrictions );
575 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
580 * @covers Block::insert
582 public function testInsertExistingBlock() {
583 $badActor = $this->getTestUser()->getUser();
584 $sysop = $this->getTestSysop()->getUser();
586 $block = new Block( [
587 'address' => $badActor->getName(),
588 'user' => $badActor->getId(),
589 'by' => $sysop->getId(),
590 'expiry' => 'infinity',
592 $page = $this->getExistingTestPage( 'Foo' );
593 $restriction = new PageRestriction( 0, $page->getId() );
594 $block->setRestrictions( [ $restriction ] );
597 // Insert the block again, which should result in a failur
598 $result = $block->insert();
600 $this->assertFalse( $result );
602 // Ensure that there are no restrictions where the blockId is 0.
603 $count = $this->db
->selectRowCount(
604 'ipblocks_restrictions',
606 [ 'ir_ipb_id' => 0 ],
609 $this->assertSame( 0, $count );
615 * @covers Block::appliesToTitle
617 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
618 $user = $this->getTestUser()->getUser();
619 $block = new Block( [
620 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
621 'allowUsertalk' => true,
625 $block->setTarget( $user );
626 $block->setBlocker( $this->getTestSysop()->getUser() );
629 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
631 $this->assertTrue( $block->appliesToTitle( $title ) );
633 // appliesToTitle() ignores allowUsertalk
634 $title = $user->getTalkPage();
635 $this->assertTrue( $block->appliesToTitle( $title ) );
641 * @covers Block::appliesToTitle
643 public function testAppliesToTitleOnPartialBlock() {
644 $user = $this->getTestUser()->getUser();
645 $block = new Block( [
646 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
647 'allowUsertalk' => true,
651 $block->setTarget( $user );
652 $block->setBlocker( $this->getTestSysop()->getUser() );
655 $pageFoo = $this->getExistingTestPage( 'Foo' );
656 $pageBar = $this->getExistingTestPage( 'Bar' );
658 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
659 BlockRestriction
::insert( [ $pageRestriction ] );
661 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
662 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );