3 use MediaWiki\Block\BlockRestriction
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
11 class BlockTest
extends MediaWikiLangTestCase
{
16 private function getUserForBlocking() {
17 $testUser = $this->getMutableTestUser();
18 $user = $testUser->getUser();
19 $user->addToDatabase();
20 TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
21 $user->saveSettings();
31 private function addBlockForUser( User
$user ) {
32 // Delete the last round's block if it's still there
33 $oldBlock = Block
::newFromTarget( $user->getName() );
35 // An old block will prevent our new one from saving.
40 'address' => $user->getName(),
41 'user' => $user->getId(),
42 'by' => $this->getTestSysop()->getUser()->getId(),
43 'reason' => 'Parce que',
44 'expiry' => time() +
100500,
46 $block = new Block( $blockOptions );
49 // save up ID for use in assertion. Since ID is an autoincrement,
50 // its value might change depending on the order the tests are run.
51 // ApiBlockTest insert its own blocks!
52 if ( !$block->getId() ) {
53 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
56 $this->addXffBlocks();
62 * @covers Block::newFromTarget
64 public function testINewFromTargetReturnsCorrectBlock() {
65 $user = $this->getUserForBlocking();
66 $block = $this->addBlockForUser( $user );
69 $block->equals( Block
::newFromTarget( $user->getName() ) ),
70 "newFromTarget() returns the same block as the one that was made"
75 * @covers Block::newFromID
77 public function testINewFromIDReturnsCorrectBlock() {
78 $user = $this->getUserForBlocking();
79 $block = $this->addBlockForUser( $user );
82 $block->equals( Block
::newFromID( $block->getId() ) ),
83 "newFromID() returns the same block as the one that was made"
89 * @covers Block::__construct
91 public function testT28425BlockTimestampDefaultsToTime() {
92 $user = $this->getUserForBlocking();
93 $block = $this->addBlockForUser( $user );
94 $madeAt = wfTimestamp( TS_MW
);
96 // delta to stop one-off errors when things happen to go over a second mark.
97 $delta = abs( $madeAt - $block->mTimestamp
);
98 $this->assertLessThan(
101 "If no timestamp is specified, the block is recorded as time()"
106 * CheckUser since being changed to use Block::newFromTarget started failing
107 * because the new function didn't accept empty strings like Block::load()
108 * had. Regression T31116.
110 * @dataProvider provideT31116Data
111 * @covers Block::newFromTarget
113 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
114 $user = $this->getUserForBlocking();
115 $initialBlock = $this->addBlockForUser( $user );
116 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
119 $initialBlock->equals( $block ),
120 "newFromTarget() returns the same block as the one that was made when "
121 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
125 public static function provideT31116Data() {
134 * @covers Block::prevents
136 public function testBlockedUserCanNotCreateAccount() {
137 $username = 'BlockedUserToCreateAccountWith';
138 $u = User
::newFromName( $username );
140 $userId = $u->getId();
141 $this->assertNotEquals( 0, $userId, 'sanity' );
142 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
147 Block
::newFromTarget( $username ),
148 "$username should not be blocked"
152 $u = User
::newFromName( $username );
154 $u->isBlockedFromCreateAccount(),
155 "Our sandbox user should be able to create account before being blocked"
158 // Foreign perspective (blockee not on current wiki)...
160 'address' => $username,
162 'reason' => 'crosswiki block...',
163 'timestamp' => wfTimestampNow(),
164 'expiry' => $this->db
->getInfinity(),
165 'createAccount' => true,
166 'enableAutoblock' => true,
168 'blockEmail' => true,
169 'byText' => 'm>MetaWikiUser',
171 $block = new Block( $blockOptions );
174 // Reload block from DB
175 $userBlock = Block
::newFromTarget( $username );
177 (bool)$block->prevents( 'createaccount' ),
178 "Block object in DB should prevents 'createaccount'"
181 $this->assertInstanceOf(
184 "'$username' block block object should be existent"
188 $u = User
::newFromName( $username );
190 (bool)$u->isBlockedFromCreateAccount(),
191 "Our sandbox user '$username' should NOT be able to create account"
196 * @covers Block::insert
198 public function testCrappyCrossWikiBlocks() {
199 // Delete the last round's block if it's still there
200 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
202 // An old block will prevent our new one from saving.
206 // Local perspective (blockee on current wiki)...
207 $user = User
::newFromName( 'UserOnForeignWiki' );
208 $user->addToDatabase();
209 $userId = $user->getId();
210 $this->assertNotEquals( 0, $userId, 'sanity' );
212 // Foreign perspective (blockee not on current wiki)...
214 'address' => 'UserOnForeignWiki',
215 'user' => $user->getId(),
216 'reason' => 'crosswiki block...',
217 'timestamp' => wfTimestampNow(),
218 'expiry' => $this->db
->getInfinity(),
219 'createAccount' => true,
220 'enableAutoblock' => true,
222 'blockEmail' => true,
223 'byText' => 'Meta>MetaWikiUser',
225 $block = new Block( $blockOptions );
227 $res = $block->insert( $this->db
);
228 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
230 $user = null; // clear
232 $block = Block
::newFromID( $res['id'] );
235 $block->getTarget()->getName(),
236 'Correct blockee name'
238 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
239 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
240 'Correct blocker name' );
241 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
242 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
245 protected function addXffBlocks() {
246 static $inited = false;
255 [ 'target' => '70.2.0.0/16',
256 'type' => Block
::TYPE_RANGE
,
257 'desc' => 'Range Hardblock',
258 'ACDisable' => false,
259 'isHardblock' => true,
260 'isAutoBlocking' => false,
262 [ 'target' => '2001:4860:4001::/48',
263 'type' => Block
::TYPE_RANGE
,
264 'desc' => 'Range6 Hardblock',
265 'ACDisable' => false,
266 'isHardblock' => true,
267 'isAutoBlocking' => false,
269 [ 'target' => '60.2.0.0/16',
270 'type' => Block
::TYPE_RANGE
,
271 'desc' => 'Range Softblock with AC Disabled',
273 'isHardblock' => false,
274 'isAutoBlocking' => false,
276 [ 'target' => '50.2.0.0/16',
277 'type' => Block
::TYPE_RANGE
,
278 'desc' => 'Range Softblock',
279 'ACDisable' => false,
280 'isHardblock' => false,
281 'isAutoBlocking' => false,
283 [ 'target' => '50.1.1.1',
284 'type' => Block
::TYPE_IP
,
285 'desc' => 'Exact Softblock',
286 'ACDisable' => false,
287 'isHardblock' => false,
288 'isAutoBlocking' => false,
292 $blocker = $this->getTestUser()->getUser();
293 foreach ( $blockList as $insBlock ) {
294 $target = $insBlock['target'];
296 if ( $insBlock['type'] === Block
::TYPE_IP
) {
297 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
298 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
299 $target = IP
::sanitizeRange( $target );
302 $block = new Block();
303 $block->setTarget( $target );
304 $block->setBlocker( $blocker );
305 $block->mReason
= $insBlock['desc'];
306 $block->mExpiry
= 'infinity';
307 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
308 $block->isHardblock( $insBlock['isHardblock'] );
309 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
314 public static function providerXff() {
316 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
318 'result' => 'Range Hardblock'
320 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
322 'result' => 'Range Softblock with AC Disabled'
324 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
326 'result' => 'Exact Softblock'
328 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
330 'result' => 'Exact Softblock'
332 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
334 'result' => 'Range Hardblock'
336 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
338 'result' => 'Range Hardblock'
340 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
342 'result' => 'Range Softblock with AC Disabled'
344 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
346 'result' => 'Exact Softblock'
348 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
350 'result' => 'Range Softblock with AC Disabled'
352 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
354 'result' => 'Range6 Hardblock'
360 * @dataProvider providerXff
361 * @covers Block::getBlocksForIPList
362 * @covers Block::chooseBlock
364 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
365 $user = $this->getUserForBlocking();
366 $this->addBlockForUser( $user );
368 $list = array_map( 'trim', explode( ',', $xff ) );
369 $xffblocks = Block
::getBlocksForIPList( $list, true );
370 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
371 $block = Block
::chooseBlock( $xffblocks, $list );
372 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );
376 * @covers Block::__construct
378 public function testDeprecatedConstructor() {
379 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
380 $username = 'UnthinkablySecretRandomUsername';
381 $reason = 'being irrational';
384 $u = User
::newFromName( $username );
385 if ( $u->getId() == 0 ) {
387 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
391 # Make sure the user isn't blocked
393 Block
::newFromTarget( $username ),
394 "$username should not be blocked"
399 /* address */ $username,
401 /* by */ $this->getTestSysop()->getUser()->getId(),
402 /* reason */ $reason,
411 $block->getTarget()->getName(),
413 "Target should be set properly"
416 # Check supplied parameter
420 "Reason should be non-default"
423 # Check default parameter
425 (bool)$block->prevents( 'createaccount' ),
426 "Account creation should not be blocked by default"
431 * @covers Block::getSystemBlockType
432 * @covers Block::insert
433 * @covers Block::doAutoblock
435 public function testSystemBlocks() {
436 $user = $this->getUserForBlocking();
437 $this->addBlockForUser( $user );
440 'address' => $user->getName(),
441 'reason' => 'test system block',
442 'timestamp' => wfTimestampNow(),
443 'expiry' => $this->db
->getInfinity(),
444 'byText' => 'MediaWiki default',
445 'systemBlock' => 'test',
446 'enableAutoblock' => true,
448 $block = new Block( $blockOptions );
450 $this->assertSame( 'test', $block->getSystemBlockType() );
454 $this->fail( 'Expected exception not thrown' );
455 } catch ( MWException
$ex ) {
456 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
460 $block->doAutoblock( '192.0.2.2' );
461 $this->fail( 'Expected exception not thrown' );
462 } catch ( MWException
$ex ) {
463 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
468 * @covers Block::newFromRow
470 public function testNewFromRow() {
471 $badActor = $this->getTestUser()->getUser();
472 $sysop = $this->getTestSysop()->getUser();
474 $block = new Block( [
475 'address' => $badActor->getName(),
476 'user' => $badActor->getId(),
477 'by' => $sysop->getId(),
478 'expiry' => 'infinity',
482 $blockQuery = Block
::getQueryInfo();
483 $row = $this->db
->select(
484 $blockQuery['tables'],
485 $blockQuery['fields'],
487 'ipb_id' => $block->getId(),
494 $block = Block
::newFromRow( $row );
495 $this->assertInstanceOf( Block
::class, $block );
496 $this->assertEquals( $block->getBy(), $sysop->getId() );
497 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
502 * @covers Block::equals
504 public function testEquals() {
505 $block = new Block();
507 $this->assertTrue( $block->equals( $block ) );
509 $partial = new Block( [
512 $this->assertFalse( $block->equals( $partial ) );
516 * @covers Block::isSitewide
518 public function testIsSitewide() {
519 $block = new Block();
520 $this->assertTrue( $block->isSitewide() );
522 $block = new Block( [
525 $this->assertTrue( $block->isSitewide() );
527 $block = new Block( [
530 $this->assertFalse( $block->isSitewide() );
532 $block = new Block( [
535 $block->isSitewide( true );
536 $this->assertTrue( $block->isSitewide() );
540 * @covers Block::getRestrictions
541 * @covers Block::setRestrictions
543 public function testRestrictions() {
544 $block = new Block();
546 new PageRestriction( 0, 1 )
548 $block->setRestrictions( $restrictions );
550 $this->assertSame( $restrictions, $block->getRestrictions() );
554 * @covers Block::getRestrictions
555 * @covers Block::insert
557 public function testRestrictionsFromDatabase() {
558 $badActor = $this->getTestUser()->getUser();
559 $sysop = $this->getTestSysop()->getUser();
561 $block = new Block( [
562 'address' => $badActor->getName(),
563 'user' => $badActor->getId(),
564 'by' => $sysop->getId(),
565 'expiry' => 'infinity',
567 $page = $this->getExistingTestPage( 'Foo' );
568 $restriction = new PageRestriction( 0, $page->getId() );
569 $block->setRestrictions( [ $restriction ] );
572 // Refresh the block from the database.
573 $block = Block
::newFromID( $block->getId() );
574 $restrictions = $block->getRestrictions();
575 $this->assertCount( 1, $restrictions );
576 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
581 * @covers Block::insert
583 public function testInsertExistingBlock() {
584 $badActor = $this->getTestUser()->getUser();
585 $sysop = $this->getTestSysop()->getUser();
587 $block = new Block( [
588 'address' => $badActor->getName(),
589 'user' => $badActor->getId(),
590 'by' => $sysop->getId(),
591 'expiry' => 'infinity',
593 $page = $this->getExistingTestPage( 'Foo' );
594 $restriction = new PageRestriction( 0, $page->getId() );
595 $block->setRestrictions( [ $restriction ] );
598 // Insert the block again, which should result in a failur
599 $result = $block->insert();
601 $this->assertFalse( $result );
603 // Ensure that there are no restrictions where the blockId is 0.
604 $count = $this->db
->selectRowCount(
605 'ipblocks_restrictions',
607 [ 'ir_ipb_id' => 0 ],
610 $this->assertSame( 0, $count );
616 * @covers Block::appliesToTitle
618 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
619 $user = $this->getTestUser()->getUser();
620 $block = new Block( [
621 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
622 'allowUsertalk' => true,
626 $block->setTarget( $user );
627 $block->setBlocker( $this->getTestSysop()->getUser() );
630 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
632 $this->assertTrue( $block->appliesToTitle( $title ) );
634 // appliesToTitle() ignores allowUsertalk
635 $title = $user->getTalkPage();
636 $this->assertTrue( $block->appliesToTitle( $title ) );
642 * @covers Block::appliesToTitle
644 public function testAppliesToTitleOnPartialBlock() {
645 $user = $this->getTestUser()->getUser();
646 $block = new Block( [
647 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
648 'allowUsertalk' => true,
652 $block->setTarget( $user );
653 $block->setBlocker( $this->getTestSysop()->getUser() );
656 $pageFoo = $this->getExistingTestPage( 'Foo' );
657 $pageBar = $this->getExistingTestPage( 'Bar' );
658 $pageJohn = $this->getExistingTestPage( 'User:John' );
660 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
661 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
662 BlockRestriction
::insert( [ $pageRestriction, $namespaceRestriction ] );
664 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
665 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
666 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
672 * @covers Block::appliesToNamespace
673 * @covers Block::appliesToPage
675 public function testAppliesToReturnsTrueOnSitewideBlock() {
676 $user = $this->getTestUser()->getUser();
677 $block = new Block( [
678 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
679 'allowUsertalk' => true,
683 $block->setTarget( $user );
684 $block->setBlocker( $this->getTestSysop()->getUser() );
687 $title = $this->getExistingTestPage()->getTitle();
689 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
690 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
691 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
697 * @covers Block::appliesToPage
699 public function testAppliesToPageOnPartialPageBlock() {
700 $user = $this->getTestUser()->getUser();
701 $block = new Block( [
702 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
703 'allowUsertalk' => true,
707 $block->setTarget( $user );
708 $block->setBlocker( $this->getTestSysop()->getUser() );
711 $title = $this->getExistingTestPage()->getTitle();
713 $pageRestriction = new PageRestriction(
715 $title->getArticleID()
717 BlockRestriction
::insert( [ $pageRestriction ] );
719 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
725 * @covers Block::appliesToNamespace
727 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
728 $user = $this->getTestUser()->getUser();
729 $block = new Block( [
730 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
731 'allowUsertalk' => true,
735 $block->setTarget( $user );
736 $block->setBlocker( $this->getTestSysop()->getUser() );
739 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN
);
740 BlockRestriction
::insert( [ $namespaceRestriction ] );
742 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
743 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );