Merge "Drop zh-tw message "saveprefs""
[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( array(
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 array(
32
33 ### ANONYMOUS USER ########################################
34 array(
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 array(
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 array(
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 array(
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 array(
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 array(
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 array(
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( $expected, $comment, $title = false, $local = false, $wikiId = null ) {
97 $conf = new SiteConfiguration();
98 $conf->settings = array(
99 'wgServer' => array(
100 'enwiki' => '//en.example.org',
101 'dewiki' => '//de.example.org',
102 ),
103 'wgArticlePath' => array(
104 'enwiki' => '/w/$1',
105 'dewiki' => '/w/$1',
106 ),
107 );
108 $conf->suffixes = array( 'wiki' );
109
110 $this->setMwGlobals( array(
111 'wgScript' => '/wiki/index.php',
112 'wgArticlePath' => '/wiki/$1',
113 'wgWellFormedXml' => true,
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 return array(
133 // Linker::formatComment
134 array(
135 'a&lt;script&gt;b',
136 'a<script>b',
137 ),
138 array(
139 'a—b',
140 'a&mdash;b',
141 ),
142 array(
143 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
144 "'''not bolded'''",
145 ),
146 array(
147 "try &lt;script&gt;evil&lt;/scipt&gt; things",
148 "try <script>evil</scipt> things",
149 ),
150 // Linker::formatAutocomments
151 array(
152 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
153 "/* autocomment */",
154 ),
155 array(
156 '<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>',
157 "/* [[linkie?]] */",
158 ),
159 array(
160 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
161 "/* autocomment */ post",
162 ),
163 array(
164 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
165 "pre /* autocomment */",
166 ),
167 array(
168 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
169 "pre /* autocomment */ post",
170 ),
171 array(
172 '<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>',
173 "/* autocomment */ multiple? /* autocomment2 */ ",
174 ),
175 array(
176 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
177 "/* autocomment containing /* */ T70361"
178 ),
179 array(
180 '<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>',
181 "/* autocomment containing \"quotes\" */"
182 ),
183 array(
184 '<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>',
185 "/* autocomment containing <script>tags</script> */"
186 ),
187 array(
188 '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
189 "/* autocomment */",
190 false, true
191 ),
192 array(
193 '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
194 "/* autocomment */",
195 null
196 ),
197 array(
198 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
199 "/* autocomment */",
200 false, false
201 ),
202 array(
203 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
204 "/* autocomment */",
205 false, false, $wikiId
206 ),
207 // Linker::formatLinksInComment
208 array(
209 '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',
210 "abc [[link]] def",
211 ),
212 array(
213 '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',
214 "abc [[link|text]] def",
215 ),
216 array(
217 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
218 "abc [[Special:BlankPage|]] def",
219 ),
220 array(
221 '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',
222 "abc [[%C4%85%C5%9B%C5%BC]] def",
223 ),
224 array(
225 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
226 "abc [[#section]] def",
227 ),
228 array(
229 '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',
230 "abc [[/subpage]] def",
231 ),
232 array(
233 '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',
234 "abc [[\"evil!\"]] def",
235 ),
236 array(
237 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
238 "abc [[<script>very evil</script>]] def",
239 ),
240 array(
241 'abc [[|]] def',
242 "abc [[|]] def",
243 ),
244 array(
245 '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',
246 "abc [[link]] def",
247 false, false
248 ),
249 array(
250 'abc <a class="external" rel="nofollow" href="//en.example.org/w/Link">link</a> def',
251 "abc [[link]] def",
252 false, false, $wikiId
253 )
254 );
255 }
256
257 /**
258 * @covers Linker::formatLinksInComment
259 * @dataProvider provideCasesForFormatLinksInComment
260 */
261 public function testFormatLinksInComment( $expected, $input, $wiki ) {
262
263 $conf = new SiteConfiguration();
264 $conf->settings = array(
265 'wgServer' => array(
266 'enwiki' => '//en.example.org'
267 ),
268 'wgArticlePath' => array(
269 'enwiki' => '/w/$1',
270 ),
271 );
272 $conf->suffixes = array( 'wiki' );
273 $this->setMwGlobals( array(
274 'wgScript' => '/wiki/index.php',
275 'wgArticlePath' => '/wiki/$1',
276 'wgWellFormedXml' => true,
277 'wgCapitalLinks' => true,
278 'wgConf' => $conf,
279 ) );
280
281 $this->assertEquals(
282 $expected,
283 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
284 );
285 }
286
287 public static function provideCasesForFormatLinksInComment() {
288 return array(
289 array(
290 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
291 'foo bar [[Special:BlankPage]]',
292 null,
293 ),
294 array(
295 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
296 "[[Foo'bar]]",
297 'enwiki',
298 ),
299 array(
300 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
301 'foo bar [[Special:BlankPage]]',
302 'enwiki',
303 ),
304 );
305 }
306 }