(bug 33321. Sort of) Adding a line to MediaWiki:Sidebar that contains a pipe, but...
[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 /** bug 33321 */
110 function testTrickyPipe() {
111 $this->assertSidebar(
112 array( 'Title' => array(
113 # The first 2 are skipped
114 # Doesn't really test the url properly
115 # because it will vary with $wgArticlePath et al.
116 # ** Baz|Fred
117 array(
118 'text' => 'Fred',
119 'href' => Title::newFromText( 'Baz' )->getLocalUrl(),
120 'id' => 'n-Fred',
121 'active' => null,
122 ),
123 array(
124 'text' => 'title-to-display',
125 'href' => Title::newFromText( 'page-to-go-to' )->getLocalUrl(),
126 'id' => 'n-title-to-display',
127 'active' => null,
128 ),
129 )),
130 '* Title
131 ** {{PAGENAME|Foo}}
132 ** Bar
133 ** Baz|Fred
134 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
135 '
136 );
137
138 }
139
140
141 #### Attributes for external links ##########################
142 private function getAttribs( ) {
143 # Sidebar text we will use everytime
144 $text = '* Title
145 ** http://www.mediawiki.org/| Home';
146
147 $bar = array();
148 $this->skin->addToSideBarPlain( $bar, $text );
149
150 return $bar['Title'][0];
151 }
152
153 /**
154 * Simple test to verify our helper assertAttribs() is functional
155 * Please note this assume MediaWiki default settings:
156 * $wgNoFollowLinks = true
157 * $wgExternalLinkTarget = false
158 */
159 function testTestAttributesAssertionHelper() {
160 $attribs = $this->getAttribs();
161
162 $this->assertArrayHasKey( 'rel', $attribs );
163 $this->assertEquals( 'nofollow', $attribs['rel'] );
164
165 $this->assertArrayNotHasKey( 'target', $attribs );
166 }
167
168 /**
169 * Test $wgNoFollowLinks in sidebar
170 */
171 function testRespectWgnofollowlinks() {
172 global $wgNoFollowLinks;
173 $saved = $wgNoFollowLinks;
174 $wgNoFollowLinks = false;
175
176 $attribs = $this->getAttribs();
177 $this->assertArrayNotHasKey( 'rel', $attribs,
178 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
179 );
180
181 // Restore global
182 $wgNoFollowLinks = $saved;
183 }
184
185 /**
186 * Test $wgExternaLinkTarget in sidebar
187 */
188 function testRespectExternallinktarget() {
189 global $wgExternalLinkTarget;
190 $saved = $wgExternalLinkTarget;
191
192 $wgExternalLinkTarget = '_blank';
193 $attribs = $this->getAttribs();
194 $this->assertArrayHasKey( 'target', $attribs );
195 $this->assertEquals( $attribs['target'], '_blank' );
196
197 $wgExternalLinkTarget = '_self';
198 $attribs = $this->getAttribs();
199 $this->assertArrayHasKey( 'target', $attribs );
200 $this->assertEquals( $attribs['target'], '_self' );
201
202 // Restore global
203 $wgExternalLinkTarget = $saved;
204 }
205
206 }