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