wiki.techinc.nl/tests/phpunit/includes/block/BlockPermissionCheckerTest.php
Martin Urbanec 3c2c7b0f39 Move SpecialBlock::canBlockEmail to BlockPermissionChecker
Also soft deprecate the old function. Replacing usages will be done
in a follow-up patch.

Bug: T263327
Change-Id: Iffa9337f3c5bd198693fdf51be55c4b1ecfacaf6
2020-09-19 17:00:47 +00:00

195 lines
4.6 KiB
PHP

<?php
use MediaWiki\Block\BlockPermissionCheckerFactory;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\MediaWikiServices;
/**
* @group Database
* @group Blocking
* @coversDefaultClass \MediaWiki\Block\BlockPermissionChecker
*/
class BlockPermissionCheckerTest extends MediaWikiLangTestCase {
/**
* @var User
*/
private $sysop;
/**
* @var User
*/
private $user;
/**
* @var BlockPermissionCheckerFactory
*/
private $factory;
protected function setUp() : void {
parent::setUp();
$this->setGroupPermissions( 'sysop', 'unblockself', false );
$this->setGroupPermissions( 'bureaucrat', 'unblockself', true );
$this->sysop = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
$this->user = $this->getTestUser()->getUser();
$this->factory = MediaWikiServices::getInstance()
->getBlockPermissionCheckerFactory();
}
/**
* @covers ::checkBlockPermissions
*/
public function testNotBlockedPerformer() {
$this->assertTrue(
$this->factory->newBlockPermissionChecker(
$this->user,
$this->sysop
)->checkBlockPermissions()
);
}
/**
* @covers ::checkBlockPermissions
*/
public function testBlockedPerformer() {
$block = new DatabaseBlock( [
'address' => $this->sysop->getName(),
'user' => $this->sysop->getId(),
'by' => $this->getMutableTestUser( [ 'sysop' ] )->getUser()->getId(),
'expiry' => 'infinity',
'sitewide' => true,
'enableAutoblock' => true,
] );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertEquals(
'ipbblocked',
$this->factory->newBlockPermissionChecker(
$this->user,
$this->sysop
)->checkBlockPermissions()
);
}
/**
* @covers ::checkBlockPermissions
*/
public function testSelfblockedPerformer() {
$block = new DatabaseBlock( [
'address' => $this->sysop->getName(),
'user' => $this->sysop->getId(),
'by' => $this->sysop->getId(),
'expiry' => 'infinity',
'sitewide' => true,
'enableAutoblock' => true,
] );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertTrue(
$this->factory->newBlockPermissionChecker(
$this->sysop,
$this->sysop
)->checkBlockPermissions()
);
}
/**
* @covers ::checkBlockPermissions
*/
public function testNoUnblockself() {
$block = new DatabaseBlock( [
'address' => $this->sysop->getName(),
'user' => $this->sysop->getId(),
'by' => $this->getMutableTestUser( [ 'sysop' ] )->getUser()->getId(),
'expiry' => 'infinity',
'sitewide' => true,
'enableAutoblock' => true,
] );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertEquals(
'ipbnounblockself',
$this->factory->newBlockPermissionChecker(
$this->sysop,
$this->sysop
)->checkBlockPermissions()
);
}
/**
* @covers ::checkBlockPermissions
*/
public function testHasUnblockself() {
$bureaucrat = $this->getMutableTestUser( [ 'sysop', 'bureaucrat' ] )->getUser();
$block = new DatabaseBlock( [
'address' => $bureaucrat->getName(),
'user' => $bureaucrat->getId(),
'by' => $this->getMutableTestUser( [ 'sysop' ] )->getUser()->getId(),
'expiry' => 'infinity',
'sitewide' => true,
'enableAutoblock' => true,
] );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertTrue(
$this->factory->newBlockPermissionChecker(
$bureaucrat,
$bureaucrat
)->checkBlockPermissions()
);
}
/**
* @covers ::checkBlockPermissions
*/
public function testBlocker() {
$blockee = $this->sysop;
$blocker = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
$block = new DatabaseBlock( [
'address' => $blockee->getName(),
'user' => $blockee->getId(),
'by' => $blocker->getId(),
'expiry' => 'infinity',
'sitewide' => true,
'enableAutoblock' => true,
] );
MediaWikiServices::getInstance()->getDatabaseBlockStore()->insertBlock( $block );
$this->assertTrue(
$this->factory->newBlockPermissionChecker(
$blocker,
$blockee
)->checkBlockPermissions()
);
}
/**
* @covers ::checkEmailPermissions
*/
public function testCheckEmailPermissionOkay() {
$this->assertTrue(
$this->factory->newBlockPermissionChecker(
null,
$this->sysop
)
->checkEmailPermissions()
);
}
/**
* @covers ::checkEmailPermissions
*/
public function testCheckEmailPermissionNoPermission() {
$this->overrideUserPermissions( $this->sysop, [ 'blockemail' => false ] );
$this->assertFalse(
$this->factory->newBlockPermissionChecker(
null,
$this->sysop
)
->checkEmailPermissions()
);
}
}