2017-07-12 15:12:40 +00:00
|
|
|
<?php
|
|
|
|
|
2017-07-14 23:56:16 +00:00
|
|
|
namespace Tests\MediaWiki\Minerva;
|
|
|
|
|
|
|
|
use SkinMinerva;
|
|
|
|
use MediaWikiTestCase;
|
|
|
|
use Title;
|
|
|
|
use RequestContext;
|
2018-11-01 13:35:57 +00:00
|
|
|
use MediaWiki\Minerva\SkinUserPageHelper;
|
2017-07-14 23:56:16 +00:00
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
// FIXME: That this class exists is an indicator that at least SkinMinerva#isAllowedPageAction
|
|
|
|
// should be extracted from SkinMinerva.
|
2018-04-15 23:12:19 +00:00
|
|
|
// phpcs:ignore MediaWiki.Files.ClassMatchesFilename.NotMatch
|
2017-07-12 15:12:40 +00:00
|
|
|
class TestSkinMinerva extends SkinMinerva {
|
2019-04-10 12:52:34 +00:00
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
public function isAllowedPageAction( $action ) {
|
|
|
|
return parent::isAllowedPageAction( $action );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDoesPageHaveLanguages( $doesPageHaveLanguages ) {
|
|
|
|
$this->doesPageHaveLanguages = $doesPageHaveLanguages;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-14 23:56:16 +00:00
|
|
|
* @group MinervaNeue
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
class SkinMinervaPageActionsTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TestSkinMinerva
|
|
|
|
*/
|
|
|
|
private $skin;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->skin = $this->getSkin( Title::newFromText( 'SkinMinervaPageActionsTest' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
|
|
|
* @return TestSkinMinerva
|
|
|
|
*/
|
|
|
|
private function getSkin( Title $title ) {
|
2019-04-10 12:52:34 +00:00
|
|
|
$requestContext = RequestContext::getMain();
|
2017-07-12 15:12:40 +00:00
|
|
|
$requestContext->setTitle( $title );
|
|
|
|
|
|
|
|
$result = new TestSkinMinerva();
|
|
|
|
$result->setContext( $requestContext );
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testPageActionsArentAllowedWhenOnTheMainPage() {
|
2017-07-12 15:12:40 +00:00
|
|
|
$skin = $this->getSkin( Title::newMainPage() );
|
|
|
|
|
|
|
|
$this->assertFalse( $skin->isAllowedPageAction( 'watch' ) );
|
2018-10-21 15:10:41 +00:00
|
|
|
$this->assertFalse( $skin->isAllowedPageAction( 'edit' ) );
|
|
|
|
|
|
|
|
// Check to make sure 'talk' and 'switch-language' are enabled on the Main page.
|
|
|
|
$this->assertTrue( $skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
$this->assertTrue( $skin->isAllowedPageAction( 'switch-language' ) );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testInvalidPageActionsArentAllowed() {
|
2017-07-12 15:12:40 +00:00
|
|
|
$this->setMwGlobals( 'wgMinervaPageActions', [] );
|
|
|
|
|
|
|
|
// By default, the "talk" and "watch" page actions are allowed but are now deemed invalid.
|
|
|
|
$this->assertFalse( $this->skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
$this->assertFalse( $this->skin->isAllowedPageAction( 'watch' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testValidPageActionsAreAllowed() {
|
2017-07-12 15:12:40 +00:00
|
|
|
$this->assertTrue( $this->skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
$this->assertTrue( $this->skin->isAllowedPageAction( '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 SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testEditPageAction(
|
2017-07-12 15:12:40 +00:00
|
|
|
$supportsDirectEditing,
|
|
|
|
$supportsDirectApiEditing,
|
|
|
|
$expected
|
|
|
|
) {
|
|
|
|
$contentHandler = $this->getMockBuilder( 'ContentHandler' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$contentHandler->method( 'supportsDirectEditing' )
|
|
|
|
->will( $this->returnValue( $supportsDirectEditing ) );
|
|
|
|
|
|
|
|
$contentHandler->method( 'supportsDirectApiEditing' )
|
|
|
|
->will( $this->returnValue( $supportsDirectApiEditing ) );
|
|
|
|
|
2019-04-10 12:52:34 +00:00
|
|
|
$this->setService( 'Minerva.ContentHandler', $contentHandler );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$this->assertEquals( $expected, $this->skin->isAllowedPageAction( 'edit' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
|
|
|
public function testPageActionsWhenOnUserPage() {
|
2018-11-01 13:35:57 +00:00
|
|
|
$userPageHelper = $this->getMockBuilder( SkinUserPageHelper::class )
|
2017-07-12 15:12:40 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$skin = $this->getSkin( Title::newFromText( 'User:Admin' ) );
|
2019-04-10 12:52:34 +00:00
|
|
|
|
|
|
|
$this->setService( 'Minerva.SkinUserPageHelper', $userPageHelper );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-05-23 09:55:21 +00:00
|
|
|
$this->assertTrue( $skin->isAllowedPageAction( 'talk' ) );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
|
|
|
public function testPageActionsWhenNotOnUserPage() {
|
2018-11-01 13:35:57 +00:00
|
|
|
$userPageHelper = $this->getMockBuilder( SkinUserPageHelper::class )
|
2017-07-12 15:12:40 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2019-05-23 09:55:21 +00:00
|
|
|
$skin = $this->getSkin( Title::newFromText( 'A_test_page' ) );
|
|
|
|
$this->setService( 'Minerva.SkinUserPageHelper', $userPageHelper );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2019-05-23 09:55:21 +00:00
|
|
|
$this->assertTrue( $skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
|
|
|
public function testPageActionsWhenOnAnonUserPage() {
|
|
|
|
$userPageHelper = $this->getMockBuilder( SkinUserPageHelper::class )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$skin = $this->getSkin( Title::newFromText( 'User:1.1.1.1' ) );
|
2019-04-10 12:52:34 +00:00
|
|
|
$this->setService( 'Minerva.SkinUserPageHelper', $userPageHelper );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
$this->assertTrue( $skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function switchLanguagePageActionProvider() {
|
|
|
|
return [
|
|
|
|
[ true, false, true ],
|
|
|
|
[ false, true, true ],
|
|
|
|
[ false, false, false ],
|
|
|
|
[ true, false, true ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testSwitchLanguagePageAction(
|
2017-07-12 15:12:40 +00:00
|
|
|
$doesPageHaveLanguages,
|
|
|
|
$minervaAlwaysShowLanguageButton,
|
|
|
|
$expected
|
|
|
|
) {
|
|
|
|
$this->skin->setDoesPageHaveLanguages( $doesPageHaveLanguages );
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgMinervaAlwaysShowLanguageButton' => $minervaAlwaysShowLanguageButton,
|
|
|
|
] );
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $this->skin->isAllowedPageAction( 'switch-language' ) );
|
|
|
|
}
|
2019-05-23 19:25:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 SkinMinerva::isAllowedPageAction
|
|
|
|
*/
|
2019-06-03 16:53:40 +00:00
|
|
|
public function testWatchIsAllowedOnlyWhenWatchlistPermissionsAreGranted() {
|
2019-05-23 19:25:39 +00:00
|
|
|
$title = Title::newFromText( 'test_watchstar_permissions' );
|
|
|
|
$requestContext = RequestContext::getMain();
|
|
|
|
$requestContext->setTitle( $title );
|
|
|
|
$userMock = $this->getMockBuilder( 'User' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods( [ 'isAllowedAll' ] )
|
|
|
|
->getMock();
|
|
|
|
$userMock->expects( $this->once() )
|
|
|
|
->method( 'isAllowedAll' )
|
|
|
|
->with( 'viewmywatchlist', 'editmywatchlist' )
|
|
|
|
->willReturn( false );
|
|
|
|
$requestContext->setUser( $userMock );
|
|
|
|
|
|
|
|
$result = new TestSkinMinerva();
|
|
|
|
$result->setContext( $requestContext );
|
|
|
|
|
|
|
|
$this->assertTrue( $this->skin->isAllowedPageAction( 'talk' ) );
|
|
|
|
$this->assertFalse( $this->skin->isAllowedPageAction( 'watch' ) );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|