Fix 'Tags' padding to keep it farther from the edge and document the source of the...
[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 'wgAllowUserJs' => false,
36 'wgDefaultLanguageVariant' => false,
37 'wgMetaNamespace' => 'Project',
38 'wgLocalInterwikis' => [ 'localtestiw' ],
39 'wgCapitalLinks' => true,
40
41 // NOTE: this is why global state is evil.
42 // TODO: refactor access to the interwiki codes so it can be injected.
43 'wgHooks' => [
44 'InterwikiLoadPrefix' => [
45 function ( $prefix, &$data ) {
46 if ( $prefix === 'localtestiw' ) {
47 $data = [ 'iw_url' => 'localtestiw' ];
48 } elseif ( $prefix === 'remotetestiw' ) {
49 $data = [ 'iw_url' => 'remotetestiw' ];
50 }
51 return false;
52 }
53 ]
54 ]
55 ] );
56 $this->setUserLang( 'en' );
57 $this->setContentLang( 'en' );
58 }
59
60 /**
61 * Returns a mock GenderCache that will consider a user "female" if the
62 * first part of the user name ends with "a".
63 *
64 * @return GenderCache
65 */
66 private function getGenderCache() {
67 $genderCache = $this->getMockBuilder( GenderCache::class )
68 ->disableOriginalConstructor()
69 ->getMock();
70
71 $genderCache->expects( $this->any() )
72 ->method( 'getGenderOf' )
73 ->will( $this->returnCallback( function ( $userName ) {
74 return preg_match( '/^[^- _]+a( |_|$)/u', $userName ) ? 'female' : 'male';
75 } ) );
76
77 return $genderCache;
78 }
79
80 protected function makeCodec( $lang ) {
81 $gender = $this->getGenderCache();
82 $lang = Language::factory( $lang );
83 // language object can came from cache, which does not respect test settings
84 $lang->resetNamespaces();
85 return new MediaWikiTitleCodec( $lang, $gender );
86 }
87
88 public static function provideFormat() {
89 return [
90 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo Bar' ],
91 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi Maier#stuff and so on' ],
92 [ false, 'Hansi_Maier', '', '', 'en', 'Hansi Maier' ],
93 [
94 NS_USER_TALK,
95 'hansi__maier',
96 '',
97 '',
98 'en',
99 'User talk:hansi maier',
100 'User talk:Hansi maier'
101 ],
102
103 // getGenderCache() provides a mock that considers first
104 // names ending in "a" to be female.
105 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa Müller' ],
106 [ NS_MAIN, 'FooBar', '', 'remotetestiw', 'en', 'remotetestiw:FooBar' ],
107 // Strip soft hyphen and Unicode directional formatting characters
108 [ NS_MAIN, "Foo\xC2\xAD\xD8\x9C\xE2\x80\x8E\xE2\x80\x8F\xE2\x80\xAA\xE2\x80\xAB" .
109 "\xE2\x80\xAC\xE2\x80\xAD\xE2\x80\xAE\xE2\x81\xA6\xE2\x81\xA7" .
110 "\xE2\x81\xA8\xE2\x81\xA9bar", '', '', 'en',
111 "Foo\xC2\xAD\xD8\x9C\xE2\x80\x8E\xE2\x80\x8F\xE2\x80\xAA\xE2\x80\xAB" .
112 "\xE2\x80\xAC\xE2\x80\xAD\xE2\x80\xAE\xE2\x81\xA6\xE2\x81\xA7" .
113 "\xE2\x81\xA8\xE2\x81\xA9bar", 'Foobar' ],
114 ];
115 }
116
117 /**
118 * @dataProvider provideFormat
119 */
120 public function testFormat( $namespace, $text, $fragment, $interwiki, $lang, $expected,
121 $normalized = null
122 ) {
123 if ( $normalized === null ) {
124 $normalized = $expected;
125 }
126
127 $codec = $this->makeCodec( $lang );
128 $actual = $codec->formatTitle( $namespace, $text, $fragment, $interwiki );
129
130 $this->assertEquals( $expected, $actual, 'formatted' );
131
132 // test round trip
133 $parsed = $codec->parseTitle( $actual, NS_MAIN );
134 $actual2 = $codec->formatTitle(
135 $parsed->getNamespace(),
136 $parsed->getText(),
137 $parsed->getFragment(),
138 $parsed->getInterwiki()
139 );
140
141 $this->assertEquals( $normalized, $actual2, 'normalized after round trip' );
142 }
143
144 public static function provideGetText() {
145 return [
146 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
147 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'Hansi Maier' ],
148 ];
149 }
150
151 /**
152 * @dataProvider provideGetText
153 */
154 public function testGetText( $namespace, $dbkey, $fragment, $lang, $expected ) {
155 $codec = $this->makeCodec( $lang );
156 $title = new TitleValue( $namespace, $dbkey, $fragment );
157
158 $actual = $codec->getText( $title );
159
160 $this->assertEquals( $expected, $actual );
161 }
162
163 public static function provideGetPrefixedText() {
164 return [
165 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
166 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier' ],
167
168 // No capitalization or normalization is applied while formatting!
169 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
170
171 // getGenderCache() provides a mock that considers first
172 // names ending in "a" to be female.
173 [ NS_USER, 'Lisa_Müller', '', 'de', 'Benutzerin:Lisa Müller' ],
174 [ 1000000, 'Invalid_namespace', '', 'en', ':Invalid namespace' ],
175 ];
176 }
177
178 /**
179 * @dataProvider provideGetPrefixedText
180 */
181 public function testGetPrefixedText( $namespace, $dbkey, $fragment, $lang, $expected ) {
182 $codec = $this->makeCodec( $lang );
183 $title = new TitleValue( $namespace, $dbkey, $fragment );
184
185 $actual = $codec->getPrefixedText( $title );
186
187 $this->assertEquals( $expected, $actual );
188 }
189
190 public static function provideGetPrefixedDBkey() {
191 return [
192 [ NS_MAIN, 'Foo_Bar', '', '', 'en', 'Foo_Bar' ],
193 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', '', 'en', 'User:Hansi_Maier' ],
194
195 // No capitalization or normalization is applied while formatting!
196 [ NS_USER_TALK, 'hansi__maier', '', '', 'en', 'User_talk:hansi__maier' ],
197
198 // getGenderCache() provides a mock that considers first
199 // names ending in "a" to be female.
200 [ NS_USER, 'Lisa_Müller', '', '', 'de', 'Benutzerin:Lisa_Müller' ],
201
202 [ NS_MAIN, 'Remote_page', '', 'remotetestiw', 'en', 'remotetestiw:Remote_page' ],
203
204 // non-existent namespace
205 [ 10000000, 'Foobar', '', '', 'en', ':Foobar' ],
206 ];
207 }
208
209 /**
210 * @dataProvider provideGetPrefixedDBkey
211 */
212 public function testGetPrefixedDBkey( $namespace, $dbkey, $fragment,
213 $interwiki, $lang, $expected
214 ) {
215 $codec = $this->makeCodec( $lang );
216 $title = new TitleValue( $namespace, $dbkey, $fragment, $interwiki );
217
218 $actual = $codec->getPrefixedDBkey( $title );
219
220 $this->assertEquals( $expected, $actual );
221 }
222
223 public static function provideGetFullText() {
224 return [
225 [ NS_MAIN, 'Foo_Bar', '', 'en', 'Foo Bar' ],
226 [ NS_USER, 'Hansi_Maier', 'stuff_and_so_on', 'en', 'User:Hansi Maier#stuff and so on' ],
227
228 // No capitalization or normalization is applied while formatting!
229 [ NS_USER_TALK, 'hansi__maier', '', 'en', 'User talk:hansi maier' ],
230 ];
231 }
232
233 /**
234 * @dataProvider provideGetFullText
235 */
236 public function testGetFullText( $namespace, $dbkey, $fragment, $lang, $expected ) {
237 $codec = $this->makeCodec( $lang );
238 $title = new TitleValue( $namespace, $dbkey, $fragment );
239
240 $actual = $codec->getFullText( $title );
241
242 $this->assertEquals( $expected, $actual );
243 }
244
245 public static function provideParseTitle() {
246 // TODO: test capitalization and trimming
247 // TODO: test unicode normalization
248
249 return [
250 [ ' : Hansi_Maier _ ', NS_MAIN, 'en',
251 new TitleValue( NS_MAIN, 'Hansi_Maier', '' ) ],
252 [ 'User:::1', NS_MAIN, 'de',
253 new TitleValue( NS_USER, '0:0:0:0:0:0:0:1', '' ) ],
254 [ ' lisa Müller', NS_USER, 'de',
255 new TitleValue( NS_USER, 'Lisa_Müller', '' ) ],
256 [ 'benutzerin:lisa Müller#stuff', NS_MAIN, 'de',
257 new TitleValue( NS_USER, 'Lisa_Müller', 'stuff' ) ],
258
259 [ ':Category:Quux', NS_MAIN, 'en',
260 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
261 [ 'Category:Quux', NS_MAIN, 'en',
262 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
263 [ 'Category:Quux', NS_CATEGORY, 'en',
264 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
265 [ 'Quux', NS_CATEGORY, 'en',
266 new TitleValue( NS_CATEGORY, 'Quux', '' ) ],
267 [ ':Quux', NS_CATEGORY, 'en',
268 new TitleValue( NS_MAIN, 'Quux', '' ) ],
269
270 // getGenderCache() provides a mock that considers first
271 // names ending in "a" to be female.
272
273 [ 'a b c', NS_MAIN, 'en',
274 new TitleValue( NS_MAIN, 'A_b_c' ) ],
275 [ ' a b c ', NS_MAIN, 'en',
276 new TitleValue( NS_MAIN, 'A_b_c' ) ],
277 [ ' _ Foo __ Bar_ _', NS_MAIN, 'en',
278 new TitleValue( NS_MAIN, 'Foo_Bar' ) ],
279
280 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
281 [ 'Sandbox', NS_MAIN, 'en', ],
282 [ 'A "B"', NS_MAIN, 'en', ],
283 [ 'A \'B\'', NS_MAIN, 'en', ],
284 [ '.com', NS_MAIN, 'en', ],
285 [ '~', NS_MAIN, 'en', ],
286 [ '"', NS_MAIN, 'en', ],
287 [ '\'', NS_MAIN, 'en', ],
288
289 [ 'Talk:Sandbox', NS_MAIN, 'en',
290 new TitleValue( NS_TALK, 'Sandbox' ) ],
291 [ 'Talk:Foo:Sandbox', NS_MAIN, 'en',
292 new TitleValue( NS_TALK, 'Foo:Sandbox' ) ],
293 [ 'File:Example.svg', NS_MAIN, 'en',
294 new TitleValue( NS_FILE, 'Example.svg' ) ],
295 [ 'File_talk:Example.svg', NS_MAIN, 'en',
296 new TitleValue( NS_FILE_TALK, 'Example.svg' ) ],
297 [ 'Foo/.../Sandbox', NS_MAIN, 'en',
298 'Foo/.../Sandbox' ],
299 [ 'Sandbox/...', NS_MAIN, 'en',
300 'Sandbox/...' ],
301 [ 'A~~', NS_MAIN, 'en',
302 'A~~' ],
303 // Length is 256 total, but only title part matters
304 [ 'Category:' . str_repeat( 'x', 248 ), NS_MAIN, 'en',
305 new TitleValue( NS_CATEGORY,
306 'X' . str_repeat( 'x', 247 ) ) ],
307 [ str_repeat( 'x', 252 ), NS_MAIN, 'en',
308 'X' . str_repeat( 'x', 251 ) ]
309 ];
310 }
311
312 /**
313 * @dataProvider provideParseTitle
314 */
315 public function testParseTitle( $text, $ns, $lang, $title = null ) {
316 if ( $title === null ) {
317 $title = str_replace( ' ', '_', trim( $text ) );
318 }
319
320 if ( is_string( $title ) ) {
321 $title = new TitleValue( NS_MAIN, $title, '' );
322 }
323
324 $codec = $this->makeCodec( $lang );
325 $actual = $codec->parseTitle( $text, $ns );
326
327 $this->assertEquals( $title, $actual );
328 }
329
330 public static function provideParseTitle_invalid() {
331 // TODO: test unicode errors
332
333 return [
334 [ '#' ],
335 [ '::' ],
336 [ '::xx' ],
337 [ '::##' ],
338 [ ' :: x' ],
339
340 [ 'Talk:File:Foo.jpg' ],
341 [ 'Talk:localtestiw:Foo' ],
342 [ '::1' ], // only valid in user namespace
343 [ 'User::x' ], // leading ":" in a user name is only valid of IPv6 addresses
344
345 // NOTE: cases copied from TitleTest::testSecureAndSplit. Keep in sync.
346 [ '' ],
347 [ ':' ],
348 [ '__ __' ],
349 [ ' __ ' ],
350 // Bad characters forbidden regardless of wgLegalTitleChars
351 [ 'A [ B' ],
352 [ 'A ] B' ],
353 [ 'A { B' ],
354 [ 'A } B' ],
355 [ 'A < B' ],
356 [ 'A > B' ],
357 [ 'A | B' ],
358 // URL encoding
359 [ 'A%20B' ],
360 [ 'A%23B' ],
361 [ 'A%2523B' ],
362 // XML/HTML character entity references
363 // Note: Commented out because they are not marked invalid by the PHP test as
364 // Title::newFromText runs Sanitizer::decodeCharReferencesAndNormalize first.
365 // [ 'A &eacute; B' ],
366 // [ 'A &#233; B' ],
367 // [ 'A &#x00E9; B' ],
368 // Subject of NS_TALK does not roundtrip to NS_MAIN
369 [ 'Talk:File:Example.svg' ],
370 // Directory navigation
371 [ '.' ],
372 [ '..' ],
373 [ './Sandbox' ],
374 [ '../Sandbox' ],
375 [ 'Foo/./Sandbox' ],
376 [ 'Foo/../Sandbox' ],
377 [ 'Sandbox/.' ],
378 [ 'Sandbox/..' ],
379 // Tilde
380 [ 'A ~~~ Name' ],
381 [ 'A ~~~~ Signature' ],
382 [ 'A ~~~~~ Timestamp' ],
383 [ str_repeat( 'x', 256 ) ],
384 // Namespace prefix without actual title
385 [ 'Talk:' ],
386 [ 'Category: ' ],
387 [ 'Category: #bar' ]
388 ];
389 }
390
391 /**
392 * @dataProvider provideParseTitle_invalid
393 */
394 public function testParseTitle_invalid( $text ) {
395 $this->setExpectedException( MalformedTitleException::class );
396
397 $codec = $this->makeCodec( 'en' );
398 $codec->parseTitle( $text, NS_MAIN );
399 }
400
401 public static function provideGetNamespaceName() {
402 return [
403 [ NS_MAIN, 'Foo', 'en', '' ],
404 [ NS_USER, 'Foo', 'en', 'User' ],
405 [ NS_USER, 'Hansi Maier', 'de', 'Benutzer' ],
406
407 // getGenderCache() provides a mock that considers first
408 // names ending in "a" to be female.
409 [ NS_USER, 'Lisa Müller', 'de', 'Benutzerin' ],
410 ];
411 }
412
413 /**
414 * @dataProvider provideGetNamespaceName
415 */
416 public function testGetNamespaceName( $namespace, $text, $lang, $expected ) {
417 $codec = $this->makeCodec( $lang );
418 $name = $codec->getNamespaceName( $namespace, $text );
419
420 $this->assertEquals( $expected, $name );
421 }
422 }