Merge "Improve documentation of constants throughout the codebase"
[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 /**
289 * @covers Linker::generateRollback
290 * @dataProvider provideCasesForRollbackGeneration
291 */
292 public function testGenerateRollback( $rollbackEnabled, $expectedModules ) {
293 $this->markTestSkippedIfDbType( 'postgres' );
294
295 $context = RequestContext::getMain();
296 $user = $context->getUser();
297 $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
298
299 $pageData = $this->insertPage( 'Rollback_Test_Page' );
300 $page = WikiPage::factory( $pageData['title'] );
301
302 $updater = $page->newPageUpdater( $user );
303 $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN,
304 new TextContent( 'Technical Wishes 123!' )
305 );
306 $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' );
307 $updater->saveRevision( $summary );
308
309 $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
310 $modules = $context->getOutput()->getModules();
311
312 $this->assertEquals( $expectedModules, $modules );
313 $this->assertContains( 'rollback 1 edit', $rollbackOutput );
314 }
315
316 public static function provideCasesForRollbackGeneration() {
317 return [
318 [
319 true,
320 [ 'mediawiki.page.rollback.confirmation' ]
321
322 ],
323 [
324 false,
325 []
326 ]
327 ];
328 }
329
330 public static function provideCasesForFormatLinksInComment() {
331 // phpcs:disable Generic.Files.LineLength
332 return [
333 [
334 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
335 'foo bar [[Special:BlankPage]]',
336 null,
337 ],
338 [
339 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
340 '[[ :Special:BlankPage]]',
341 null,
342 ],
343 [
344 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
345 "[[Foo'bar]]",
346 'enwiki',
347 ],
348 [
349 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
350 'foo bar [[Special:BlankPage]]',
351 'enwiki',
352 ],
353 [
354 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
355 'foo bar [[Image:Example]]',
356 'enwiki',
357 ],
358 ];
359 // phpcs:enable
360 }
361
362 public static function provideLinkBeginHook() {
363 // phpcs:disable Generic.Files.LineLength
364 return [
365 // Modify $html
366 [
367 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
368 $html = 'foobar';
369 },
370 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
371 ],
372 // Modify $attribs
373 [
374 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
375 $attribs['bar'] = 'baz';
376 },
377 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
378 ],
379 // Modify $query
380 [
381 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
382 $query['bar'] = 'baz';
383 },
384 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
385 ],
386 // Force HTTP $options
387 [
388 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
389 $options = [ 'http' ];
390 },
391 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
392 ],
393 // Force 'forcearticlepath' in $options
394 [
395 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
396 $options = [ 'forcearticlepath' ];
397 $query['foo'] = 'bar';
398 },
399 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
400 ],
401 // Abort early
402 [
403 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
404 $ret = 'foobar';
405 return false;
406 },
407 'foobar'
408 ],
409 ];
410 // phpcs:enable
411 }
412
413 /**
414 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
415 * @dataProvider provideLinkBeginHook
416 */
417 public function testLinkBeginHook( $callback, $expected ) {
418 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
419 $this->setMwGlobals( [
420 'wgArticlePath' => '/wiki/$1',
421 'wgServer' => '//example.org',
422 'wgCanonicalServer' => 'http://example.org',
423 'wgScriptPath' => '/w',
424 'wgScript' => '/w/index.php',
425 ] );
426
427 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
428 $title = SpecialPage::getTitleFor( 'Blankpage' );
429 $out = Linker::link( $title );
430 $this->assertEquals( $expected, $out );
431 }
432
433 public static function provideLinkEndHook() {
434 return [
435 // Override $html
436 [
437 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
438 $html = 'foobar';
439 },
440 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
441 ],
442 // Modify $attribs
443 [
444 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
445 $attribs['bar'] = 'baz';
446 },
447 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
448 ],
449 // Fully override return value and abort hook
450 [
451 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
452 $ret = 'blahblahblah';
453 return false;
454 },
455 'blahblahblah'
456 ],
457
458 ];
459 }
460
461 /**
462 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
463 * @dataProvider provideLinkEndHook
464 */
465 public function testLinkEndHook( $callback, $expected ) {
466 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
467 $this->setMwGlobals( [
468 'wgArticlePath' => '/wiki/$1',
469 ] );
470
471 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
472
473 $title = SpecialPage::getTitleFor( 'Blankpage' );
474 $out = Linker::link( $title );
475 $this->assertEquals( $expected, $out );
476 }
477 }