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::newFromRow
443 public function testNewFromRow() {
444 $badActor = $this->getTestUser()->getUser();
445 $sysop = $this->getTestSysop()->getUser();
447 $block = new Block( [
448 'address' => $badActor->getName(),
449 'user' => $badActor->getId(),
450 'by' => $sysop->getId(),
451 'expiry' => 'infinity',
455 $blockQuery = Block
::getQueryInfo();
456 $row = $this->db
->select(
457 $blockQuery['tables'],
458 $blockQuery['fields'],
460 'ipb_id' => $block->getId(),
467 $block = Block
::newFromRow( $row );
468 $this->assertInstanceOf( Block
::class, $block );
469 $this->assertEquals( $block->getBy(), $sysop->getId() );
470 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
475 * @covers Block::equals
477 public function testEquals() {
478 $block = new Block();
480 $this->assertTrue( $block->equals( $block ) );
482 $partial = new Block( [
485 $this->assertFalse( $block->equals( $partial ) );
489 * @covers Block::isSitewide
491 public function testIsSitewide() {
492 $block = new Block();
493 $this->assertTrue( $block->isSitewide() );
495 $block = new Block( [
498 $this->assertTrue( $block->isSitewide() );
500 $block = new Block( [
503 $this->assertFalse( $block->isSitewide() );
505 $block = new Block( [
508 $block->isSitewide( true );
509 $this->assertTrue( $block->isSitewide() );
513 * @covers Block::getRestrictions
514 * @covers Block::setRestrictions
516 public function testRestrictions() {
517 $block = new Block();
519 new PageRestriction( 0, 1 )
521 $block->setRestrictions( $restrictions );
523 $this->assertSame( $restrictions, $block->getRestrictions() );
527 * @covers Block::getRestrictions
528 * @covers Block::insert
530 public function testRestrictionsFromDatabase() {
531 $badActor = $this->getTestUser()->getUser();
532 $sysop = $this->getTestSysop()->getUser();
534 $block = new Block( [
535 'address' => $badActor->getName(),
536 'user' => $badActor->getId(),
537 'by' => $sysop->getId(),
538 'expiry' => 'infinity',
540 $page = $this->getExistingTestPage( 'Foo' );
541 $restriction = new PageRestriction( 0, $page->getId() );
542 $block->setRestrictions( [ $restriction ] );
545 // Refresh the block from the database.
546 $block = Block
::newFromID( $block->getId() );
547 $restrictions = $block->getRestrictions();
548 $this->assertCount( 1, $restrictions );
549 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
554 * @covers Block::insert
556 public function testInsertExistingBlock() {
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 // Insert the block again, which should result in a failur
572 $result = $block->insert();
574 $this->assertFalse( $result );
576 // Ensure that there are no restrictions where the blockId is 0.
577 $count = $this->db
->selectRowCount(
578 'ipblocks_restrictions',
580 [ 'ir_ipb_id' => 0 ],
583 $this->assertSame( 0, $count );
589 * @covers Block::appliesToTitle
591 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
592 $this->setMwGlobals( [
593 'wgBlockDisablesLogin' => false,
595 $user = $this->getTestUser()->getUser();
596 $block = new Block( [
597 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
598 'allowUsertalk' => true,
602 $block->setTarget( $user );
603 $block->setBlocker( $this->getTestSysop()->getUser() );
606 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
608 $this->assertTrue( $block->appliesToTitle( $title ) );
610 // appliesToTitle() ignores allowUsertalk
611 $title = $user->getTalkPage();
612 $this->assertTrue( $block->appliesToTitle( $title ) );
618 * @covers Block::appliesToTitle
620 public function testAppliesToTitleOnPartialBlock() {
621 $this->setMwGlobals( [
622 'wgBlockDisablesLogin' => false,
624 $user = $this->getTestUser()->getUser();
625 $block = new Block( [
626 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
627 'allowUsertalk' => true,
631 $block->setTarget( $user );
632 $block->setBlocker( $this->getTestSysop()->getUser() );
635 $pageFoo = $this->getExistingTestPage( 'Foo' );
636 $pageBar = $this->getExistingTestPage( 'Bar' );
637 $pageJohn = $this->getExistingTestPage( 'User:John' );
639 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
640 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
641 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
643 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
644 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
645 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
651 * @covers Block::appliesToNamespace
652 * @covers Block::appliesToPage
654 public function testAppliesToReturnsTrueOnSitewideBlock() {
655 $this->setMwGlobals( [
656 'wgBlockDisablesLogin' => false,
658 $user = $this->getTestUser()->getUser();
659 $block = new Block( [
660 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
661 'allowUsertalk' => true,
665 $block->setTarget( $user );
666 $block->setBlocker( $this->getTestSysop()->getUser() );
669 $title = $this->getExistingTestPage()->getTitle();
671 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
672 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
673 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
679 * @covers Block::appliesToPage
681 public function testAppliesToPageOnPartialPageBlock() {
682 $this->setMwGlobals( [
683 'wgBlockDisablesLogin' => false,
685 $user = $this->getTestUser()->getUser();
686 $block = new Block( [
687 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
688 'allowUsertalk' => true,
692 $block->setTarget( $user );
693 $block->setBlocker( $this->getTestSysop()->getUser() );
696 $title = $this->getExistingTestPage()->getTitle();
698 $pageRestriction = new PageRestriction(
700 $title->getArticleID()
702 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
704 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
710 * @covers Block::appliesToNamespace
712 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
713 $this->setMwGlobals( [
714 'wgBlockDisablesLogin' => false,
716 $user = $this->getTestUser()->getUser();
717 $block = new Block( [
718 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
719 'allowUsertalk' => true,
723 $block->setTarget( $user );
724 $block->setBlocker( $this->getTestSysop()->getUser() );
727 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN
);
728 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
730 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
731 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );
737 * @covers Block::appliesToRight
739 public function testBlockAllowsPurge() {
740 $this->setMwGlobals( [
741 'wgBlockDisablesLogin' => false,
743 $block = new Block();
744 $this->assertFalse( $block->appliesToRight( 'purge' ) );
748 * Get an instance of BlockRestrictionStore
750 * @return BlockRestrictionStore
752 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
753 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();