Per Platonides, fix for r100445: make sure user's language is English so that it...
[lhc/web/wiklou.git] / tests / phpunit / skins / SideBarTest.php
1 <?php
2
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
7
8 /** A skin template, reinitialized before each test */
9 private $skin;
10 /** Local cache for sidebar messages */
11 private $messages;
12
13 function __construct() {
14 parent::__construct();
15 }
16
17 /** Build $this->messages array */
18 private function initMessagesHref() {
19 # List of default messages for the sidebar:
20 $URL_messages = array(
21 'mainpage',
22 'portal-url',
23 'currentevents-url',
24 'recentchanges-url',
25 'randompage-url',
26 'helppage',
27 );
28
29 foreach( $URL_messages as $m ) {
30 $titleName = MessageCache::singleton()->get($m);
31 $title = Title::newFromText( $titleName );
32 $this->messages[$m]['href'] = $title->getLocalURL();
33 }
34 }
35
36 function setUp() {
37 parent::setUp();
38 $this->initMessagesHref();
39 $this->skin = new SkinTemplate();
40 $this->skin->getContext()->setLang( Language::factory( 'en' ) );
41 }
42 function tearDown() {
43 parent::tearDown();
44 $this->skin = null;
45 }
46
47 /**
48 * Internal helper to test the sidebar
49 * @param $expected
50 * @param $text
51 * @param $message (Default: '')
52 */
53 private function assertSideBar( $expected, $text, $message = '' ) {
54 $bar = array();
55 $this->skin->addToSidebarPlain( $bar, $text );
56 $this->assertEquals( $expected, $bar, $message );
57 }
58
59 function testSidebarWithOnlyTwoTitles() {
60 $this->assertSideBar(
61 array(
62 'Title1' => array(),
63 'Title2' => array(),
64 ),
65 '* Title1
66 * Title2
67 '
68 );
69 }
70
71 function testExpandMessages() {
72 $this->assertSidebar(
73 array( 'Title' => array(
74 array(
75 'text' => 'Help',
76 'href' => $this->messages['helppage']['href'],
77 'id' => 'n-help',
78 'active' => null
79 )
80 )),
81 '* Title
82 ** helppage|help
83 '
84 );
85 }
86
87 function testExternalUrlsRequireADescription() {
88 $this->assertSidebar(
89 array( 'Title' => array(
90 # ** http://www.mediawiki.org/| Home
91 array(
92 'text' => 'Home',
93 'href' => 'http://www.mediawiki.org/',
94 'id' => 'n-Home',
95 'active' => null,
96 'rel' => 'nofollow',
97 ),
98 # ** http://valid.no.desc.org/
99 # ... skipped since it is missing a pipe with a description
100 )),
101 '* Title
102 ** http://www.mediawiki.org/| Home
103 ** http://valid.no.desc.org/
104 '
105
106 );
107
108 }
109
110
111 #### Attributes for external links ##########################
112 private function getAttribs( ) {
113 # Sidebar text we will use everytime
114 $text = '* Title
115 ** http://www.mediawiki.org/| Home';
116
117 $bar = array();
118 $this->skin->addToSideBarPlain( $bar, $text );
119
120 return $bar['Title'][0];
121 }
122
123 /**
124 * Simple test to verify our helper assertAttribs() is functional
125 * Please note this assume MediaWiki default settings:
126 * $wgNoFollowLinks = true
127 * $wgExternalLinkTarget = false
128 */
129 function testTestAttributesAssertionHelper() {
130 $attribs = $this->getAttribs();
131
132 $this->assertArrayHasKey( 'rel', $attribs );
133 $this->assertEquals( 'nofollow', $attribs['rel'] );
134
135 $this->assertArrayNotHasKey( 'target', $attribs );
136 }
137
138 /**
139 * Test wgNoFollowLinks in sidebar
140 */
141 function testRespectWgnofollowlinks() {
142 global $wgNoFollowLinks;
143 $saved = $wgNoFollowLinks;
144 $wgNoFollowLinks = false;
145
146 $attribs = $this->getAttribs();
147 $this->assertArrayNotHasKey( 'rel', $attribs,
148 'External URL in sidebar do not have rel=nofollow when wgNoFollowLinks = false'
149 );
150
151 // Restore global
152 $wgNoFollowLinks = $saved;
153 }
154
155 /**
156 * Test wgExternaLinkTarget in sidebar
157 */
158 function testRespectExternallinktarget() {
159 global $wgExternalLinkTarget;
160 $saved = $wgExternalLinkTarget;
161
162 $wgExternalLinkTarget = '_blank';
163 $attribs = $this->getAttribs();
164 $this->assertArrayHasKey( 'target', $attribs );
165 $this->assertEquals( $attribs['target'], '_blank' );
166
167 $wgExternalLinkTarget = '_self';
168 $attribs = $this->getAttribs();
169 $this->assertArrayHasKey( 'target', $attribs );
170 $this->assertEquals( $attribs['target'], '_self' );
171
172 // Restore global
173 $wgExternalLinkTarget = $saved;
174 }
175
176 }