Merge "Revert "Add type hint against LinkTarget""
[lhc/web/wiklou.git] / tests / phpunit / includes / title / MediaWikiTitleCodecTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Daniel Kinzler
20 */
21
22 /**
23 * @covers MediaWikiTitleCodec
24 *
25 * @group Title
26 * @group Database
27 * ^--- needed because of global state in
28 */
29 class MediaWikiTitleCodecTest extends MediaWikiTestCase {
30
31 public function setUp() {
32 parent::setUp();
33
34 $this->setMwGlobals( [
35 'wgLanguageCode' => 'en',
36 'wgContLang' => Language::factory( 'en' ),
37 // User language
38 'wgLang' => Language::factory( 'en' ),
39 'wgAllowUserJs' => false,
40 'wgDefaultLanguageVariant' => false,
41 'wgMetaNamespace' => 'Project',
42 'wgLocalInterwikis' => [ 'localtestiw' ],
43 'wgCapitalLinks' => true,
44
45 // NOTE: this is why global state is evil.
46 // TODO: refactor access to the interwiki codes so it can be injected.
47 'wgHooks' => [
48 'InterwikiLoadPrefix' => [
49 function ( $prefix, &$data ) {
50 if ( $prefix === 'localtestiw' ) {
51 $data = [ 'iw_url' => 'localtestiw' ];
52 } elseif ( $prefix === 'remotetestiw' ) {
53 $data = [ 'iw_url' => 'remotetestiw' ];
54 }
55 return false;
56 }
57 ]
58 ]
59 ] );
60 }
61
62 /**
63 * Returns a mock GenderCache that will consider a user "female" if the
64 * first part of the user name ends with "a".
65 *
66 * @return GenderCache
67 */
68 private function getGenderCache() {
69 $genderCache = $this->getMockBuilder( 'GenderCache' )
70 ->disableOriginalConstructor()
71 ->getMock();
72
73 $genderCache->expects( $this->any() )
74 ->method( 'getGenderOf' )
75 ->will( $this->returnCallback( function ( $userName ) {
76 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
77 } ) );
78
79 return $genderCache;
80 }
81
82 protected function makeCodec( $lang ) {
83 $gender = $this->getGenderCache();
84 $lang = Language::factory( $lang );
85 // language object can came from cache, which does not respect test settings
86 $lang->resetNamespaces();
87 return new MediaWikiTitleCodec( $lang, $gender );
88 }
89
90 public static function provideFormat() {
91 return [
92 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
93 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
94 [ false, 'Hansi_Maier', '', 'en', 'Hansi Maier' ],
95 [
96 NS_USER_TALK,
97 'hansi__maier',
98 '',
99 'en',
100 'User talk:hansi maier',
101 'User talk:Hansi maier'
102 ],
103
104 // getGenderCache() provides a mock that considers first
105 // names ending in "a" to be female.
106 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
107 ];
108 }
109
110 /**
111 * @dataProvider provideFormat
112 */
113 public function testFormat( $namespace, $text, $fragment, $lang, $expected, $normalized = null ) {
114 if ( $normalized === null ) {
115 $normalized = $expected;
116 }
117
118 $codec = $this->makeCodec( $lang );
119 $actual = $codec->formatTitle( $namespace, $text, $fragment );
120
121 $this->assertEquals( $expected, $actual, 'formatted' );
122
123 // test round trip
124 $parsed = $codec->parseTitle( $actual, NS_MAIN );
125 $actual2 = $codec->formatTitle(
126 $parsed->getNamespace(),
127 $parsed->getText(),
128 $parsed->getFragment()
129 );
130
131 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
132 }
133
134 public static function provideGetText() {
135 return [
136 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
137 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
138 ];
139 }
140
141 /**
142 * @dataProvider provideGetText
143 */
144 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
145 $codec = $this->makeCodec( $lang );
146 $title = new TitleValue( $namespace, $dbkey, $fragment );
147
148 $actual = $codec->getText( $title );
149
150 $this->assertEquals( $expected, $actual );
151 }
152
153 public static function provideGetPrefixedText() {
154 return [
155 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
156 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
157
158 // No capitalization or normalization is applied while formatting!
159 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
160
161 // getGenderCache() provides a mock that considers first
162 // names ending in "a" to be female.
163 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
164 ];
165 }
166
167 /**
168 * @dataProvider provideGetPrefixedText
169 */
170 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
171 $codec = $this->makeCodec( $lang );
172 $title = new TitleValue( $namespace, $dbkey, $fragment );
173
174 $actual = $codec->getPrefixedText( $title );
175
176 $this->assertEquals( $expected, $actual );
177 }
178
179 public static function provideGetFullText() {
180 return [
181 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
182 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
183
184 // No capitalization or normalization is applied while formatting!
185 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
186 ];
187 }
188
189 /**
190 * @dataProvider provideGetFullText
191 */
192 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
193 $codec = $this->makeCodec( $lang );
194 $title = new TitleValue( $namespace, $dbkey, $fragment );
195
196 $actual = $codec->getFullText( $title );
197
198 $this->assertEquals( $expected, $actual );
199 }
200
201 public static function provideParseTitle() {
202 // TODO: test capitalization and trimming
203 // TODO: test unicode normalization
204
205 return [
206 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
207 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
208 [ 'User:::1', NS_MAIN, 'de',
209 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
210 [ ' lisa Müller', NS_USER, 'de',
211 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
212 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
213 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
214
215 [ ':Category:Quux', NS_MAIN, 'en',
216 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
217 [ 'Category:Quux', NS_MAIN, 'en',
218 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
219 [ 'Category:Quux', NS_CATEGORY, 'en',
220 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
221 [ 'Quux', NS_CATEGORY, 'en',
222 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
223 [ ':Quux', NS_CATEGORY, 'en',
224 new TitleValue( NS_MAIN, 'Quux', '' ) ],
225
226 // getGenderCache() provides a mock that considers first
227 // names ending in "a" to be female.
228
229 [ 'a b c', NS_MAIN, 'en',
230 new TitleValue( NS_MAIN, 'A_b_c' ) ],
231 [ ' a b c ', NS_MAIN, 'en',
232 new TitleValue( NS_MAIN, 'A_b_c' ) ],
233 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
234 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
235
236 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
237 [ 'Sandbox', NS_MAIN, 'en', ],
238 [ 'A "B"', NS_MAIN, 'en', ],
239 [ 'A \'B\'', NS_MAIN, 'en', ],
240 [ '.com', NS_MAIN, 'en', ],
241 [ '~', NS_MAIN, 'en', ],
242 [ '"', NS_MAIN, 'en', ],
243 [ '\'', NS_MAIN, 'en', ],
244
245 [ 'Talk:Sandbox', NS_MAIN, 'en',
246 new TitleValue( NS_TALK, 'Sandbox' ) ],
247 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
248 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
249 [ 'File:Example.svg', NS_MAIN, 'en',
250 new TitleValue( NS_FILE, 'Example.svg' ) ],
251 [ 'File_talk:Example.svg', NS_MAIN, 'en',
252 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
253 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
254 'Foo/.../Sandbox' ],
255 [ 'Sandbox/...', NS_MAIN, 'en',
256 'Sandbox/...' ],
257 [ 'A~~', NS_MAIN, 'en',
258 'A~~' ],
259 // Length is 256 total, but only title part matters
260 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
261 new TitleValue( NS_CATEGORY,
262 'X' . str_repeat( 'x', 247 ) ) ],
263 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
264 'X' . str_repeat( 'x', 251 ) ]
265 ];
266 }
267
268 /**
269 * @dataProvider provideParseTitle
270 */
271 public function testParseTitle( $text, $ns, $lang, $title = null ) {
272 if ( $title === null ) {
273 $title = str_replace( ' ', '_', trim( $text ) );
274 }
275
276 if ( is_string( $title ) ) {
277 $title = new TitleValue( NS_MAIN, $title, '' );
278 }
279
280 $codec = $this->makeCodec( $lang );
281 $actual = $codec->parseTitle( $text, $ns );
282
283 $this->assertEquals( $title, $actual );
284 }
285
286 public static function provideParseTitle_invalid() {
287 // TODO: test unicode errors
288
289 return [
290 [ '#' ],
291 [ '::' ],
292 [ '::xx' ],
293 [ '::##' ],
294 [ ' :: x' ],
295
296 [ 'Talk:File:Foo.jpg' ],
297 [ 'Talk:localtestiw:Foo' ],
298 [ 'remotetestiw:Foo' ],
299 [ '::1' ], // only valid in user namespace
300 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
301
302 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
303 [ '' ],
304 [ ':' ],
305 [ '__ __' ],
306 [ ' __ ' ],
307 // Bad characters forbidden regardless of wgLegalTitleChars
308 [ 'A [ B' ],
309 [ 'A ] B' ],
310 [ 'A { B' ],
311 [ 'A } B' ],
312 [ 'A < B' ],
313 [ 'A > B' ],
314 [ 'A | B' ],
315 // URL encoding
316 [ 'A%20B' ],
317 [ 'A%23B' ],
318 [ 'A%2523B' ],
319 // XML/HTML character entity references
320 // Note: Commented out because they are not marked invalid by the PHP test as
321 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
322 // array( 'A &eacute; B' ),
323 // array( 'A &#233; B' ),
324 // array( 'A &#x00E9; B' ),
325 // Subject of NS_TALK does not roundtrip to NS_MAIN
326 [ 'Talk:File:Example.svg' ],
327 // Directory navigation
328 [ '.' ],
329 [ '..' ],
330 [ './Sandbox' ],
331 [ '../Sandbox' ],
332 [ 'Foo/./Sandbox' ],
333 [ 'Foo/../Sandbox' ],
334 [ 'Sandbox/.' ],
335 [ 'Sandbox/..' ],
336 // Tilde
337 [ 'A ~~~ Name' ],
338 [ 'A ~~~~ Signature' ],
339 [ 'A ~~~~~ Timestamp' ],
340 [ str_repeat( 'x', 256 ) ],
341 // Namespace prefix without actual title
342 [ 'Talk:' ],
343 [ 'Category: ' ],
344 [ 'Category: #bar' ]
345 ];
346 }
347
348 /**
349 * @dataProvider provideParseTitle_invalid
350 */
351 public function testParseTitle_invalid( $text ) {
352 $this->setExpectedException( 'MalformedTitleException' );
353
354 $codec = $this->makeCodec( 'en' );
355 $codec->parseTitle( $text, NS_MAIN );
356 }
357
358 public static function provideGetNamespaceName() {
359 return [
360 [ NS_MAIN, 'Foo', 'en', '' ],
361 [ NS_USER, 'Foo', 'en', 'User' ],
362 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
363
364 // getGenderCache() provides a mock that considers first
365 // names ending in "a" to be female.
366 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
367 ];
368 }
369
370 /**
371 * @dataProvider provideGetNamespaceName
372 */
373 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
374 $codec = $this->makeCodec( $lang );
375 $name = $codec->getNamespaceName( $namespace, $text );
376
377 $this->assertEquals( $expected, $name );
378 }
379 }