MediaWiki  REL1_24
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->assertSidebar(
00082             array( 'Title' => array(
00083                 array(
00084                     'text' => 'Help',
00085                     'href' => $this->messages['helppage']['href'],
00086                     'id' => 'n-help',
00087                     'active' => null
00088                 )
00089             ) ),
00090             '* Title
00091 ** helppage|help
00092 '
00093         );
00094     }
00095 
00099     public function testExternalUrlsRequireADescription() {
00100         $this->setMwGlobals( array(
00101             'wgNoFollowLinks' => true,
00102             'wgNoFollowDomainExceptions' => array(),
00103             'wgNoFollowNsExceptions' => array(),
00104         ) );
00105         $this->assertSidebar(
00106             array( 'Title' => array(
00107                 # ** http://www.mediawiki.org/| Home
00108                 array(
00109                     'text' => 'Home',
00110                     'href' => 'http://www.mediawiki.org/',
00111                     'id' => 'n-Home',
00112                     'active' => null,
00113                     'rel' => 'nofollow',
00114                 ),
00115                 # ** http://valid.no.desc.org/
00116                 # ... skipped since it is missing a pipe with a description
00117             ) ),
00118             '* Title
00119 ** http://www.mediawiki.org/| Home
00120 ** http://valid.no.desc.org/
00121 '
00122         );
00123     }
00124 
00130     public function testTrickyPipe() {
00131         $this->assertSidebar(
00132             array( 'Title' => array(
00133                 # The first 2 are skipped
00134                 # Doesn't really test the url properly
00135                 # because it will vary with $wgArticlePath et al.
00136                 # ** Baz|Fred
00137                 array(
00138                     'text' => 'Fred',
00139                     'href' => Title::newFromText( 'Baz' )->getLocalURL(),
00140                     'id' => 'n-Fred',
00141                     'active' => null,
00142                 ),
00143                 array(
00144                     'text' => 'title-to-display',
00145                     'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
00146                     'id' => 'n-title-to-display',
00147                     'active' => null,
00148                 ),
00149             ) ),
00150             '* Title
00151 ** {{PAGENAME|Foo}}
00152 ** Bar
00153 ** Baz|Fred
00154 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
00155 '
00156         );
00157     }
00158 
00159     #### Attributes for external links ##########################
00160     private function getAttribs() {
00161         # Sidebar text we will use everytime
00162         $text = '* Title
00163 ** http://www.mediawiki.org/| Home';
00164 
00165         $bar = array();
00166         $this->skin->addToSideBarPlain( $bar, $text );
00167 
00168         return $bar['Title'][0];
00169     }
00170 
00174     public function testTestAttributesAssertionHelper() {
00175         $this->setMwGlobals( array(
00176             'wgNoFollowLinks' => true,
00177             'wgNoFollowDomainExceptions' => array(),
00178             'wgNoFollowNsExceptions' => array(),
00179             'wgExternalLinkTarget' => false,
00180         ) );
00181         $attribs = $this->getAttribs();
00182 
00183         $this->assertArrayHasKey( 'rel', $attribs );
00184         $this->assertEquals( 'nofollow', $attribs['rel'] );
00185 
00186         $this->assertArrayNotHasKey( 'target', $attribs );
00187     }
00188 
00192     public function testRespectWgnofollowlinks() {
00193         $this->setMwGlobals( 'wgNoFollowLinks', false );
00194 
00195         $attribs = $this->getAttribs();
00196         $this->assertArrayNotHasKey( 'rel', $attribs,
00197             'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
00198         );
00199     }
00200 
00205     public function testRespectExternallinktarget( $externalLinkTarget ) {
00206         $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
00207 
00208         $attribs = $this->getAttribs();
00209         $this->assertArrayHasKey( 'target', $attribs );
00210         $this->assertEquals( $attribs['target'], $externalLinkTarget );
00211     }
00212 
00213     public static function dataRespectExternallinktarget() {
00214         return array(
00215             array( '_blank' ),
00216             array( '_self' ),
00217         );
00218     }
00219 }