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 narrow IPv4 range and IP' => [
179 [ '0.0.0.0/31', '0.0.0.0' ],
183 'Blocks to wide IPv6 range and IP' => [
184 [ '0:0:0:0:0:0:0:0/19', '0:0:0:0: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' ],
193 'Blocks to wide IPv6 range and IP, large numbers' => [
194 [ '2000:DEAD:BEEF:A:0:0:0:0/19', '2000:DEAD:BEEF:A:0:0:0:0' ],
195 '2000:DEAD:BEEF:A:0:0:0:0',
196 '2000:DEAD:BEEF:A:0:0:0:0'
198 'Blocks to narrow IPv6 range and IP, large numbers' => [
199 [ '2000:DEAD:BEEF:A:0:0:0:0/127', '2000:DEAD:BEEF:A:0:0:0:0' ],
200 '2000:DEAD:BEEF:A:0:0:0:0',
201 '2000:DEAD:BEEF:A:0:0:0:0'
207 * @covers Block::appliesToRight
209 public function testBlockedUserCanNotCreateAccount() {
210 $username = 'BlockedUserToCreateAccountWith';
211 $u = User
::newFromName( $username );
213 $userId = $u->getId();
214 $this->assertNotEquals( 0, $userId, 'sanity' );
215 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
220 Block
::newFromTarget( $username ),
221 "$username should not be blocked"
225 $u = User
::newFromName( $username );
227 $u->isBlockedFromCreateAccount(),
228 "Our sandbox user should be able to create account before being blocked"
231 // Foreign perspective (blockee not on current wiki)...
233 'address' => $username,
235 'reason' => 'crosswiki block...',
236 'timestamp' => wfTimestampNow(),
237 'expiry' => $this->db
->getInfinity(),
238 'createAccount' => true,
239 'enableAutoblock' => true,
241 'blockEmail' => true,
242 'byText' => 'm>MetaWikiUser',
244 $block = new Block( $blockOptions );
247 // Reload block from DB
248 $userBlock = Block
::newFromTarget( $username );
250 (bool)$block->appliesToRight( 'createaccount' ),
251 "Block object in DB should block right 'createaccount'"
254 $this->assertInstanceOf(
257 "'$username' block block object should be existent"
261 $u = User
::newFromName( $username );
263 (bool)$u->isBlockedFromCreateAccount(),
264 "Our sandbox user '$username' should NOT be able to create account"
269 * @covers Block::insert
271 public function testCrappyCrossWikiBlocks() {
272 // Delete the last round's block if it's still there
273 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
275 // An old block will prevent our new one from saving.
279 // Local perspective (blockee on current wiki)...
280 $user = User
::newFromName( 'UserOnForeignWiki' );
281 $user->addToDatabase();
282 $userId = $user->getId();
283 $this->assertNotEquals( 0, $userId, 'sanity' );
285 // Foreign perspective (blockee not on current wiki)...
287 'address' => 'UserOnForeignWiki',
288 'user' => $user->getId(),
289 'reason' => 'crosswiki block...',
290 'timestamp' => wfTimestampNow(),
291 'expiry' => $this->db
->getInfinity(),
292 'createAccount' => true,
293 'enableAutoblock' => true,
295 'blockEmail' => true,
296 'byText' => 'Meta>MetaWikiUser',
298 $block = new Block( $blockOptions );
300 $res = $block->insert( $this->db
);
301 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
303 $user = null; // clear
305 $block = Block
::newFromID( $res['id'] );
308 $block->getTarget()->getName(),
309 'Correct blockee name'
311 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
312 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
313 'Correct blocker name' );
314 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
315 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
318 protected function addXffBlocks() {
319 static $inited = false;
328 [ 'target' => '70.2.0.0/16',
329 'type' => Block
::TYPE_RANGE
,
330 'desc' => 'Range Hardblock',
331 'ACDisable' => false,
332 'isHardblock' => true,
333 'isAutoBlocking' => false,
335 [ 'target' => '2001:4860:4001::/48',
336 'type' => Block
::TYPE_RANGE
,
337 'desc' => 'Range6 Hardblock',
338 'ACDisable' => false,
339 'isHardblock' => true,
340 'isAutoBlocking' => false,
342 [ 'target' => '60.2.0.0/16',
343 'type' => Block
::TYPE_RANGE
,
344 'desc' => 'Range Softblock with AC Disabled',
346 'isHardblock' => false,
347 'isAutoBlocking' => false,
349 [ 'target' => '50.2.0.0/16',
350 'type' => Block
::TYPE_RANGE
,
351 'desc' => 'Range Softblock',
352 'ACDisable' => false,
353 'isHardblock' => false,
354 'isAutoBlocking' => false,
356 [ 'target' => '50.1.1.1',
357 'type' => Block
::TYPE_IP
,
358 'desc' => 'Exact Softblock',
359 'ACDisable' => false,
360 'isHardblock' => false,
361 'isAutoBlocking' => false,
365 $blocker = $this->getTestUser()->getUser();
366 foreach ( $blockList as $insBlock ) {
367 $target = $insBlock['target'];
369 if ( $insBlock['type'] === Block
::TYPE_IP
) {
370 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
371 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
372 $target = IP
::sanitizeRange( $target );
375 $block = new Block();
376 $block->setTarget( $target );
377 $block->setBlocker( $blocker );
378 $block->setReason( $insBlock['desc'] );
379 $block->setExpiry( 'infinity' );
380 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
381 $block->isHardblock( $insBlock['isHardblock'] );
382 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
387 public static function providerXff() {
389 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
391 'result' => 'Range Hardblock'
393 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
395 'result' => 'Range Softblock with AC Disabled'
397 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
399 'result' => 'Exact Softblock'
401 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
403 'result' => 'Exact Softblock'
405 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
407 'result' => 'Range Hardblock'
409 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
411 'result' => 'Range Hardblock'
413 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
415 'result' => 'Range Softblock with AC Disabled'
417 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
419 'result' => 'Exact Softblock'
421 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
423 'result' => 'Range Softblock with AC Disabled'
425 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
427 'result' => 'Range6 Hardblock'
433 * @dataProvider providerXff
434 * @covers Block::getBlocksForIPList
435 * @covers Block::chooseBlock
437 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
438 $user = $this->getUserForBlocking();
439 $this->addBlockForUser( $user );
441 $list = array_map( 'trim', explode( ',', $xff ) );
442 $xffblocks = Block
::getBlocksForIPList( $list, true );
443 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
444 $block = Block
::chooseBlock( $xffblocks, $list );
446 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
451 * @covers Block::newFromRow
453 public function testNewFromRow() {
454 $badActor = $this->getTestUser()->getUser();
455 $sysop = $this->getTestSysop()->getUser();
457 $block = new Block( [
458 'address' => $badActor->getName(),
459 'user' => $badActor->getId(),
460 'by' => $sysop->getId(),
461 'expiry' => 'infinity',
465 $blockQuery = Block
::getQueryInfo();
466 $row = $this->db
->select(
467 $blockQuery['tables'],
468 $blockQuery['fields'],
470 'ipb_id' => $block->getId(),
477 $block = Block
::newFromRow( $row );
478 $this->assertInstanceOf( Block
::class, $block );
479 $this->assertEquals( $block->getBy(), $sysop->getId() );
480 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
485 * @covers Block::equals
487 public function testEquals() {
488 $block = new Block();
490 $this->assertTrue( $block->equals( $block ) );
492 $partial = new Block( [
495 $this->assertFalse( $block->equals( $partial ) );
499 * @covers Block::isSitewide
501 public function testIsSitewide() {
502 $block = new Block();
503 $this->assertTrue( $block->isSitewide() );
505 $block = new Block( [
508 $this->assertTrue( $block->isSitewide() );
510 $block = new Block( [
513 $this->assertFalse( $block->isSitewide() );
515 $block = new Block( [
518 $block->isSitewide( true );
519 $this->assertTrue( $block->isSitewide() );
523 * @covers Block::getRestrictions
524 * @covers Block::setRestrictions
526 public function testRestrictions() {
527 $block = new Block();
529 new PageRestriction( 0, 1 )
531 $block->setRestrictions( $restrictions );
533 $this->assertSame( $restrictions, $block->getRestrictions() );
537 * @covers Block::getRestrictions
538 * @covers Block::insert
540 public function testRestrictionsFromDatabase() {
541 $badActor = $this->getTestUser()->getUser();
542 $sysop = $this->getTestSysop()->getUser();
544 $block = new Block( [
545 'address' => $badActor->getName(),
546 'user' => $badActor->getId(),
547 'by' => $sysop->getId(),
548 'expiry' => 'infinity',
550 $page = $this->getExistingTestPage( 'Foo' );
551 $restriction = new PageRestriction( 0, $page->getId() );
552 $block->setRestrictions( [ $restriction ] );
555 // Refresh the block from the database.
556 $block = Block
::newFromID( $block->getId() );
557 $restrictions = $block->getRestrictions();
558 $this->assertCount( 1, $restrictions );
559 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
564 * @covers Block::insert
566 public function testInsertExistingBlock() {
567 $badActor = $this->getTestUser()->getUser();
568 $sysop = $this->getTestSysop()->getUser();
570 $block = new Block( [
571 'address' => $badActor->getName(),
572 'user' => $badActor->getId(),
573 'by' => $sysop->getId(),
574 'expiry' => 'infinity',
576 $page = $this->getExistingTestPage( 'Foo' );
577 $restriction = new PageRestriction( 0, $page->getId() );
578 $block->setRestrictions( [ $restriction ] );
581 // Insert the block again, which should result in a failur
582 $result = $block->insert();
584 $this->assertFalse( $result );
586 // Ensure that there are no restrictions where the blockId is 0.
587 $count = $this->db
->selectRowCount(
588 'ipblocks_restrictions',
590 [ 'ir_ipb_id' => 0 ],
593 $this->assertSame( 0, $count );
599 * @covers Block::appliesToTitle
601 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
602 $this->setMwGlobals( [
603 'wgBlockDisablesLogin' => false,
605 $user = $this->getTestUser()->getUser();
606 $block = new Block( [
607 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
608 'allowUsertalk' => true,
612 $block->setTarget( $user );
613 $block->setBlocker( $this->getTestSysop()->getUser() );
616 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
618 $this->assertTrue( $block->appliesToTitle( $title ) );
620 // appliesToTitle() ignores allowUsertalk
621 $title = $user->getTalkPage();
622 $this->assertTrue( $block->appliesToTitle( $title ) );
628 * @covers Block::appliesToTitle
630 public function testAppliesToTitleOnPartialBlock() {
631 $this->setMwGlobals( [
632 'wgBlockDisablesLogin' => false,
634 $user = $this->getTestUser()->getUser();
635 $block = new Block( [
636 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
637 'allowUsertalk' => true,
641 $block->setTarget( $user );
642 $block->setBlocker( $this->getTestSysop()->getUser() );
645 $pageFoo = $this->getExistingTestPage( 'Foo' );
646 $pageBar = $this->getExistingTestPage( 'Bar' );
647 $pageJohn = $this->getExistingTestPage( 'User:John' );
649 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
650 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
651 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
653 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
654 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
655 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
661 * @covers Block::appliesToNamespace
662 * @covers Block::appliesToPage
664 public function testAppliesToReturnsTrueOnSitewideBlock() {
665 $this->setMwGlobals( [
666 'wgBlockDisablesLogin' => false,
668 $user = $this->getTestUser()->getUser();
669 $block = new Block( [
670 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
671 'allowUsertalk' => true,
675 $block->setTarget( $user );
676 $block->setBlocker( $this->getTestSysop()->getUser() );
679 $title = $this->getExistingTestPage()->getTitle();
681 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
682 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
683 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
689 * @covers Block::appliesToPage
691 public function testAppliesToPageOnPartialPageBlock() {
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 $pageRestriction = new PageRestriction(
710 $title->getArticleID()
712 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
714 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
720 * @covers Block::appliesToNamespace
722 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
723 $this->setMwGlobals( [
724 'wgBlockDisablesLogin' => false,
726 $user = $this->getTestUser()->getUser();
727 $block = new Block( [
728 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
729 'allowUsertalk' => true,
733 $block->setTarget( $user );
734 $block->setBlocker( $this->getTestSysop()->getUser() );
737 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN
);
738 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
740 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
741 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );
747 * @covers Block::appliesToRight
749 public function testBlockAllowsPurge() {
750 $this->setMwGlobals( [
751 'wgBlockDisablesLogin' => false,
753 $block = new Block();
754 $this->assertFalse( $block->appliesToRight( 'purge' ) );
758 * Get an instance of BlockRestrictionStore
760 * @return BlockRestrictionStore
762 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
763 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();