2019-05-24 00:07:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\MediaWiki\Minerva;
|
|
|
|
|
|
|
|
use ContentHandler;
|
2021-05-28 20:15:31 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2019-07-09 16:10:31 +00:00
|
|
|
use MediaWiki\Minerva\LanguagesHelper;
|
2019-05-24 00:07:27 +00:00
|
|
|
use MediaWiki\Minerva\Permissions\IMinervaPagePermissions;
|
|
|
|
use MediaWiki\Minerva\Permissions\MinervaPagePermissions;
|
|
|
|
use MediaWiki\Minerva\SkinOptions;
|
2019-08-27 20:27:57 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2021-04-29 17:20:44 +00:00
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2021-10-11 22:53:10 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2019-10-08 17:22:31 +00:00
|
|
|
use RequestContext;
|
2019-05-24 00:07:27 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group MinervaNeue
|
|
|
|
* @coversDefaultClass \MediaWiki\Minerva\Permissions\MinervaPagePermissions
|
|
|
|
*/
|
2021-10-11 22:53:10 +00:00
|
|
|
class MinervaPagePermissionsTest extends MediaWikiIntegrationTestCase {
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
private function buildPermissionsObject(
|
|
|
|
Title $title,
|
2021-10-18 21:04:03 +00:00
|
|
|
$actions = null, /* unused */
|
2019-05-24 00:07:27 +00:00
|
|
|
array $options = [],
|
|
|
|
ContentHandler $contentHandler = null,
|
|
|
|
User $user = null,
|
2019-07-09 16:10:31 +00:00
|
|
|
$hasOtherLanguagesOrVariants = false,
|
2021-03-09 19:43:14 +00:00
|
|
|
$alwaysShowLanguageButton = true,
|
|
|
|
$isSuperUser = false
|
2019-05-24 00:07:27 +00:00
|
|
|
) {
|
2019-10-22 17:13:55 +00:00
|
|
|
$languageHelper = $this->createMock( LanguagesHelper::class );
|
2019-07-09 16:10:31 +00:00
|
|
|
$languageHelper->expects( $this->any() )
|
|
|
|
->method( 'doesTitleHasLanguagesOrVariants' )
|
|
|
|
->willReturn( $hasOtherLanguagesOrVariants );
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
$user = $user ?? $this->getTestUser()->getUser();
|
|
|
|
$actions = $actions ?? [
|
2019-08-29 14:16:10 +00:00
|
|
|
IMinervaPagePermissions::CONTENT_EDIT,
|
2019-05-24 00:07:27 +00:00
|
|
|
IMinervaPagePermissions::WATCH,
|
|
|
|
IMinervaPagePermissions::TALK,
|
|
|
|
IMinervaPagePermissions::SWITCH_LANGUAGE,
|
2021-03-09 19:43:14 +00:00
|
|
|
IMinervaPagePermissions::MOVE,
|
|
|
|
IMinervaPagePermissions::DELETE,
|
|
|
|
IMinervaPagePermissions::PROTECT,
|
2019-05-24 00:07:27 +00:00
|
|
|
];
|
|
|
|
$contentHandler = $contentHandler ??
|
|
|
|
$this->getMockForAbstractClass( ContentHandler::class, [], '', false );
|
|
|
|
$skinOptions = new SkinOptions();
|
|
|
|
if ( $options ) {
|
|
|
|
$skinOptions->setMultiple( $options );
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:22:31 +00:00
|
|
|
$context = new RequestContext();
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$context->setConfig( new \HashConfig( [
|
|
|
|
'MinervaAlwaysShowLanguageButton' => $alwaysShowLanguageButton
|
|
|
|
] ) );
|
|
|
|
$context->setUser( $user );
|
|
|
|
|
2021-03-09 19:43:14 +00:00
|
|
|
$permissionManager = $this->getMockBuilder( PermissionManager::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$permissionManager->expects( $this->any() )
|
|
|
|
->method( 'quickUserCan' )
|
|
|
|
->willReturn( $isSuperUser );
|
|
|
|
|
2021-05-28 20:15:31 +00:00
|
|
|
$contentHandlerFactory = $this->createMock( IContentHandlerFactory::class );
|
|
|
|
|
|
|
|
$contentHandlerFactory->expects( $this->any() )
|
|
|
|
->method( 'getContentHandler' )
|
|
|
|
->willReturn( $contentHandler );
|
|
|
|
|
2019-10-08 17:22:31 +00:00
|
|
|
return ( new MinervaPagePermissions(
|
2019-05-24 00:07:27 +00:00
|
|
|
$skinOptions,
|
2019-08-27 20:27:57 +00:00
|
|
|
$languageHelper,
|
2021-05-28 20:15:31 +00:00
|
|
|
$permissionManager,
|
|
|
|
$contentHandlerFactory
|
|
|
|
) )->setContext( $context );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testWatchAndEditNotAllowedOnMainPage() {
|
2021-10-18 21:04:03 +00:00
|
|
|
$userMock = $this->getMockBuilder( User::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->onlyMethods( [ 'isRegistered' ] )
|
|
|
|
->getMock();
|
|
|
|
$userMock->expects( $this->once() )
|
|
|
|
->method( 'isRegistered' )
|
|
|
|
->willReturn( false );
|
|
|
|
$permsAnon = $this->buildPermissionsObject( Title::newMainPage(), null, [], null, $userMock );
|
2019-05-24 00:07:27 +00:00
|
|
|
$perms = $this->buildPermissionsObject( Title::newMainPage() );
|
|
|
|
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::WATCH ) );
|
2019-08-29 14:16:10 +00:00
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::CONTENT_EDIT ) );
|
2021-10-18 21:04:03 +00:00
|
|
|
$this->assertFalse( $permsAnon->isAllowed( IMinervaPagePermissions::TALK ) );
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
// Check to make sure 'talk' and 'switch-language' are enabled on the Main page.
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::SWITCH_LANGUAGE ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testInvalidPageActionsArentAllowed() {
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), [] );
|
|
|
|
|
2021-10-18 21:04:03 +00:00
|
|
|
$this->assertFalse( $perms->isAllowed( 'blah' ) );
|
|
|
|
$this->assertFalse( $perms->isAllowed( 'wah' ) );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testValidPageActionsAreAllowed() {
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'test' ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::WATCH ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function editPageActionProvider() {
|
|
|
|
return [
|
|
|
|
[ false, false, false ],
|
|
|
|
[ true, false, false ],
|
|
|
|
[ true, true, true ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The "edit" page action is allowed when the page doesn't support direct editing via the API.
|
|
|
|
*
|
|
|
|
* @dataProvider editPageActionProvider
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testEditPageAction(
|
|
|
|
$supportsDirectEditing,
|
|
|
|
$supportsDirectApiEditing,
|
|
|
|
$expected
|
|
|
|
) {
|
2021-04-08 18:58:17 +00:00
|
|
|
$contentHandler = $this->getMockBuilder( ContentHandler::class )
|
2019-05-24 00:07:27 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$contentHandler->method( 'supportsDirectEditing' )
|
|
|
|
->will( $this->returnValue( $supportsDirectEditing ) );
|
|
|
|
|
|
|
|
$contentHandler->method( 'supportsDirectApiEditing' )
|
|
|
|
->will( $this->returnValue( $supportsDirectApiEditing ) );
|
|
|
|
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), null, [],
|
|
|
|
$contentHandler );
|
|
|
|
|
2019-08-29 14:16:10 +00:00
|
|
|
$this->assertEquals( $expected, $perms->isAllowed( IMinervaPagePermissions::CONTENT_EDIT ) );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testPageActionsWhenOnUserPage() {
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'User:Admin' ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testPageActionsWhenOnAnonUserPage() {
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'User:1.1.1.1' ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function switchLanguagePageActionProvider() {
|
|
|
|
return [
|
2019-07-09 16:10:31 +00:00
|
|
|
[ true, false, true ],
|
|
|
|
[ false, true, true ],
|
|
|
|
[ false, false, false ],
|
2019-05-24 00:07:27 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:46:44 +00:00
|
|
|
/**
|
|
|
|
* MediaWiki defines wgHideInterlanguageLinks which is default set to false, but some wikis
|
|
|
|
* can set this config to true. Minerva page permissions must respect that
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testGlobalHideLanguageLinksTakesPrecedenceOnMainPage() {
|
|
|
|
$this->setMwGlobals( [ 'wgHideInterlanguageLinks' => true ] );
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newMainPage() );
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::SWITCH_LANGUAGE ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MediaWiki defines wgHideInterlanguageLinks which is default set to false, but some wikis
|
|
|
|
* can set this config to true. Minerva page permissions must respect that
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testGlobalHideLanguageLinksTakesPrecedence() {
|
|
|
|
$this->setMwGlobals( [ 'wgHideInterlanguageLinks' => true ] );
|
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'test' ) );
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::SWITCH_LANGUAGE ) );
|
|
|
|
}
|
|
|
|
|
2019-05-24 00:07:27 +00:00
|
|
|
/**
|
|
|
|
* The "switch-language" page action is allowed when: v2 of the page action bar is enabled and
|
|
|
|
* if the page has interlanguage links or if the <code>$wgMinervaAlwaysShowLanguageButton</code>
|
|
|
|
* configuration variable is set to truthy.
|
|
|
|
*
|
|
|
|
* @dataProvider switchLanguagePageActionProvider
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testSwitchLanguagePageAction(
|
2019-07-09 16:10:31 +00:00
|
|
|
$hasLanguagesOrVariants,
|
2019-05-24 00:07:27 +00:00
|
|
|
$minervaAlwaysShowLanguageButton,
|
|
|
|
$expected
|
|
|
|
) {
|
2019-10-22 17:13:55 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
2019-05-24 00:07:27 +00:00
|
|
|
$title->expects( $this->once() )
|
|
|
|
->method( 'isMainPage' )
|
|
|
|
->willReturn( false );
|
2021-05-28 20:15:31 +00:00
|
|
|
$title->expects( $this->any() )
|
|
|
|
->method( 'getContentModel' )
|
|
|
|
->willReturn( CONTENT_MODEL_WIKITEXT );
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
$permissions = $this->buildPermissionsObject(
|
|
|
|
$title,
|
|
|
|
null,
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
null,
|
2019-07-09 16:10:31 +00:00
|
|
|
$hasLanguagesOrVariants,
|
2019-05-24 00:07:27 +00:00
|
|
|
$minervaAlwaysShowLanguageButton
|
|
|
|
);
|
|
|
|
|
|
|
|
$actual = $permissions->isAllowed( IMinervaPagePermissions::SWITCH_LANGUAGE );
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch action requires 'viewmywatchlist' and 'editmywatchlist' permissions
|
|
|
|
* to be grated. Verify that isAllowedAction('watch') returns false when user
|
|
|
|
* do not have those permissions granted
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testWatchIsAllowedOnlyWhenWatchlistPermissionsAreGranted() {
|
|
|
|
$title = Title::newFromText( 'test_watchstar_permissions' );
|
|
|
|
|
2021-04-08 18:58:17 +00:00
|
|
|
$userMock = $this->getMockBuilder( User::class )
|
2019-05-24 00:07:27 +00:00
|
|
|
->disableOriginalConstructor()
|
2021-05-03 14:12:42 +00:00
|
|
|
->onlyMethods( [ 'isAllowedAll' ] )
|
2019-05-24 00:07:27 +00:00
|
|
|
->getMock();
|
|
|
|
$userMock->expects( $this->once() )
|
|
|
|
->method( 'isAllowedAll' )
|
|
|
|
->with( 'viewmywatchlist', 'editmywatchlist' )
|
|
|
|
->willReturn( false );
|
|
|
|
|
|
|
|
$perms = $this->buildPermissionsObject( $title, null, [], null, $userMock );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::TALK ) );
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::WATCH ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If Title is not watchable, it cannot be watched
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testCannotWatchNotWatchableTitle() {
|
2019-10-22 17:13:55 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
2019-05-24 00:07:27 +00:00
|
|
|
$title->expects( $this->once() )
|
|
|
|
->method( 'isMainPage' )
|
|
|
|
->willReturn( false );
|
2021-05-28 20:15:31 +00:00
|
|
|
$title->expects( $this->any() )
|
|
|
|
->method( 'getContentModel' )
|
|
|
|
->willReturn( CONTENT_MODEL_UNKNOWN );
|
2021-04-29 17:20:44 +00:00
|
|
|
|
|
|
|
$watchlistManager = $this->createMock( WatchlistManager::class );
|
|
|
|
$watchlistManager->expects( $this->once() )
|
2019-05-24 00:07:27 +00:00
|
|
|
->method( 'isWatchable' )
|
|
|
|
->willReturn( false );
|
2021-04-29 17:20:44 +00:00
|
|
|
$this->setService( 'WatchlistManager', $watchlistManager );
|
2019-05-24 00:07:27 +00:00
|
|
|
|
|
|
|
$permissions = $this->buildPermissionsObject( $title );
|
2020-01-26 19:26:31 +00:00
|
|
|
$this->assertFalse( $permissions->isAllowed( IMinervaPagePermissions::WATCH ) );
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:43:14 +00:00
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testMoveAndDeleteAndProtectNotAllowedByDefault() {
|
2021-10-18 21:04:03 +00:00
|
|
|
$perms = $this->buildPermissionsObject( Title::newFromText( 'test' ), null );
|
2021-03-09 19:43:14 +00:00
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::MOVE ) );
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::DELETE ) );
|
|
|
|
$this->assertFalse( $perms->isAllowed( IMinervaPagePermissions::PROTECT ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::isAllowed
|
|
|
|
*/
|
|
|
|
public function testMoveAndDeleteAndProtectAllowedForUserWithPermissions() {
|
|
|
|
$title = $this->createMock( Title::class );
|
|
|
|
$title->expects( $this->any() )
|
|
|
|
->method( 'exists' )
|
|
|
|
->willReturn( true );
|
2021-05-28 20:15:31 +00:00
|
|
|
$title->expects( $this->any() )
|
|
|
|
->method( 'getContentModel' )
|
|
|
|
->willReturn( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
2021-03-09 19:43:14 +00:00
|
|
|
$perms = $this->buildPermissionsObject(
|
|
|
|
$title,
|
|
|
|
[
|
|
|
|
IMinervaPagePermissions::CONTENT_EDIT,
|
|
|
|
IMinervaPagePermissions::WATCH,
|
|
|
|
IMinervaPagePermissions::TALK,
|
|
|
|
IMinervaPagePermissions::SWITCH_LANGUAGE,
|
|
|
|
IMinervaPagePermissions::MOVE,
|
|
|
|
IMinervaPagePermissions::DELETE,
|
|
|
|
IMinervaPagePermissions::PROTECT,
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
null,
|
|
|
|
$this->getTestUser()->getUser(),
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::MOVE ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::DELETE ) );
|
|
|
|
$this->assertTrue( $perms->isAllowed( IMinervaPagePermissions::PROTECT ) );
|
|
|
|
}
|
2019-05-24 00:07:27 +00:00
|
|
|
}
|