Advertise feeds only if $wgFeed is enabled
[lhc/web/wiklou.git] / tests / phpunit / includes / LinkerTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class LinkerTest extends MediaWikiLangTestCase {
7
8 /**
9 * @dataProvider provideCasesForUserLink
10 * @covers Linker::userLink
11 */
12 public function testUserLink( $expected, $userId, $userName, $altUserName = false, $msg = '' ) {
13 $this->setMwGlobals( [
14 'wgArticlePath' => '/wiki/$1',
15 ] );
16
17 $this->assertEquals(
18 $expected,
19 Linker::userLink( $userId, $userName, $altUserName ),
20 $msg
21 );
22 }
23
24 public static function provideCasesForUserLink() {
25 # Format:
26 # - expected
27 # - userid
28 # - username
29 # - optional altUserName
30 # - optional message
31 return [
32
33 # ## ANONYMOUS USER ########################################
34 [
35 '<a href="/wiki/Special:Contributions/JohnDoe" '
36 . 'class="mw-userlink mw-anonuserlink" '
37 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
38 0, 'JohnDoe', false,
39 ],
40 [
41 '<a href="/wiki/Special:Contributions/::1" '
42 . 'class="mw-userlink mw-anonuserlink" '
43 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
44 0, '::1', false,
45 'Anonymous with pretty IPv6'
46 ],
47 [
48 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
49 . 'class="mw-userlink mw-anonuserlink" '
50 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
51 0, '0:0:0:0:0:0:0:1', false,
52 'Anonymous with almost pretty IPv6'
53 ],
54 [
55 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
56 . 'class="mw-userlink mw-anonuserlink" '
57 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
58 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
59 'Anonymous with full IPv6'
60 ],
61 [
62 '<a href="/wiki/Special:Contributions/::1" '
63 . 'class="mw-userlink mw-anonuserlink" '
64 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
65 0, '::1', 'AlternativeUsername',
66 'Anonymous with pretty IPv6 and an alternative username'
67 ],
68
69 # IPV4
70 [
71 '<a href="/wiki/Special:Contributions/127.0.0.1" '
72 . 'class="mw-userlink mw-anonuserlink" '
73 . 'title="Special:Contributions/127.0.0.1"><bdi>127.0.0.1</bdi></a>',
74 0, '127.0.0.1', false,
75 'Anonymous with IPv4'
76 ],
77 [
78 '<a href="/wiki/Special:Contributions/127.0.0.1" '
79 . 'class="mw-userlink mw-anonuserlink" '
80 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
81 0, '127.0.0.1', 'AlternativeUsername',
82 'Anonymous with IPv4 and an alternative username'
83 ],
84
85 # ## Regular user ##########################################
86 # TODO!
87 ];
88 }
89
90 /**
91 * @dataProvider provideCasesForFormatComment
92 * @covers Linker::formatComment
93 * @covers Linker::formatAutocomments
94 * @covers Linker::formatLinksInComment
95 */
96 public function testFormatComment(
97 $expected, $comment, $title = false, $local = false, $wikiId = null
98 ) {
99 $conf = new SiteConfiguration();
100 $conf->settings = [
101 'wgServer' => [
102 'enwiki' => '//en.example.org',
103 'dewiki' => '//de.example.org',
104 ],
105 'wgArticlePath' => [
106 'enwiki' => '/w/$1',
107 'dewiki' => '/w/$1',
108 ],
109 ];
110 $conf->suffixes = [ 'wiki' ];
111
112 $this->setMwGlobals( [
113 'wgScript' => '/wiki/index.php',
114 'wgArticlePath' => '/wiki/$1',
115 'wgCapitalLinks' => true,
116 'wgConf' => $conf,
117 ] );
118
119 if ( $title === false ) {
120 // We need a page title that exists
121 $title = Title::newFromText( 'Special:BlankPage' );
122 }
123
124 $this->assertEquals(
125 $expected,
126 Linker::formatComment( $comment, $title, $local, $wikiId )
127 );
128 }
129
130 public function provideCasesForFormatComment() {
131 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
132
133 // phpcs:disable Generic.Files.LineLength
134 return [
135 // Linker::formatComment
136 [
137 'a&lt;script&gt;b',
138 'a<script>b',
139 ],
140 [
141 'a—b',
142 'a&mdash;b',
143 ],
144 [
145 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
146 "'''not bolded'''",
147 ],
148 [
149 "try &lt;script&gt;evil&lt;/scipt&gt; things",
150 "try <script>evil</scipt> things",
151 ],
152 // Linker::formatAutocomments
153 [
154 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
155 "/* autocomment */",
156 ],
157 [
158 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎&#91;[linkie?]]</a></span></span>',
159 "/* [[linkie?]] */",
160 ],
161 [
162 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
163 "/* autocomment */ post",
164 ],
165 [
166 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
167 "pre /* autocomment */",
168 ],
169 [
170 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
171 "pre /* autocomment */ post",
172 ],
173 [
174 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> multiple? <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment2" title="Special:BlankPage">→‎autocomment2</a></span></span></span>',
175 "/* autocomment */ multiple? /* autocomment2 */",
176 ],
177 [
178 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
179 "/* autocomment containing /* */ T70361"
180 ],
181 [
182 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.22quotes.22" title="Special:BlankPage">→‎autocomment containing &quot;quotes&quot;</a></span></span>',
183 "/* autocomment containing \"quotes\" */"
184 ],
185 [
186 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.3Cscript.3Etags.3C.2Fscript.3E" title="Special:BlankPage">→‎autocomment containing &lt;script&gt;tags&lt;/script&gt;</a></span></span>',
187 "/* autocomment containing <script>tags</script> */"
188 ],
189 [
190 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
191 "/* autocomment */",
192 false, true
193 ],
194 [
195 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
196 "/* autocomment */",
197 null
198 ],
199 [
200 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
201 "/* autocomment */",
202 false, false
203 ],
204 [
205 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
206 "/* autocomment */",
207 false, false, $wikiId
208 ],
209 // Linker::formatLinksInComment
210 [
211 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
212 "abc [[link]] def",
213 ],
214 [
215 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">text</a> def',
216 "abc [[link|text]] def",
217 ],
218 [
219 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
220 "abc [[Special:BlankPage|]] def",
221 ],
222 [
223 'abc <a href="/wiki/index.php?title=%C4%84%C5%9B%C5%BC&amp;action=edit&amp;redlink=1" class="new" title="Ąśż (page does not exist)">ąśż</a> def',
224 "abc [[%C4%85%C5%9B%C5%BC]] def",
225 ],
226 [
227 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
228 "abc [[#section]] def",
229 ],
230 [
231 'abc <a href="/wiki/index.php?title=/subpage&amp;action=edit&amp;redlink=1" class="new" title="/subpage (page does not exist)">/subpage</a> def',
232 "abc [[/subpage]] def",
233 ],
234 [
235 'abc <a href="/wiki/index.php?title=%22evil!%22&amp;action=edit&amp;redlink=1" class="new" title="&quot;evil!&quot; (page does not exist)">&quot;evil!&quot;</a> def',
236 "abc [[\"evil!\"]] def",
237 ],
238 [
239 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
240 "abc [[<script>very evil</script>]] def",
241 ],
242 [
243 'abc [[|]] def',
244 "abc [[|]] def",
245 ],
246 [
247 'abc <a href="/wiki/index.php?title=Link&amp;action=edit&amp;redlink=1" class="new" title="Link (page does not exist)">link</a> def',
248 "abc [[link]] def",
249 false, false
250 ],
251 [
252 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
253 "abc [[link]] def",
254 false, false, $wikiId
255 ],
256 ];
257 // phpcs:enable
258 }
259
260 /**
261 * @covers Linker::formatLinksInComment
262 * @dataProvider provideCasesForFormatLinksInComment
263 */
264 public function testFormatLinksInComment( $expected, $input, $wiki ) {
265 $conf = new SiteConfiguration();
266 $conf->settings = [
267 'wgServer' => [
268 'enwiki' => '//en.example.org'
269 ],
270 'wgArticlePath' => [
271 'enwiki' => '/w/$1',
272 ],
273 ];
274 $conf->suffixes = [ 'wiki' ];
275 $this->setMwGlobals( [
276 'wgScript' => '/wiki/index.php',
277 'wgArticlePath' => '/wiki/$1',
278 'wgCapitalLinks' => true,
279 'wgConf' => $conf,
280 ] );
281
282 $this->assertEquals(
283 $expected,
284 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
285 );
286 }
287
288 public static function provideCasesForFormatLinksInComment() {
289 // phpcs:disable Generic.Files.LineLength
290 return [
291 [
292 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
293 'foo bar [[Special:BlankPage]]',
294 null,
295 ],
296 [
297 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
298 '[[ :Special:BlankPage]]',
299 null,
300 ],
301 [
302 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
303 "[[Foo'bar]]",
304 'enwiki',
305 ],
306 [
307 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
308 'foo bar [[Special:BlankPage]]',
309 'enwiki',
310 ],
311 [
312 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
313 'foo bar [[Image:Example]]',
314 'enwiki',
315 ],
316 ];
317 // phpcs:enable
318 }
319
320 public static function provideLinkBeginHook() {
321 // phpcs:disable Generic.Files.LineLength
322 return [
323 // Modify $html
324 [
325 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
326 $html = 'foobar';
327 },
328 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
329 ],
330 // Modify $attribs
331 [
332 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
333 $attribs['bar'] = 'baz';
334 },
335 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
336 ],
337 // Modify $query
338 [
339 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
340 $query['bar'] = 'baz';
341 },
342 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
343 ],
344 // Force HTTP $options
345 [
346 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
347 $options = [ 'http' ];
348 },
349 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
350 ],
351 // Force 'forcearticlepath' in $options
352 [
353 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
354 $options = [ 'forcearticlepath' ];
355 $query['foo'] = 'bar';
356 },
357 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
358 ],
359 // Abort early
360 [
361 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
362 $ret = 'foobar';
363 return false;
364 },
365 'foobar'
366 ],
367 ];
368 // phpcs:enable
369 }
370
371 /**
372 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
373 * @dataProvider provideLinkBeginHook
374 */
375 public function testLinkBeginHook( $callback, $expected ) {
376 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
377 $this->setMwGlobals( [
378 'wgArticlePath' => '/wiki/$1',
379 'wgServer' => '//example.org',
380 'wgCanonicalServer' => 'http://example.org',
381 'wgScriptPath' => '/w',
382 'wgScript' => '/w/index.php',
383 ] );
384
385 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
386 $title = SpecialPage::getTitleFor( 'Blankpage' );
387 $out = Linker::link( $title );
388 $this->assertEquals( $expected, $out );
389 }
390
391 public static function provideLinkEndHook() {
392 return [
393 // Override $html
394 [
395 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
396 $html = 'foobar';
397 },
398 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
399 ],
400 // Modify $attribs
401 [
402 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
403 $attribs['bar'] = 'baz';
404 },
405 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
406 ],
407 // Fully override return value and abort hook
408 [
409 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
410 $ret = 'blahblahblah';
411 return false;
412 },
413 'blahblahblah'
414 ],
415
416 ];
417 }
418
419 /**
420 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
421 * @dataProvider provideLinkEndHook
422 */
423 public function testLinkEndHook( $callback, $expected ) {
424 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
425 $this->setMwGlobals( [
426 'wgArticlePath' => '/wiki/$1',
427 ] );
428
429 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
430
431 $title = SpecialPage::getTitleFor( 'Blankpage' );
432 $out = Linker::link( $title );
433 $this->assertEquals( $expected, $out );
434 }
435 }