MediaWiki  REL1_21
SideBarTest.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class SideBarTest extends MediaWikiLangTestCase {
00007 
00009         private $skin;
00011         private $messages;
00012 
00014         private function initMessagesHref() {
00015                 # List of default messages for the sidebar:
00016                 $URL_messages = array(
00017                         'mainpage',
00018                         'portal-url',
00019                         'currentevents-url',
00020                         'recentchanges-url',
00021                         'randompage-url',
00022                         'helppage',
00023                 );
00024 
00025                 foreach ( $URL_messages as $m ) {
00026                         $titleName = MessageCache::singleton()->get( $m );
00027                         $title = Title::newFromText( $titleName );
00028                         $this->messages[$m]['href'] = $title->getLocalURL();
00029                 }
00030         }
00031 
00032         protected function setUp() {
00033                 parent::setUp();
00034                 $this->initMessagesHref();
00035                 $this->skin = new SkinTemplate();
00036                 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
00037         }
00038 
00039         protected function tearDown() {
00040                 parent::tearDown();
00041                 $this->skin = null;
00042         }
00043 
00050         private function assertSideBar( $expected, $text, $message = '' ) {
00051                 $bar = array();
00052                 $this->skin->addToSidebarPlain( $bar, $text );
00053                 $this->assertEquals( $expected, $bar, $message );
00054         }
00055 
00056         function testSidebarWithOnlyTwoTitles() {
00057                 $this->assertSideBar(
00058                         array(
00059                                 'Title1' => array(),
00060                                 'Title2' => array(),
00061                         ),
00062                         '* Title1
00063 * Title2
00064 '
00065                 );
00066         }
00067 
00068         function testExpandMessages() {
00069                 $this->assertSidebar(
00070                         array( 'Title' => array(
00071                                 array(
00072                                         'text' => 'Help',
00073                                         'href' => $this->messages['helppage']['href'],
00074                                         'id' => 'n-help',
00075                                         'active' => null
00076                                 )
00077                         ) ),
00078                         '* Title
00079 ** helppage|help
00080 '
00081                 );
00082         }
00083 
00084         function testExternalUrlsRequireADescription() {
00085                 $this->assertSidebar(
00086                         array( 'Title' => array(
00087                                 # ** http://www.mediawiki.org/| Home
00088                                 array(
00089                                         'text' => 'Home',
00090                                         'href' => 'http://www.mediawiki.org/',
00091                                         'id' => 'n-Home',
00092                                         'active' => null,
00093                                         'rel' => 'nofollow',
00094                                 ),
00095                                 # ** http://valid.no.desc.org/
00096                                 # ... skipped since it is missing a pipe with a description
00097                         ) ),
00098                         '* Title
00099 ** http://www.mediawiki.org/| Home
00100 ** http://valid.no.desc.org/
00101 '
00102                 );
00103 
00104         }
00105 
00110         function testTrickyPipe() {
00111                 $this->assertSidebar(
00112                         array( 'Title' => array(
00113                                 # The first 2 are skipped
00114                                 # Doesn't really test the url properly
00115                                 # because it will vary with $wgArticlePath et al.
00116                                 # ** Baz|Fred
00117                                 array(
00118                                         'text' => 'Fred',
00119                                         'href' => Title::newFromText( 'Baz' )->getLocalUrl(),
00120                                         'id' => 'n-Fred',
00121                                         'active' => null,
00122                                 ),
00123                                 array(
00124                                         'text' => 'title-to-display',
00125                                         'href' => Title::newFromText( 'page-to-go-to' )->getLocalUrl(),
00126                                         'id' => 'n-title-to-display',
00127                                         'active' => null,
00128                                 ),
00129                         ) ),
00130                         '* Title
00131 ** {{PAGENAME|Foo}}
00132 ** Bar
00133 ** Baz|Fred
00134 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
00135 '
00136                 );
00137         }
00138 
00139 
00140         #### Attributes for external links ##########################
00141         private function getAttribs() {
00142                 # Sidebar text we will use everytime
00143                 $text = '* Title
00144 ** http://www.mediawiki.org/| Home';
00145 
00146                 $bar = array();
00147                 $this->skin->addToSideBarPlain( $bar, $text );
00148 
00149                 return $bar['Title'][0];
00150         }
00151 
00158         function testTestAttributesAssertionHelper() {
00159                 $attribs = $this->getAttribs();
00160 
00161                 $this->assertArrayHasKey( 'rel', $attribs );
00162                 $this->assertEquals( 'nofollow', $attribs['rel'] );
00163 
00164                 $this->assertArrayNotHasKey( 'target', $attribs );
00165         }
00166 
00170         function testRespectWgnofollowlinks() {
00171                 global $wgNoFollowLinks;
00172                 $saved = $wgNoFollowLinks;
00173                 $wgNoFollowLinks = false;
00174 
00175                 $attribs = $this->getAttribs();
00176                 $this->assertArrayNotHasKey( 'rel', $attribs,
00177                         'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
00178                 );
00179 
00180                 // Restore global
00181                 $wgNoFollowLinks = $saved;
00182         }
00183 
00187         function testRespectExternallinktarget() {
00188                 global $wgExternalLinkTarget;
00189                 $saved = $wgExternalLinkTarget;
00190 
00191                 $wgExternalLinkTarget = '_blank';
00192                 $attribs = $this->getAttribs();
00193                 $this->assertArrayHasKey( 'target', $attribs );
00194                 $this->assertEquals( $attribs['target'], '_blank' );
00195 
00196                 $wgExternalLinkTarget = '_self';
00197                 $attribs = $this->getAttribs();
00198                 $this->assertArrayHasKey( 'target', $attribs );
00199                 $this->assertEquals( $attribs['target'], '_self' );
00200 
00201                 // Restore global
00202                 $wgExternalLinkTarget = $saved;
00203         }
00204 
00205 }