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