CommentStore: Hard-deprecate newKey()
[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 href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
299 '[[ :Special:BlankPage]]',
300 null,
301 ],
302 [
303 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
304 "[[Foo'bar]]",
305 'enwiki',
306 ],
307 [
308 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
309 'foo bar [[Special:BlankPage]]',
310 'enwiki',
311 ],
312 [
313 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/File:Example">Image:Example</a>',
314 'foo bar [[Image:Example]]',
315 'enwiki',
316 ],
317 ];
318 // phpcs:enable
319 }
320
321 public static function provideLinkBeginHook() {
322 // phpcs:disable Generic.Files.LineLength
323 return [
324 // Modify $html
325 [
326 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
327 $html = 'foobar';
328 },
329 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
330 ],
331 // Modify $attribs
332 [
333 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
334 $attribs['bar'] = 'baz';
335 },
336 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
337 ],
338 // Modify $query
339 [
340 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
341 $query['bar'] = 'baz';
342 },
343 '<a href="/w/index.php?title=Special:BlankPage&amp;bar=baz" title="Special:BlankPage">Special:BlankPage</a>'
344 ],
345 // Force HTTP $options
346 [
347 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
348 $options = [ 'http' ];
349 },
350 '<a href="http://example.org/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>'
351 ],
352 // Force 'forcearticlepath' in $options
353 [
354 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
355 $options = [ 'forcearticlepath' ];
356 $query['foo'] = 'bar';
357 },
358 '<a href="/wiki/Special:BlankPage?foo=bar" title="Special:BlankPage">Special:BlankPage</a>'
359 ],
360 // Abort early
361 [
362 function ( $dummy, $title, &$html, &$attribs, &$query, &$options, &$ret ) {
363 $ret = 'foobar';
364 return false;
365 },
366 'foobar'
367 ],
368 ];
369 // phpcs:enable
370 }
371
372 /**
373 * @covers MediaWiki\Linker\LinkRenderer::runLegacyBeginHook
374 * @dataProvider provideLinkBeginHook
375 */
376 public function testLinkBeginHook( $callback, $expected ) {
377 $this->hideDeprecated( 'LinkBegin hook (used in hook-LinkBegin-closure)' );
378 $this->setMwGlobals( [
379 'wgArticlePath' => '/wiki/$1',
380 'wgServer' => '//example.org',
381 'wgCanonicalServer' => 'http://example.org',
382 'wgScriptPath' => '/w',
383 'wgScript' => '/w/index.php',
384 ] );
385
386 $this->setMwGlobals( 'wgHooks', [ 'LinkBegin' => [ $callback ] ] );
387 $title = SpecialPage::getTitleFor( 'Blankpage' );
388 $out = Linker::link( $title );
389 $this->assertEquals( $expected, $out );
390 }
391
392 public static function provideLinkEndHook() {
393 return [
394 // Override $html
395 [
396 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
397 $html = 'foobar';
398 },
399 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">foobar</a>'
400 ],
401 // Modify $attribs
402 [
403 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
404 $attribs['bar'] = 'baz';
405 },
406 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage" bar="baz">Special:BlankPage</a>'
407 ],
408 // Fully override return value and abort hook
409 [
410 function ( $dummy, $title, $options, &$html, &$attribs, &$ret ) {
411 $ret = 'blahblahblah';
412 return false;
413 },
414 'blahblahblah'
415 ],
416
417 ];
418 }
419
420 /**
421 * @covers MediaWiki\Linker\LinkRenderer::buildAElement
422 * @dataProvider provideLinkEndHook
423 */
424 public function testLinkEndHook( $callback, $expected ) {
425 $this->hideDeprecated( 'LinkEnd hook (used in hook-LinkEnd-closure)' );
426 $this->setMwGlobals( [
427 'wgArticlePath' => '/wiki/$1',
428 ] );
429
430 $this->setMwGlobals( 'wgHooks', [ 'LinkEnd' => [ $callback ] ] );
431
432 $title = SpecialPage::getTitleFor( 'Blankpage' );
433 $out = Linker::link( $title );
434 $this->assertEquals( $expected, $out );
435 }
436 }