Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/301103

Change subject: [Hygiene] Test SkinMinerva#isAllowedPageAction
......................................................................

[Hygiene] Test SkinMinerva#isAllowedPageAction

Bug: T140260
Change-Id: Iaa22edec9694983c245ed3915ee5dfd6b152050d
---
M includes/skins/SkinMinerva.php
A tests/phpunit/skins/SkinMinervaPageActionsTest.php
2 files changed, 133 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/03/301103/1

diff --git a/includes/skins/SkinMinerva.php b/includes/skins/SkinMinerva.php
index 784b369..4f5ea67 100644
--- a/includes/skins/SkinMinerva.php
+++ b/includes/skins/SkinMinerva.php
@@ -122,27 +122,39 @@
        }
 
        /**
-        * Returns true, if the pageaction is configured to be displayed.
+        * Gets whether or not the page action is allowed.
+        *
+        * Page actions isn't allowed when:
+        * <ul>
+        *   <li>
+        *     the action is disabled (by removing it from the 
<code>MinervaPageActions</code>
+        *     configuration variable; or
+        *   </li>
+        *   <li>the user is on the main page</li>
+        * </ul>
+        *
+        * Furthermore, the "edit" page action isn't allowed if the content of 
the page doesn't support
+        * direct editing via the API.
+        *
         * @param string $action
         * @return boolean
         */
        protected function isAllowedPageAction( $action ) {
-               $title = $this->getTitle();
-               // All actions disabled on main apge.
-               if ( !$title->isMainPage() &&
-                       in_array( $action, $this->getMFConfig()->get( 
'MinervaPageActions' ) ) ) {
-
-                       if ( $action === 'edit' ) {
-                               $contentHandler = $this->getContentHandler();
-
-                               return $contentHandler->supportsDirectEditing() 
&&
-                                       
$contentHandler->supportsDirectApiEditing();
-                       } else {
-                               return true;
-                       }
-               } else {
+               if (
+                       ! in_array( $action, $this->getMFConfig()->get( 
'MinervaPageActions' ) )
+                       || $this->getTitle()->isMainPage()
+               ) {
                        return false;
                }
+
+               if ( $action === 'edit' ) {
+                       $contentHandler = $this->getContentHandler();
+
+                       return $contentHandler->supportsDirectEditing() &&
+                               $contentHandler->supportsDirectApiEditing();
+               }
+
+               return true;
        }
 
        /**
diff --git a/tests/phpunit/skins/SkinMinervaPageActionsTest.php 
b/tests/phpunit/skins/SkinMinervaPageActionsTest.php
new file mode 100644
index 0000000..f3826b5
--- /dev/null
+++ b/tests/phpunit/skins/SkinMinervaPageActionsTest.php
@@ -0,0 +1,106 @@
+<?php
+
+// FIXME: That this class exists is an indicator that at least 
SkinMinerva#isAllowedPageAction
+// should be extracted from SkinMinerva.
+class TestSkinMinerva extends SkinMinerva {
+       public function isAllowedPageAction( $action ) {
+               return parent::isAllowedPageAction( $action );
+       }
+
+       public function setContentHandler( ContentHandler $contentHandler ) {
+               $this->contentHandler = $contentHandler;
+       }
+}
+
+/**
+ * @group MobileFrontend
+ */
+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 ) {
+               $requestContext = RequestContext::getMain();
+               $requestContext->setTitle( $title );
+
+               $result = new TestSkinMinerva();
+               $result->setContext( $requestContext );
+
+               return $result;
+       }
+
+       /**
+        * @covers SkinMinerva::isAllowedPageAction
+        */
+       public function test_page_actions_arent_allowed_when_on_the_main_page() 
{
+               $skin = $this->getSkin( Title::newMainPage() );
+
+               $this->assertFalse( $skin->isAllowedPageAction( 'watch' ) );
+       }
+
+       /**
+        * @covers SkinMinerva::isAllowedPageAction
+        */
+       public function test_invalid_page_actions_arent_allowed() {
+               $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
+        */
+       public function test_valid_page_actions_are_allowed() {
+               $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
+        */
+       public function test_edit_page_action(
+               $supportsDirectEditing,
+               $supportsDirectApiEditing,
+               $expected
+       ) {
+               $contentHandler = $this->getMockBuilder( 'ContentHandler' )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $contentHandler->method( 'supportsDirectEditing' )
+                       ->will( $this->returnValue( $supportsDirectEditing ) );
+
+               $contentHandler->method( 'supportsDirectApiEditing' )
+                       ->will( $this->returnValue( $supportsDirectApiEditing ) 
);
+
+               $this->skin->setContentHandler( $contentHandler );
+
+               $this->assertEquals( $expected, 
$this->skin->isAllowedPageAction( 'edit' ) );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/301103
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iaa22edec9694983c245ed3915ee5dfd6b152050d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to