MediaWiki  REL1_22
SideBarTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class SideBarTest extends MediaWikiLangTestCase {
00007 
00012     private $skin;
00014     private $messages;
00015 
00017     private function initMessagesHref() {
00018         # List of default messages for the sidebar. The sidebar doesn't care at
00019         # all whether they are full URLs, interwiki links or local titles.
00020         $URL_messages = array(
00021             'mainpage',
00022             'portal-url',
00023             'currentevents-url',
00024             'recentchanges-url',
00025             'randompage-url',
00026             'helppage',
00027         );
00028 
00029         # We're assuming that isValidURI works as advertised: it's also
00030         # tested separately, in tests/phpunit/includes/HttpTest.php.
00031         foreach ( $URL_messages as $m ) {
00032             $titleName = MessageCache::singleton()->get( $m );
00033             if ( Http::isValidURI( $titleName ) ) {
00034                 $this->messages[$m]['href'] = $titleName;
00035             } else {
00036                 $title = Title::newFromText( $titleName );
00037                 $this->messages[$m]['href'] = $title->getLocalURL();
00038             }
00039         }
00040     }
00041 
00042     protected function setUp() {
00043         parent::setUp();
00044         $this->initMessagesHref();
00045         $this->skin = new SkinTemplate();
00046         $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
00047     }
00048 
00056     private function assertSideBar( $expected, $text, $message = '' ) {
00057         $bar = array();
00058         $this->skin->addToSidebarPlain( $bar, $text );
00059         $this->assertEquals( $expected, $bar, $message );
00060     }
00061 
00065     public function testSidebarWithOnlyTwoTitles() {
00066         $this->assertSideBar(
00067             array(
00068                 'Title1' => array(),
00069                 'Title2' => array(),
00070             ),
00071             '* Title1
00072 * Title2
00073 '
00074         );
00075     }
00076 
00080     public function testExpandMessages() {
00081         $this->setMwGlobals( array(
00082             'wgNoFollowDomainExceptions' => array( 'mediawiki.org' ),
00083         ) );
00084         $this->assertSidebar(
00085             array( 'Title' => array(
00086                 array(
00087                     'text' => 'Help',
00088                     'href' => $this->messages['helppage']['href'],
00089                     'id' => 'n-help',
00090                     'active' => null
00091                 )
00092             ) ),
00093             '* Title
00094 ** helppage|help
00095 '
00096         );
00097     }
00098 
00102     public function testExternalUrlsRequireADescription() {
00103     $this->setMwGlobals( array(
00104             'wgNoFollowLinks' => true,
00105             'wgNoFollowDomainExceptions' => array(),
00106             'wgNoFollowNsExceptions' => array(),
00107         ) );
00108         $this->assertSidebar(
00109             array( 'Title' => array(
00110                 # ** http://www.mediawiki.org/| Home
00111                 array(
00112                     'text' => 'Home',
00113                     'href' => 'http://www.mediawiki.org/',
00114                     'id' => 'n-Home',
00115                     'active' => null,
00116                     'rel' => 'nofollow',
00117                 ),
00118                 # ** http://valid.no.desc.org/
00119                 # ... skipped since it is missing a pipe with a description
00120             ) ),
00121             '* Title
00122 ** http://www.mediawiki.org/| Home
00123 ** http://valid.no.desc.org/
00124 '
00125         );
00126     }
00127 
00133     public function testTrickyPipe() {
00134         $this->assertSidebar(
00135             array( 'Title' => array(
00136                 # The first 2 are skipped
00137                 # Doesn't really test the url properly
00138                 # because it will vary with $wgArticlePath et al.
00139                 # ** Baz|Fred
00140                 array(
00141                     'text' => 'Fred',
00142                     'href' => Title::newFromText( 'Baz' )->getLocalURL(),
00143                     'id' => 'n-Fred',
00144                     'active' => null,
00145                 ),
00146                 array(
00147                     'text' => 'title-to-display',
00148                     'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
00149                     'id' => 'n-title-to-display',
00150                     'active' => null,
00151                 ),
00152             ) ),
00153             '* Title
00154 ** {{PAGENAME|Foo}}
00155 ** Bar
00156 ** Baz|Fred
00157 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
00158 '
00159         );
00160     }
00161 
00162 
00163     #### Attributes for external links ##########################
00164     private function getAttribs() {
00165         # Sidebar text we will use everytime
00166         $text = '* Title
00167 ** http://www.mediawiki.org/| Home';
00168 
00169         $bar = array();
00170         $this->skin->addToSideBarPlain( $bar, $text );
00171 
00172         return $bar['Title'][0];
00173     }
00174 
00178     public function testTestAttributesAssertionHelper() {
00179         $this->setMwGlobals( array(
00180             'wgNoFollowLinks' => true,
00181             'wgNoFollowDomainExceptions' => array(),
00182             'wgNoFollowNsExceptions' => array(),
00183             'wgExternalLinkTarget' => false,
00184         ) );
00185         $attribs = $this->getAttribs();
00186 
00187         $this->assertArrayHasKey( 'rel', $attribs );
00188         $this->assertEquals( 'nofollow', $attribs['rel'] );
00189 
00190         $this->assertArrayNotHasKey( 'target', $attribs );
00191     }
00192 
00196     public function testRespectWgnofollowlinks() {
00197         $this->setMwGlobals( 'wgNoFollowLinks', false );
00198 
00199         $attribs = $this->getAttribs();
00200         $this->assertArrayNotHasKey( 'rel', $attribs,
00201             'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
00202         );
00203     }
00204 
00209     public function testRespectExternallinktarget( $externalLinkTarget ) {
00210         $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
00211 
00212         $attribs = $this->getAttribs();
00213         $this->assertArrayHasKey( 'target', $attribs );
00214         $this->assertEquals( $attribs['target'], $externalLinkTarget );
00215     }
00216 
00217     public static function dataRespectExternallinktarget() {
00218         return array(
00219             array( '_blank' ),
00220             array( '_self' ),
00221         );
00222     }
00223 }