Merge "Mention $wgRateLimitsExcludedIPs semantics change in release notes"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
1 ( function ( mw, $ ) {
2 var repeat = function ( input, multiplier ) {
3 return new Array( multiplier + 1 ).join( input );
4 },
5 cases = {
6 // See also TitleTest.php#testSecureAndSplit
7 valid: [
8 'Sandbox',
9 'A "B"',
10 'A \'B\'',
11 '.com',
12 '~',
13 '"',
14 '\'',
15 'Talk:Sandbox',
16 'Talk:Foo:Sandbox',
17 'File:Example.svg',
18 'File_talk:Example.svg',
19 'Foo/.../Sandbox',
20 'Sandbox/...',
21 'A~~',
22 ':A',
23 // Length is 256 total, but only title part matters
24 'Category:' + repeat( 'x', 248 ),
25 repeat( 'x', 252 )
26 ],
27 invalid: [
28 '',
29 ':',
30 '__ __',
31 ' __ ',
32 // Bad characters forbidden regardless of wgLegalTitleChars
33 'A [ B',
34 'A ] B',
35 'A { B',
36 'A } B',
37 'A < B',
38 'A > B',
39 'A | B',
40 'A \t B',
41 'A \n B',
42 // URL encoding
43 'A%20B',
44 'A%23B',
45 'A%2523B',
46 // XML/HTML character entity references
47 // Note: The ones with # are commented out as those are interpreted as fragment and
48 // as such end up being valid.
49 'A &eacute; B',
50 // 'A &#233; B',
51 // 'A &#x00E9; B',
52 // Subject of NS_TALK does not roundtrip to NS_MAIN
53 'Talk:File:Example.svg',
54 // Directory navigation
55 '.',
56 '..',
57 './Sandbox',
58 '../Sandbox',
59 'Foo/./Sandbox',
60 'Foo/../Sandbox',
61 'Sandbox/.',
62 'Sandbox/..',
63 // Tilde
64 'A ~~~ Name',
65 'A ~~~~ Signature',
66 'A ~~~~~ Timestamp',
67 repeat( 'x', 256 ),
68 // Extension separation is a js invention, for length
69 // purposes it is part of the title
70 repeat( 'x', 252 ) + '.json',
71 // Namespace prefix without actual title
72 'Talk:',
73 'Category: ',
74 'Category: #bar'
75 ]
76 };
77
78 QUnit.module( 'mediawiki.Title', QUnit.newMwEnvironment( {
79 // mw.Title relies on these three config vars
80 // Restore them after each test run
81 config: {
82 wgFormattedNamespaces: {
83 '-2': 'Media',
84 '-1': 'Special',
85 0: '',
86 1: 'Talk',
87 2: 'User',
88 3: 'User talk',
89 4: 'Wikipedia',
90 5: 'Wikipedia talk',
91 6: 'File',
92 7: 'File talk',
93 8: 'MediaWiki',
94 9: 'MediaWiki talk',
95 10: 'Template',
96 11: 'Template talk',
97 12: 'Help',
98 13: 'Help talk',
99 14: 'Category',
100 15: 'Category talk',
101 // testing custom / localized namespace
102 100: 'Penguins'
103 },
104 // jscs: disable requireCamelCaseOrUpperCaseIdentifiers
105 wgNamespaceIds: {
106 media: -2,
107 special: -1,
108 '': 0,
109 talk: 1,
110 user: 2,
111 user_talk: 3,
112 wikipedia: 4,
113 wikipedia_talk: 5,
114 file: 6,
115 file_talk: 7,
116 mediawiki: 8,
117 mediawiki_talk: 9,
118 template: 10,
119 template_talk: 11,
120 help: 12,
121 help_talk: 13,
122 category: 14,
123 category_talk: 15,
124 image: 6,
125 image_talk: 7,
126 project: 4,
127 project_talk: 5,
128 // Testing custom namespaces and aliases
129 penguins: 100,
130 antarctic_waterfowl: 100
131 },
132 // jscs: enable requireCamelCaseOrUpperCaseIdentifiers
133 wgCaseSensitiveNamespaces: []
134 }
135 } ) );
136
137 QUnit.test( 'constructor', cases.invalid.length, function ( assert ) {
138 var i, title;
139 for ( i = 0; i < cases.valid.length; i++ ) {
140 title = new mw.Title( cases.valid[ i ] );
141 }
142 for ( i = 0; i < cases.invalid.length; i++ ) {
143 /*jshint loopfunc:true */
144 title = cases.invalid[ i ];
145 assert.throws( function () {
146 return new mw.Title( title );
147 }, cases.invalid[ i ] );
148 }
149 } );
150
151 QUnit.test( 'newFromText', cases.valid.length + cases.invalid.length, function ( assert ) {
152 var i;
153 for ( i = 0; i < cases.valid.length; i++ ) {
154 assert.equal(
155 $.type( mw.Title.newFromText( cases.valid[ i ] ) ),
156 'object',
157 cases.valid[ i ]
158 );
159 }
160 for ( i = 0; i < cases.invalid.length; i++ ) {
161 assert.equal(
162 $.type( mw.Title.newFromText( cases.invalid[ i ] ) ),
163 'null',
164 cases.invalid[ i ]
165 );
166 }
167 } );
168
169 QUnit.test( 'makeTitle', 6, function ( assert ) {
170 var cases, i, title, expected,
171 NS_MAIN = 0,
172 NS_TALK = 1,
173 NS_TEMPLATE = 10;
174
175 cases = [
176 [ NS_TEMPLATE, 'Foo', 'Template:Foo' ],
177 [ NS_TEMPLATE, 'Category:Foo', 'Template:Category:Foo' ],
178 [ NS_TEMPLATE, 'Template:Foo', 'Template:Template:Foo' ],
179 [ NS_TALK, 'Help:Foo', null ],
180 [ NS_TEMPLATE, '<', null ],
181 [ NS_MAIN, 'Help:Foo', 'Help:Foo' ]
182 ];
183
184 for ( i = 0; i < cases.length; i++ ) {
185 title = mw.Title.makeTitle( cases[ i ][ 0 ], cases[ i ][ 1 ] );
186 expected = cases[ i ][ 2 ];
187 if ( expected === null ) {
188 assert.strictEqual( title, expected );
189 } else {
190 assert.strictEqual( title.getPrefixedText(), expected );
191 }
192 }
193 } );
194
195 QUnit.test( 'Basic parsing', 21, function ( assert ) {
196 var title;
197 title = new mw.Title( 'File:Foo_bar.JPG' );
198
199 assert.equal( title.getNamespaceId(), 6 );
200 assert.equal( title.getNamespacePrefix(), 'File:' );
201 assert.equal( title.getName(), 'Foo_bar' );
202 assert.equal( title.getNameText(), 'Foo bar' );
203 assert.equal( title.getExtension(), 'JPG' );
204 assert.equal( title.getDotExtension(), '.JPG' );
205 assert.equal( title.getMain(), 'Foo_bar.JPG' );
206 assert.equal( title.getMainText(), 'Foo bar.JPG' );
207 assert.equal( title.getPrefixedDb(), 'File:Foo_bar.JPG' );
208 assert.equal( title.getPrefixedText(), 'File:Foo bar.JPG' );
209
210 title = new mw.Title( 'Foo#bar' );
211 assert.equal( title.getPrefixedText(), 'Foo' );
212 assert.equal( title.getFragment(), 'bar' );
213
214 title = new mw.Title( '.foo' );
215 assert.equal( title.getPrefixedText(), '.foo' );
216 assert.equal( title.getName(), '' );
217 assert.equal( title.getNameText(), '' );
218 assert.equal( title.getExtension(), 'foo' );
219 assert.equal( title.getDotExtension(), '.foo' );
220 assert.equal( title.getMain(), '.foo' );
221 assert.equal( title.getMainText(), '.foo' );
222 assert.equal( title.getPrefixedDb(), '.foo' );
223 assert.equal( title.getPrefixedText(), '.foo' );
224 } );
225
226 QUnit.test( 'Transformation', 12, function ( assert ) {
227 var title;
228
229 title = new mw.Title( 'File:quux pif.jpg' );
230 assert.equal( title.getNameText(), 'Quux pif', 'First character of title' );
231
232 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
233 assert.equal( title.getNameText(), 'Glarg foo glang', 'Underscores' );
234
235 title = new mw.Title( 'User:ABC.DEF' );
236 assert.equal( title.toText(), 'User:ABC.DEF', 'Round trip text' );
237 assert.equal( title.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
238
239 title = new mw.Title( 'Image:quux pix.jpg' );
240 assert.equal( title.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
241
242 title = new mw.Title( 'uSEr:hAshAr' );
243 assert.equal( title.toText(), 'User:HAshAr' );
244 assert.equal( title.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
245
246 title = new mw.Title( 'Foo \u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000 bar' );
247 assert.equal( title.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
248
249 title = new mw.Title( 'Foo\u200E\u200F\u202A\u202B\u202C\u202D\u202Ebar' );
250 assert.equal( title.getMain(), 'Foobar', 'Strip Unicode bidi override characters' );
251
252 // Regression test: Previously it would only detect an extension if there is no space after it
253 title = new mw.Title( 'Example.js ' );
254 assert.equal( title.getExtension(), 'js', 'Space after an extension is stripped' );
255
256 title = new mw.Title( 'Example#foo' );
257 assert.equal( title.getFragment(), 'foo', 'Fragment' );
258
259 title = new mw.Title( 'Example#_foo_bar baz_' );
260 assert.equal( title.getFragment(), ' foo bar baz', 'Fragment' );
261 } );
262
263 QUnit.test( 'Namespace detection and conversion', 10, function ( assert ) {
264 var title;
265
266 title = new mw.Title( 'File:User:Example' );
267 assert.equal( title.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
268
269 title = new mw.Title( 'Example', 6 );
270 assert.equal( title.getNamespaceId(), 6, 'Default namespace passed is used' );
271
272 title = new mw.Title( 'User:Example', 6 );
273 assert.equal( title.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
274
275 title = new mw.Title( ':Example', 6 );
276 assert.equal( title.getNamespaceId(), 0, 'Colon forces main namespace' );
277
278 title = new mw.Title( 'something.PDF', 6 );
279 assert.equal( title.toString(), 'File:Something.PDF' );
280
281 title = new mw.Title( 'NeilK', 3 );
282 assert.equal( title.toString(), 'User_talk:NeilK' );
283 assert.equal( title.toText(), 'User talk:NeilK' );
284
285 title = new mw.Title( 'Frobisher', 100 );
286 assert.equal( title.toString(), 'Penguins:Frobisher' );
287
288 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
289 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
290
291 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
292 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
293 } );
294
295 QUnit.test( 'Throw error on invalid title', 1, function ( assert ) {
296 assert.throws( function () {
297 return new mw.Title( '' );
298 }, 'Throw error on empty string' );
299 } );
300
301 QUnit.test( 'Case-sensivity', 5, function ( assert ) {
302 var title;
303
304 // Default config
305 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
306
307 title = new mw.Title( 'article' );
308 assert.equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
309
310 title = new mw.Title( 'ß' );
311 assert.equal( title.toString(), 'ß', 'Uppercasing matches PHP behaviour (ß -> ß, not SS)' );
312
313 title = new mw.Title( 'dž (digraph)' );
314 assert.equal( title.toString(), 'Dž_(digraph)', 'Uppercasing matches PHP behaviour (dž -> Dž, not DŽ)' );
315
316 // $wgCapitalLinks = false;
317 mw.config.set( 'wgCaseSensitiveNamespaces', [ 0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15 ] );
318
319 title = new mw.Title( 'article' );
320 assert.equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
321
322 title = new mw.Title( 'john', 2 );
323 assert.equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
324 } );
325
326 QUnit.test( 'toString / toText', 2, function ( assert ) {
327 var title = new mw.Title( 'Some random page' );
328
329 assert.equal( title.toString(), title.getPrefixedDb() );
330 assert.equal( title.toText(), title.getPrefixedText() );
331 } );
332
333 QUnit.test( 'getExtension', 7, function ( assert ) {
334 function extTest( pagename, ext, description ) {
335 var title = new mw.Title( pagename );
336 assert.equal( title.getExtension(), ext, description || pagename );
337 }
338
339 extTest( 'MediaWiki:Vector.js', 'js' );
340 extTest( 'User:Example/common.css', 'css' );
341 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (T38151)' );
342 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
343 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
344 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
345 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
346
347 // @broken: Throws an exception
348 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
349 } );
350
351 QUnit.test( 'exists', 3, function ( assert ) {
352 var title;
353
354 // Empty registry, checks default to null
355
356 title = new mw.Title( 'Some random page', 4 );
357 assert.strictEqual( title.exists(), null, 'Return null with empty existance registry' );
358
359 // Basic registry, checks default to boolean
360 mw.Title.exist.set( [ 'Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules' ], true );
361 mw.Title.exist.set( [ 'Does_not_exist', 'User:John', 'Foobar' ], false );
362
363 title = new mw.Title( 'Project:Sandbox rules' );
364 assert.assertTrue( title.exists(), 'Return true for page titles marked as existing' );
365 title = new mw.Title( 'Foobar' );
366 assert.assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
367
368 } );
369
370 QUnit.test( 'getUrl', 4, function ( assert ) {
371 var title;
372 mw.config.set( {
373 wgScript: '/w/index.php',
374 wgArticlePath: '/wiki/$1'
375 } );
376
377 title = new mw.Title( 'Foobar' );
378 assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
379 assert.equal( title.getUrl( { action: 'edit' } ), '/w/index.php?title=Foobar&action=edit', 'Basic functionality, \'params\' parameter' );
380
381 title = new mw.Title( 'John Doe', 3 );
382 assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
383
384 title = new mw.Title( 'John Cena#And_His_Name_Is', 3 );
385 assert.equal( title.getUrl( { meme: true } ), '/w/index.php?title=User_talk:John_Cena&meme=true#And_His_Name_Is', 'title with fragment and query parameter' );
386 } );
387
388 QUnit.test( 'newFromImg', 44, function ( assert ) {
389 var title, i, thisCase, prefix,
390 cases = [
391 {
392 url: '//upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Princess_Alexandra_of_Denmark_%28later_Queen_Alexandra%2C_wife_of_Edward_VII%29_with_her_two_eldest_sons%2C_Prince_Albert_Victor_%28Eddy%29_and_George_Frederick_Ernest_Albert_%28later_George_V%29.jpg/939px-thumbnail.jpg',
393 typeOfUrl: 'Hashed thumb with shortened path',
394 nameText: 'Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V)',
395 prefixedText: 'File:Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V).jpg'
396 },
397
398 {
399 url: '//upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Princess_Alexandra_of_Denmark_%28later_Queen_Alexandra%2C_wife_of_Edward_VII%29_with_her_two_eldest_sons%2C_Prince_Albert_Victor_%28Eddy%29_and_George_Frederick_Ernest_Albert_%28later_George_V%29.jpg/939px-ki708pr1r6g2dl5lbhvwdqxenhait13.jpg',
400 typeOfUrl: 'Hashed thumb with sha1-ed path',
401 nameText: 'Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V)',
402 prefixedText: 'File:Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V).jpg'
403 },
404
405 {
406 url: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
407 typeOfUrl: 'Normal hashed directory thumbnail',
408 nameText: 'Anticlockwise heliotrope\'s',
409 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
410 },
411
412 {
413 url: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
414 typeOfUrl: 'Normal hashed directory thumbnail with complex thumbnail parameters',
415 nameText: 'Wikipedia-logo-v2',
416 prefixedText: 'File:Wikipedia-logo-v2.svg'
417 },
418
419 {
420 url: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
421 typeOfUrl: 'Commons thumbnail',
422 nameText: 'Wikipedia-logo-v2',
423 prefixedText: 'File:Wikipedia-logo-v2.svg'
424 },
425
426 {
427 url: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
428 typeOfUrl: 'Full image',
429 nameText: 'Anticlockwise heliotrope\'s',
430 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
431 },
432
433 {
434 url: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
435 typeOfUrl: 'thumb.php-based thumbnail',
436 nameText: 'Stuffless Figaro\'s',
437 prefixedText: 'File:Stuffless Figaro\'s.jpg'
438 },
439
440 {
441 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
442 typeOfUrl: 'Commons unhashed thumbnail',
443 nameText: 'Wikipedia-logo-v2',
444 prefixedText: 'File:Wikipedia-logo-v2.svg'
445 },
446
447 {
448 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
449 typeOfUrl: 'Commons unhashed thumbnail with complex thumbnail parameters',
450 nameText: 'Wikipedia-logo-v2',
451 prefixedText: 'File:Wikipedia-logo-v2.svg'
452 },
453
454 {
455 url: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
456 typeOfUrl: 'Unhashed local file',
457 nameText: 'Anticlockwise heliotrope\'s',
458 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
459 },
460
461 {
462 url: '',
463 typeOfUrl: 'Empty string'
464 },
465
466 {
467 url: 'foo',
468 typeOfUrl: 'String with only alphabet characters'
469 },
470
471 {
472 url: 'foobar.foobar',
473 typeOfUrl: 'Not a file path'
474 },
475
476 {
477 url: '/a/a0/blah blah blah',
478 typeOfUrl: 'Space characters'
479 }
480 ];
481
482 for ( i = 0; i < cases.length; i++ ) {
483 thisCase = cases[ i ];
484 title = mw.Title.newFromImg( { src: thisCase.url } );
485
486 if ( thisCase.nameText !== undefined ) {
487 prefix = '[' + thisCase.typeOfUrl + ' URL' + '] ';
488
489 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
490 assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
491 assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
492 assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
493 } else {
494 assert.strictEqual( title, null, thisCase.typeOfUrl + ', should not produce an mw.Title object' );
495 }
496 }
497 } );
498
499 QUnit.test( 'getRelativeText', 5, function ( assert ) {
500 var i, thisCase, title,
501 cases = [
502 {
503 text: 'asd',
504 relativeTo: 123,
505 expectedResult: ':Asd'
506 },
507 {
508 text: 'dfg',
509 relativeTo: 0,
510 expectedResult: 'Dfg'
511 },
512 {
513 text: 'Template:Ghj',
514 relativeTo: 0,
515 expectedResult: 'Template:Ghj'
516 },
517 {
518 text: 'Template:1',
519 relativeTo: 10,
520 expectedResult: '1'
521 },
522 {
523 text: 'User:Hi',
524 relativeTo: 10,
525 expectedResult: 'User:Hi'
526 }
527 ];
528
529 for ( i = 0; i < cases.length; i++ ) {
530 thisCase = cases[ i ];
531
532 title = mw.Title.newFromText( thisCase.text );
533 assert.equal( title.getRelativeText( thisCase.relativeTo ), thisCase.expectedResult );
534 }
535 } );
536
537 QUnit.test( 'normalizeExtension', 5, function ( assert ) {
538 var extension, i, thisCase, prefix,
539 cases = [
540 {
541 extension: 'png',
542 expected: 'png',
543 description: 'Extension already in canonical form'
544 },
545 {
546 extension: 'PNG',
547 expected: 'png',
548 description: 'Extension lowercased in canonical form'
549 },
550 {
551 extension: 'jpeg',
552 expected: 'jpg',
553 description: 'Extension changed in canonical form'
554 },
555 {
556 extension: 'JPEG',
557 expected: 'jpg',
558 description: 'Extension lowercased and changed in canonical form'
559 },
560 {
561 extension: '~~~',
562 expected: '',
563 description: 'Extension invalid and discarded'
564 }
565 ];
566
567 for ( i = 0; i < cases.length; i++ ) {
568 thisCase = cases[ i ];
569 extension = mw.Title.normalizeExtension( thisCase.extension );
570
571 prefix = '[' + thisCase.description + '] ';
572 assert.equal( extension, thisCase.expected, prefix + 'Extension as expected' );
573 }
574 } );
575
576 QUnit.test( 'newFromUserInput', 12, function ( assert ) {
577 var title, i, thisCase, prefix,
578 cases = [
579 {
580 title: 'DCS0001557854455.JPG',
581 expected: 'DCS0001557854455.JPG',
582 description: 'Title in normal namespace without anything invalid but with "file extension"'
583 },
584 {
585 title: 'MediaWiki:Msg-awesome',
586 expected: 'MediaWiki:Msg-awesome',
587 description: 'Full title (page in MediaWiki namespace) supplied as string'
588 },
589 {
590 title: 'The/Mw/Sound.flac',
591 defaultNamespace: -2,
592 expected: 'Media:The-Mw-Sound.flac',
593 description: 'Page in Media-namespace without explicit options'
594 },
595 {
596 title: 'File:The/Mw/Sound.kml',
597 defaultNamespace: 6,
598 options: {
599 forUploading: false
600 },
601 expected: 'File:The/Mw/Sound.kml',
602 description: 'Page in File-namespace without explicit options'
603 },
604 {
605 title: 'File:Foo.JPEG',
606 expected: 'File:Foo.JPEG',
607 description: 'Page in File-namespace with non-canonical extension'
608 },
609 {
610 title: 'File:Foo.JPEG ',
611 expected: 'File:Foo.JPEG',
612 description: 'Page in File-namespace with trailing whitespace'
613 }
614 ];
615
616 for ( i = 0; i < cases.length; i++ ) {
617 thisCase = cases[ i ];
618 title = mw.Title.newFromUserInput( thisCase.title, thisCase.defaultNamespace, thisCase.options );
619
620 if ( thisCase.expected !== undefined ) {
621 prefix = '[' + thisCase.description + '] ';
622
623 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
624 assert.equal( title.toText(), thisCase.expected, prefix + 'Title as expected' );
625 } else {
626 assert.strictEqual( title, null, thisCase.description + ', should not produce an mw.Title object' );
627 }
628 }
629 } );
630
631 QUnit.test( 'newFromFileName', 54, function ( assert ) {
632 var title, i, thisCase, prefix,
633 cases = [
634 {
635 fileName: 'DCS0001557854455.JPG',
636 typeOfName: 'Standard camera output',
637 nameText: 'DCS0001557854455',
638 prefixedText: 'File:DCS0001557854455.JPG'
639 },
640 {
641 fileName: 'File:Sample.png',
642 typeOfName: 'Carrying namespace',
643 nameText: 'File-Sample',
644 prefixedText: 'File:File-Sample.png'
645 },
646 {
647 fileName: 'Treppe 2222 Test upload.jpg',
648 typeOfName: 'File name with spaces in it and lower case file extension',
649 nameText: 'Treppe 2222 Test upload',
650 prefixedText: 'File:Treppe 2222 Test upload.jpg'
651 },
652 {
653 fileName: 'I contain a \ttab.jpg',
654 typeOfName: 'Name containing a tab character',
655 nameText: 'I contain a tab',
656 prefixedText: 'File:I contain a tab.jpg'
657 },
658 {
659 fileName: 'I_contain multiple__ ___ _underscores.jpg',
660 typeOfName: 'Name containing multiple underscores',
661 nameText: 'I contain multiple underscores',
662 prefixedText: 'File:I contain multiple underscores.jpg'
663 },
664 {
665 fileName: 'I like ~~~~~~~~es.jpg',
666 typeOfName: 'Name containing more than three consecutive tilde characters',
667 nameText: 'I like ~~es',
668 prefixedText: 'File:I like ~~es.jpg'
669 },
670 {
671 fileName: 'BI\u200EDI.jpg',
672 typeOfName: 'Name containing BIDI overrides',
673 nameText: 'BIDI',
674 prefixedText: 'File:BIDI.jpg'
675 },
676 {
677 fileName: '100%ab progress.jpg',
678 typeOfName: 'File name with URL encoding',
679 nameText: '100% ab progress',
680 prefixedText: 'File:100% ab progress.jpg'
681 },
682 {
683 fileName: '<([>]):/#.jpg',
684 typeOfName: 'File name with characters not permitted in titles that are replaced',
685 nameText: '((()))---',
686 prefixedText: 'File:((()))---.jpg'
687 },
688 {
689 fileName: 'spaces\u0009\u2000\u200A\u200Bx.djvu',
690 typeOfName: 'File name with different kind of spaces',
691 nameText: 'Spaces \u200Bx',
692 prefixedText: 'File:Spaces \u200Bx.djvu'
693 },
694 {
695 fileName: 'dot.dot.dot.dot.dotdot',
696 typeOfName: 'File name with a lot of dots',
697 nameText: 'Dot.dot.dot.dot',
698 prefixedText: 'File:Dot.dot.dot.dot.dotdot'
699 },
700 {
701 fileName: 'dot. dot ._dot',
702 typeOfName: 'File name with multiple dots and spaces',
703 nameText: 'Dot. dot',
704 prefixedText: 'File:Dot. dot. dot'
705 },
706 {
707 fileName: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂.png',
708 typeOfName: 'File name longer than 240 bytes',
709 nameText: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵',
710 prefixedText: 'File:𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵.png'
711 },
712 {
713 fileName: '',
714 typeOfName: 'Empty string'
715 },
716 {
717 fileName: 'foo',
718 typeOfName: 'String with only alphabet characters'
719 }
720 ];
721
722 for ( i = 0; i < cases.length; i++ ) {
723 thisCase = cases[ i ];
724 title = mw.Title.newFromFileName( thisCase.fileName );
725
726 if ( thisCase.nameText !== undefined ) {
727 prefix = '[' + thisCase.typeOfName + '] ';
728
729 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
730 assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
731 assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
732 assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
733 } else {
734 assert.strictEqual( title, null, thisCase.typeOfName + ', should not produce an mw.Title object' );
735 }
736 }
737 } );
738
739 }( mediaWiki, jQuery ) );