phpcs: Normalize methods declarations to "[final abstract] [visibility]".
[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 /** 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',
23 );
24
25 foreach( $URL_messages as $m ) {
26 $titleName = MessageCache::singleton()->get($m);
27 $title = Title::newFromText( $titleName );
28 $this->messages[$m]['href'] = $title->getLocalURL();
29 }
30 }
31
32 protected function setUp() {
33 parent::setUp();
34 $this->initMessagesHref();
35 $this->skin = new SkinTemplate();
36 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
37 }
38 protected function tearDown() {
39 parent::tearDown();
40 $this->skin = null;
41 }
42
43 /**
44 * Internal helper to test the sidebar
45 * @param $expected
46 * @param $text
47 * @param $message (Default: '')
48 */
49 private function assertSideBar( $expected, $text, $message = '' ) {
50 $bar = array();
51 $this->skin->addToSidebarPlain( $bar, $text );
52 $this->assertEquals( $expected, $bar, $message );
53 }
54
55 function testSidebarWithOnlyTwoTitles() {
56 $this->assertSideBar(
57 array(
58 'Title1' => array(),
59 'Title2' => array(),
60 ),
61 '* Title1
62 * Title2
63 '
64 );
65 }
66
67 function testExpandMessages() {
68 $this->assertSidebar(
69 array( 'Title' => array(
70 array(
71 'text' => 'Help',
72 'href' => $this->messages['helppage']['href'],
73 'id' => 'n-help',
74 'active' => null
75 )
76 )),
77 '* Title
78 ** helppage|help
79 '
80 );
81 }
82
83 function testExternalUrlsRequireADescription() {
84 $this->assertSidebar(
85 array( 'Title' => array(
86 # ** http://www.mediawiki.org/| Home
87 array(
88 'text' => 'Home',
89 'href' => 'http://www.mediawiki.org/',
90 'id' => 'n-Home',
91 'active' => null,
92 'rel' => 'nofollow',
93 ),
94 # ** http://valid.no.desc.org/
95 # ... skipped since it is missing a pipe with a description
96 )),
97 '* Title
98 ** http://www.mediawiki.org/| Home
99 ** http://valid.no.desc.org/
100 '
101
102 );
103
104 }
105 /**
106 * bug 33321 - Make sure there's a | after transforming.
107 * @group Database
108 */
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,
121 ),
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,
127 ),
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}}
134 '
135 );
136
137 }
138
139
140 #### Attributes for external links ##########################
141 private function getAttribs( ) {
142 # Sidebar text we will use everytime
143 $text = '* Title
144 ** http://www.mediawiki.org/| Home';
145
146 $bar = array();
147 $this->skin->addToSideBarPlain( $bar, $text );
148
149 return $bar['Title'][0];
150 }
151
152 /**
153 * Simple test to verify our helper assertAttribs() is functional
154 * Please note this assume MediaWiki default settings:
155 * $wgNoFollowLinks = true
156 * $wgExternalLinkTarget = false
157 */
158 function testTestAttributesAssertionHelper() {
159 $attribs = $this->getAttribs();
160
161 $this->assertArrayHasKey( 'rel', $attribs );
162 $this->assertEquals( 'nofollow', $attribs['rel'] );
163
164 $this->assertArrayNotHasKey( 'target', $attribs );
165 }
166
167 /**
168 * Test $wgNoFollowLinks in sidebar
169 */
170 function testRespectWgnofollowlinks() {
171 global $wgNoFollowLinks;
172 $saved = $wgNoFollowLinks;
173 $wgNoFollowLinks = false;
174
175 $attribs = $this->getAttribs();
176 $this->assertArrayNotHasKey( 'rel', $attribs,
177 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
178 );
179
180 // Restore global
181 $wgNoFollowLinks = $saved;
182 }
183
184 /**
185 * Test $wgExternaLinkTarget in sidebar
186 */
187 function testRespectExternallinktarget() {
188 global $wgExternalLinkTarget;
189 $saved = $wgExternalLinkTarget;
190
191 $wgExternalLinkTarget = '_blank';
192 $attribs = $this->getAttribs();
193 $this->assertArrayHasKey( 'target', $attribs );
194 $this->assertEquals( $attribs['target'], '_blank' );
195
196 $wgExternalLinkTarget = '_self';
197 $attribs = $this->getAttribs();
198 $this->assertArrayHasKey( 'target', $attribs );
199 $this->assertEquals( $attribs['target'], '_self' );
200
201 // Restore global
202 $wgExternalLinkTarget = $saved;
203 }
204
205 }