Merge "mediawiki.mixins: Move .box-sizing mixin from mediawiki.ui utilities"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.Title.test.js
1 /*jshint -W024 */
2 ( function ( mw, $ ) {
3 // mw.Title relies on these three config vars
4 // Restore them after each test run
5 var config = {
6 wgFormattedNamespaces: {
7 '-2': 'Media',
8 '-1': 'Special',
9 0: '',
10 1: 'Talk',
11 2: 'User',
12 3: 'User talk',
13 4: 'Wikipedia',
14 5: 'Wikipedia talk',
15 6: 'File',
16 7: 'File talk',
17 8: 'MediaWiki',
18 9: 'MediaWiki talk',
19 10: 'Template',
20 11: 'Template talk',
21 12: 'Help',
22 13: 'Help talk',
23 14: 'Category',
24 15: 'Category talk',
25 // testing custom / localized namespace
26 100: 'Penguins'
27 },
28 wgNamespaceIds: {
29 'media': -2,
30 'special': -1,
31 '': 0,
32 'talk': 1,
33 'user': 2,
34 'user_talk': 3,
35 'wikipedia': 4,
36 'wikipedia_talk': 5,
37 'file': 6,
38 'file_talk': 7,
39 'mediawiki': 8,
40 'mediawiki_talk': 9,
41 'template': 10,
42 'template_talk': 11,
43 'help': 12,
44 'help_talk': 13,
45 'category': 14,
46 'category_talk': 15,
47 'image': 6,
48 'image_talk': 7,
49 'project': 4,
50 'project_talk': 5,
51 // Testing custom namespaces and aliases
52 'penguins': 100,
53 'antarctic_waterfowl': 100
54 },
55 wgCaseSensitiveNamespaces: []
56 },
57 repeat = function ( input, multiplier ) {
58 return new Array( multiplier + 1 ).join( input );
59 },
60 cases = {
61 // See also TitleTest.php#testSecureAndSplit
62 valid: [
63 'Sandbox',
64 'A "B"',
65 'A \'B\'',
66 '.com',
67 '~',
68 '"',
69 '\'',
70 'Talk:Sandbox',
71 'Talk:Foo:Sandbox',
72 'File:Example.svg',
73 'File_talk:Example.svg',
74 'Foo/.../Sandbox',
75 'Sandbox/...',
76 'A~~',
77 ':A',
78 // Length is 256 total, but only title part matters
79 'Category:' + repeat( 'x', 248 ),
80 repeat( 'x', 252 )
81 ],
82 invalid: [
83 '',
84 ':',
85 '__ __',
86 ' __ ',
87 // Bad characters forbidden regardless of wgLegalTitleChars
88 'A [ B',
89 'A ] B',
90 'A { B',
91 'A } B',
92 'A < B',
93 'A > B',
94 'A | B',
95 // URL encoding
96 'A%20B',
97 'A%23B',
98 'A%2523B',
99 // XML/HTML character entity references
100 // Note: The ones with # are commented out as those are interpreted as fragment and
101 // as such end up being valid.
102 'A &eacute; B',
103 //'A &#233; B',
104 //'A &#x00E9; B',
105 // Subject of NS_TALK does not roundtrip to NS_MAIN
106 'Talk:File:Example.svg',
107 // Directory navigation
108 '.',
109 '..',
110 './Sandbox',
111 '../Sandbox',
112 'Foo/./Sandbox',
113 'Foo/../Sandbox',
114 'Sandbox/.',
115 'Sandbox/..',
116 // Tilde
117 'A ~~~ Name',
118 'A ~~~~ Signature',
119 'A ~~~~~ Timestamp',
120 repeat( 'x', 256 ),
121 // Extension separation is a js invention, for length
122 // purposes it is part of the title
123 repeat( 'x', 252 ) + '.json',
124 // Namespace prefix without actual title
125 'Talk:',
126 'Category: ',
127 'Category: #bar'
128 ]
129 };
130
131 QUnit.module( 'mediawiki.Title', QUnit.newMwEnvironment( { config: config } ) );
132
133 QUnit.test( 'constructor', cases.invalid.length, function ( assert ) {
134 var i, title;
135 for ( i = 0; i < cases.valid.length; i++ ) {
136 title = new mw.Title( cases.valid[i] );
137 }
138 for ( i = 0; i < cases.invalid.length; i++ ) {
139 /*jshint loopfunc:true */
140 title = cases.invalid[i];
141 assert.throws( function () {
142 return new mw.Title( title );
143 }, cases.invalid[i] );
144 }
145 } );
146
147 QUnit.test( 'newFromText', cases.valid.length + cases.invalid.length, function ( assert ) {
148 var i;
149 for ( i = 0; i < cases.valid.length; i++ ) {
150 assert.equal(
151 $.type( mw.Title.newFromText( cases.valid[i] ) ),
152 'object',
153 cases.valid[i]
154 );
155 }
156 for ( i = 0; i < cases.invalid.length; i++ ) {
157 assert.equal(
158 $.type( mw.Title.newFromText( cases.invalid[i] ) ),
159 'null',
160 cases.invalid[i]
161 );
162 }
163 } );
164
165 QUnit.test( 'Basic parsing', 12, function ( assert ) {
166 var title;
167 title = new mw.Title( 'File:Foo_bar.JPG' );
168
169 assert.equal( title.getNamespaceId(), 6 );
170 assert.equal( title.getNamespacePrefix(), 'File:' );
171 assert.equal( title.getName(), 'Foo_bar' );
172 assert.equal( title.getNameText(), 'Foo bar' );
173 assert.equal( title.getExtension(), 'JPG' );
174 assert.equal( title.getDotExtension(), '.JPG' );
175 assert.equal( title.getMain(), 'Foo_bar.JPG' );
176 assert.equal( title.getMainText(), 'Foo bar.JPG' );
177 assert.equal( title.getPrefixedDb(), 'File:Foo_bar.JPG' );
178 assert.equal( title.getPrefixedText(), 'File:Foo bar.JPG' );
179
180 title = new mw.Title( 'Foo#bar' );
181 assert.equal( title.getPrefixedText(), 'Foo' );
182 assert.equal( title.getFragment(), 'bar' );
183 } );
184
185 QUnit.test( 'Transformation', 11, function ( assert ) {
186 var title;
187
188 title = new mw.Title( 'File:quux pif.jpg' );
189 assert.equal( title.getNameText(), 'Quux pif', 'First character of title' );
190
191 title = new mw.Title( 'File:Glarg_foo_glang.jpg' );
192 assert.equal( title.getNameText(), 'Glarg foo glang', 'Underscores' );
193
194 title = new mw.Title( 'User:ABC.DEF' );
195 assert.equal( title.toText(), 'User:ABC.DEF', 'Round trip text' );
196 assert.equal( title.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
197
198 title = new mw.Title( 'Image:quux pix.jpg' );
199 assert.equal( title.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
200
201 title = new mw.Title( 'uSEr:hAshAr' );
202 assert.equal( title.toText(), 'User:HAshAr' );
203 assert.equal( title.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
204
205 // Don't ask why, it's the way the backend works. One space is kept of each set.
206 title = new mw.Title( 'Foo __ \t __ bar' );
207 assert.equal( title.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
208
209 // Regression test: Previously it would only detect an extension if there is no space after it
210 title = new mw.Title( 'Example.js ' );
211 assert.equal( title.getExtension(), 'js', 'Space after an extension is stripped' );
212
213 title = new mw.Title( 'Example#foo' );
214 assert.equal( title.getFragment(), 'foo', 'Fragment' );
215
216 title = new mw.Title( 'Example#_foo_bar baz_' );
217 assert.equal( title.getFragment(), ' foo bar baz', 'Fragment' );
218 } );
219
220 QUnit.test( 'Namespace detection and conversion', 10, function ( assert ) {
221 var title;
222
223 title = new mw.Title( 'File:User:Example' );
224 assert.equal( title.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
225
226 title = new mw.Title( 'Example', 6 );
227 assert.equal( title.getNamespaceId(), 6, 'Default namespace passed is used' );
228
229 title = new mw.Title( 'User:Example', 6 );
230 assert.equal( title.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
231
232 title = new mw.Title( ':Example', 6 );
233 assert.equal( title.getNamespaceId(), 0, 'Colon forces main namespace' );
234
235 title = new mw.Title( 'something.PDF', 6 );
236 assert.equal( title.toString(), 'File:Something.PDF' );
237
238 title = new mw.Title( 'NeilK', 3 );
239 assert.equal( title.toString(), 'User_talk:NeilK' );
240 assert.equal( title.toText(), 'User talk:NeilK' );
241
242 title = new mw.Title( 'Frobisher', 100 );
243 assert.equal( title.toString(), 'Penguins:Frobisher' );
244
245 title = new mw.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
246 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
247
248 title = new mw.Title( 'Penguins:flightless_yet_cute.jpg' );
249 assert.equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
250 } );
251
252 QUnit.test( 'Throw error on invalid title', 1, function ( assert ) {
253 assert.throws( function () {
254 return new mw.Title( '' );
255 }, 'Throw error on empty string' );
256 } );
257
258 QUnit.test( 'Case-sensivity', 3, function ( assert ) {
259 var title;
260
261 // Default config
262 mw.config.set( 'wgCaseSensitiveNamespaces', [] );
263
264 title = new mw.Title( 'article' );
265 assert.equal( title.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
266
267 // $wgCapitalLinks = false;
268 mw.config.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
269
270 title = new mw.Title( 'article' );
271 assert.equal( title.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
272
273 title = new mw.Title( 'john', 2 );
274 assert.equal( title.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
275 } );
276
277 QUnit.test( 'toString / toText', 2, function ( assert ) {
278 var title = new mw.Title( 'Some random page' );
279
280 assert.equal( title.toString(), title.getPrefixedDb() );
281 assert.equal( title.toText(), title.getPrefixedText() );
282 } );
283
284 QUnit.test( 'getExtension', 7, function ( assert ) {
285 function extTest( pagename, ext, description ) {
286 var title = new mw.Title( pagename );
287 assert.equal( title.getExtension(), ext, description || pagename );
288 }
289
290 extTest( 'MediaWiki:Vector.js', 'js' );
291 extTest( 'User:Example/common.css', 'css' );
292 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
293 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
294 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
295 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
296 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
297
298 // @broken: Throws an exception
299 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
300 } );
301
302 QUnit.test( 'exists', 3, function ( assert ) {
303 var title;
304
305 // Empty registry, checks default to null
306
307 title = new mw.Title( 'Some random page', 4 );
308 assert.strictEqual( title.exists(), null, 'Return null with empty existance registry' );
309
310 // Basic registry, checks default to boolean
311 mw.Title.exist.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
312 mw.Title.exist.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
313
314 title = new mw.Title( 'Project:Sandbox rules' );
315 assert.assertTrue( title.exists(), 'Return true for page titles marked as existing' );
316 title = new mw.Title( 'Foobar' );
317 assert.assertFalse( title.exists(), 'Return false for page titles marked as nonexistent' );
318
319 } );
320
321 QUnit.test( 'getUrl', 3, function ( assert ) {
322 var title;
323
324 // Config
325 mw.config.set( 'wgArticlePath', '/wiki/$1' );
326
327 title = new mw.Title( 'Foobar' );
328 assert.equal( title.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
329 assert.equal( title.getUrl({ action: 'edit' }), '/wiki/Foobar?action=edit', 'Basic functionality, \'params\' parameter' );
330
331 title = new mw.Title( 'John Doe', 3 );
332 assert.equal( title.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
333 } );
334
335 QUnit.test( 'newFromImg', 40, function ( assert ) {
336 var title, i, thisCase, prefix,
337 cases = [
338 {
339 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',
340 typeOfUrl: 'Hashed thumb with shortened path',
341 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)',
342 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'
343 },
344 {
345 url: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
346 typeOfUrl: 'Normal hashed directory thumbnail',
347 nameText: 'Anticlockwise heliotrope\'s',
348 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
349 },
350
351 {
352 url: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
353 typeOfUrl: 'Normal hashed directory thumbnail with complex thumbnail parameters',
354 nameText: 'Wikipedia-logo-v2',
355 prefixedText: 'File:Wikipedia-logo-v2.svg'
356 },
357
358 {
359 url: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
360 typeOfUrl: 'Commons thumbnail',
361 nameText: 'Wikipedia-logo-v2',
362 prefixedText: 'File:Wikipedia-logo-v2.svg'
363 },
364
365 {
366 url: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
367 typeOfUrl: 'Full image',
368 nameText: 'Anticlockwise heliotrope\'s',
369 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
370 },
371
372 {
373 url: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
374 typeOfUrl: 'thumb.php-based thumbnail',
375 nameText: 'Stuffless Figaro\'s',
376 prefixedText: 'File:Stuffless Figaro\'s.jpg'
377 },
378
379 {
380 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
381 typeOfUrl: 'Commons unhashed thumbnail',
382 nameText: 'Wikipedia-logo-v2',
383 prefixedText: 'File:Wikipedia-logo-v2.svg'
384 },
385
386 {
387 url: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
388 typeOfUrl: 'Commons unhashed thumbnail with complex thumbnail parameters',
389 nameText: 'Wikipedia-logo-v2',
390 prefixedText: 'File:Wikipedia-logo-v2.svg'
391 },
392
393 {
394 url: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
395 typeOfUrl: 'Unhashed local file',
396 nameText: 'Anticlockwise heliotrope\'s',
397 prefixedText: 'File:Anticlockwise heliotrope\'s.jpg'
398 },
399
400 {
401 url: '',
402 typeOfUrl: 'Empty string'
403 },
404
405 {
406 url: 'foo',
407 typeOfUrl: 'String with only alphabet characters'
408 },
409
410 {
411 url: 'foobar.foobar',
412 typeOfUrl: 'Not a file path'
413 },
414
415 {
416 url: '/a/a0/blah blah blah',
417 typeOfUrl: 'Space characters'
418 }
419 ];
420
421 for ( i = 0; i < cases.length; i++ ) {
422 thisCase = cases[i];
423 title = mw.Title.newFromImg( { src: thisCase.url } );
424
425 if ( thisCase.nameText !== undefined ) {
426 prefix = '[' + thisCase.typeOfUrl + ' URL' + '] ';
427
428 assert.notStrictEqual( title, null, prefix + 'Parses successfully' );
429 assert.equal( title.getNameText(), thisCase.nameText, prefix + 'Filename matches original' );
430 assert.equal( title.getPrefixedText(), thisCase.prefixedText, prefix + 'File page title matches original' );
431 assert.equal( title.getNamespaceId(), 6, prefix + 'Namespace ID matches File namespace' );
432 } else {
433 assert.strictEqual( title, null, thisCase.typeOfUrl + ', should not produce an mw.Title object' );
434 }
435 }
436 } );
437
438 }( mediaWiki, jQuery ) );