Debian Codegraph @

tests/qunit/resources/mediawiki.Title.test.js

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