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