Add @covers tags to miscellaneous tests (#2)
[lhc/web/wiklou.git] / tests / phpunit / skins / SideBarTest.php
1 <?php
2
3 /**
4 * @group Skin
5 */
6 class SideBarTest extends MediaWikiLangTestCase {
7
8 /**
9 * A skin template, reinitialized before each test
10 * @var SkinTemplate
11 */
12 private $skin;
13 /** Local cache for sidebar messages */
14 private $messages;
15
16 /** Build $this->messages array */
17 private function initMessagesHref() {
18 # List of default messages for the sidebar. The sidebar doesn't care at
19 # all whether they are full URLs, interwiki links or local titles.
20 $URL_messages = [
21 'mainpage',
22 'portal-url',
23 'currentevents-url',
24 'recentchanges-url',
25 'randompage-url',
26 'helppage',
27 ];
28
29 # We're assuming that isValidURI works as advertised: it's also
30 # tested separately, in tests/phpunit/includes/HttpTest.php.
31 foreach ( $URL_messages as $m ) {
32 $titleName = MessageCache::singleton()->get( $m );
33 if ( Http::isValidURI( $titleName ) ) {
34 $this->messages[$m]['href'] = $titleName;
35 } else {
36 $title = Title::newFromText( $titleName );
37 $this->messages[$m]['href'] = $title->getLocalURL();
38 }
39 }
40 }
41
42 protected function setUp() {
43 parent::setUp();
44 $this->initMessagesHref();
45 $this->skin = new SkinTemplate();
46 $this->skin->getContext()->setLanguage( Language::factory( 'en' ) );
47 }
48
49 /**
50 * Internal helper to test the sidebar
51 * @param array $expected
52 * @param string $text
53 * @param string $message (Default: '')
54 * @todo this assert method to should be converted to a test using a dataprovider..
55 */
56 private function assertSideBar( $expected, $text, $message = '' ) {
57 $bar = [];
58 $this->skin->addToSidebarPlain( $bar, $text );
59 $this->assertEquals( $expected, $bar, $message );
60 }
61
62 /**
63 * @covers SkinTemplate::addToSidebarPlain
64 */
65 public function testSidebarWithOnlyTwoTitles() {
66 $this->assertSideBar(
67 [
68 'Title1' => [],
69 'Title2' => [],
70 ],
71 '* Title1
72 * Title2
73 '
74 );
75 }
76
77 /**
78 * @covers SkinTemplate::addToSidebarPlain
79 */
80 public function testExpandMessages() {
81 $this->assertSideBar(
82 [ 'Title' => [
83 [
84 'text' => 'Help',
85 'href' => $this->messages['helppage']['href'],
86 'id' => 'n-help',
87 'active' => null
88 ]
89 ] ],
90 '* Title
91 ** helppage|help
92 '
93 );
94 }
95
96 /**
97 * @covers SkinTemplate::addToSidebarPlain
98 */
99 public function testExternalUrlsRequireADescription() {
100 $this->setMwGlobals( [
101 'wgNoFollowLinks' => true,
102 'wgNoFollowDomainExceptions' => [],
103 'wgNoFollowNsExceptions' => [],
104 ] );
105 $this->assertSideBar(
106 [ 'Title' => [
107 # ** http://www.mediawiki.org/| Home
108 [
109 'text' => 'Home',
110 'href' => 'http://www.mediawiki.org/',
111 'id' => 'n-Home',
112 'active' => null,
113 'rel' => 'nofollow',
114 ],
115 # ** http://valid.no.desc.org/
116 # ... skipped since it is missing a pipe with a description
117 ] ],
118 '* Title
119 ** http://www.mediawiki.org/| Home
120 ** http://valid.no.desc.org/
121 '
122 );
123 }
124
125 /**
126 * T35321 - Make sure there's a | after transforming.
127 * @group Database
128 * @covers SkinTemplate::addToSidebarPlain
129 */
130 public function testTrickyPipe() {
131 $this->assertSideBar(
132 [ 'Title' => [
133 # The first 2 are skipped
134 # Doesn't really test the url properly
135 # because it will vary with $wgArticlePath et al.
136 # ** Baz|Fred
137 [
138 'text' => 'Fred',
139 'href' => Title::newFromText( 'Baz' )->getLocalURL(),
140 'id' => 'n-Fred',
141 'active' => null,
142 ],
143 [
144 'text' => 'title-to-display',
145 'href' => Title::newFromText( 'page-to-go-to' )->getLocalURL(),
146 'id' => 'n-title-to-display',
147 'active' => null,
148 ],
149 ] ],
150 '* Title
151 ** {{PAGENAME|Foo}}
152 ** Bar
153 ** Baz|Fred
154 ** {{PLURAL:1|page-to-go-to{{int:pipe-separator/en}}title-to-display|branch not taken}}
155 '
156 );
157 }
158
159 #### Attributes for external links ##########################
160 private function getAttribs() {
161 # Sidebar text we will use everytime
162 $text = '* Title
163 ** http://www.mediawiki.org/| Home';
164
165 $bar = [];
166 $this->skin->addToSidebarPlain( $bar, $text );
167
168 return $bar['Title'][0];
169 }
170
171 /**
172 * Simple test to verify our helper assertAttribs() is functional
173 */
174 public function testTestAttributesAssertionHelper() {
175 $this->setMwGlobals( [
176 'wgNoFollowLinks' => true,
177 'wgNoFollowDomainExceptions' => [],
178 'wgNoFollowNsExceptions' => [],
179 'wgExternalLinkTarget' => false,
180 ] );
181 $attribs = $this->getAttribs();
182
183 $this->assertArrayHasKey( 'rel', $attribs );
184 $this->assertEquals( 'nofollow', $attribs['rel'] );
185
186 $this->assertArrayNotHasKey( 'target', $attribs );
187 }
188
189 /**
190 * Test $wgNoFollowLinks in sidebar
191 * @covers Skin::addToSidebarPlain
192 */
193 public function testRespectWgnofollowlinks() {
194 $this->setMwGlobals( 'wgNoFollowLinks', false );
195
196 $attribs = $this->getAttribs();
197 $this->assertArrayNotHasKey( 'rel', $attribs,
198 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false'
199 );
200 }
201
202 /**
203 * Test $wgExternaLinkTarget in sidebar
204 * @dataProvider dataRespectExternallinktarget
205 * @covers Skin::addToSidebarPlain
206 */
207 public function testRespectExternallinktarget( $externalLinkTarget ) {
208 $this->setMwGlobals( 'wgExternalLinkTarget', $externalLinkTarget );
209
210 $attribs = $this->getAttribs();
211 $this->assertArrayHasKey( 'target', $attribs );
212 $this->assertEquals( $attribs['target'], $externalLinkTarget );
213 }
214
215 public static function dataRespectExternallinktarget() {
216 return [
217 [ '_blank' ],
218 [ '_self' ],
219 ];
220 }
221 }