2020-04-23 19:33:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
2021-03-16 01:37:57 +00:00
|
|
|
use MediaWiki\Block\BlockUserFactory;
|
2020-04-23 19:33:56 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2021-04-01 08:53:15 +00:00
|
|
|
use MediaWiki\Block\Restriction\PageRestriction;
|
2021-03-16 01:37:57 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2020-04-23 19:33:56 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @group Blocking
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class BlockUserTest extends MediaWikiIntegrationTestCase {
|
2021-03-16 01:37:57 +00:00
|
|
|
use MockAuthorityTrait;
|
2020-04-23 19:33:56 +00:00
|
|
|
|
2024-07-30 21:52:32 +00:00
|
|
|
private User $user;
|
|
|
|
|
private BlockUserFactory $blockUserFactory;
|
2020-04-23 19:33:56 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2020-04-23 19:33:56 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
// Prepare users
|
|
|
|
|
$this->user = $this->getTestUser()->getUser();
|
|
|
|
|
|
|
|
|
|
// Prepare factory
|
2021-09-05 21:12:06 +00:00
|
|
|
$this->blockUserFactory = $this->getServiceContainer()->getBlockUserFactory();
|
2020-04-23 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlock
|
2020-04-23 19:33:56 +00:00
|
|
|
*/
|
|
|
|
|
public function testValidTarget() {
|
|
|
|
|
$status = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2020-04-23 19:33:56 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test block'
|
|
|
|
|
)->placeBlock();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $status );
|
2020-04-23 19:33:56 +00:00
|
|
|
$block = $this->user->getBlock();
|
|
|
|
|
$this->assertSame( 'test block', $block->getReasonComment()->text );
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block );
|
|
|
|
|
$this->assertFalse( $block->getHideName() );
|
|
|
|
|
$this->assertFalse( $block->isCreateAccountBlocked() );
|
|
|
|
|
$this->assertTrue( $block->isUsertalkEditAllowed() );
|
|
|
|
|
$this->assertFalse( $block->isEmailBlocked() );
|
|
|
|
|
$this->assertTrue( $block->isAutoblocking() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlock
|
2020-04-23 19:33:56 +00:00
|
|
|
*/
|
|
|
|
|
public function testHideUser() {
|
|
|
|
|
$status = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
|
|
|
|
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(),
|
|
|
|
|
'infinity',
|
|
|
|
|
'test hideuser',
|
|
|
|
|
[
|
|
|
|
|
'isHideUser' => true
|
|
|
|
|
]
|
|
|
|
|
)->placeBlock();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $status );
|
2020-04-23 19:33:56 +00:00
|
|
|
$block = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block );
|
|
|
|
|
$this->assertSame( 'test hideuser', $block->getReasonComment()->text );
|
|
|
|
|
$this->assertTrue( $block->getHideName() );
|
|
|
|
|
}
|
2021-04-01 08:53:15 +00:00
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlock
|
2021-04-01 08:53:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testExistingPage() {
|
|
|
|
|
$this->getExistingTestPage( 'Existing Page' );
|
|
|
|
|
$pageRestriction = PageRestriction::class;
|
|
|
|
|
$page = $pageRestriction::newFromTitle( 'Existing Page' );
|
|
|
|
|
$status = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
|
|
|
|
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(),
|
|
|
|
|
'infinity',
|
|
|
|
|
'test existingpage',
|
|
|
|
|
[],
|
|
|
|
|
[ $page ]
|
|
|
|
|
)->placeBlock();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $status );
|
2021-04-01 08:53:15 +00:00
|
|
|
$block = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block );
|
|
|
|
|
$this->assertSame( 'test existingpage', $block->getReasonComment()->text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlock
|
2021-04-01 08:53:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testNonexistentPage() {
|
|
|
|
|
$pageRestriction = PageRestriction::class;
|
|
|
|
|
$page = $pageRestriction::newFromTitle( 'nonexistent' );
|
|
|
|
|
$status = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
|
|
|
|
$this->getTestUser( [ 'sysop', 'suppress' ] )->getUser(),
|
|
|
|
|
'infinity',
|
|
|
|
|
'test nonexistentpage',
|
|
|
|
|
[],
|
|
|
|
|
[ $page ]
|
|
|
|
|
)->placeBlock();
|
2022-03-04 22:00:02 +00:00
|
|
|
$this->assertStatusError( 'cant-block-nonexistent-page', $status );
|
2021-04-01 08:53:15 +00:00
|
|
|
}
|
2021-07-08 10:41:16 +00:00
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlockInternal
|
2021-07-08 10:41:16 +00:00
|
|
|
*/
|
|
|
|
|
public function testReblock() {
|
|
|
|
|
$blockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-07-08 10:41:16 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test block'
|
|
|
|
|
)->placeBlockUnsafe();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $blockStatus );
|
2021-07-08 10:41:16 +00:00
|
|
|
$priorBlock = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $priorBlock );
|
|
|
|
|
$this->assertSame( 'test block', $priorBlock->getReasonComment()->text );
|
|
|
|
|
|
|
|
|
|
$blockId = $priorBlock->getId();
|
|
|
|
|
|
|
|
|
|
$reblockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
|
|
|
|
$this->mockAnonUltimateAuthority(),
|
|
|
|
|
'infinity',
|
|
|
|
|
'test reblock'
|
|
|
|
|
)->placeBlockUnsafe( /*reblock=*/false );
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusError( 'ipb_already_blocked', $reblockStatus );
|
2021-07-08 10:41:16 +00:00
|
|
|
|
|
|
|
|
$this->user->clearInstanceCache();
|
|
|
|
|
$block = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block );
|
|
|
|
|
$this->assertSame( $blockId, $block->getId() );
|
|
|
|
|
|
|
|
|
|
$reblockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-07-08 10:41:16 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test reblock'
|
|
|
|
|
)->placeBlockUnsafe( /*reblock=*/true );
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $reblockStatus );
|
2021-07-08 10:41:16 +00:00
|
|
|
|
|
|
|
|
$this->user->clearInstanceCache();
|
|
|
|
|
$block = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $block );
|
|
|
|
|
$this->assertSame( 'test reblock', $block->getReasonComment()->text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlockInternal
|
2021-07-08 10:41:16 +00:00
|
|
|
*/
|
|
|
|
|
public function testPostHook() {
|
|
|
|
|
$hookBlock = false;
|
|
|
|
|
$hookPriorBlock = false;
|
|
|
|
|
$this->setTemporaryHook(
|
|
|
|
|
'BlockIpComplete',
|
|
|
|
|
static function ( $block, $legacyUser, $priorBlock )
|
|
|
|
|
use ( &$hookBlock, &$hookPriorBlock )
|
|
|
|
|
{
|
|
|
|
|
$hookBlock = $block;
|
|
|
|
|
$hookPriorBlock = $priorBlock;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$blockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-07-08 10:41:16 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test block'
|
|
|
|
|
)->placeBlockUnsafe();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $blockStatus );
|
2021-07-08 10:41:16 +00:00
|
|
|
$priorBlock = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $priorBlock );
|
|
|
|
|
$this->assertSame( $priorBlock->getId(), $hookBlock->getId() );
|
|
|
|
|
$this->assertNull( $hookPriorBlock );
|
|
|
|
|
|
|
|
|
|
$hookBlock = false;
|
|
|
|
|
$hookPriorBlock = false;
|
|
|
|
|
$reblockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-07-08 10:41:16 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test reblock'
|
|
|
|
|
)->placeBlockUnsafe( /*reblock=*/true );
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $reblockStatus );
|
2021-07-08 10:41:16 +00:00
|
|
|
|
|
|
|
|
$this->user->clearInstanceCache();
|
|
|
|
|
$newBlock = $this->user->getBlock();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $newBlock );
|
|
|
|
|
$this->assertSame( $newBlock->getId(), $hookBlock->getId() );
|
|
|
|
|
$this->assertSame( $priorBlock->getId(), $hookPriorBlock->getId() );
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 09:05:36 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \MediaWiki\Block\BlockUser::placeBlockInternal
|
2021-08-01 09:05:36 +00:00
|
|
|
*/
|
|
|
|
|
public function testIPBlockAllowedAutoblockPreserved() {
|
|
|
|
|
$blockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$this->user,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-08-01 09:05:36 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test block with autoblocking',
|
|
|
|
|
[ 'isAutoblocking' => true ]
|
|
|
|
|
)->placeBlockUnsafe();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $blockStatus );
|
2021-08-01 09:05:36 +00:00
|
|
|
$block = $blockStatus->getValue();
|
|
|
|
|
|
|
|
|
|
$target = '1.2.3.4';
|
|
|
|
|
$autoBlockId = $block->doAutoblock( $target );
|
|
|
|
|
$this->assertNotFalse( $autoBlockId );
|
|
|
|
|
|
|
|
|
|
$hookPriorBlock = false;
|
|
|
|
|
$this->setTemporaryHook(
|
|
|
|
|
'BlockIpComplete',
|
|
|
|
|
static function ( $block, $legacyUser, $priorBlock )
|
|
|
|
|
use ( &$hookPriorBlock )
|
|
|
|
|
{
|
|
|
|
|
$hookPriorBlock = $priorBlock;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$IPBlockStatus = $this->blockUserFactory->newBlockUser(
|
|
|
|
|
$target,
|
2024-06-27 10:31:40 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority(),
|
2021-08-01 09:05:36 +00:00
|
|
|
'infinity',
|
|
|
|
|
'test IP block'
|
|
|
|
|
)->placeBlockUnsafe();
|
2023-10-19 10:42:53 +00:00
|
|
|
$this->assertStatusGood( $IPBlockStatus );
|
2021-08-01 09:05:36 +00:00
|
|
|
$IPBlock = $IPBlockStatus->getValue();
|
|
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $IPBlock );
|
|
|
|
|
$this->assertNull( $hookPriorBlock );
|
|
|
|
|
|
|
|
|
|
$blockIds = array_map(
|
|
|
|
|
static function ( DatabaseBlock $block ) {
|
|
|
|
|
return $block->getId();
|
|
|
|
|
},
|
2023-10-31 07:57:33 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()
|
|
|
|
|
->newListFromTarget( $target, null, /*fromPrimary=*/true )
|
2021-08-01 09:05:36 +00:00
|
|
|
);
|
|
|
|
|
$this->assertContains( $autoBlockId, $blockIds );
|
|
|
|
|
$this->assertContains( $IPBlock->getId(), $blockIds );
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 19:33:56 +00:00
|
|
|
}
|