Add API warnings when upload is same as older versions
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
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 };
76
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 } ) );
135
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 } );
149
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 } );
167
168 QUnit.test( 'makeTitle', 6, function ( assert ) {
169 var cases, i, title, expected,
170 NS_MAIN = 0,
171 NS_TALK = 1,
172 NS_TEMPLATE = 10;
173
174 cases = [
175 [ NS_TEMPLATE, 'Foo', 'Template:Foo' ],
176 [ NS_TEMPLATE, 'Category:Foo', 'Template:Category:Foo' ],
177 [ NS_TEMPLATE, 'Template:Foo', 'Template:Template:Foo' ],
178 [ NS_TALK, 'Help:Foo', null ],
179 [ NS_TEMPLATE, '<', null ],
180 [ NS_MAIN, 'Help:Foo', 'Help:Foo' ]
181 ];
182
183 for ( i = 0; i < cases.length; i++ ) {
184 title = mw.Title.makeTitle( cases[ i ][ 0 ], cases[ i ][ 1 ] );
185 expected = cases[ i ][ 2 ];
186 if ( expected === null ) {
187 assert.strictEqual( title, expected );
188 } else {
189 assert.strictEqual( title.getPrefixedText(), expected );
190 }
191 }
192 } );
193
194 QUnit.test( 'Basic parsing', 21, function ( assert ) {
195 var title;
196 title = new mw.Title( 'File:Foo_bar.JPG' );
197
198 assert.equal( title.getNamespaceId(), 6 );
199 assert.equal( title.getNamespacePrefix(), 'File:' );
200 assert.equal( title.getName(), 'Foo_bar' );
201 assert.equal( title.getNameText(), 'Foo bar' );
202 assert.equal( title.getExtension(), 'JPG' );
203 assert.equal( title.getDotExtension(), '.JPG' );
204 assert.equal( title.getMain(), 'Foo_bar.JPG' );
205 assert.equal( title.getMainText(), 'Foo bar.JPG' );
206 assert.equal( title.getPrefixedDb(), 'File:Foo_bar.JPG' );
207 assert.equal( title.getPrefixedText(), 'File:Foo bar.JPG' );
208
209 title = new mw.Title( 'Foo#bar' );
210 assert.equal( title.getPrefixedText(), 'Foo' );
211 assert.equal( title.getFragment(), 'bar' );
212
213 title = new mw.Title( '.foo' );
214 assert.equal( title.getPrefixedText(), '.foo' );
215 assert.equal( title.getName(), '' );
216 assert.equal( title.getNameText(), '' );
217 assert.equal( title.getExtension(), 'foo' );
218 assert.equal( title.getDotExtension(), '.foo' );
219 assert.equal( title.getMain(), '.foo' );
220 assert.equal( title.getMainText(), '.foo' );
221 assert.equal( title.getPrefixedDb(), '.foo' );
222 assert.equal( title.getPrefixedText(), '.foo' );
223 } );
224
225 QUnit.test( 'Transformation', 11, function ( assert ) {
226 var title;
227
228 title = new mw.Title( 'File:quux pif.jpg' );
229 assert.equal( title.getNameText(), 'Quux pif', 'First character of title' );
230
231 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
232 assert.equal( title.getNameText(), 'Glarg foo glang', 'Underscores' );
233
234 title = new mw.Title( 'User:ABC.DEF' );
235 assert.equal( title.toText(), 'User:ABC.DEF', 'Round trip text' );
236 assert.equal( title.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
237
238 title = new mw.Title( 'Image:quux pix.jpg' );
239 assert.equal( title.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
240
241 title = new mw.Title( 'uSEr:hAshAr' );
242 assert.equal( title.toText(), 'User:HAshAr' );
243 assert.equal( title.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
244
245 // Don't ask why, it's the way the backend works. One space is kept of each set.
246 title = new mw.Title( 'Foo __ \t __ bar' );
247 assert.equal( title.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
248
249 // Regression test: Previously it would only detect an extension if there is no space after it
250 title = new mw.Title( 'Example.js ' );
251 assert.equal( title.getExtension(), 'js', 'Space after an extension is stripped' );
252
253 title = new mw.Title( 'Example#foo' );
254 assert.equal( title.getFragment(), 'foo', 'Fragment' );
255
256 title = new mw.Title( 'Example#_foo_bar baz_' );
257 assert.equal( title.getFragment(), ' foo bar baz', 'Fragment' );
258 } );
259
260 QUnit.test( 'Namespace detection and conversion', 10, function ( assert ) {
261 var title;
262
263 title = new mw.Title( 'File:User:Example' );
264 assert.equal( title.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
265
266 title = new mw.Title( 'Example', 6 );
267 assert.equal( title.getNamespaceId(), 6, 'Default namespace passed is used' );
268
269 title = new mw.Title( 'User:Example', 6 );
270 assert.equal( title.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
271
272 title = new mw.Title( ':Example', 6 );
273 assert.equal( title.getNamespaceId(), 0, 'Colon forces main namespace' );
274
275 title = new mw.Title( 'something.PDF', 6 );
276 assert.equal( title.toString(), 'File:Something.PDF' );
277
278 title = new mw.Title( 'NeilK', 3 );
279 assert.equal( title.toString(), 'User_talk:NeilK' );
280 assert.equal( title.toText(), 'User talk:NeilK' );
281
282 title = new mw.Title( 'Frobisher', 100 );
283 assert.equal( title.toString(), 'Penguins:Frobisher' );
284
285 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
286 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
287
288 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
289 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
290 } );
291
292 QUnit.test( 'Throw error on invalid title', 1, function ( assert ) {
293 assert.throws( function () {
294 return new mw.Title( '' );
295 }, 'Throw error on empty string' );
296 } );
297
298 QUnit.test( 'Case-sensivity', 3, function ( assert ) {
299 var title;
300
301 // Default config
302 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
303
304 title = new mw.Title( 'article' );
305 assert.equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
306
307 // $wgCapitalLinks = false;
308 mw.config.set( 'wgCaseSensitiveNamespaces', [ 0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15 ] );
309
310 title = new mw.Title( 'article' );
311 assert.equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
312
313 title = new mw.Title( 'john', 2 );
314 assert.equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
315 } );
316
317 QUnit.test( 'toString / toText', 2, function ( assert ) {
318 var title = new mw.Title( 'Some random page' );
319
320 assert.equal( title.toString(), title.getPrefixedDb() );
321 assert.equal( title.toText(), title.getPrefixedText() );
322 } );
323
324 QUnit.test( 'getExtension', 7, function ( assert ) {
325 function extTest( pagename, ext, description ) {
326 var title = new mw.Title( pagename );
327 assert.equal( title.getExtension(), ext, description || pagename );
328 }
329
330 extTest( 'MediaWiki:Vector.js', 'js' );
331 extTest( 'User:Example/common.css', 'css' );
332 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
333 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
334 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
335 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
336 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
337
338 // @broken: Throws an exception
339 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
340 } );
341
342 QUnit.test( 'exists', 3, function ( assert ) {
343 var title;
344
345 // Empty registry, checks default to null
346
347 title = new mw.Title( 'Some random page', 4 );
348 assert.strictEqual( title.exists(), null, 'Return null with empty existance registry' );
349
350 // Basic registry, checks default to boolean
351 mw.Title.exist.set( [ 'Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules' ], true );
352 mw.Title.exist.set( [ 'Does_not_exist', 'User:John', 'Foobar' ], false );
353
354 title = new mw.Title( 'Project:Sandbox rules' );
355 assert.assertTrue( title.exists(), 'Return true for page titles marked as existing' );
356 title = new mw.Title( 'Foobar' );
357 assert.assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
358
359 } );
360
361 QUnit.test( 'getUrl', 4, function ( assert ) {
362 var title;
363 mw.config.set( {
364 wgScript: '/w/index.php',
365 wgArticlePath: '/wiki/$1'
366 } );
367
368 title = new mw.Title( 'Foobar' );
369 assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
370 assert.equal( title.getUrl( { action: 'edit' } ), '/w/index.php?title=Foobar&action=edit', 'Basic functionality, \'params\' parameter' );
371
372 title = new mw.Title( 'John Doe', 3 );
373 assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
374
375 title = new mw.Title( 'John Cena#And_His_Name_Is', 3 );
376 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' );
377 } );
378
379 QUnit.test( 'newFromImg', 44, function ( assert ) {
380 var title, i, thisCase, prefix,
381 cases = [
382 {
383 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',
384 typeOfUrl: 'Hashed thumb with shortened path',
385 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)',
386 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'
387 },
388
389 {
390 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',
391 typeOfUrl: 'Hashed thumb with sha1-ed path',
392 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)',
393 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'
394 },
395
396 {
397 url: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
398 typeOfUrl: 'Normal hashed directory thumbnail',
399 nameText: 'Anticlockwise heliotrope\'s',
400 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
401 },
402
403 {
404 url: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
405 typeOfUrl: 'Normal hashed directory thumbnail with complex thumbnail parameters',
406 nameText: 'Wikipedia-logo-v2',
407 prefixedText: 'File:Wikipedia-logo-v2.svg'
408 },
409
410 {
411 url: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
412 typeOfUrl: 'Commons thumbnail',
413 nameText: 'Wikipedia-logo-v2',
414 prefixedText: 'File:Wikipedia-logo-v2.svg'
415 },
416
417 {
418 url: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
419 typeOfUrl: 'Full image',
420 nameText: 'Anticlockwise heliotrope\'s',
421 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
422 },
423
424 {
425 url: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
426 typeOfUrl: 'thumb.php-based thumbnail',
427 nameText: 'Stuffless Figaro\'s',
428 prefixedText: 'File:Stuffless Figaro\'s.jpg'
429 },
430
431 {
432 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
433 typeOfUrl: 'Commons unhashed thumbnail',
434 nameText: 'Wikipedia-logo-v2',
435 prefixedText: 'File:Wikipedia-logo-v2.svg'
436 },
437
438 {
439 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
440 typeOfUrl: 'Commons unhashed thumbnail with complex thumbnail parameters',
441 nameText: 'Wikipedia-logo-v2',
442 prefixedText: 'File:Wikipedia-logo-v2.svg'
443 },
444
445 {
446 url: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
447 typeOfUrl: 'Unhashed local file',
448 nameText: 'Anticlockwise heliotrope\'s',
449 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
450 },
451
452 {
453 url: '',
454 typeOfUrl: 'Empty string'
455 },
456
457 {
458 url: 'foo',
459 typeOfUrl: 'String with only alphabet characters'
460 },
461
462 {
463 url: 'foobar.foobar',
464 typeOfUrl: 'Not a file path'
465 },
466
467 {
468 url: '/a/a0/blah blah blah',
469 typeOfUrl: 'Space characters'
470 }
471 ];
472
473 for ( i = 0; i < cases.length; i++ ) {
474 thisCase = cases[ i ];
475 title = mw.Title.newFromImg( { src: thisCase.url } );
476
477 if ( thisCase.nameText !== undefined ) {
478 prefix = '[' + thisCase.typeOfUrl + ' URL' + '] ';
479
480 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
481 assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
482 assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
483 assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
484 } else {
485 assert.strictEqual( title, null, thisCase.typeOfUrl + ', should not produce an mw.Title object' );
486 }
487 }
488 } );
489
490 QUnit.test( 'getRelativeText', 5, function ( assert ) {
491 var i, thisCase, title,
492 cases = [
493 {
494 text: 'asd',
495 relativeTo: 123,
496 expectedResult: ':Asd'
497 },
498 {
499 text: 'dfg',
500 relativeTo: 0,
501 expectedResult: 'Dfg'
502 },
503 {
504 text: 'Template:Ghj',
505 relativeTo: 0,
506 expectedResult: 'Template:Ghj'
507 },
508 {
509 text: 'Template:1',
510 relativeTo: 10,
511 expectedResult: '1'
512 },
513 {
514 text: 'User:Hi',
515 relativeTo: 10,
516 expectedResult: 'User:Hi'
517 }
518 ];
519
520 for ( i = 0; i < cases.length; i++ ) {
521 thisCase = cases[ i ];
522
523 title = mw.Title.newFromText( thisCase.text );
524 assert.equal( title.getRelativeText( thisCase.relativeTo ), thisCase.expectedResult );
525 }
526 } );
527
528 QUnit.test( 'normalizeExtension', 5, function ( assert ) {
529 var extension, i, thisCase, prefix,
530 cases = [
531 {
532 extension: 'png',
533 expected: 'png',
534 description: 'Extension already in canonical form'
535 },
536 {
537 extension: 'PNG',
538 expected: 'png',
539 description: 'Extension lowercased in canonical form'
540 },
541 {
542 extension: 'jpeg',
543 expected: 'jpg',
544 description: 'Extension changed in canonical form'
545 },
546 {
547 extension: 'JPEG',
548 expected: 'jpg',
549 description: 'Extension lowercased and changed in canonical form'
550 },
551 {
552 extension: '~~~',
553 expected: '',
554 description: 'Extension invalid and discarded'
555 }
556 ];
557
558 for ( i = 0; i < cases.length; i++ ) {
559 thisCase = cases[ i ];
560 extension = mw.Title.normalizeExtension( thisCase.extension );
561
562 prefix = '[' + thisCase.description + '] ';
563 assert.equal( extension, thisCase.expected, prefix + 'Extension as expected' );
564 }
565 } );
566
567 QUnit.test( 'newFromUserInput', 12, function ( assert ) {
568 var title, i, thisCase, prefix,
569 cases = [
570 {
571 title: 'DCS0001557854455.JPG',
572 expected: 'DCS0001557854455.JPG',
573 description: 'Title in normal namespace without anything invalid but with "file extension"'
574 },
575 {
576 title: 'MediaWiki:Msg-awesome',
577 expected: 'MediaWiki:Msg-awesome',
578 description: 'Full title (page in MediaWiki namespace) supplied as string'
579 },
580 {
581 title: 'The/Mw/Sound.flac',
582 defaultNamespace: -2,
583 expected: 'Media:The-Mw-Sound.flac',
584 description: 'Page in Media-namespace without explicit options'
585 },
586 {
587 title: 'File:The/Mw/Sound.kml',
588 defaultNamespace: 6,
589 options: {
590 forUploading: false
591 },
592 expected: 'File:The/Mw/Sound.kml',
593 description: 'Page in File-namespace without explicit options'
594 },
595 {
596 title: 'File:Foo.JPEG',
597 expected: 'File:Foo.JPEG',
598 description: 'Page in File-namespace with non-canonical extension'
599 },
600 {
601 title: 'File:Foo.JPEG ',
602 expected: 'File:Foo.JPEG',
603 description: 'Page in File-namespace with trailing whitespace'
604 }
605 ];
606
607 for ( i = 0; i < cases.length; i++ ) {
608 thisCase = cases[ i ];
609 title = mw.Title.newFromUserInput( thisCase.title, thisCase.defaultNamespace, thisCase.options );
610
611 if ( thisCase.expected !== undefined ) {
612 prefix = '[' + thisCase.description + '] ';
613
614 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
615 assert.equal( title.toText(), thisCase.expected, prefix + 'Title as expected' );
616 } else {
617 assert.strictEqual( title, null, thisCase.description + ', should not produce an mw.Title object' );
618 }
619 }
620 } );
621
622 QUnit.test( 'newFromFileName', 54, function ( assert ) {
623 var title, i, thisCase, prefix,
624 cases = [
625 {
626 fileName: 'DCS0001557854455.JPG',
627 typeOfName: 'Standard camera output',
628 nameText: 'DCS0001557854455',
629 prefixedText: 'File:DCS0001557854455.JPG'
630 },
631 {
632 fileName: 'File:Sample.png',
633 typeOfName: 'Carrying namespace',
634 nameText: 'File-Sample',
635 prefixedText: 'File:File-Sample.png'
636 },
637 {
638 fileName: 'Treppe 2222 Test upload.jpg',
639 typeOfName: 'File name with spaces in it and lower case file extension',
640 nameText: 'Treppe 2222 Test upload',
641 prefixedText: 'File:Treppe 2222 Test upload.jpg'
642 },
643 {
644 fileName: 'I contain a \ttab.jpg',
645 typeOfName: 'Name containing a tab character',
646 nameText: 'I contain a tab',
647 prefixedText: 'File:I contain a tab.jpg'
648 },
649 {
650 fileName: 'I_contain multiple__ ___ _underscores.jpg',
651 typeOfName: 'Name containing multiple underscores',
652 nameText: 'I contain multiple underscores',
653 prefixedText: 'File:I contain multiple underscores.jpg'
654 },
655 {
656 fileName: 'I like ~~~~~~~~es.jpg',
657 typeOfName: 'Name containing more than three consecutive tilde characters',
658 nameText: 'I like ~~es',
659 prefixedText: 'File:I like ~~es.jpg'
660 },
661 {
662 fileName: 'BI\u200EDI.jpg',
663 typeOfName: 'Name containing BIDI overrides',
664 nameText: 'BIDI',
665 prefixedText: 'File:BIDI.jpg'
666 },
667 {
668 fileName: '100%ab progress.jpg',
669 typeOfName: 'File name with URL encoding',
670 nameText: '100% ab progress',
671 prefixedText: 'File:100% ab progress.jpg'
672 },
673 {
674 fileName: '<([>]):/#.jpg',
675 typeOfName: 'File name with characters not permitted in titles that are replaced',
676 nameText: '((()))---',
677 prefixedText: 'File:((()))---.jpg'
678 },
679 {
680 fileName: 'spaces\u0009\u2000\u200A\u200Bx.djvu',
681 typeOfName: 'File name with different kind of spaces',
682 nameText: 'Spaces \u200Bx',
683 prefixedText: 'File:Spaces \u200Bx.djvu'
684 },
685 {
686 fileName: 'dot.dot.dot.dot.dotdot',
687 typeOfName: 'File name with a lot of dots',
688 nameText: 'Dot.dot.dot.dot',
689 prefixedText: 'File:Dot.dot.dot.dot.dotdot'
690 },
691 {
692 fileName: 'dot. dot ._dot',
693 typeOfName: 'File name with multiple dots and spaces',
694 nameText: 'Dot. dot',
695 prefixedText: 'File:Dot. dot. dot'
696 },
697 {
698 fileName: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂.png',
699 typeOfName: 'File name longer than 240 bytes',
700 nameText: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵',
701 prefixedText: 'File:𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵.png'
702 },
703 {
704 fileName: '',
705 typeOfName: 'Empty string'
706 },
707 {
708 fileName: 'foo',
709 typeOfName: 'String with only alphabet characters'
710 }
711 ];
712
713 for ( i = 0; i < cases.length; i++ ) {
714 thisCase = cases[ i ];
715 title = mw.Title.newFromFileName( thisCase.fileName );
716
717 if ( thisCase.nameText !== undefined ) {
718 prefix = '[' + thisCase.typeOfName + '] ';
719
720 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
721 assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
722 assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
723 assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
724 } else {
725 assert.strictEqual( title, null, thisCase.typeOfName + ', should not produce an mw.Title object' );
726 }
727 }
728 } );
729
730 }( mediaWiki, jQuery ) );