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 * @covers Block::appliesToRight
137 public function testBlockedUserCanNotCreateAccount() {
138 $username = 'BlockedUserToCreateAccountWith';
139 $u = User
::newFromName( $username );
141 $userId = $u->getId();
142 $this->assertNotEquals( 0, $userId, 'sanity' );
143 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
148 Block
::newFromTarget( $username ),
149 "$username should not be blocked"
153 $u = User
::newFromName( $username );
155 $u->isBlockedFromCreateAccount(),
156 "Our sandbox user should be able to create account before being blocked"
159 // Foreign perspective (blockee not on current wiki)...
161 'address' => $username,
163 'reason' => 'crosswiki block...',
164 'timestamp' => wfTimestampNow(),
165 'expiry' => $this->db
->getInfinity(),
166 'createAccount' => true,
167 'enableAutoblock' => true,
169 'blockEmail' => true,
170 'byText' => 'm>MetaWikiUser',
172 $block = new Block( $blockOptions );
175 // Reload block from DB
176 $userBlock = Block
::newFromTarget( $username );
178 (bool)$block->appliesToRight( 'createaccount' ),
179 "Block object in DB should block right 'createaccount'"
182 $this->assertInstanceOf(
185 "'$username' block block object should be existent"
189 $u = User
::newFromName( $username );
191 (bool)$u->isBlockedFromCreateAccount(),
192 "Our sandbox user '$username' should NOT be able to create account"
197 * @covers Block::insert
199 public function testCrappyCrossWikiBlocks() {
200 // Delete the last round's block if it's still there
201 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
203 // An old block will prevent our new one from saving.
207 // Local perspective (blockee on current wiki)...
208 $user = User
::newFromName( 'UserOnForeignWiki' );
209 $user->addToDatabase();
210 $userId = $user->getId();
211 $this->assertNotEquals( 0, $userId, 'sanity' );
213 // Foreign perspective (blockee not on current wiki)...
215 'address' => 'UserOnForeignWiki',
216 'user' => $user->getId(),
217 'reason' => 'crosswiki block...',
218 'timestamp' => wfTimestampNow(),
219 'expiry' => $this->db
->getInfinity(),
220 'createAccount' => true,
221 'enableAutoblock' => true,
223 'blockEmail' => true,
224 'byText' => 'Meta>MetaWikiUser',
226 $block = new Block( $blockOptions );
228 $res = $block->insert( $this->db
);
229 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
231 $user = null; // clear
233 $block = Block
::newFromID( $res['id'] );
236 $block->getTarget()->getName(),
237 'Correct blockee name'
239 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
240 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
241 'Correct blocker name' );
242 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
243 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
246 protected function addXffBlocks() {
247 static $inited = false;
256 [ 'target' => '70.2.0.0/16',
257 'type' => Block
::TYPE_RANGE
,
258 'desc' => 'Range Hardblock',
259 'ACDisable' => false,
260 'isHardblock' => true,
261 'isAutoBlocking' => false,
263 [ 'target' => '2001:4860:4001::/48',
264 'type' => Block
::TYPE_RANGE
,
265 'desc' => 'Range6 Hardblock',
266 'ACDisable' => false,
267 'isHardblock' => true,
268 'isAutoBlocking' => false,
270 [ 'target' => '60.2.0.0/16',
271 'type' => Block
::TYPE_RANGE
,
272 'desc' => 'Range Softblock with AC Disabled',
274 'isHardblock' => false,
275 'isAutoBlocking' => false,
277 [ 'target' => '50.2.0.0/16',
278 'type' => Block
::TYPE_RANGE
,
279 'desc' => 'Range Softblock',
280 'ACDisable' => false,
281 'isHardblock' => false,
282 'isAutoBlocking' => false,
284 [ 'target' => '50.1.1.1',
285 'type' => Block
::TYPE_IP
,
286 'desc' => 'Exact Softblock',
287 'ACDisable' => false,
288 'isHardblock' => false,
289 'isAutoBlocking' => false,
293 $blocker = $this->getTestUser()->getUser();
294 foreach ( $blockList as $insBlock ) {
295 $target = $insBlock['target'];
297 if ( $insBlock['type'] === Block
::TYPE_IP
) {
298 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
299 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
300 $target = IP
::sanitizeRange( $target );
303 $block = new Block();
304 $block->setTarget( $target );
305 $block->setBlocker( $blocker );
306 $block->setReason( $insBlock['desc'] );
307 $block->setExpiry( 'infinity' );
308 $block->isCreateAccountBlocked( $insBlock['ACDisable'] );
309 $block->isHardblock( $insBlock['isHardblock'] );
310 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
315 public static function providerXff() {
317 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
319 'result' => 'Range Hardblock'
321 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
323 'result' => 'Range Softblock with AC Disabled'
325 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
327 'result' => 'Exact Softblock'
329 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
331 'result' => 'Exact Softblock'
333 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
335 'result' => 'Range Hardblock'
337 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
339 'result' => 'Range Hardblock'
341 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
343 'result' => 'Range Softblock with AC Disabled'
345 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
347 'result' => 'Exact Softblock'
349 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
351 'result' => 'Range Softblock with AC Disabled'
353 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
355 'result' => 'Range6 Hardblock'
361 * @dataProvider providerXff
362 * @covers Block::getBlocksForIPList
363 * @covers Block::chooseBlock
365 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
366 $user = $this->getUserForBlocking();
367 $this->addBlockForUser( $user );
369 $list = array_map( 'trim', explode( ',', $xff ) );
370 $xffblocks = Block
::getBlocksForIPList( $list, true );
371 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
372 $block = Block
::chooseBlock( $xffblocks, $list );
374 $exResult, $block->getReason(), 'Correct block type for XFF header ' . $xff
379 * @covers Block::getSystemBlockType
380 * @covers Block::insert
381 * @covers Block::doAutoblock
383 public function testSystemBlocks() {
384 $user = $this->getUserForBlocking();
385 $this->addBlockForUser( $user );
388 'address' => $user->getName(),
389 'reason' => 'test system block',
390 'timestamp' => wfTimestampNow(),
391 'expiry' => $this->db
->getInfinity(),
392 'byText' => 'MediaWiki default',
393 'systemBlock' => 'test',
394 'enableAutoblock' => true,
396 $block = new Block( $blockOptions );
398 $this->assertSame( 'test', $block->getSystemBlockType() );
402 $this->fail( 'Expected exception not thrown' );
403 } catch ( MWException
$ex ) {
404 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
408 $block->doAutoblock( '192.0.2.2' );
409 $this->fail( 'Expected exception not thrown' );
410 } catch ( MWException
$ex ) {
411 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
416 * @covers Block::newFromRow
418 public function testNewFromRow() {
419 $badActor = $this->getTestUser()->getUser();
420 $sysop = $this->getTestSysop()->getUser();
422 $block = new Block( [
423 'address' => $badActor->getName(),
424 'user' => $badActor->getId(),
425 'by' => $sysop->getId(),
426 'expiry' => 'infinity',
430 $blockQuery = Block
::getQueryInfo();
431 $row = $this->db
->select(
432 $blockQuery['tables'],
433 $blockQuery['fields'],
435 'ipb_id' => $block->getId(),
442 $block = Block
::newFromRow( $row );
443 $this->assertInstanceOf( Block
::class, $block );
444 $this->assertEquals( $block->getBy(), $sysop->getId() );
445 $this->assertEquals( $block->getTarget()->getName(), $badActor->getName() );
450 * @covers Block::equals
452 public function testEquals() {
453 $block = new Block();
455 $this->assertTrue( $block->equals( $block ) );
457 $partial = new Block( [
460 $this->assertFalse( $block->equals( $partial ) );
464 * @covers Block::isSitewide
466 public function testIsSitewide() {
467 $block = new Block();
468 $this->assertTrue( $block->isSitewide() );
470 $block = new Block( [
473 $this->assertTrue( $block->isSitewide() );
475 $block = new Block( [
478 $this->assertFalse( $block->isSitewide() );
480 $block = new Block( [
483 $block->isSitewide( true );
484 $this->assertTrue( $block->isSitewide() );
488 * @covers Block::getRestrictions
489 * @covers Block::setRestrictions
491 public function testRestrictions() {
492 $block = new Block();
494 new PageRestriction( 0, 1 )
496 $block->setRestrictions( $restrictions );
498 $this->assertSame( $restrictions, $block->getRestrictions() );
502 * @covers Block::getRestrictions
503 * @covers Block::insert
505 public function testRestrictionsFromDatabase() {
506 $badActor = $this->getTestUser()->getUser();
507 $sysop = $this->getTestSysop()->getUser();
509 $block = new Block( [
510 'address' => $badActor->getName(),
511 'user' => $badActor->getId(),
512 'by' => $sysop->getId(),
513 'expiry' => 'infinity',
515 $page = $this->getExistingTestPage( 'Foo' );
516 $restriction = new PageRestriction( 0, $page->getId() );
517 $block->setRestrictions( [ $restriction ] );
520 // Refresh the block from the database.
521 $block = Block
::newFromID( $block->getId() );
522 $restrictions = $block->getRestrictions();
523 $this->assertCount( 1, $restrictions );
524 $this->assertTrue( $restriction->equals( $restrictions[0] ) );
529 * @covers Block::insert
531 public function testInsertExistingBlock() {
532 $badActor = $this->getTestUser()->getUser();
533 $sysop = $this->getTestSysop()->getUser();
535 $block = new Block( [
536 'address' => $badActor->getName(),
537 'user' => $badActor->getId(),
538 'by' => $sysop->getId(),
539 'expiry' => 'infinity',
541 $page = $this->getExistingTestPage( 'Foo' );
542 $restriction = new PageRestriction( 0, $page->getId() );
543 $block->setRestrictions( [ $restriction ] );
546 // Insert the block again, which should result in a failur
547 $result = $block->insert();
549 $this->assertFalse( $result );
551 // Ensure that there are no restrictions where the blockId is 0.
552 $count = $this->db
->selectRowCount(
553 'ipblocks_restrictions',
555 [ 'ir_ipb_id' => 0 ],
558 $this->assertSame( 0, $count );
564 * @covers Block::appliesToTitle
566 public function testAppliesToTitleReturnsTrueOnSitewideBlock() {
567 $this->setMwGlobals( [
568 'wgBlockDisablesLogin' => false,
570 $user = $this->getTestUser()->getUser();
571 $block = new Block( [
572 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
573 'allowUsertalk' => true,
577 $block->setTarget( $user );
578 $block->setBlocker( $this->getTestSysop()->getUser() );
581 $title = $this->getExistingTestPage( 'Foo' )->getTitle();
583 $this->assertTrue( $block->appliesToTitle( $title ) );
585 // appliesToTitle() ignores allowUsertalk
586 $title = $user->getTalkPage();
587 $this->assertTrue( $block->appliesToTitle( $title ) );
593 * @covers Block::appliesToTitle
595 public function testAppliesToTitleOnPartialBlock() {
596 $this->setMwGlobals( [
597 'wgBlockDisablesLogin' => false,
599 $user = $this->getTestUser()->getUser();
600 $block = new Block( [
601 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
602 'allowUsertalk' => true,
606 $block->setTarget( $user );
607 $block->setBlocker( $this->getTestSysop()->getUser() );
610 $pageFoo = $this->getExistingTestPage( 'Foo' );
611 $pageBar = $this->getExistingTestPage( 'Bar' );
612 $pageJohn = $this->getExistingTestPage( 'User:John' );
614 $pageRestriction = new PageRestriction( $block->getId(), $pageFoo->getId() );
615 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER
);
616 $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] );
618 $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) );
619 $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) );
620 $this->assertTrue( $block->appliesToTitle( $pageJohn->getTitle() ) );
626 * @covers Block::appliesToNamespace
627 * @covers Block::appliesToPage
629 public function testAppliesToReturnsTrueOnSitewideBlock() {
630 $this->setMwGlobals( [
631 'wgBlockDisablesLogin' => false,
633 $user = $this->getTestUser()->getUser();
634 $block = new Block( [
635 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
636 'allowUsertalk' => true,
640 $block->setTarget( $user );
641 $block->setBlocker( $this->getTestSysop()->getUser() );
644 $title = $this->getExistingTestPage()->getTitle();
646 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
647 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
648 $this->assertTrue( $block->appliesToNamespace( NS_USER_TALK
) );
654 * @covers Block::appliesToPage
656 public function testAppliesToPageOnPartialPageBlock() {
657 $this->setMwGlobals( [
658 'wgBlockDisablesLogin' => false,
660 $user = $this->getTestUser()->getUser();
661 $block = new Block( [
662 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
663 'allowUsertalk' => true,
667 $block->setTarget( $user );
668 $block->setBlocker( $this->getTestSysop()->getUser() );
671 $title = $this->getExistingTestPage()->getTitle();
673 $pageRestriction = new PageRestriction(
675 $title->getArticleID()
677 $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] );
679 $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) );
685 * @covers Block::appliesToNamespace
687 public function testAppliesToNamespaceOnPartialNamespaceBlock() {
688 $this->setMwGlobals( [
689 'wgBlockDisablesLogin' => false,
691 $user = $this->getTestUser()->getUser();
692 $block = new Block( [
693 'expiry' => wfTimestamp( TS_MW
, wfTimestamp() +
( 40 * 60 * 60 ) ),
694 'allowUsertalk' => true,
698 $block->setTarget( $user );
699 $block->setBlocker( $this->getTestSysop()->getUser() );
702 $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN
);
703 $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] );
705 $this->assertTrue( $block->appliesToNamespace( NS_MAIN
) );
706 $this->assertFalse( $block->appliesToNamespace( NS_USER
) );
712 * @covers Block::appliesToRight
714 public function testBlockAllowsPurge() {
715 $this->setMwGlobals( [
716 'wgBlockDisablesLogin' => false,
718 $block = new Block();
719 $this->assertFalse( $block->appliesToRight( 'purge' ) );
723 * Get an instance of BlockRestrictionStore
725 * @return BlockRestrictionStore
727 protected function getBlockRestrictionStore() : BlockRestrictionStore
{
728 return MediaWikiServices
::getInstance()->getBlockRestrictionStore();