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