Merge "Better punctuation in uploadstash-badtoken"
[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 'wgWellFormedXml' => true,
17 ] );
18
19 $this->assertEquals( $expected,
20 Linker::userLink( $userId, $userName, $altUserName, $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 . 'title="Special:Contributions/JohnDoe" '
37 . 'class="mw-userlink mw-anonuserlink">JohnDoe</a>',
38 0, 'JohnDoe', false,
39 ],
40 [
41 '<a href="/wiki/Special:Contributions/::1" '
42 . 'title="Special:Contributions/::1" '
43 . 'class="mw-userlink mw-anonuserlink">::1</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 . 'title="Special:Contributions/0:0:0:0:0:0:0:1" '
50 . 'class="mw-userlink mw-anonuserlink">::1</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 . 'title="Special:Contributions/0000:0000:0000:0000:0000:0000:0000:0001" '
57 . 'class="mw-userlink mw-anonuserlink">::1</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 . 'title="Special:Contributions/::1" '
64 . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</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 . 'title="Special:Contributions/127.0.0.1" '
73 . 'class="mw-userlink mw-anonuserlink">127.0.0.1</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 . 'title="Special:Contributions/127.0.0.1" '
80 . 'class="mw-userlink mw-anonuserlink">AlternativeUsername</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 'wgWellFormedXml' => true,
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 // @codingStandardsIgnoreStart 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 // @codingStandardsIgnoreEnd
259 }
260
261 /**
262 * @covers Linker::formatLinksInComment
263 * @dataProvider provideCasesForFormatLinksInComment
264 */
265 public function testFormatLinksInComment( $expected, $input, $wiki ) {
266
267 $conf = new SiteConfiguration();
268 $conf->settings = [
269 'wgServer' => [
270 'enwiki' => '//en.example.org'
271 ],
272 'wgArticlePath' => [
273 'enwiki' => '/w/$1',
274 ],
275 ];
276 $conf->suffixes = [ 'wiki' ];
277 $this->setMwGlobals( [
278 'wgScript' => '/wiki/index.php',
279 'wgArticlePath' => '/wiki/$1',
280 'wgWellFormedXml' => true,
281 'wgCapitalLinks' => true,
282 'wgConf' => $conf,
283 ] );
284
285 $this->assertEquals(
286 $expected,
287 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
288 );
289 }
290
291 public static function provideCasesForFormatLinksInComment() {
292 // @codingStandardsIgnoreStart Generic.Files.LineLength
293 return [
294 [
295 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
296 'foo bar [[Special:BlankPage]]',
297 null,
298 ],
299 [
300 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
301 "[[Foo'bar]]",
302 'enwiki',
303 ],
304 [
305 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
306 'foo bar [[Special:BlankPage]]',
307 'enwiki',
308 ],
309 ];
310 // @codingStandardsIgnoreEnd
311 }
312 }