aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit
diff options
context:
space:
mode:
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>2023-10-20 07:25:10 +0000
committerGerrit Code Review <gerrit@wikimedia.org>2023-10-20 07:25:10 +0000
commit343443a1cf097eb968010df4a43c91107c004917 (patch)
treeacdbd5953e8d7708d820a0dc3392f48f97e118c4 /tests/phpunit
parent85c514f9ac1a6b498fec1669229e5e36b8e27825 (diff)
parent9e7c9c13b900fb822af6a748d9d59f32d686ca52 (diff)
downloadmediawikicore-343443a1cf097eb968010df4a43c91107c004917.tar.gz
mediawikicore-343443a1cf097eb968010df4a43c91107c004917.zip
Merge "Multiblocks preparation in User::getBlock(), PermissionManager and BlockManager"
Diffstat (limited to 'tests/phpunit')
-rw-r--r--tests/phpunit/includes/Permissions/PermissionManagerTest.php145
-rw-r--r--tests/phpunit/includes/block/DatabaseBlockTest.php8
2 files changed, 119 insertions, 34 deletions
diff --git a/tests/phpunit/includes/Permissions/PermissionManagerTest.php b/tests/phpunit/includes/Permissions/PermissionManagerTest.php
index 3c008c1014cb..d5a96c5d82b7 100644
--- a/tests/phpunit/includes/Permissions/PermissionManagerTest.php
+++ b/tests/phpunit/includes/Permissions/PermissionManagerTest.php
@@ -3,7 +3,10 @@
namespace MediaWiki\Tests\Integration\Permissions;
use Action;
+use MediaWiki\Block\Block;
use MediaWiki\Block\BlockActionInfo;
+use MediaWiki\Block\BlockManager;
+use MediaWiki\Block\CompositeBlock;
use MediaWiki\Block\DatabaseBlock;
use MediaWiki\Block\Restriction\ActionRestriction;
use MediaWiki\Block\Restriction\NamespaceRestriction;
@@ -472,11 +475,7 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
$block->setRestrictions( [ $pageRestriction ] );
}
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'getBlock' ] )
- ->getMock();
- $user->method( 'getBlock' )
- ->willReturn( $block );
+ $user = $this->createUserWithBlock( $block );
$this->overrideUserPermissions( $user, [
'createpage',
@@ -499,7 +498,8 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
$action,
$user,
$this->title
- )
+ ),
+ "Number of permission errors for action \"$action\""
);
}
@@ -511,8 +511,10 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
$action,
$user,
PermissionManager::RIGOR_FULL,
- $this->title
- ) !== null
+ $this->title,
+ null
+ ) !== null,
+ "Block returned by getApplicableBlock() for action \"$action\""
);
}
@@ -523,6 +525,26 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
}
/**
+ * Create a user that is blocked in global state
+ *
+ * @param Block|null $block
+ * @return User
+ */
+ private function createUserWithBlock( ?Block $block ) {
+ $newUser = new User;
+ $blockManager = $this->getMockBuilder( BlockManager::class )
+ ->disableOriginalConstructor()
+ ->onlyMethods( [ 'getUserBlock' ] )
+ ->getMock();
+ $blockManager->method( 'getUserBlock' )
+ ->willReturnCallback( static function ( $paramUser ) use ( $newUser, $block ) {
+ return $paramUser === $newUser ? $block : null;
+ } );
+ $this->setService( 'BlockManager', $blockManager );
+ return $newUser;
+ }
+
+ /**
* Regression test for T348451
*/
public function testGetApplicableBlockForSpecialPage() {
@@ -532,12 +554,7 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
'auto' => true,
] );
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'getBlock' ] )
- ->getMock();
- $user->method( 'getBlock' )
- ->willReturn( $block );
-
+ $user = $this->createUserWithBlock( $block );
$title = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
$this->overrideUserPermissions( $user, [
@@ -555,7 +572,8 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
'edit',
$user,
PermissionManager::RIGOR_FULL,
- $title
+ $title,
+ null
)
);
}
@@ -675,6 +693,83 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
}
/**
+ * A test of the filter() calls in getApplicableBlock()
+ */
+ public function testGetApplicableBlockCompositeFilter() {
+ $this->overrideConfigValues( [
+ MainConfigNames::EnablePartialActionBlocks => true,
+ ] );
+ $blockOptions = [
+ 'address' => '127.0.8.1',
+ 'by' => UserIdentityValue::newRegistered( 100, 'Test' ),
+ 'sitewide' => false,
+ ];
+
+ $uploadBlock = new DatabaseBlock( $blockOptions );
+ $uploadBlock->setRestrictions( [
+ new ActionRestriction( 0, BlockActionInfo::ACTION_UPLOAD )
+ ] );
+
+ $emailBlock = new DatabaseBlock(
+ [
+ 'blockEmail' => true,
+ 'sitewide' => true
+ ] + $blockOptions
+ );
+
+ $page = $this->getExistingTestPage();
+ $page2 = $this->getExistingTestPage( __FUNCTION__ . ' page2' );
+ $pageBlock = new DatabaseBlock( $blockOptions );
+ $pageBlock->setRestrictions( [
+ new PageRestriction( 0, $page->getId() )
+ ] );
+
+ $compositeBlock = new CompositeBlock( [
+ 'originalBlocks' => [
+ $uploadBlock,
+ $emailBlock,
+ $pageBlock
+ ]
+ ] );
+ $user = $this->createUserWithBlock( $compositeBlock );
+ $permissionManager = $this->getServiceContainer()->getPermissionManager();
+
+ // The email block, being a sitewide block with an additional
+ // blockEmail option, also blocks upload.
+ // assertEquals() gives nicer failure messages than assertSame().
+ $this->assertEquals(
+ [ $uploadBlock, $emailBlock ],
+ $permissionManager->getApplicableBlock(
+ 'upload', $user, PermissionManager::RIGOR_FULL, null, null
+ )->toArray()
+ );
+
+ // Emailing is only blocked by the email block
+ $this->assertEquals(
+ [ $emailBlock ],
+ $permissionManager->getApplicableBlock(
+ 'sendemail', $user, PermissionManager::RIGOR_FULL, null, null
+ )->toArray()
+ );
+
+ // As for upload, the email block applies to sitewide editing
+ $this->assertEquals(
+ [ $emailBlock, $pageBlock ],
+ $permissionManager->getApplicableBlock(
+ 'edit', $user, PermissionManager::RIGOR_FULL, $page->getTitle(), null
+ )->toArray()
+ );
+
+ // Test filtering by page -- we use $page2 so $pageBlock does not apply
+ $this->assertEquals(
+ [ $emailBlock ],
+ $permissionManager->getApplicableBlock(
+ 'edit', $user, PermissionManager::RIGOR_FULL, $page2->getTitle(), null
+ )->toArray()
+ );
+ }
+
+ /**
* @dataProvider provideTestCheckUserBlockMessage
*/
public function testCheckUserBlockMessage( $blockType, $blockParams, $restriction, $expected ) {
@@ -695,12 +790,7 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
$block->setRestrictions( [ $pageRestriction ] );
}
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'getBlock' ] )
- ->getMock();
- $user->method( 'getBlock' )
- ->willReturn( $block );
-
+ $user = $this->createUserWithBlock( $block );
$this->overrideUserPermissions( $user, [ 'edit', 'createpage' ] );
$permissionManager = $this->getServiceContainer()->getPermissionManager();
@@ -818,15 +908,10 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
],
] );
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'getBlock' ] )
- ->getMock();
- $user->method( 'getBlock' )
- ->willReturn( new DatabaseBlock( [
- 'address' => '127.0.8.1',
- 'by' => $this->user,
- ] ) );
-
+ $user = $this->createUserWithBlock( new DatabaseBlock( [
+ 'address' => '127.0.8.1',
+ 'by' => $this->user,
+ ] ) );
$this->assertCount( 1, $this->getServiceContainer()->getPermissionManager()
->getPermissionErrors( 'tester', $user, $this->title )
);
diff --git a/tests/phpunit/includes/block/DatabaseBlockTest.php b/tests/phpunit/includes/block/DatabaseBlockTest.php
index c66e753261be..11dafd4f091d 100644
--- a/tests/phpunit/includes/block/DatabaseBlockTest.php
+++ b/tests/phpunit/includes/block/DatabaseBlockTest.php
@@ -334,8 +334,8 @@ class DatabaseBlockTest extends MediaWikiLangTestCase {
// Reload user
$u = User::newFromName( $username );
- $this->assertFalse(
- $u->isBlockedFromCreateAccount(),
+ $this->assertTrue(
+ $u->isDefinitelyAllowed( 'createaccount' ),
"Our sandbox user should be able to create account before being blocked"
);
@@ -370,8 +370,8 @@ class DatabaseBlockTest extends MediaWikiLangTestCase {
// Reload user
$u = User::newFromName( $username );
- $this->assertTrue(
- (bool)$u->isBlockedFromCreateAccount(),
+ $this->assertFalse(
+ $u->isDefinitelyAllowed( 'createaccount' ),
"Our sandbox user '$username' should NOT be able to create account"
);
}