Merge "Block: Avoid deprecated wfMemcKey()"
[lhc/web/wiklou.git] / tests / phpunit / skins / SideBarTest.php
index 850d39c..af03fe6 100644 (file)
@@ -5,27 +5,37 @@
  */
 class SideBarTest extends MediaWikiLangTestCase {
 
-       /** A skin template, reinitialized before each test */
+       /**
+        * A skin template, reinitialized before each test
+        * @var SkinTemplate
+        */
        private $skin;
        /** Local cache for sidebar messages */
        private $messages;
 
        /** Build $this->messages array */
        private function initMessagesHref() {
-               # List of default messages for the sidebar:
-               $URL_messages = array(
+               # List of default messages for the sidebar. The sidebar doesn't care at
+               # all whether they are full URLs, interwiki links or local titles.
+               $URL_messages = [
                        'mainpage',
                        'portal-url',
                        'currentevents-url',
                        'recentchanges-url',
                        'randompage-url',
                        'helppage',
-               );
+               ];
 
+               # We're assuming that isValidURI works as advertised: it's also
+               # tested separately, in tests/phpunit/includes/HttpTest.php.
                foreach ( $URL_messages as $m ) {
                        $titleName = MessageCache::singleton()->get( $m );
-                       $title = Title::newFromText( $titleName );
-                       $this->messages[$m]['href'] = $title->getLocalURL();
+                       if ( Http::isValidURI( $titleName ) ) {
+                               $this->messages[$m]['href'] = $titleName;
+                       } else {
+                               $title = Title::newFromText( $titleName );
+                               $this->messages[$m]['href'] = $title->getLocalURL();
+                       }
                }
        }
 
@@ -36,65 +46,75 @@ class SideBarTest extends MediaWikiLangTestCase {
                $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
        }
 
-       protected function tearDown() {
-               parent::tearDown();
-               $this->skin = null;
-       }
-
        /**
         * Internal helper to test the sidebar
-        * @param $expected
-        * @param $text
-        * @param $message (Default: '')
+        * @param array $expected
+        * @param string $text
+        * @param string $message (Default: '')
+        * @todo this assert method to should be converted to a test using a dataprovider..
         */
        private function assertSideBar( $expected, $text, $message = '' ) {
-               $bar = array();
+               $bar = [];
                $this->skin->addToSidebarPlain( $bar, $text );
                $this->assertEquals( $expected, $bar, $message );
        }
 
-       function testSidebarWithOnlyTwoTitles() {
+       /**
+        * @covers SkinTemplate::addToSidebarPlain
+        */
+       public function testSidebarWithOnlyTwoTitles() {
                $this->assertSideBar(
-                       array(
-                               'Title1' => array(),
-                               'Title2' => array(),
-                       ),
+                       [
+                               'Title1' => [],
+                               'Title2' => [],
+                       ],
                        '* Title1
 * Title2
 '
                );
        }
 
-       function testExpandMessages() {
-               $this->assertSidebar(
-                       array( 'Title' => array(
-                               array(
+       /**
+        * @covers SkinTemplate::addToSidebarPlain
+        */
+       public function testExpandMessages() {
+               $this->assertSideBar(
+                       [ 'Title' => [
+                               [
                                        'text' => 'Help',
                                        'href' => $this->messages['helppage']['href'],
                                        'id' => 'n-help',
                                        'active' => null
-                               )
-                       ) ),
+                               ]
+                       ] ],
                        '* Title
 ** helppage|help
 '
                );
        }
 
-       function testExternalUrlsRequireADescription() {
-               $this->assertSidebar(
-                       array( 'Title' => array(
+       /**
+        * @covers SkinTemplate::addToSidebarPlain
+        */
+       public function testExternalUrlsRequireADescription() {
+               $this->setMwGlobals( [
+                       'wgNoFollowLinks' => true,
+                       'wgNoFollowDomainExceptions' => [],
+                       'wgNoFollowNsExceptions' => [],
+               ] );
+               $this->assertSideBar(
+                       [ 'Title' => [
                                # ** http://www.mediawiki.org/| Home
-                               array(
+                               [
                                        'text' => 'Home',
                                        'href' => 'http://www.mediawiki.org/',
                                        'id' => 'n-Home',
                                        'active' => null,
                                        'rel' => 'nofollow',
-                               ),
+                               ],
                                # ** http://valid.no.desc.org/
                                # ... skipped since it is missing a pipe with a description
-                       ) ),
+                       ] ],
                        '* Title
 ** http://www.mediawiki.org/| Home
 ** http://valid.no.desc.org/
@@ -103,29 +123,30 @@ class SideBarTest extends MediaWikiLangTestCase {
        }
 
        /**
-        * bug 33321 - Make sure there's a | after transforming.
+        * T35321 - Make sure there's a | after transforming.
         * @group Database
+        * @covers SkinTemplate::addToSidebarPlain
         */
-       function testTrickyPipe() {
-               $this->assertSidebar(
-                       array( 'Title' => array(
+       public function testTrickyPipe() {
+               $this->assertSideBar(
+                       [ 'Title' => [
                                # The first 2 are skipped
                                # Doesn't really test the url properly
                                # because it will vary with $wgArticlePath et al.
                                # ** Baz|Fred
-                               array(
+                               [
                                        'text' => 'Fred',
                                        'href' => Title::newFromText( 'Baz' )->getLocalURL(),
                                        'id' => 'n-Fred',
                                        'active' => null,
-                               ),
-                               array(
+                               ],
+                               [
                                        'text' => 'title-to-display',
                                        'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
                                        'id' => 'n-title-to-display',
                                        'active' => null,
-                               ),
-                       ) ),
+                               ],
+                       ] ],
                        '* Title
 ** {{PAGENAME|Foo}}
 ** Bar
@@ -135,15 +156,14 @@ class SideBarTest extends MediaWikiLangTestCase {
                );
        }
 
-
        #### Attributes for external links ##########################
        private function getAttribs() {
                # Sidebar text we will use everytime
                $text = '* Title
 ** http://www.mediawiki.org/| Home';
 
-               $bar = array();
-               $this->skin->addToSideBarPlain( $bar, $text );
+               $bar = [];
+               $this->skin->addToSidebarPlain( $bar, $text );
 
                return $bar['Title'][0];
        }
@@ -151,11 +171,13 @@ class SideBarTest extends MediaWikiLangTestCase {
        /**
         * Simple test to verify our helper assertAttribs() is functional
         */
-       function testTestAttributesAssertionHelper() {
-               $this->setMwGlobals( array(
+       public function testTestAttributesAssertionHelper() {
+               $this->setMwGlobals( [
                        'wgNoFollowLinks' => true,
+                       'wgNoFollowDomainExceptions' => [],
+                       'wgNoFollowNsExceptions' => [],
                        'wgExternalLinkTarget' => false,
-               ) );
+               ] );
                $attribs = $this->getAttribs();
 
                $this->assertArrayHasKey( 'rel', $attribs );
@@ -167,7 +189,7 @@ class SideBarTest extends MediaWikiLangTestCase {
        /**
         * Test $wgNoFollowLinks in sidebar
         */
-       function testRespectWgnofollowlinks() {
+       public function testRespectWgnofollowlinks() {
                $this->setMwGlobals( 'wgNoFollowLinks', false );
 
                $attribs = $this->getAttribs();
@@ -180,7 +202,7 @@ class SideBarTest extends MediaWikiLangTestCase {
         * Test $wgExternaLinkTarget in sidebar
         * @dataProvider dataRespectExternallinktarget
         */
-       function testRespectExternallinktarget( $externalLinkTarget ) {
+       public function testRespectExternallinktarget( $externalLinkTarget ) {
                $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
 
                $attribs = $this->getAttribs();
@@ -188,10 +210,10 @@ class SideBarTest extends MediaWikiLangTestCase {
                $this->assertEquals( $attribs['target'], $externalLinkTarget );
        }
 
-       function dataRespectExternallinktarget() {
-               return array(
-                       array( '_blank' ),
-                       array( '_self' ),
-               );
+       public static function dataRespectExternallinktarget() {
+               return [
+                       [ '_blank' ],
+                       [ '_self' ],
+               ];
        }
 }