14d076277632bd7baeb5cf41865c63fcbfea4796
[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(
97 $expected, $comment, $title = false, $local = false, $wikiId = null
98 ) {
99 $conf = new SiteConfiguration();
100 $conf->settings = array(
101 'wgServer' => array(
102 'enwiki' => '//en.example.org',
103 'dewiki' => '//de.example.org',
104 ),
105 'wgArticlePath' => array(
106 'enwiki' => '/w/$1',
107 'dewiki' => '/w/$1',
108 ),
109 );
110 $conf->suffixes = array( 'wiki' );
111
112 $this->setMwGlobals( array(
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 return array(
135 // Linker::formatComment
136 array(
137 'a&lt;script&gt;b',
138 'a<script>b',
139 ),
140 array(
141 'a—b',
142 'a&mdash;b',
143 ),
144 array(
145 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
146 "'''not bolded'''",
147 ),
148 array(
149 "try &lt;script&gt;evil&lt;/scipt&gt; things",
150 "try <script>evil</scipt> things",
151 ),
152 // Linker::formatAutocomments
153 array(
154 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
155 "/* autocomment */",
156 ),
157 array(
158 '<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>',
159 "/* [[linkie?]] */",
160 ),
161 array(
162 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
163 "/* autocomment */ post",
164 ),
165 array(
166 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
167 "pre /* autocomment */",
168 ),
169 array(
170 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
171 "pre /* autocomment */ post",
172 ),
173 array(
174 '<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>',
175 "/* autocomment */ multiple? /* autocomment2 */ ",
176 ),
177 array(
178 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
179 "/* autocomment containing /* */ T70361"
180 ),
181 array(
182 '<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>',
183 "/* autocomment containing \"quotes\" */"
184 ),
185 array(
186 '<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>',
187 "/* autocomment containing <script>tags</script> */"
188 ),
189 array(
190 '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
191 "/* autocomment */",
192 false, true
193 ),
194 array(
195 '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
196 "/* autocomment */",
197 null
198 ),
199 array(
200 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
201 "/* autocomment */",
202 false, false
203 ),
204 array(
205 '<a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
206 "/* autocomment */",
207 false, false, $wikiId
208 ),
209 // Linker::formatLinksInComment
210 array(
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 array(
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 array(
219 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
220 "abc [[Special:BlankPage|]] def",
221 ),
222 array(
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 array(
227 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
228 "abc [[#section]] def",
229 ),
230 array(
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 array(
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 array(
239 'abc [[&lt;script&gt;very evil&lt;/script&gt;]] def',
240 "abc [[<script>very evil</script>]] def",
241 ),
242 array(
243 'abc [[|]] def',
244 "abc [[|]] def",
245 ),
246 array(
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 array(
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 }
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 = array(
267 'wgServer' => array(
268 'enwiki' => '//en.example.org'
269 ),
270 'wgArticlePath' => array(
271 'enwiki' => '/w/$1',
272 ),
273 );
274 $conf->suffixes = array( 'wiki' );
275 $this->setMwGlobals( array(
276 'wgScript' => '/wiki/index.php',
277 'wgArticlePath' => '/wiki/$1',
278 'wgWellFormedXml' => true,
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 return array(
291 array(
292 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
293 'foo bar [[Special:BlankPage]]',
294 null,
295 ),
296 array(
297 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
298 "[[Foo'bar]]",
299 'enwiki',
300 ),
301 array(
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 }
308 }