3 use MediaWiki\Block\BlockRestrictionStore
;
4 use MediaWiki\Block\DatabaseBlock
;
5 use MediaWiki\Block\Restriction\PageRestriction
;
6 use MediaWiki\Block\Restriction\NamespaceRestriction
;
7 use MediaWiki\MediaWikiServices
;
12 * @coversDefaultClass \MediaWiki\Block\DatabaseBlock
14 class DatabaseBlockTest
extends MediaWikiLangTestCase
{
19 private function getUserForBlocking() {
20 $testUser = $this->getMutableTestUser();
21 $user = $testUser->getUser();
22 $user->addToDatabase();
23 TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
24 $user->saveSettings();
31 * @return DatabaseBlock
34 private function addBlockForUser( User
$user ) {
35 // Delete the last round's block if it's still there
36 $oldBlock = DatabaseBlock
::newFromTarget( $user->getName() );
38 // An old block will prevent our new one from saving.
43 'address' => $user->getName(),
44 'user' => $user->getId(),
45 'by' => $this->getTestSysop()->getUser()->getId(),
46 'reason' => 'Parce que',
47 'expiry' => time() +
100500,
49 $block = new DatabaseBlock( $blockOptions );
52 // save up ID for use in assertion. Since ID is an autoincrement,
53 // its value might change depending on the order the tests are run.
54 // ApiBlockTest insert its own blocks!
55 if ( !$block->getId() ) {
56 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
59 $this->addXffBlocks();
65 * @covers ::newFromTarget
67 public function testINewFromTargetReturnsCorrectBlock() {
68 $user = $this->getUserForBlocking();
69 $block = $this->addBlockForUser( $user );
72 $block->equals( DatabaseBlock
::newFromTarget( $user->getName() ) ),
73 "newFromTarget() returns the same block as the one that was made"
80 public function testINewFromIDReturnsCorrectBlock() {
81 $user = $this->getUserForBlocking();
82 $block = $this->addBlockForUser( $user );
85 $block->equals( DatabaseBlock
::newFromID( $block->getId() ) ),
86 "newFromID() returns the same block as the one that was made"
92 * @covers ::__construct
94 public function testT28425BlockTimestampDefaultsToTime() {
95 $user = $this->getUserForBlocking();
96 $block = $this->addBlockForUser( $user );
97 $madeAt = wfTimestamp( TS_MW
);
99 // delta to stop one-off errors when things happen to go over a second mark.
100 $delta = abs( $madeAt - $block->getTimestamp() );
101 $this->assertLessThan(
104 "If no timestamp is specified, the block is recorded as time()"
109 * CheckUser since being changed to use DatabaseBlock::newFromTarget started failing
110 * because the new function didn't accept empty strings like DatabaseBlock::load()
111 * had. Regression T31116.
113 * @dataProvider provideT31116Data
114 * @covers ::newFromTarget
116 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
117 $user = $this->getUserForBlocking();
118 $initialBlock = $this->addBlockForUser( $user );
119 $block = DatabaseBlock
::newFromTarget( $user->getName(), $vagueTarget );
122 $initialBlock->equals( $block ),
123 "newFromTarget() returns the same block as the one that was made when "
124 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
128 public static function provideT31116Data() {
137 * @dataProvider provideNewFromTargetRangeBlocks
138 * @covers ::newFromTarget
140 public function testNewFromTargetRangeBlocks( $targets, $ip, $expectedTarget ) {
141 $blocker = $this->getTestSysop()->getUser();
143 foreach ( $targets as $target ) {
144 $block = new DatabaseBlock();
145 $block->setTarget( $target );
146 $block->setBlocker( $blocker );
150 // Should find the block with the narrowest range
151 $blockTarget = DatabaseBlock
::newFromTarget( $this->getTestUser()->getUser(), $ip )->getTarget();
153 $blockTarget instanceof User ?
$blockTarget->getName() : $blockTarget,
157 foreach ( $targets as $target ) {
158 $block = DatabaseBlock
::newFromTarget( $target );
163 function provideNewFromTargetRangeBlocks() {
165 'Blocks to IPv4 ranges' => [
166 [ '0.0.0.0/20', '0.0.0.0/30', '0.0.0.0/25' ],
170 'Blocks to IPv6 ranges' => [
171 [ '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' ],
175 'Blocks to wide IPv4 range and IP' => [
176 [ '0.0.0.0/16', '0.0.0.0' ],
180 'Blocks to narrow IPv4 range and IP' => [
181 [ '0.0.0.0/31', '0.0.0.0' ],
185 'Blocks to wide IPv6 range and IP' => [
186 [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ],
190 'Blocks to narrow IPv6 range and IP' => [
191 [ '0:0:0:0:0:0:0:0/127', '0:0:0:0:0:0:0:0' ],
195 'Blocks to wide IPv6 range and IP, large numbers' => [
196 [ '2000:DEAD:BEEF:A:0:0:0:0/19', '2000:DEAD:BEEF:A:0:0:0:0' ],
197 '2000:DEAD:BEEF:A:0:0:0:0',
198 '2000:DEAD:BEEF:A:0:0:0:0'
200 'Blocks to narrow IPv6 range and IP, large numbers' => [
201 [ '2000:DEAD:BEEF:A:0:0:0:0/127', '2000:DEAD:BEEF:A:0:0:0:0' ],
202 '2000:DEAD:BEEF:A:0:0:0:0',
203 '2000:DEAD:BEEF:A:0:0:0:0'
209 * @covers ::appliesToRight
211 public function testBlockedUserCanNotCreateAccount() {
212 $username = 'BlockedUserToCreateAccountWith';
213 $u = User
::newFromName( $username );
215 $userId = $u->getId();
216 $this->assertNotEquals( 0, $userId, 'sanity' );
217 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
222 DatabaseBlock
::newFromTarget( $username ),
223 "$username should not be blocked"
227 $u = User
::newFromName( $username );
229 $u->isBlockedFromCreateAccount(),
230 "Our sandbox user should be able to create account before being blocked"
233 // Foreign perspective (blockee not on current wiki)...
235 'address' => $username,
237 'reason' => 'crosswiki block...',
238 'timestamp' => wfTimestampNow(),
239 'expiry' => $this->db
->getInfinity(),
240 'createAccount' => true,
241 'enableAutoblock' => true,
243 'blockEmail' => true,
244 'byText' => 'm>MetaWikiUser',
246 $block = new DatabaseBlock( $blockOptions );
249 // Reload block from DB
250 $userBlock = DatabaseBlock
::newFromTarget( $username );
252 (bool)$block->appliesToRight( 'createaccount' ),
253 "Block object in DB should block right 'createaccount'"
256 $this->assertInstanceOf(
257 DatabaseBlock
::class,
259 "'$username' block block object should be existent"
263 $u = User
::newFromName( $username );
265 (bool)$u->isBlockedFromCreateAccount(),
266 "Our sandbox user '$username' should NOT be able to create account"
273 public function testCrappyCrossWikiBlocks() {
274 // Delete the last round's block if it's still there
275 $oldBlock = DatabaseBlock
::newFromTarget( 'UserOnForeignWiki' );
277 // An old block will prevent our new one from saving.
281 // Local perspective (blockee on current wiki)...
282 $user = User
::newFromName( 'UserOnForeignWiki' );
283 $user->addToDatabase();
284 $userId = $user->getId();
285 $this->assertNotEquals( 0, $userId, 'sanity' );
287 // Foreign perspective (blockee not on current wiki)...
289 'address' => 'UserOnForeignWiki',
290 'user' => $user->getId(),
291 'reason' => 'crosswiki block...',
292 'timestamp' => wfTimestampNow(),
293 'expiry' => $this->db
->getInfinity(),
294 'createAccount' => true,
295 'enableAutoblock' => true,
297 'blockEmail' => true,
298 'byText' => 'Meta>MetaWikiUser',
300 $block = new DatabaseBlock( $blockOptions );
302 $res = $block->insert( $this->db
);
303 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
305 $user = null; // clear
307 $block = DatabaseBlock
::newFromID( $res['id'] );
310 $block->getTarget()->getName(),
311 'Correct blockee name'
313 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
314 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
315 'Correct blocker name' );
316 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
317 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
320 protected function addXffBlocks() {
321 static $inited = false;
330 [ 'target' => '70.2.0.0/16',
331 'type' => DatabaseBlock
::TYPE_RANGE
,
332 'desc' => 'Range Hardblock',
333 'ACDisable' => false,
334 'isHardblock' => true,
335 'isAutoBlocking' => false,
337 [ 'target' => '2001:4860:4001::/48',
338 'type' => DatabaseBlock
::TYPE_RANGE
,
339 'desc' => 'Range6 Hardblock',
340 'ACDisable' => false,
341 'isHardblock' => true,
342 'isAutoBlocking' => false,
344 [ 'target' => '60.2.0.0/16',
345 'type' => DatabaseBlock
::TYPE_RANGE
,
346 'desc' => 'Range Softblock with AC Disabled',
348 'isHardblock' => false,
349 'isAutoBlocking' => false,
351 [ 'target' => '50.2.0.0/16',
352 'type' => DatabaseBlock
::TYPE_RANGE
,
353 'desc' => 'Range Softblock',
354 'ACDisable' => false,
355 'isHardblock' => false,
356 'isAutoBlocking' => false,
358 [ 'target' => '50.1.1.1',
359 'type' => DatabaseBlock
::TYPE_IP
,
360 'desc' => 'Exact Softblock',
361 'ACDisable' => false,
362 'isHardblock' => false,
363 'isAutoBlocking' => false,
367 $blocker = $this->getTestUser()->getUser();
368 foreach ( $blockList as $insBlock ) {
369 $target = $insBlock['target'];
371 if ( $insBlock['type'] === DatabaseBlock
::TYPE_IP
) {
372 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
373 } elseif ( $insBlock['type'] === DatabaseBlock
::TYPE_RANGE
) {
374 $target = IP
::sanitizeRange( $target );
377 $block = new DatabaseBlock();
378 $block->setTarget( $target );
379 $block->setBlocker( $blocker );
380 $block->setReason( $insBlock['desc'] );
381 $block->setExpiry( 'infinity' );
382 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
383 $block->isHardblock( $insBlock['isHardblock'] );
384 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
389 public static function providerXff() {
391 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
393 'result' => 'Range Hardblock'
395 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
397 'result' => 'Range Softblock with AC Disabled'
399 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
401 'result' => 'Exact Softblock'
403 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
405 'result' => 'Exact Softblock'
407 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
409 'result' => 'Range Hardblock'
411 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
413 'result' => 'Range Hardblock'
415 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
417 'result' => 'Range Softblock with AC Disabled'
419 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
421 'result' => 'Exact Softblock'
423 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
425 'result' => 'Range Softblock with AC Disabled'
427 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
429 'result' => 'Range6 Hardblock'
435 * @dataProvider providerXff
436 * @covers ::getBlocksForIPList
437 * @covers ::chooseBlock
439 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
440 $user = $this->getUserForBlocking();
441 $this->addBlockForUser( $user );
443 $list = array_map( 'trim', explode( ',', $xff ) );
444 $xffblocks = DatabaseBlock
::getBlocksForIPList( $list, true );
445 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
446 $block = DatabaseBlock
::chooseBlock( $xffblocks, $list );
448 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
453 * @covers ::newFromRow
455 public function testNewFromRow() {
456 $badActor = $this->getTestUser()->getUser();
457 $sysop = $this->getTestSysop()->getUser();
459 $block = new DatabaseBlock( [
460 'address' => $badActor->getName(),
461 'user' => $badActor->getId(),
462 'by' => $sysop->getId(),
463 'expiry' => 'infinity',
467 $blockQuery = DatabaseBlock
::getQueryInfo();
468 $row = $this->db
->select(
469 $blockQuery['tables'],
470 $blockQuery['fields'],
472 'ipb_id' => $block->getId(),
479 $block = DatabaseBlock
::newFromRow( $row );
480 $this->assertInstanceOf( DatabaseBlock
::class, $block );
481 $this->assertEquals( $block->getBy(), $sysop->getId() );
482 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
489 public function testEquals() {
490 $block = new DatabaseBlock();
492 $this->assertTrue( $block->equals( $block ) );
494 $partial = new DatabaseBlock( [
497 $this->assertFalse( $block->equals( $partial ) );
501 * @covers ::isSitewide
503 public function testIsSitewide() {
504 $block = new DatabaseBlock();
505 $this->assertTrue( $block->isSitewide() );
507 $block = new DatabaseBlock( [
510 $this->assertTrue( $block->isSitewide() );
512 $block = new DatabaseBlock( [
515 $this->assertFalse( $block->isSitewide() );
517 $block = new DatabaseBlock( [
520 $block->isSitewide( true );
521 $this->assertTrue( $block->isSitewide() );
525 * @covers ::getRestrictions
526 * @covers ::setRestrictions
528 public function testRestrictions() {
529 $block = new DatabaseBlock();
531 new PageRestriction( 0, 1 )
533 $block->setRestrictions( $restrictions );
535 $this->assertSame( $restrictions, $block->getRestrictions() );
539 * @covers ::getRestrictions
542 public function testRestrictionsFromDatabase() {
543 $badActor = $this->getTestUser()->getUser();
544 $sysop = $this->getTestSysop()->getUser();
546 $block = new DatabaseBlock( [
547 'address' => $badActor->getName(),
548 'user' => $badActor->getId(),
549 'by' => $sysop->getId(),
550 'expiry' => 'infinity',
552 $page = $this->getExistingTestPage( 'Foo' );
553 $restriction = new PageRestriction( 0, $page->getId() );
554 $block->setRestrictions( [ $restriction ] );
557 // Refresh the block from the database.
558 $block = DatabaseBlock
::newFromID( $block->getId() );
559 $restrictions = $block->getRestrictions();
560 $this->assertCount( 1, $restrictions );
561 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
568 public function testInsertExistingBlock() {
569 $badActor = $this->getTestUser()->getUser();
570 $sysop = $this->getTestSysop()->getUser();
572 $block = new DatabaseBlock( [
573 'address' => $badActor->getName(),
574 'user' => $badActor->getId(),
575 'by' => $sysop->getId(),
576 'expiry' => 'infinity',
578 $page = $this->getExistingTestPage( 'Foo' );
579 $restriction = new PageRestriction( 0, $page->getId() );
580 $block->setRestrictions( [ $restriction ] );
583 // Insert the block again, which should result in a failur
584 $result = $block->insert();
586 $this->assertFalse( $result );
588 // Ensure that there are no restrictions where the blockId is 0.
589 $count = $this->db
->selectRowCount(
590 'ipblocks_restrictions',
592 [ 'ir_ipb_id' => 0 ],
595 $this->assertSame( 0, $count );
601 * @covers ::appliesToTitle
603 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
604 $this->setMwGlobals( [
605 'wgBlockDisablesLogin' => false,
607 $user = $this->getTestUser()->getUser();
608 $block = new DatabaseBlock( [
609 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
610 'allowUsertalk' => true,
614 $block->setTarget( $user );
615 $block->setBlocker( $this->getTestSysop()->getUser() );
618 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
620 $this->assertTrue( $block->appliesToTitle( $title ) );
622 // appliesToTitle() ignores allowUsertalk
623 $title = $user->getTalkPage();
624 $this->assertTrue( $block->appliesToTitle( $title ) );
630 * @covers ::appliesToTitle
632 public function testAppliesToTitleOnPartialBlock() {
633 $this->setMwGlobals( [
634 'wgBlockDisablesLogin' => false,
636 $user = $this->getTestUser()->getUser();
637 $block = new DatabaseBlock( [
638 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
639 'allowUsertalk' => true,
643 $block->setTarget( $user );
644 $block->setBlocker( $this->getTestSysop()->getUser() );
647 $pageFoo = $this->getExistingTestPage( 'Foo' );
648 $pageBar = $this->getExistingTestPage( 'Bar' );
649 $pageJohn = $this->getExistingTestPage( 'User:John' );
651 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
652 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
653 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
655 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
656 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
657 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
663 * @covers ::appliesToNamespace
664 * @covers ::appliesToPage
666 public function testAppliesToReturnsTrueOnSitewideBlock() {
667 $this->setMwGlobals( [
668 'wgBlockDisablesLogin' => false,
670 $user = $this->getTestUser()->getUser();
671 $block = new DatabaseBlock( [
672 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
673 'allowUsertalk' => true,
677 $block->setTarget( $user );
678 $block->setBlocker( $this->getTestSysop()->getUser() );
681 $title = $this->getExistingTestPage()->getTitle();
683 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
684 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
685 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
691 * @covers ::appliesToPage
693 public function testAppliesToPageOnPartialPageBlock() {
694 $this->setMwGlobals( [
695 'wgBlockDisablesLogin' => false,
697 $user = $this->getTestUser()->getUser();
698 $block = new DatabaseBlock( [
699 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
700 'allowUsertalk' => true,
704 $block->setTarget( $user );
705 $block->setBlocker( $this->getTestSysop()->getUser() );
708 $title = $this->getExistingTestPage()->getTitle();
710 $pageRestriction = new PageRestriction(
712 $title->getArticleID()
714 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
716 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
722 * @covers ::appliesToNamespace
724 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
725 $this->setMwGlobals( [
726 'wgBlockDisablesLogin' => false,
728 $user = $this->getTestUser()->getUser();
729 $block = new DatabaseBlock( [
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 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
742 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
743 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );
749 * @covers ::appliesToRight
751 public function testBlockAllowsPurge() {
752 $this->setMwGlobals( [
753 'wgBlockDisablesLogin' => false,
755 $block = new DatabaseBlock();
756 $this->assertFalse( $block->appliesToRight( 'purge' ) );
760 * Get an instance of BlockRestrictionStore
762 * @return BlockRestrictionStore
764 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
765 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();