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