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