Merge "Fix empty auto-summaries triggering a fatal error."
[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 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
18 if ( $userName === '' ) {
19 Wikimedia\suppressWarnings();
20 }
21 $actual = Linker::userLink( $userId, $userName, $altUserName );
22 if ( $userName === '' ) {
23 Wikimedia\restoreWarnings();
24 }
25
26 $this->assertEquals( $expected, $actual, $msg );
27 }
28
29 public static function provideCasesForUserLink() {
30 # Format:
31 # - expected
32 # - userid
33 # - username
34 # - optional altUserName
35 # - optional message
36 return [
37 # Empty name (T222529)
38 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
39 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
40
41 # ## ANONYMOUS USER ########################################
42 [
43 '<a href="/wiki/Special:Contributions/JohnDoe" '
44 . 'class="mw-userlink mw-anonuserlink" '
45 . 'title="Special:Contributions/JohnDoe"><bdi>JohnDoe</bdi></a>',
46 0, 'JohnDoe', false,
47 ],
48 [
49 '<a href="/wiki/Special:Contributions/::1" '
50 . 'class="mw-userlink mw-anonuserlink" '
51 . 'title="Special:Contributions/::1"><bdi>::1</bdi></a>',
52 0, '::1', false,
53 'Anonymous with pretty IPv6'
54 ],
55 [
56 '<a href="/wiki/Special:Contributions/0:0:0:0:0:0:0:1" '
57 . 'class="mw-userlink mw-anonuserlink" '
58 . 'title="Special:Contributions/0:0:0:0:0:0:0:1"><bdi>::1</bdi></a>',
59 0, '0:0:0:0:0:0:0:1', false,
60 'Anonymous with almost pretty IPv6'
61 ],
62 [
63 '<a href="/wiki/Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
64 . 'class="mw-userlink mw-anonuserlink" '
65 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001"><bdi>::1</bdi></a>',
66 0, '0000:0000:0000:0000:0000:0000:0000:0001', false,
67 'Anonymous with full IPv6'
68 ],
69 [
70 '<a href="/wiki/Special:Contributions/::1" '
71 . 'class="mw-userlink mw-anonuserlink" '
72 . 'title="Special:Contributions/::1"><bdi>AlternativeUsername</bdi></a>',
73 0, '::1', 'AlternativeUsername',
74 'Anonymous with pretty IPv6 and an alternative username'
75 ],
76
77 # IPV4
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>127.0.0.1</bdi></a>',
82 0, '127.0.0.1', false,
83 'Anonymous with IPv4'
84 ],
85 [
86 '<a href="/wiki/Special:Contributions/127.0.0.1" '
87 . 'class="mw-userlink mw-anonuserlink" '
88 . 'title="Special:Contributions/127.0.0.1"><bdi>AlternativeUsername</bdi></a>',
89 0, '127.0.0.1', 'AlternativeUsername',
90 'Anonymous with IPv4 and an alternative username'
91 ],
92
93 # ## Regular user ##########################################
94 # TODO!
95 ];
96 }
97
98 /**
99 * @dataProvider provideUserToolLinks
100 * @covers Linker::userToolLinks
101 * @param string $expected
102 * @param int $userId
103 * @param string $userText
104 */
105 public function testUserToolLinks( $expected, $userId, $userText ) {
106 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
107 if ( $userText === '' ) {
108 Wikimedia\suppressWarnings();
109 }
110 $actual = Linker::userToolLinks( $userId, $userText );
111 if ( $userText === '' ) {
112 Wikimedia\restoreWarnings();
113 }
114
115 $this->assertSame( $expected, $actual );
116 }
117
118 public static function provideUserToolLinks() {
119 return [
120 // Empty name (T222529)
121 'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
122 'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
123 ];
124 }
125
126 /**
127 * @dataProvider provideUserTalkLink
128 * @covers Linker::userTalkLink
129 * @param string $expected
130 * @param int $userId
131 * @param string $userText
132 */
133 public function testUserTalkLink( $expected, $userId, $userText ) {
134 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
135 if ( $userText === '' ) {
136 Wikimedia\suppressWarnings();
137 }
138 $actual = Linker::userTalkLink( $userId, $userText );
139 if ( $userText === '' ) {
140 Wikimedia\restoreWarnings();
141 }
142
143 $this->assertSame( $expected, $actual );
144 }
145
146 public static function provideUserTalkLink() {
147 return [
148 // Empty name (T222529)
149 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
150 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
151 ];
152 }
153
154 /**
155 * @dataProvider provideBlockLink
156 * @covers Linker::blockLink
157 * @param string $expected
158 * @param int $userId
159 * @param string $userText
160 */
161 public function testBlockLink( $expected, $userId, $userText ) {
162 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
163 if ( $userText === '' ) {
164 Wikimedia\suppressWarnings();
165 }
166 $actual = Linker::blockLink( $userId, $userText );
167 if ( $userText === '' ) {
168 Wikimedia\restoreWarnings();
169 }
170
171 $this->assertSame( $expected, $actual );
172 }
173
174 public static function provideBlockLink() {
175 return [
176 // Empty name (T222529)
177 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
178 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
179 ];
180 }
181
182 /**
183 * @dataProvider provideEmailLink
184 * @covers Linker::emailLink
185 * @param string $expected
186 * @param int $userId
187 * @param string $userText
188 */
189 public function testEmailLink( $expected, $userId, $userText ) {
190 // We'd also test the warning, but injecting a mock logger into a static method is tricky.
191 if ( $userText === '' ) {
192 Wikimedia\suppressWarnings();
193 }
194 $actual = Linker::emailLink( $userId, $userText );
195 if ( $userText === '' ) {
196 Wikimedia\restoreWarnings();
197 }
198
199 $this->assertSame( $expected, $actual );
200 }
201
202 public static function provideEmailLink() {
203 return [
204 // Empty name (T222529)
205 'Empty username, userid 0' => [ '(no username available)', 0, '' ],
206 'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
207 ];
208 }
209
210 /**
211 * @dataProvider provideCasesForFormatComment
212 * @covers Linker::formatComment
213 * @covers Linker::formatAutocomments
214 * @covers Linker::formatLinksInComment
215 */
216 public function testFormatComment(
217 $expected, $comment, $title = false, $local = false, $wikiId = null
218 ) {
219 $conf = new SiteConfiguration();
220 $conf->settings = [
221 'wgServer' => [
222 'enwiki' => '//en.example.org',
223 'dewiki' => '//de.example.org',
224 ],
225 'wgArticlePath' => [
226 'enwiki' => '/w/$1',
227 'dewiki' => '/w/$1',
228 ],
229 ];
230 $conf->suffixes = [ 'wiki' ];
231
232 $this->setMwGlobals( [
233 'wgScript' => '/wiki/index.php',
234 'wgArticlePath' => '/wiki/$1',
235 'wgCapitalLinks' => true,
236 'wgConf' => $conf,
237 ] );
238
239 if ( $title === false ) {
240 // We need a page title that exists
241 $title = Title::newFromText( 'Special:BlankPage' );
242 }
243
244 $this->assertEquals(
245 $expected,
246 Linker::formatComment( $comment, $title, $local, $wikiId )
247 );
248 }
249
250 public function provideCasesForFormatComment() {
251 $wikiId = 'enwiki'; // $wgConf has a fake entry for this
252
253 // phpcs:disable Generic.Files.LineLength
254 return [
255 // Linker::formatComment
256 [
257 'a&lt;script&gt;b',
258 'a<script>b',
259 ],
260 [
261 'a—b',
262 'a&mdash;b',
263 ],
264 [
265 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
266 "'''not bolded'''",
267 ],
268 [
269 "try &lt;script&gt;evil&lt;/scipt&gt; things",
270 "try <script>evil</scipt> things",
271 ],
272 // Linker::formatAutocomments
273 [
274 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
275 "/* autocomment */",
276 ],
277 [
278 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎&#91;[linkie?]]</a></span></span>',
279 "/* [[linkie?]] */",
280 ],
281 [
282 '<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>',
283 // Regression test for T222857
284 "/* */ // Edit via via",
285 ],
286 [
287 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
288 "/* autocomment */ post",
289 ],
290 [
291 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
292 "pre /* autocomment */",
293 ],
294 [
295 'pre <span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
296 "pre /* autocomment */ post",
297 ],
298 [
299 '<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>',
300 "/* autocomment */ multiple? /* autocomment2 */",
301 ],
302 [
303 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→‎autocomment containing /*</a>: </span> T70361</span>',
304 "/* autocomment containing /* */ T70361"
305 ],
306 [
307 '<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>',
308 "/* autocomment containing \"quotes\" */"
309 ],
310 [
311 '<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>',
312 "/* autocomment containing <script>tags</script> */"
313 ],
314 [
315 '<span dir="auto"><span class="autocomment"><a href="#autocomment">→‎autocomment</a></span></span>',
316 "/* autocomment */",
317 false, true
318 ],
319 [
320 '<span dir="auto"><span class="autocomment">autocomment</span></span>',
321 "/* autocomment */",
322 null
323 ],
324 [
325 '',
326 "/* */",
327 false, true
328 ],
329 [
330 '',
331 "/* */",
332 null
333 ],
334 [
335 '<span dir="auto"><span class="autocomment">[[</span></span>',
336 "/* [[ */",
337 false, true
338 ],
339 [
340 '<span dir="auto"><span class="autocomment">[[</span></span>',
341 "/* [[ */",
342 null
343 ],
344 [
345 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎&#91;[#_\t_]]</a></span></span>",
346 "foo /* [[#_\t_]] */",
347 false, true
348 ],
349 [
350 "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
351 "foo /* [[#_\t_]] */",
352 null
353 ],
354 [
355 '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
356 "/* autocomment */",
357 false, false
358 ],
359 [
360 '<span dir="auto"><span class="autocomment"><a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→‎autocomment</a></span></span>',
361 "/* autocomment */",
362 false, false, $wikiId
363 ],
364 // Linker::formatLinksInComment
365 [
366 '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',
367 "abc [[link]] def",
368 ],
369 [
370 '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',
371 "abc [[link|text]] def",
372 ],
373 [
374 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
375 "abc [[Special:BlankPage|]] def",
376 ],
377 [
378 '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',
379 "abc [[%C4%85%C5%9B%C5%BC]] def",
380 ],
381 [
382 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
383 "abc [[#section]] def",
384 ],
385 [
386 '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',
387 "abc [[/subpage]] def",
388 ],
389 [
390 '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',
391 "abc [[\"evil!\"]] def",
392 ],
393 [
394 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
395 "abc [[<script>very evil</script>]] def",
396 ],
397 [
398 'abc [[|]] def',
399 "abc [[|]] def",
400 ],
401 [
402 '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',
403 "abc [[link]] def",
404 false, false
405 ],
406 [
407 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
408 "abc [[link]] def",
409 false, false, $wikiId
410 ],
411 ];
412 // phpcs:enable
413 }
414
415 /**
416 * @covers Linker::formatLinksInComment
417 * @dataProvider provideCasesForFormatLinksInComment
418 */
419 public function testFormatLinksInComment( $expected, $input, $wiki ) {
420 $conf = new SiteConfiguration();
421 $conf->settings = [
422 'wgServer' => [
423 'enwiki' => '//en.example.org'
424 ],
425 'wgArticlePath' => [
426 'enwiki' => '/w/$1',
427 ],
428 ];
429 $conf->suffixes = [ 'wiki' ];
430 $this->setMwGlobals( [
431 'wgScript' => '/wiki/index.php',
432 'wgArticlePath' => '/wiki/$1',
433 'wgCapitalLinks' => true,
434 'wgConf' => $conf,
435 ] );
436
437 $this->assertEquals(
438 $expected,
439 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
440 );
441 }
442
443 /**
444 * @covers Linker::generateRollback
445 * @dataProvider provideCasesForRollbackGeneration
446 */
447 public function testGenerateRollback( $rollbackEnabled, $expectedModules ) {
448 $this->markTestSkippedIfDbType( 'postgres' );
449
450 $context = RequestContext::getMain();
451 $user = $context->getUser();
452 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
453
454 $pageData = $this->insertPage( 'Rollback_Test_Page' );
455 $page = WikiPage::factory( $pageData['title'] );
456
457 $updater = $page->newPageUpdater( $user );
458 $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
459 new TextContent( 'Technical Wishes 123!' )
460 );
461 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
462 $updater->saveRevision( $summary );
463
464 $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
465 $modules = $context->getOutput()->getModules();
466
467 $this->assertEquals( $expectedModules, $modules );
468 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
469 }
470
471 public static function provideCasesForRollbackGeneration() {
472 return [
473 [
474 true,
475 [ 'mediawiki.page.rollback.confirmation' ]
476
477 ],
478 [
479 false,
480 []
481 ]
482 ];
483 }
484
485 public static function provideCasesForFormatLinksInComment() {
486 // phpcs:disable Generic.Files.LineLength
487 return [
488 [
489 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
490 'foo bar [[Special:BlankPage]]',
491 null,
492 ],
493 [
494 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
495 '[[ :Special:BlankPage]]',
496 null,
497 ],
498 [
499 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
500 "[[Foo'bar]]",
501 'enwiki',
502 ],
503 [
504 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
505 'foo bar [[Special:BlankPage]]',
506 'enwiki',
507 ],
508 [
509 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
510 'foo bar [[Image:Example]]',
511 'enwiki',
512 ],
513 ];
514 // phpcs:enable
515 }
516
517 public static function provideLinkBeginHook() {
518 // phpcs:disable Generic.Files.LineLength
519 return [
520 // Modify $html
521 [
522 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
523 $html = 'foobar';
524 },
525 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
526 ],
527 // Modify $attribs
528 [
529 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
530 $attribs['bar'] = 'baz';
531 },
532 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
533 ],
534 // Modify $query
535 [
536 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
537 $query['bar'] = 'baz';
538 },
539 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
540 ],
541 // Force HTTP $options
542 [
543 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
544 $options = [ 'http' ];
545 },
546 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
547 ],
548 // Force 'forcearticlepath' in $options
549 [
550 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
551 $options = [ 'forcearticlepath' ];
552 $query['foo'] = 'bar';
553 },
554 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
555 ],
556 // Abort early
557 [
558 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
559 $ret = 'foobar';
560 return false;
561 },
562 'foobar'
563 ],
564 ];
565 // phpcs:enable
566 }
567
568 /**
569 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
570 * @dataProvider provideLinkBeginHook
571 */
572 public function testLinkBeginHook( $callback, $expected ) {
573 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
574 $this->setMwGlobals( [
575 'wgArticlePath' => '/wiki/$1',
576 'wgServer' => '//example.org',
577 'wgCanonicalServer' => 'http://example.org',
578 'wgScriptPath' => '/w',
579 'wgScript' => '/w/index.php',
580 ] );
581
582 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
583 $title = SpecialPage::getTitleFor( 'Blankpage' );
584 $out = Linker::link( $title );
585 $this->assertEquals( $expected, $out );
586 }
587
588 public static function provideLinkEndHook() {
589 return [
590 // Override $html
591 [
592 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
593 $html = 'foobar';
594 },
595 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
596 ],
597 // Modify $attribs
598 [
599 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
600 $attribs['bar'] = 'baz';
601 },
602 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
603 ],
604 // Fully override return value and abort hook
605 [
606 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
607 $ret = 'blahblahblah';
608 return false;
609 },
610 'blahblahblah'
611 ],
612
613 ];
614 }
615
616 /**
617 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
618 * @dataProvider provideLinkEndHook
619 */
620 public function testLinkEndHook( $callback, $expected ) {
621 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
622 $this->setMwGlobals( [
623 'wgArticlePath' => '/wiki/$1',
624 ] );
625
626 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
627
628 $title = SpecialPage::getTitleFor( 'Blankpage' );
629 $out = Linker::link( $title );
630 $this->assertEquals( $expected, $out );
631 }
632 }