Localisation updates from http://translatewiki.net.
[mediawiki.git] / tests / phpunit / skins / SideBarTest.php
blob850d39c791986e0c82df0663e7cb9f2a21a67a7d
1 <?php
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
8 /** A skin template, reinitialized before each test */
9 private $skin;
10 /** Local cache for sidebar messages */
11 private $messages;
13 /** Build $this->messages array */
14 private function initMessagesHref() {
15 # List of default messages for the sidebar:
16 $URL_messages = array(
17 'mainpage',
18 'portal-url',
19 'currentevents-url',
20 'recentchanges-url',
21 'randompage-url',
22 'helppage',
25 foreach ( $URL_messages as $m ) {
26 $titleName = MessageCache::singleton()->get( $m );
27 $title = Title::newFromText( $titleName );
28 $this->messages[$m]['href'] = $title->getLocalURL();
32 protected function setUp() {
33 parent::setUp();
34 $this->initMessagesHref();
35 $this->skin = new SkinTemplate();
36 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
39 protected function tearDown() {
40 parent::tearDown();
41 $this->skin = null;
44 /**
45 * Internal helper to test the sidebar
46 * @param $expected
47 * @param $text
48 * @param $message (Default: '')
50 private function assertSideBar( $expected, $text, $message = '' ) {
51 $bar = array();
52 $this->skin->addToSidebarPlain( $bar, $text );
53 $this->assertEquals( $expected, $bar, $message );
56 function testSidebarWithOnlyTwoTitles() {
57 $this->assertSideBar(
58 array(
59 'Title1' => array(),
60 'Title2' => array(),
62 '* Title1
63 * Title2
68 function testExpandMessages() {
69 $this->assertSidebar(
70 array( 'Title' => array(
71 array(
72 'text' => 'Help',
73 'href' => $this->messages['helppage']['href'],
74 'id' => 'n-help',
75 'active' => null
77 ) ),
78 '* Title
79 ** helppage|help
84 function testExternalUrlsRequireADescription() {
85 $this->assertSidebar(
86 array( 'Title' => array(
87 # ** http://www.mediawiki.org/| Home
88 array(
89 'text' => 'Home',
90 'href' => 'http://www.mediawiki.org/',
91 'id' => 'n-Home',
92 'active' => null,
93 'rel' => 'nofollow',
95 # ** http://valid.no.desc.org/
96 # ... skipped since it is missing a pipe with a description
97 ) ),
98 '* Title
99 ** http://www.mediawiki.org/| Home
100 ** http://valid.no.desc.org/
106 * bug 33321 - Make sure there's a | after transforming.
107 * @group Database
109 function testTrickyPipe() {
110 $this->assertSidebar(
111 array( 'Title' => array(
112 # The first 2 are skipped
113 # Doesn't really test the url properly
114 # because it will vary with $wgArticlePath et al.
115 # ** Baz|Fred
116 array(
117 'text' => 'Fred',
118 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
119 'id' => 'n-Fred',
120 'active' => null,
122 array(
123 'text' => 'title-to-display',
124 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
125 'id' => 'n-title-to-display',
126 'active' => null,
128 ) ),
129 '* Title
130 ** {{PAGENAME|Foo}}
131 ** Bar
132 ** Baz|Fred
133 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
139 #### Attributes for external links ##########################
140 private function getAttribs() {
141 # Sidebar text we will use everytime
142 $text = '* Title
143 ** http://www.mediawiki.org/| Home';
145 $bar = array();
146 $this->skin->addToSideBarPlain( $bar, $text );
148 return $bar['Title'][0];
152 * Simple test to verify our helper assertAttribs() is functional
154 function testTestAttributesAssertionHelper() {
155 $this->setMwGlobals( array(
156 'wgNoFollowLinks' => true,
157 'wgExternalLinkTarget' => false,
158 ) );
159 $attribs = $this->getAttribs();
161 $this->assertArrayHasKey( 'rel', $attribs );
162 $this->assertEquals( 'nofollow', $attribs['rel'] );
164 $this->assertArrayNotHasKey( 'target', $attribs );
168 * Test $wgNoFollowLinks in sidebar
170 function testRespectWgnofollowlinks() {
171 $this->setMwGlobals( 'wgNoFollowLinks', false );
173 $attribs = $this->getAttribs();
174 $this->assertArrayNotHasKey( 'rel', $attribs,
175 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
180 * Test $wgExternaLinkTarget in sidebar
181 * @dataProvider dataRespectExternallinktarget
183 function testRespectExternallinktarget( $externalLinkTarget ) {
184 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
186 $attribs = $this->getAttribs();
187 $this->assertArrayHasKey( 'target', $attribs );
188 $this->assertEquals( $attribs['target'], $externalLinkTarget );
191 function dataRespectExternallinktarget() {
192 return array(
193 array( '_blank' ),
194 array( '_self' ),