3 use MediaWiki\Block\BlockRestrictionStore
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
6 use MediaWiki\MediaWikiServices
;
12 class BlockTest
extends MediaWikiLangTestCase
{
17 private function getUserForBlocking() {
18 $testUser = $this->getMutableTestUser();
19 $user = $testUser->getUser();
20 $user->addToDatabase();
21 TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
22 $user->saveSettings();
32 private function addBlockForUser( User
$user ) {
33 // Delete the last round's block if it's still there
34 $oldBlock = Block
::newFromTarget( $user->getName() );
36 // An old block will prevent our new one from saving.
41 'address' => $user->getName(),
42 'user' => $user->getId(),
43 'by' => $this->getTestSysop()->getUser()->getId(),
44 'reason' => 'Parce que',
45 'expiry' => time() +
100500,
47 $block = new Block( $blockOptions );
50 // save up ID for use in assertion. Since ID is an autoincrement,
51 // its value might change depending on the order the tests are run.
52 // ApiBlockTest insert its own blocks!
53 if ( !$block->getId() ) {
54 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
57 $this->addXffBlocks();
63 * @covers Block::newFromTarget
65 public function testINewFromTargetReturnsCorrectBlock() {
66 $user = $this->getUserForBlocking();
67 $block = $this->addBlockForUser( $user );
70 $block->equals( Block
::newFromTarget( $user->getName() ) ),
71 "newFromTarget() returns the same block as the one that was made"
76 * @covers Block::newFromID
78 public function testINewFromIDReturnsCorrectBlock() {
79 $user = $this->getUserForBlocking();
80 $block = $this->addBlockForUser( $user );
83 $block->equals( Block
::newFromID( $block->getId() ) ),
84 "newFromID() returns the same block as the one that was made"
90 * @covers Block::__construct
92 public function testT28425BlockTimestampDefaultsToTime() {
93 $user = $this->getUserForBlocking();
94 $block = $this->addBlockForUser( $user );
95 $madeAt = wfTimestamp( TS_MW
);
97 // delta to stop one-off errors when things happen to go over a second mark.
98 $delta = abs( $madeAt - $block->getTimestamp() );
99 $this->assertLessThan(
102 "If no timestamp is specified, the block is recorded as time()"
107 * CheckUser since being changed to use Block::newFromTarget started failing
108 * because the new function didn't accept empty strings like Block::load()
109 * had. Regression T31116.
111 * @dataProvider provideT31116Data
112 * @covers Block::newFromTarget
114 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
115 $user = $this->getUserForBlocking();
116 $initialBlock = $this->addBlockForUser( $user );
117 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
120 $initialBlock->equals( $block ),
121 "newFromTarget() returns the same block as the one that was made when "
122 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
126 public static function provideT31116Data() {
135 * @dataProvider provideNewFromTargetRangeBlocks
136 * @covers Block::newFromTarget
138 public function testNewFromTargetRangeBlocks( $targets, $ip, $expectedTarget ) {
139 $blocker = $this->getTestSysop()->getUser();
141 foreach ( $targets as $target ) {
142 $block = new Block();
143 $block->setTarget( $target );
144 $block->setBlocker( $blocker );
148 // Should find the block with the narrowest range
149 $blockTarget = Block
::newFromTarget( $this->getTestUser()->getUser(), $ip )->getTarget();
151 $blockTarget instanceof User ?
$blockTarget->getName() : $blockTarget,
155 foreach ( $targets as $target ) {
156 $block = Block
::newFromTarget( $target );
161 function provideNewFromTargetRangeBlocks() {
163 'Blocks to IPv4 ranges' => [
164 [ '0.0.0.0/20', '0.0.0.0/30', '0.0.0.0/25' ],
168 'Blocks to IPv6 ranges' => [
169 [ '0:0:0:0:0:0:0:0/20', '0:0:0:0:0:0:0:0/30', '0:0:0:0:0:0:0:0/25' ],
173 'Blocks to wide IPv4 range and IP' => [
174 [ '0.0.0.0/16', '0.0.0.0' ],
178 'Blocks to wide IPv6 range and IP' => [
179 [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ],
183 'Blocks to narrow IPv4 range and IP' => [
184 [ '0.0.0.0/31', '0.0.0.0' ],
188 'Blocks to narrow IPv6 range and IP' => [
189 [ '0:0:0:0:0:0:0:0/127', '0:0:0:0:0:0:0:0' ],
197 * @covers Block::appliesToRight
199 public function testBlockedUserCanNotCreateAccount() {
200 $username = 'BlockedUserToCreateAccountWith';
201 $u = User
::newFromName( $username );
203 $userId = $u->getId();
204 $this->assertNotEquals( 0, $userId, 'sanity' );
205 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
210 Block
::newFromTarget( $username ),
211 "$username should not be blocked"
215 $u = User
::newFromName( $username );
217 $u->isBlockedFromCreateAccount(),
218 "Our sandbox user should be able to create account before being blocked"
221 // Foreign perspective (blockee not on current wiki)...
223 'address' => $username,
225 'reason' => 'crosswiki block...',
226 'timestamp' => wfTimestampNow(),
227 'expiry' => $this->db
->getInfinity(),
228 'createAccount' => true,
229 'enableAutoblock' => true,
231 'blockEmail' => true,
232 'byText' => 'm>MetaWikiUser',
234 $block = new Block( $blockOptions );
237 // Reload block from DB
238 $userBlock = Block
::newFromTarget( $username );
240 (bool)$block->appliesToRight( 'createaccount' ),
241 "Block object in DB should block right 'createaccount'"
244 $this->assertInstanceOf(
247 "'$username' block block object should be existent"
251 $u = User
::newFromName( $username );
253 (bool)$u->isBlockedFromCreateAccount(),
254 "Our sandbox user '$username' should NOT be able to create account"
259 * @covers Block::insert
261 public function testCrappyCrossWikiBlocks() {
262 // Delete the last round's block if it's still there
263 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
265 // An old block will prevent our new one from saving.
269 // Local perspective (blockee on current wiki)...
270 $user = User
::newFromName( 'UserOnForeignWiki' );
271 $user->addToDatabase();
272 $userId = $user->getId();
273 $this->assertNotEquals( 0, $userId, 'sanity' );
275 // Foreign perspective (blockee not on current wiki)...
277 'address' => 'UserOnForeignWiki',
278 'user' => $user->getId(),
279 'reason' => 'crosswiki block...',
280 'timestamp' => wfTimestampNow(),
281 'expiry' => $this->db
->getInfinity(),
282 'createAccount' => true,
283 'enableAutoblock' => true,
285 'blockEmail' => true,
286 'byText' => 'Meta>MetaWikiUser',
288 $block = new Block( $blockOptions );
290 $res = $block->insert( $this->db
);
291 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
293 $user = null; // clear
295 $block = Block
::newFromID( $res['id'] );
298 $block->getTarget()->getName(),
299 'Correct blockee name'
301 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
302 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
303 'Correct blocker name' );
304 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
305 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
308 protected function addXffBlocks() {
309 static $inited = false;
318 [ 'target' => '70.2.0.0/16',
319 'type' => Block
::TYPE_RANGE
,
320 'desc' => 'Range Hardblock',
321 'ACDisable' => false,
322 'isHardblock' => true,
323 'isAutoBlocking' => false,
325 [ 'target' => '2001:4860:4001::/48',
326 'type' => Block
::TYPE_RANGE
,
327 'desc' => 'Range6 Hardblock',
328 'ACDisable' => false,
329 'isHardblock' => true,
330 'isAutoBlocking' => false,
332 [ 'target' => '60.2.0.0/16',
333 'type' => Block
::TYPE_RANGE
,
334 'desc' => 'Range Softblock with AC Disabled',
336 'isHardblock' => false,
337 'isAutoBlocking' => false,
339 [ 'target' => '50.2.0.0/16',
340 'type' => Block
::TYPE_RANGE
,
341 'desc' => 'Range Softblock',
342 'ACDisable' => false,
343 'isHardblock' => false,
344 'isAutoBlocking' => false,
346 [ 'target' => '50.1.1.1',
347 'type' => Block
::TYPE_IP
,
348 'desc' => 'Exact Softblock',
349 'ACDisable' => false,
350 'isHardblock' => false,
351 'isAutoBlocking' => false,
355 $blocker = $this->getTestUser()->getUser();
356 foreach ( $blockList as $insBlock ) {
357 $target = $insBlock['target'];
359 if ( $insBlock['type'] === Block
::TYPE_IP
) {
360 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
361 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
362 $target = IP
::sanitizeRange( $target );
365 $block = new Block();
366 $block->setTarget( $target );
367 $block->setBlocker( $blocker );
368 $block->setReason( $insBlock['desc'] );
369 $block->setExpiry( 'infinity' );
370 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
371 $block->isHardblock( $insBlock['isHardblock'] );
372 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
377 public static function providerXff() {
379 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
381 'result' => 'Range Hardblock'
383 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
385 'result' => 'Range Softblock with AC Disabled'
387 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
389 'result' => 'Exact Softblock'
391 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
393 'result' => 'Exact Softblock'
395 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
397 'result' => 'Range Hardblock'
399 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
401 'result' => 'Range Hardblock'
403 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
405 'result' => 'Range Softblock with AC Disabled'
407 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
409 'result' => 'Exact Softblock'
411 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
413 'result' => 'Range Softblock with AC Disabled'
415 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
417 'result' => 'Range6 Hardblock'
423 * @dataProvider providerXff
424 * @covers Block::getBlocksForIPList
425 * @covers Block::chooseBlock
427 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
428 $user = $this->getUserForBlocking();
429 $this->addBlockForUser( $user );
431 $list = array_map( 'trim', explode( ',', $xff ) );
432 $xffblocks = Block
::getBlocksForIPList( $list, true );
433 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
434 $block = Block
::chooseBlock( $xffblocks, $list );
436 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
441 * @covers Block::getSystemBlockType
442 * @covers Block::insert
443 * @covers Block::doAutoblock
445 public function testSystemBlocks() {
446 $user = $this->getUserForBlocking();
447 $this->addBlockForUser( $user );
450 'address' => $user->getName(),
451 'reason' => 'test system block',
452 'timestamp' => wfTimestampNow(),
453 'expiry' => $this->db
->getInfinity(),
454 'byText' => 'MediaWiki default',
455 'systemBlock' => 'test',
456 'enableAutoblock' => true,
458 $block = new Block( $blockOptions );
460 $this->assertSame( 'test', $block->getSystemBlockType() );
464 $this->fail( 'Expected exception not thrown' );
465 } catch ( MWException
$ex ) {
466 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
470 $block->doAutoblock( '192.0.2.2' );
471 $this->fail( 'Expected exception not thrown' );
472 } catch ( MWException
$ex ) {
473 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
478 * @covers Block::newFromRow
480 public function testNewFromRow() {
481 $badActor = $this->getTestUser()->getUser();
482 $sysop = $this->getTestSysop()->getUser();
484 $block = new Block( [
485 'address' => $badActor->getName(),
486 'user' => $badActor->getId(),
487 'by' => $sysop->getId(),
488 'expiry' => 'infinity',
492 $blockQuery = Block
::getQueryInfo();
493 $row = $this->db
->select(
494 $blockQuery['tables'],
495 $blockQuery['fields'],
497 'ipb_id' => $block->getId(),
504 $block = Block
::newFromRow( $row );
505 $this->assertInstanceOf( Block
::class, $block );
506 $this->assertEquals( $block->getBy(), $sysop->getId() );
507 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
512 * @covers Block::equals
514 public function testEquals() {
515 $block = new Block();
517 $this->assertTrue( $block->equals( $block ) );
519 $partial = new Block( [
522 $this->assertFalse( $block->equals( $partial ) );
526 * @covers Block::isSitewide
528 public function testIsSitewide() {
529 $block = new Block();
530 $this->assertTrue( $block->isSitewide() );
532 $block = new Block( [
535 $this->assertTrue( $block->isSitewide() );
537 $block = new Block( [
540 $this->assertFalse( $block->isSitewide() );
542 $block = new Block( [
545 $block->isSitewide( true );
546 $this->assertTrue( $block->isSitewide() );
550 * @covers Block::getRestrictions
551 * @covers Block::setRestrictions
553 public function testRestrictions() {
554 $block = new Block();
556 new PageRestriction( 0, 1 )
558 $block->setRestrictions( $restrictions );
560 $this->assertSame( $restrictions, $block->getRestrictions() );
564 * @covers Block::getRestrictions
565 * @covers Block::insert
567 public function testRestrictionsFromDatabase() {
568 $badActor = $this->getTestUser()->getUser();
569 $sysop = $this->getTestSysop()->getUser();
571 $block = new Block( [
572 'address' => $badActor->getName(),
573 'user' => $badActor->getId(),
574 'by' => $sysop->getId(),
575 'expiry' => 'infinity',
577 $page = $this->getExistingTestPage( 'Foo' );
578 $restriction = new PageRestriction( 0, $page->getId() );
579 $block->setRestrictions( [ $restriction ] );
582 // Refresh the block from the database.
583 $block = Block
::newFromID( $block->getId() );
584 $restrictions = $block->getRestrictions();
585 $this->assertCount( 1, $restrictions );
586 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
591 * @covers Block::insert
593 public function testInsertExistingBlock() {
594 $badActor = $this->getTestUser()->getUser();
595 $sysop = $this->getTestSysop()->getUser();
597 $block = new Block( [
598 'address' => $badActor->getName(),
599 'user' => $badActor->getId(),
600 'by' => $sysop->getId(),
601 'expiry' => 'infinity',
603 $page = $this->getExistingTestPage( 'Foo' );
604 $restriction = new PageRestriction( 0, $page->getId() );
605 $block->setRestrictions( [ $restriction ] );
608 // Insert the block again, which should result in a failur
609 $result = $block->insert();
611 $this->assertFalse( $result );
613 // Ensure that there are no restrictions where the blockId is 0.
614 $count = $this->db
->selectRowCount(
615 'ipblocks_restrictions',
617 [ 'ir_ipb_id' => 0 ],
620 $this->assertSame( 0, $count );
626 * @covers Block::appliesToTitle
628 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
629 $this->setMwGlobals( [
630 'wgBlockDisablesLogin' => false,
632 $user = $this->getTestUser()->getUser();
633 $block = new Block( [
634 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
635 'allowUsertalk' => true,
639 $block->setTarget( $user );
640 $block->setBlocker( $this->getTestSysop()->getUser() );
643 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
645 $this->assertTrue( $block->appliesToTitle( $title ) );
647 // appliesToTitle() ignores allowUsertalk
648 $title = $user->getTalkPage();
649 $this->assertTrue( $block->appliesToTitle( $title ) );
655 * @covers Block::appliesToTitle
657 public function testAppliesToTitleOnPartialBlock() {
658 $this->setMwGlobals( [
659 'wgBlockDisablesLogin' => false,
661 $user = $this->getTestUser()->getUser();
662 $block = new Block( [
663 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
664 'allowUsertalk' => true,
668 $block->setTarget( $user );
669 $block->setBlocker( $this->getTestSysop()->getUser() );
672 $pageFoo = $this->getExistingTestPage( 'Foo' );
673 $pageBar = $this->getExistingTestPage( 'Bar' );
674 $pageJohn = $this->getExistingTestPage( 'User:John' );
676 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
677 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
678 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
680 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
681 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
682 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
688 * @covers Block::appliesToNamespace
689 * @covers Block::appliesToPage
691 public function testAppliesToReturnsTrueOnSitewideBlock() {
692 $this->setMwGlobals( [
693 'wgBlockDisablesLogin' => false,
695 $user = $this->getTestUser()->getUser();
696 $block = new Block( [
697 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
698 'allowUsertalk' => true,
702 $block->setTarget( $user );
703 $block->setBlocker( $this->getTestSysop()->getUser() );
706 $title = $this->getExistingTestPage()->getTitle();
708 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
709 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
710 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
716 * @covers Block::appliesToPage
718 public function testAppliesToPageOnPartialPageBlock() {
719 $this->setMwGlobals( [
720 'wgBlockDisablesLogin' => false,
722 $user = $this->getTestUser()->getUser();
723 $block = new Block( [
724 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
725 'allowUsertalk' => true,
729 $block->setTarget( $user );
730 $block->setBlocker( $this->getTestSysop()->getUser() );
733 $title = $this->getExistingTestPage()->getTitle();
735 $pageRestriction = new PageRestriction(
737 $title->getArticleID()
739 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
741 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
747 * @covers Block::appliesToNamespace
749 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
750 $this->setMwGlobals( [
751 'wgBlockDisablesLogin' => false,
753 $user = $this->getTestUser()->getUser();
754 $block = new Block( [
755 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
756 'allowUsertalk' => true,
760 $block->setTarget( $user );
761 $block->setBlocker( $this->getTestSysop()->getUser() );
764 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN
);
765 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
767 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
768 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );
774 * @covers Block::appliesToRight
776 public function testBlockAllowsPurge() {
777 $this->setMwGlobals( [
778 'wgBlockDisablesLogin' => false,
780 $block = new Block();
781 $this->assertFalse( $block->appliesToRight( 'purge' ) );
785 * Get an instance of BlockRestrictionStore
787 * @return BlockRestrictionStore
789 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
790 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();