Merge "PostgreSQL: Fix text search on moved pages"
[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 ) {
97 $this->setMwGlobals( array(
98 'wgScript' => '/wiki/index.php',
99 'wgArticlePath' => '/wiki/$1',
100 'wgWellFormedXml' => true,
101 'wgCapitalLinks' => true,
102 ) );
103
104 if ( $title === false ) {
105 // We need a page title that exists
106 $title = Title::newFromText( 'Special:BlankPage' );
107 }
108
109 $this->assertEquals(
110 $expected,
111 Linker::formatComment( $comment, $title, $local )
112 );
113 }
114
115 public static function provideCasesForFormatComment() {
116 return array(
117 // Linker::formatComment
118 array(
119 'a&lt;script&gt;b',
120 'a<script>b',
121 ),
122 array(
123 'a—b',
124 'a&mdash;b',
125 ),
126 array(
127 "&#039;&#039;&#039;not bolded&#039;&#039;&#039;",
128 "'''not bolded'''",
129 ),
130 // Linker::formatAutocomments
131 array(
132 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
133 "/* autocomment */",
134 ),
135 array(
136 '<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>',
137 "/* [[linkie?]] */",
138 ),
139 array(
140 '<a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
141 "/* autocomment */ post",
142 ),
143 array(
144 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
145 "pre /* autocomment */",
146 ),
147 array(
148 'pre <a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment: </span> post</span>',
149 "pre /* autocomment */ post",
150 ),
151 array(
152 '<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>',
153 "/* autocomment */ multiple? /* autocomment2 */ ",
154 ),
155 array(
156 '<a href="/wiki/Special:BlankPage#autocomment_containing_.2F.2A" title="Special:BlankPage">→</a>‎<span dir="auto"><span class="autocomment">autocomment containing /*: </span> T70361</span>',
157 "/* autocomment containing /* */ T70361"
158 ),
159 array(
160 '<a href="#autocomment">→</a>‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
161 "/* autocomment */",
162 false, true
163 ),
164 array(
165 '‎<span dir="auto"><span class="autocomment">autocomment</span></span>',
166 "/* autocomment */",
167 null
168 ),
169 // Linker::formatLinksInComment
170 array(
171 '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',
172 "abc [[link]] def",
173 ),
174 array(
175 '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',
176 "abc [[link|text]] def",
177 ),
178 array(
179 'abc <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a> def',
180 "abc [[Special:BlankPage|]] def",
181 ),
182 array(
183 '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',
184 "abc [[%C4%85%C5%9B%C5%BC]] def",
185 ),
186 array(
187 'abc <a href="/wiki/Special:BlankPage#section" title="Special:BlankPage">#section</a> def',
188 "abc [[#section]] def",
189 ),
190 array(
191 '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',
192 "abc [[/subpage]] def",
193 ),
194 );
195 }
196
197 /**
198 * @covers Linker::formatLinksInComment
199 * @dataProvider provideCasesForFormatLinksInComment
200 */
201 public function testFormatLinksInComment( $expected, $input, $wiki ) {
202
203 $conf = new SiteConfiguration();
204 $conf->settings = array(
205 'wgServer' => array(
206 'enwiki' => '//en.example.org'
207 ),
208 'wgArticlePath' => array(
209 'enwiki' => '/w/$1',
210 ),
211 );
212 $conf->suffixes = array( 'wiki' );
213 $this->setMwGlobals( array(
214 'wgScript' => '/wiki/index.php',
215 'wgArticlePath' => '/wiki/$1',
216 'wgWellFormedXml' => true,
217 'wgCapitalLinks' => true,
218 'wgConf' => $conf,
219 ) );
220
221 $this->assertEquals(
222 $expected,
223 Linker::formatLinksInComment( $input, Title::newFromText( 'Special:BlankPage' ), false, $wiki )
224 );
225 }
226
227 public static function provideCasesForFormatLinksInComment() {
228 return array(
229 array(
230 'foo bar <a href="/wiki/Special:BlankPage" title="Special:BlankPage">Special:BlankPage</a>',
231 'foo bar [[Special:BlankPage]]',
232 null,
233 ),
234 array(
235 '<a class="external" rel="nofollow" href="//en.example.org/w/Foo%27bar">Foo\'bar</a>',
236 "[[Foo'bar]]",
237 'enwiki',
238 ),
239 array(
240 'foo bar <a class="external" rel="nofollow" href="//en.example.org/w/Special:BlankPage">Special:BlankPage</a>',
241 'foo bar [[Special:BlankPage]]',
242 'enwiki',
243 ),
244 );
245 }
246 }