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