Merge "Add schema version param for API export."
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
1 ( function () {
2 /* eslint-disable camelcase */
3 var repeat = function ( input, multiplier ) {
4 return new Array( multiplier + 1 ).join( input );
5 },
6 // See also TitleTest.php#testSecureAndSplit
7 cases = {
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 wgNamespaceIds: {
106 media: -2,
107 special: -1,
108 '': 0,
109 talk: 1,
110 user: 2,
111 user_talk: 3,
112 wikipedia: 4,
113 wikipedia_talk: 5,
114 file: 6,
115 file_talk: 7,
116 mediawiki: 8,
117 mediawiki_talk: 9,
118 template: 10,
119 template_talk: 11,
120 help: 12,
121 help_talk: 13,
122 category: 14,
123 category_talk: 15,
124 image: 6,
125 image_talk: 7,
126 project: 4,
127 project_talk: 5,
128 // Testing custom namespaces and aliases
129 penguins: 100,
130 antarctic_waterfowl: 100
131 },
132 wgCaseSensitiveNamespaces: []
133 }
134 } ) );
135
136 QUnit.test( 'constructor', 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 title = cases.invalid[ i ];
143 // eslint-disable-next-line no-loop-func
144 assert.throws( function () {
145 return new mw.Title( title );
146 }, cases.invalid[ i ] );
147 }
148 } );
149
150 QUnit.test( 'newFromText', function ( assert ) {
151 var i;
152 for ( i = 0; i < cases.valid.length; i++ ) {
153 assert.strictEqual(
154 typeof 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.strictEqual(
161 mw.Title.newFromText( cases.invalid[ i ] ),
162 null,
163 cases.invalid[ i ]
164 );
165 }
166 } );
167
168 QUnit.test( 'makeTitle', 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', function ( assert ) {
195 var title;
196 title = new mw.Title( 'File:Foo_bar.JPG' );
197
198 assert.strictEqual( title.getNamespaceId(), 6 );
199 assert.strictEqual( title.getNamespacePrefix(), 'File:' );
200 assert.strictEqual( title.getName(), 'Foo_bar' );
201 assert.strictEqual( title.getNameText(), 'Foo bar' );
202 assert.strictEqual( title.getExtension(), 'JPG' );
203 assert.strictEqual( title.getDotExtension(), '.JPG' );
204 assert.strictEqual( title.getMain(), 'Foo_bar.JPG' );
205 assert.strictEqual( title.getMainText(), 'Foo bar.JPG' );
206 assert.strictEqual( title.getPrefixedDb(), 'File:Foo_bar.JPG' );
207 assert.strictEqual( title.getPrefixedText(), 'File:Foo bar.JPG' );
208
209 title = new mw.Title( 'Foo#bar' );
210 assert.strictEqual( title.getPrefixedText(), 'Foo' );
211 assert.strictEqual( title.getFragment(), 'bar' );
212
213 title = new mw.Title( '.foo' );
214 assert.strictEqual( title.getPrefixedText(), '.foo' );
215 assert.strictEqual( title.getName(), '' );
216 assert.strictEqual( title.getNameText(), '' );
217 assert.strictEqual( title.getExtension(), 'foo' );
218 assert.strictEqual( title.getDotExtension(), '.foo' );
219 assert.strictEqual( title.getMain(), '.foo' );
220 assert.strictEqual( title.getMainText(), '.foo' );
221 assert.strictEqual( title.getPrefixedDb(), '.foo' );
222 assert.strictEqual( title.getPrefixedText(), '.foo' );
223 } );
224
225 QUnit.test( 'Transformation', function ( assert ) {
226 var title;
227
228 title = new mw.Title( 'File:quux pif.jpg' );
229 assert.strictEqual( title.getNameText(), 'Quux pif', 'First character of title' );
230
231 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
232 assert.strictEqual( title.getNameText(), 'Glarg foo glang', 'Underscores' );
233
234 title = new mw.Title( 'User:ABC.DEF' );
235 assert.strictEqual( title.toText(), 'User:ABC.DEF', 'Round trip text' );
236 assert.strictEqual( title.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
237
238 title = new mw.Title( 'Image:quux pix.jpg' );
239 assert.strictEqual( title.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
240
241 title = new mw.Title( 'uSEr:hAshAr' );
242 assert.strictEqual( title.toText(), 'User:HAshAr' );
243 assert.strictEqual( title.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
244
245 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' );
246 assert.strictEqual( title.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
247
248 title = new mw.Title( 'Foo\u200E\u200F\u202A\u202B\u202C\u202D\u202Ebar' );
249 assert.strictEqual( title.getMain(), 'Foobar', 'Strip Unicode bidi override characters' );
250
251 // Regression test: Previously it would only detect an extension if there is no space after it
252 title = new mw.Title( 'Example.js ' );
253 assert.strictEqual( title.getExtension(), 'js', 'Space after an extension is stripped' );
254
255 title = new mw.Title( 'Example#foo' );
256 assert.strictEqual( title.getFragment(), 'foo', 'Fragment' );
257
258 title = new mw.Title( 'Example#_foo_bar baz_' );
259 assert.strictEqual( title.getFragment(), ' foo bar baz', 'Fragment' );
260 } );
261
262 QUnit.test( 'Namespace detection and conversion', function ( assert ) {
263 var title;
264
265 title = new mw.Title( 'File:User:Example' );
266 assert.strictEqual( title.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
267
268 title = new mw.Title( 'Example', 6 );
269 assert.strictEqual( title.getNamespaceId(), 6, 'Default namespace passed is used' );
270
271 title = new mw.Title( 'User:Example', 6 );
272 assert.strictEqual( title.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
273
274 title = new mw.Title( ':Example', 6 );
275 assert.strictEqual( title.getNamespaceId(), 0, 'Colon forces main namespace' );
276
277 title = new mw.Title( 'something.PDF', 6 );
278 assert.strictEqual( title.toString(), 'File:Something.PDF' );
279
280 title = new mw.Title( 'NeilK', 3 );
281 assert.strictEqual( title.toString(), 'User_talk:NeilK' );
282 assert.strictEqual( title.toText(), 'User talk:NeilK' );
283
284 title = new mw.Title( 'Frobisher', 100 );
285 assert.strictEqual( title.toString(), 'Penguins:Frobisher' );
286
287 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
288 assert.strictEqual( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
289
290 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
291 assert.strictEqual( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
292 } );
293
294 QUnit.test( 'isTalkPage/getTalkPage/getSubjectPage', function ( assert ) {
295 var title;
296
297 title = new mw.Title( 'User:Foo' );
298 assert.strictEqual( title.isTalkPage(), false, 'Non-talk page detected as such' );
299 assert.strictEqual( title.getSubjectPage().getPrefixedText(), 'User:Foo', 'getSubjectPage on a subject page is a no-op' );
300
301 title = title.getTalkPage();
302 assert.strictEqual( title.getPrefixedText(), 'User talk:Foo', 'getTalkPage creates correct title' );
303 assert.strictEqual( title.getTalkPage().getPrefixedText(), 'User talk:Foo', 'getTalkPage on a talk page is a no-op' );
304 assert.strictEqual( title.isTalkPage(), true, 'Talk page is detected as such' );
305
306 title = title.getSubjectPage();
307 assert.strictEqual( title.getPrefixedText(), 'User:Foo', 'getSubjectPage creates correct title' );
308
309 title = new mw.Title( 'Special:AllPages' );
310 assert.strictEqual( title.isTalkPage(), false, 'Special page is not a talk page' );
311 assert.strictEqual( title.getTalkPage(), null, 'getTalkPage not valid for this namespace' );
312 assert.strictEqual( title.getSubjectPage().getPrefixedText(), 'Special:AllPages', 'getSubjectPage is self for special pages' );
313
314 title = new mw.Title( 'Category:Project:Maintenance' );
315 assert.strictEqual( title.getTalkPage().getPrefixedText(), 'Category talk:Project:Maintenance', 'getTalkPage is not confused by colon in main text' );
316 title = new mw.Title( 'Category talk:Project:Maintenance' );
317 assert.strictEqual( title.getSubjectPage().getPrefixedText(), 'Category:Project:Maintenance', 'getSubjectPage is not confused by colon in main text' );
318
319 title = new mw.Title( 'Foo#Caption' );
320 assert.strictEqual( title.getFragment(), 'Caption', 'Subject page has a fragment' );
321 title = title.getTalkPage();
322 assert.strictEqual( title.getPrefixedText(), 'Talk:Foo', 'getTalkPage creates correct title' );
323 assert.strictEqual( title.getFragment(), null, 'getTalkPage does not copy the fragment' );
324 } );
325
326 QUnit.test( 'wantSignaturesNamespace', function ( assert ) {
327 mw.config.set( 'wgExtraSignatureNamespaces', [] );
328 assert.strictEqual( mw.Title.wantSignaturesNamespace( 0 ), false, 'Main namespace has no signatures' );
329 assert.strictEqual( mw.Title.wantSignaturesNamespace( 1 ), true, 'Talk namespace has signatures' );
330 assert.strictEqual( mw.Title.wantSignaturesNamespace( 2 ), false, 'NS2 has no signatures' );
331 assert.strictEqual( mw.Title.wantSignaturesNamespace( 3 ), true, 'NS3 has signatures' );
332
333 mw.config.set( 'wgExtraSignatureNamespaces', [ 0 ] );
334 assert.strictEqual( mw.Title.wantSignaturesNamespace( 0 ), true, 'Main namespace has signatures when explicitly defined' );
335 } );
336
337 QUnit.test( 'Throw error on invalid title', function ( assert ) {
338 assert.throws( function () {
339 return new mw.Title( '' );
340 }, 'Throw error on empty string' );
341 } );
342
343 QUnit.test( 'Case-sensivity', function ( assert ) {
344 var title;
345
346 // Default config
347 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
348
349 title = new mw.Title( 'article' );
350 assert.strictEqual( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
351
352 title = new mw.Title( 'ß' );
353 assert.strictEqual( title.toString(), 'ß', 'Uppercasing matches PHP behaviour (ß -> ß, not SS)' );
354
355 title = new mw.Title( 'dž (digraph)' );
356 assert.strictEqual( title.toString(), 'Dž_(digraph)', 'Uppercasing matches PHP behaviour (dž -> Dž, not DŽ)' );
357
358 // $wgCapitalLinks = false;
359 mw.config.set( 'wgCaseSensitiveNamespaces', [ 0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15 ] );
360
361 title = new mw.Title( 'article' );
362 assert.strictEqual( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
363
364 title = new mw.Title( 'john', 2 );
365 assert.strictEqual( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
366 } );
367
368 QUnit.test( 'toString / toText', function ( assert ) {
369 var title = new mw.Title( 'Some random page' );
370
371 assert.strictEqual( title.toString(), title.getPrefixedDb() );
372 assert.strictEqual( title.toText(), title.getPrefixedText() );
373 } );
374
375 QUnit.test( 'getExtension', function ( assert ) {
376 function extTest( pagename, ext, description ) {
377 var title = new mw.Title( pagename );
378 assert.strictEqual( title.getExtension(), ext, description || pagename );
379 }
380
381 extTest( 'MediaWiki:Vector.js', 'js' );
382 extTest( 'User:Example/common.css', 'css' );
383 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (T38151)' );
384 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
385 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
386 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
387 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
388
389 // @broken: Throws an exception
390 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
391 } );
392
393 QUnit.test( 'exists', function ( assert ) {
394 var title;
395
396 // Empty registry, checks default to null
397
398 title = new mw.Title( 'Some random page', 4 );
399 assert.strictEqual( title.exists(), null, 'Return null with empty existance registry' );
400
401 // Basic registry, checks default to boolean
402 mw.Title.exist.set( [ 'Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules' ], true );
403 mw.Title.exist.set( [ 'Does_not_exist', 'User:John', 'Foobar' ], false );
404
405 title = new mw.Title( 'Project:Sandbox rules' );
406 assert.assertTrue( title.exists(), 'Return true for page titles marked as existing' );
407 title = new mw.Title( 'Foobar' );
408 assert.assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
409
410 } );
411
412 QUnit.test( 'getUrl', function ( assert ) {
413 var title;
414 mw.config.set( {
415 wgScript: '/w/index.php',
416 wgArticlePath: '/wiki/$1'
417 } );
418
419 title = new mw.Title( 'Foobar' );
420 assert.strictEqual( title.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
421 assert.strictEqual( title.getUrl( { action: 'edit' } ), '/w/index.php?title=Foobar&action=edit', 'Basic functionality, \'params\' parameter' );
422
423 title = new mw.Title( 'John Doe', 3 );
424 assert.strictEqual( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
425
426 title = new mw.Title( 'John Cena#And_His_Name_Is', 3 );
427 assert.strictEqual( title.getUrl( { meme: true } ), '/w/index.php?title=User_talk:John_Cena&meme=true#And_His_Name_Is', 'title with fragment and query parameter' );
428 } );
429
430 QUnit.test( 'newFromImg', function ( assert ) {
431 var title, i, thisCase, prefix,
432 cases = [
433 {
434 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',
435 typeOfUrl: 'Hashed thumb with shortened path',
436 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)',
437 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'
438 },
439
440 {
441 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',
442 typeOfUrl: 'Hashed thumb with sha1-ed path',
443 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)',
444 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'
445 },
446
447 {
448 url: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
449 typeOfUrl: 'Normal hashed directory thumbnail',
450 nameText: 'Anticlockwise heliotrope\'s',
451 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
452 },
453
454 {
455 url: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
456 typeOfUrl: 'Normal hashed directory thumbnail with complex thumbnail parameters',
457 nameText: 'Wikipedia-logo-v2',
458 prefixedText: 'File:Wikipedia-logo-v2.svg'
459 },
460
461 {
462 url: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
463 typeOfUrl: 'Commons thumbnail',
464 nameText: 'Wikipedia-logo-v2',
465 prefixedText: 'File:Wikipedia-logo-v2.svg'
466 },
467
468 {
469 url: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
470 typeOfUrl: 'Full image',
471 nameText: 'Anticlockwise heliotrope\'s',
472 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
473 },
474
475 {
476 url: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
477 typeOfUrl: 'thumb.php-based thumbnail',
478 nameText: 'Stuffless Figaro\'s',
479 prefixedText: 'File:Stuffless Figaro\'s.jpg'
480 },
481
482 {
483 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
484 typeOfUrl: 'Commons unhashed thumbnail',
485 nameText: 'Wikipedia-logo-v2',
486 prefixedText: 'File:Wikipedia-logo-v2.svg'
487 },
488
489 {
490 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
491 typeOfUrl: 'Commons unhashed thumbnail with complex thumbnail parameters',
492 nameText: 'Wikipedia-logo-v2',
493 prefixedText: 'File:Wikipedia-logo-v2.svg'
494 },
495
496 {
497 url: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
498 typeOfUrl: 'Unhashed local file',
499 nameText: 'Anticlockwise heliotrope\'s',
500 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
501 },
502
503 {
504 url: '',
505 typeOfUrl: 'Empty string'
506 },
507
508 {
509 url: 'foo',
510 typeOfUrl: 'String with only alphabet characters'
511 },
512
513 {
514 url: 'foobar.foobar',
515 typeOfUrl: 'Not a file path'
516 },
517
518 {
519 url: '/a/a0/blah blah blah',
520 typeOfUrl: 'Space characters'
521 }
522 ];
523
524 for ( i = 0; i < cases.length; i++ ) {
525 thisCase = cases[ i ];
526 title = mw.Title.newFromImg( { src: thisCase.url } );
527
528 if ( thisCase.nameText !== undefined ) {
529 prefix = '[' + thisCase.typeOfUrl + ' URL] ';
530
531 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
532 assert.strictEqual( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
533 assert.strictEqual( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
534 assert.strictEqual( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
535 } else {
536 assert.strictEqual( title, null, thisCase.typeOfUrl + ', should not produce an mw.Title object' );
537 }
538 }
539 } );
540
541 QUnit.test( 'getRelativeText', function ( assert ) {
542 var i, thisCase, title,
543 cases = [
544 {
545 text: 'asd',
546 relativeTo: 123,
547 expectedResult: ':Asd'
548 },
549 {
550 text: 'dfg',
551 relativeTo: 0,
552 expectedResult: 'Dfg'
553 },
554 {
555 text: 'Template:Ghj',
556 relativeTo: 0,
557 expectedResult: 'Template:Ghj'
558 },
559 {
560 text: 'Template:1',
561 relativeTo: 10,
562 expectedResult: '1'
563 },
564 {
565 text: 'User:Hi',
566 relativeTo: 10,
567 expectedResult: 'User:Hi'
568 }
569 ];
570
571 for ( i = 0; i < cases.length; i++ ) {
572 thisCase = cases[ i ];
573
574 title = mw.Title.newFromText( thisCase.text );
575 assert.strictEqual( title.getRelativeText( thisCase.relativeTo ), thisCase.expectedResult );
576 }
577 } );
578
579 QUnit.test( 'normalizeExtension', function ( assert ) {
580 var extension, i, thisCase, prefix,
581 cases = [
582 {
583 extension: 'png',
584 expected: 'png',
585 description: 'Extension already in canonical form'
586 },
587 {
588 extension: 'PNG',
589 expected: 'png',
590 description: 'Extension lowercased in canonical form'
591 },
592 {
593 extension: 'jpeg',
594 expected: 'jpg',
595 description: 'Extension changed in canonical form'
596 },
597 {
598 extension: 'JPEG',
599 expected: 'jpg',
600 description: 'Extension lowercased and changed in canonical form'
601 },
602 {
603 extension: '~~~',
604 expected: '',
605 description: 'Extension invalid and discarded'
606 }
607 ];
608
609 for ( i = 0; i < cases.length; i++ ) {
610 thisCase = cases[ i ];
611 extension = mw.Title.normalizeExtension( thisCase.extension );
612
613 prefix = '[' + thisCase.description + '] ';
614 assert.strictEqual( extension, thisCase.expected, prefix + 'Extension as expected' );
615 }
616 } );
617
618 QUnit.test( 'newFromUserInput', function ( assert ) {
619 var title, i, thisCase, prefix,
620 cases = [
621 {
622 title: 'DCS0001557854455.JPG',
623 expected: 'DCS0001557854455.JPG',
624 description: 'Title in normal namespace without anything invalid but with "file extension"'
625 },
626 {
627 title: 'MediaWiki:Msg-awesome',
628 expected: 'MediaWiki:Msg-awesome',
629 description: 'Full title (page in MediaWiki namespace) supplied as string'
630 },
631 {
632 title: 'The/Mw/Sound.flac',
633 defaultNamespace: -2,
634 expected: 'Media:The-Mw-Sound.flac',
635 description: 'Page in Media-namespace without explicit options'
636 },
637 {
638 title: 'File:The/Mw/Sound.kml',
639 defaultNamespace: 6,
640 options: {
641 forUploading: false
642 },
643 expected: 'File:The/Mw/Sound.kml',
644 description: 'Page in File-namespace without explicit options'
645 },
646 {
647 title: 'File:Foo.JPEG',
648 expected: 'File:Foo.JPEG',
649 description: 'Page in File-namespace with non-canonical extension'
650 },
651 {
652 title: 'File:Foo.JPEG ',
653 expected: 'File:Foo.JPEG',
654 description: 'Page in File-namespace with trailing whitespace'
655 },
656 {
657 title: 'File:Foo',
658 description: 'File name without file extension'
659 },
660 {
661 title: 'File:Foo.',
662 description: 'File name with empty file extension'
663 }
664 ];
665
666 for ( i = 0; i < cases.length; i++ ) {
667 thisCase = cases[ i ];
668 title = mw.Title.newFromUserInput( thisCase.title, thisCase.defaultNamespace, thisCase.options );
669
670 if ( thisCase.expected !== undefined ) {
671 prefix = '[' + thisCase.description + '] ';
672
673 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
674 assert.strictEqual( title.toText(), thisCase.expected, prefix + 'Title as expected' );
675 if ( thisCase.defaultNamespace === undefined ) {
676 title = mw.Title.newFromUserInput( thisCase.title, thisCase.options );
677 assert.strictEqual( title.toText(), thisCase.expected, prefix + 'Skipping namespace argument' );
678 }
679 } else {
680 assert.strictEqual( title, null, thisCase.description + ', should not produce an mw.Title object' );
681 }
682 }
683 } );
684
685 QUnit.test( 'newFromFileName', function ( assert ) {
686 var title, i, thisCase, prefix,
687 cases = [
688 {
689 fileName: 'DCS0001557854455.JPG',
690 typeOfName: 'Standard camera output',
691 nameText: 'DCS0001557854455',
692 prefixedText: 'File:DCS0001557854455.JPG'
693 },
694 {
695 fileName: 'File:Sample.png',
696 typeOfName: 'Carrying namespace',
697 nameText: 'File-Sample',
698 prefixedText: 'File:File-Sample.png'
699 },
700 {
701 fileName: 'Treppe 2222 Test upload.jpg',
702 typeOfName: 'File name with spaces in it and lower case file extension',
703 nameText: 'Treppe 2222 Test upload',
704 prefixedText: 'File:Treppe 2222 Test upload.jpg'
705 },
706 {
707 fileName: 'I contain a \ttab.jpg',
708 typeOfName: 'Name containing a tab character',
709 nameText: 'I contain a tab',
710 prefixedText: 'File:I contain a tab.jpg'
711 },
712 {
713 fileName: 'I_contain multiple__ ___ _underscores.jpg',
714 typeOfName: 'Name containing multiple underscores',
715 nameText: 'I contain multiple underscores',
716 prefixedText: 'File:I contain multiple underscores.jpg'
717 },
718 {
719 fileName: 'I like ~~~~~~~~es.jpg',
720 typeOfName: 'Name containing more than three consecutive tilde characters',
721 nameText: 'I like ~~es',
722 prefixedText: 'File:I like ~~es.jpg'
723 },
724 {
725 fileName: 'BI\u200EDI.jpg',
726 typeOfName: 'Name containing BIDI overrides',
727 nameText: 'BIDI',
728 prefixedText: 'File:BIDI.jpg'
729 },
730 {
731 fileName: '100%ab progress.jpg',
732 typeOfName: 'File name with URL encoding',
733 nameText: '100% ab progress',
734 prefixedText: 'File:100% ab progress.jpg'
735 },
736 {
737 fileName: '<([>]):/#.jpg',
738 typeOfName: 'File name with characters not permitted in titles that are replaced',
739 nameText: '((()))---',
740 prefixedText: 'File:((()))---.jpg'
741 },
742 {
743 fileName: 'spaces\u0009\u2000\u200A\u200Bx.djvu',
744 typeOfName: 'File name with different kind of spaces',
745 nameText: 'Spaces \u200Bx',
746 prefixedText: 'File:Spaces \u200Bx.djvu'
747 },
748 {
749 fileName: 'dot.dot.dot.dot.dotdot',
750 typeOfName: 'File name with a lot of dots',
751 nameText: 'Dot.dot.dot.dot',
752 prefixedText: 'File:Dot.dot.dot.dot.dotdot'
753 },
754 {
755 fileName: 'dot. dot ._dot',
756 typeOfName: 'File name with multiple dots and spaces',
757 nameText: 'Dot. dot',
758 prefixedText: 'File:Dot. dot. dot'
759 },
760 {
761 fileName: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂.png',
762 typeOfName: 'File name longer than 240 bytes',
763 nameText: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵',
764 prefixedText: 'File:𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵.png'
765 },
766 {
767 fileName: '',
768 typeOfName: 'Empty string'
769 },
770 {
771 fileName: 'foo',
772 typeOfName: 'String with only alphabet characters'
773 }
774 ];
775
776 for ( i = 0; i < cases.length; i++ ) {
777 thisCase = cases[ i ];
778 title = mw.Title.newFromFileName( thisCase.fileName );
779
780 if ( thisCase.nameText !== undefined ) {
781 prefix = '[' + thisCase.typeOfName + '] ';
782
783 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
784 assert.strictEqual( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
785 assert.strictEqual( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
786 assert.strictEqual( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
787 } else {
788 assert.strictEqual( title, null, thisCase.typeOfName + ', should not produce an mw.Title object' );
789 }
790 }
791 } );
792
793 }() );