Merge "Fix API output formatting (change lines delimited with * as bold)"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.jqueryMsg.test.js
1 ( function ( mw, $ ) {
2 var mwLanguageCache = {}, oldGetOuterHtml, formatnumTests, specialCharactersPageName,
3 expectedListUsers, expectedEntrypoints;
4
5 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
6 setup: function () {
7 this.orgMwLangauge = mw.language;
8 mw.language = $.extend( true, {}, this.orgMwLangauge );
9 oldGetOuterHtml = $.fn.getOuterHtml;
10 $.fn.getOuterHtml = function () {
11 var $div = $( '<div>' ), html;
12 $div.append( $( this ).eq( 0 ).clone() );
13 html = $div.html();
14 $div.empty();
15 $div = undefined;
16 return html;
17 };
18
19 // Messages that are reused in multiple tests
20 mw.messages.set( {
21 // The values for gender are not significant,
22 // what matters is which of the values is choosen by the parser
23 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
24
25 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
26
27 // Assume the grammar form grammar_case_foo is not valid in any language
28 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
29
30 'formatnum-msg': '{{formatnum:$1}}',
31
32 'portal-url': 'Project:Community portal',
33 'see-portal-url': '{{Int:portal-url}} is an important community page.',
34
35 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
36
37 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
38
39 'external-link-replace': 'Foo [$1 bar]'
40 } );
41
42 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
43
44 expectedListUsers = '注册' + $( '<a>' ).attr( {
45 title: 'Special:ListUsers',
46 href: mw.util.wikiGetlink( 'Special:ListUsers' )
47 } ).text( '用户' ).getOuterHtml();
48
49 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
50 },
51 teardown: function () {
52 mw.language = this.orgMwLangauge;
53 $.fn.getOuterHtml = oldGetOuterHtml;
54 }
55 } ) );
56
57 function getMwLanguage( langCode, cb ) {
58 if ( mwLanguageCache[langCode] !== undefined ) {
59 mwLanguageCache[langCode].add( cb );
60 return;
61 }
62 mwLanguageCache[langCode] = $.Callbacks( 'once memory' );
63 mwLanguageCache[langCode].add( cb );
64 $.ajax( {
65 url: mw.util.wikiScript( 'load' ),
66 data: {
67 skin: mw.config.get( 'skin' ),
68 lang: langCode,
69 debug: mw.config.get( 'debug' ),
70 modules: [
71 'mediawiki.language.data',
72 'mediawiki.language'
73 ].join( '|' ),
74 only: 'scripts'
75 },
76 dataType: 'script'
77 } ).done(function () {
78 mwLanguageCache[langCode].fire( mw.language );
79 } ).fail( function () {
80 mwLanguageCache[langCode].fire( false );
81 } );
82 }
83
84 QUnit.test( 'Replace', 9, function ( assert ) {
85 var parser = mw.jqueryMsg.getMessageFunction();
86
87 mw.messages.set( 'simple', 'Foo $1 baz $2' );
88
89 assert.equal( parser( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
90 assert.equal( parser( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
91 assert.equal( parser( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
92
93 /* Temporarily disabling until 2013-03 --Kaldari
94 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y&lt;</foo>z' );
95
96 assert.equal(
97 parser( 'plain-input', 'bar' ),
98 '&lt;foo foo="foo"&gt;xbary&amp;lt;&lt;/foo&gt;z',
99 'Input is not considered html'
100 );
101
102 mw.messages.set( 'plain-replace', 'Foo $1' );
103
104 assert.equal(
105 parser( 'plain-replace', '<bar bar="bar">&gt;</bar>' ),
106 'Foo &lt;bar bar="bar"&gt;&amp;gt;&lt;/bar&gt;',
107 'Replacement is not considered html'
108 );
109 */
110
111 mw.messages.set( 'object-replace', 'Foo $1' );
112
113 assert.equal(
114 parser( 'object-replace', $( '<div class="bar">&gt;</div>' ) ),
115 'Foo <div class="bar">&gt;</div>',
116 'jQuery objects are preserved as raw html'
117 );
118
119 assert.equal(
120 parser( 'object-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
121 'Foo <div class="bar">&gt;</div>',
122 'HTMLElement objects are preserved as raw html'
123 );
124
125 assert.equal(
126 parser( 'object-replace', $( '<div class="bar">&gt;</div>' ).toArray() ),
127 'Foo <div class="bar">&gt;</div>',
128 'HTMLElement[] arrays are preserved as raw html'
129 );
130
131 assert.equal(
132 parser( 'external-link-replace', 'http://example.org/?x=y&z' ),
133 'Foo <a href="http://example.org/?x=y&amp;z">bar</a>',
134 'Href is not double-escaped in wikilink function'
135 );
136 } );
137
138 QUnit.test( 'Plural', 3, function ( assert ) {
139 var parser = mw.jqueryMsg.getMessageFunction();
140
141 assert.equal( parser( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
142 assert.equal( parser( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
143 assert.equal( parser( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
144 } );
145
146 QUnit.test( 'Gender', 11, function ( assert ) {
147 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
148 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
149 var user = mw.user,
150 parser = mw.jqueryMsg.getMessageFunction();
151
152 user.options.set( 'gender', 'male' );
153 assert.equal(
154 parser( 'gender-msg', 'Bob', 'male' ),
155 'Bob: blue',
156 'Masculine from string "male"'
157 );
158 assert.equal(
159 parser( 'gender-msg', 'Bob', user ),
160 'Bob: blue',
161 'Masculine from mw.user object'
162 );
163
164 user.options.set( 'gender', 'unknown' );
165 assert.equal(
166 parser( 'gender-msg', 'Foo', user ),
167 'Foo: green',
168 'Neutral from mw.user object' );
169 assert.equal(
170 parser( 'gender-msg', 'Alice', 'female' ),
171 'Alice: pink',
172 'Feminine from string "female"' );
173 assert.equal(
174 parser( 'gender-msg', 'User' ),
175 'User: green',
176 'Neutral when no parameter given' );
177 assert.equal(
178 parser( 'gender-msg', 'User', 'unknown' ),
179 'User: green',
180 'Neutral from string "unknown"'
181 );
182
183 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
184
185 assert.equal(
186 parser( 'gender-msg-one-form', 'male', 10 ),
187 'User: 10 edits',
188 'Gender neutral and plural form'
189 );
190 assert.equal(
191 parser( 'gender-msg-one-form', 'female', 1 ),
192 'User: 1 edit',
193 'Gender neutral and singular form'
194 );
195
196 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
197 assert.equal(
198 parser( 'gender-msg-lowercase', 'male' ),
199 'he is awesome',
200 'Gender masculine'
201 );
202 assert.equal(
203 parser( 'gender-msg-lowercase', 'female' ),
204 'she is awesome',
205 'Gender feminine'
206 );
207
208 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
209 assert.equal(
210 parser( 'gender-msg-wrong', 'female' ),
211 ' test',
212 'Invalid syntax should result in {{gender}} simply being stripped away'
213 );
214 } );
215
216 QUnit.test( 'Grammar', 2, function ( assert ) {
217 var parser = mw.jqueryMsg.getMessageFunction();
218
219 assert.equal( parser( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' );
220
221 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
222 assert.equal( parser( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
223 } );
224
225 QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
226 mw.messages.set( mw.libs.phpParserData.messages );
227 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
228 QUnit.stop();
229 getMwLanguage( test.lang, function ( langClass ) {
230 QUnit.start();
231 if ( !langClass ) {
232 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
233 return;
234 }
235 mw.config.set( 'wgUserLanguage', test.lang );
236 var parser = new mw.jqueryMsg.parser( { language: langClass } );
237 assert.equal(
238 parser.parse( test.key, test.args ).html(),
239 test.result,
240 test.name
241 );
242 } );
243 } );
244 } );
245
246 QUnit.test( 'Links', 6, function ( assert ) {
247 var parser = mw.jqueryMsg.getMessageFunction(),
248 expectedDisambiguationsText,
249 expectedMultipleBars,
250 expectedSpecialCharacters;
251
252 /*
253 The below three are all identical to or based on real messages. For disambiguations-text,
254 the bold was removed because it is not yet implemented.
255 */
256
257 assert.equal(
258 parser( 'jquerymsg-test-statistics-users' ),
259 expectedListUsers,
260 'Piped wikilink'
261 );
262
263 expectedDisambiguationsText = 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from ' +
264 $( '<a>' ).attr( {
265 title: 'MediaWiki:Disambiguationspage',
266 href: mw.util.wikiGetlink( 'MediaWiki:Disambiguationspage' )
267 } ).text( 'MediaWiki:Disambiguationspage' ).getOuterHtml() + '.';
268 mw.messages.set( 'disambiguations-text', 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from [[MediaWiki:Disambiguationspage]].' );
269 assert.equal(
270 parser( 'disambiguations-text' ),
271 expectedDisambiguationsText,
272 'Wikilink without pipe'
273 );
274
275 assert.equal(
276 parser( 'jquerymsg-test-version-entrypoints-index-php' ),
277 expectedEntrypoints,
278 'External link'
279 );
280
281 // Pipe trick is not supported currently, but should not parse as text either.
282 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
283 assert.equal(
284 parser( 'pipe-trick' ),
285 'pipe-trick: Parse error at position 0 in input: [[Tampa, Florida|]]',
286 'Pipe trick should return error string.'
287 );
288
289 expectedMultipleBars = $( '<a>' ).attr( {
290 title: 'Main Page',
291 href: mw.util.wikiGetlink( 'Main Page' )
292 } ).text( 'Main|Page' ).getOuterHtml();
293 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
294 assert.equal(
295 parser( 'multiple-bars' ),
296 expectedMultipleBars,
297 'Bar in anchor'
298 );
299
300 expectedSpecialCharacters = $( '<a>' ).attr( {
301 title: specialCharactersPageName,
302 href: mw.util.wikiGetlink( specialCharactersPageName )
303 } ).text( specialCharactersPageName ).getOuterHtml();
304
305 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
306 assert.equal(
307 parser( 'special-characters' ),
308 expectedSpecialCharacters,
309 'Special characters'
310 );
311 } );
312
313 // Tests that {{-transformation vs. general parsing are done as requested
314 QUnit.test( 'Curly brace transformation', 14, function ( assert ) {
315 var formatText, formatParse, oldUserLang;
316
317 oldUserLang = mw.config.get( 'wgUserLanguage' );
318
319 formatText = mw.jqueryMsg.getMessageFunction( {
320 format: 'text'
321 } );
322
323 formatParse = mw.jqueryMsg.getMessageFunction( {
324 format: 'parse'
325 } );
326
327 // When the expected result is the same in both modes
328 function assertBothModes( parserArguments, expectedResult, assertMessage ) {
329 assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
330 assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
331 }
332
333 assertBothModes( ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' );
334
335 assertBothModes( ['plural-msg', 5], 'Found 5 items', 'plural is resolved' );
336
337 assertBothModes( ['grammar-msg'], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
338
339 mw.config.set( 'wgUserLanguage', 'en' );
340 assertBothModes( ['formatnum-msg', '987654321.654321'], '987654321.654321', 'formatnum is resolved' );
341
342 // Test non-{{ wikitext, where behavior differs
343
344 // Wikilink
345 assert.equal(
346 formatText( 'jquerymsg-test-statistics-users' ),
347 mw.messages.get( 'jquerymsg-test-statistics-users' ),
348 'Internal link message unchanged when format is \'text\''
349 );
350 assert.equal(
351 formatParse( 'jquerymsg-test-statistics-users' ),
352 expectedListUsers,
353 'Internal link message parsed when format is \'parse\''
354 );
355
356 // External link
357 assert.equal(
358 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
359 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
360 'External link message unchanged when format is \'text\''
361 );
362 assert.equal(
363 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
364 expectedEntrypoints,
365 'External link message processed when format is \'parse\''
366 );
367
368 // External link with parameter
369 assert.equal(
370 formatText( 'external-link-replace', 'http://example.com' ),
371 'Foo [http://example.com bar]',
372 'External link message only substitutes parameter when format is \'text\''
373 );
374 assert.equal(
375 formatParse( 'external-link-replace', 'http://example.com' ),
376 'Foo <a href="http://example.com">bar</a>',
377 'External link message processed when format is \'parse\''
378 );
379
380 mw.config.set( 'wgUserLanguage', oldUserLang );
381 } );
382
383 QUnit.test( 'Int', 4, function ( assert ) {
384 var parser = mw.jqueryMsg.getMessageFunction(),
385 newarticletextSource = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the [[{{Int:Helppage}}|help page]] for more info). If you are here by mistake, click your browser\'s back button.',
386 expectedNewarticletext;
387
388 mw.messages.set( 'helppage', 'Help:Contents' );
389
390 expectedNewarticletext = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the ' +
391 $( '<a>' ).attr( {
392 title: mw.msg( 'helppage' ),
393 href: mw.util.wikiGetlink( mw.msg( 'helppage' ) )
394 } ).text( 'help page' ).getOuterHtml() + ' for more info). If you are here by mistake, click your browser\'s back button.';
395
396 mw.messages.set( 'newarticletext', newarticletextSource );
397
398 assert.equal(
399 parser( 'newarticletext' ),
400 expectedNewarticletext,
401 'Link with nested message'
402 );
403
404 assert.equal(
405 parser( 'see-portal-url' ),
406 'Project:Community portal is an important community page.',
407 'Nested message'
408 );
409
410 mw.messages.set( 'newarticletext-lowercase',
411 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
412
413 assert.equal(
414 parser( 'newarticletext-lowercase' ),
415 expectedNewarticletext,
416 'Link with nested message, lowercase include'
417 );
418
419 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
420
421 assert.equal(
422 parser( 'uses-missing-int' ),
423 '[doesnt-exist]',
424 'int: where nested message does not exist'
425 );
426 } );
427
428 // Tests that getMessageFunction is used for non-plain messages with curly braces or
429 // square brackets, but not otherwise.
430 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
431 var oldGMF, outerCalled, innerCalled;
432
433 mw.messages.set( {
434 'curly-brace': '{{int:message}}',
435 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
436 'double-square-bracket': '[[Some page]]',
437 'regular': 'Other message'
438 } );
439
440 oldGMF = mw.jqueryMsg.getMessageFunction;
441
442 mw.jqueryMsg.getMessageFunction = function () {
443 outerCalled = true;
444 return function () {
445 innerCalled = true;
446 };
447 };
448
449 function verifyGetMessageFunction( key, format, shouldCall ) {
450 var message;
451 outerCalled = false;
452 innerCalled = false;
453 message = mw.message( key );
454 message[format]();
455 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
456 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
457 }
458
459 verifyGetMessageFunction( 'curly-brace', 'parse', true );
460 verifyGetMessageFunction( 'curly-brace', 'plain', false );
461
462 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
463 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
464
465 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
466 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
467
468 verifyGetMessageFunction( 'regular', 'parse', false );
469 verifyGetMessageFunction( 'regular', 'plain', false );
470
471 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
472 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
473 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
474
475 mw.jqueryMsg.getMessageFunction = oldGMF;
476 } );
477
478 formatnumTests = [
479 {
480 lang: 'en',
481 number: 987654321.654321,
482 result: '987654321.654321',
483 description: 'formatnum test for English, decimal seperator'
484 },
485 {
486 lang: 'ar',
487 number: 987654321.654321,
488 result: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
489 description: 'formatnum test for Arabic, with decimal seperator'
490 },
491 {
492 lang: 'ar',
493 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
494 result: 987654321,
495 integer: true,
496 description: 'formatnum test for Arabic, with decimal seperator, reverse'
497 },
498 {
499 lang: 'ar',
500 number: -12.89,
501 result: '-١٢٫٨٩',
502 description: 'formatnum test for Arabic, negative number'
503 },
504 {
505 lang: 'ar',
506 number: '-١٢٫٨٩',
507 result: -12,
508 integer: true,
509 description: 'formatnum test for Arabic, negative number, reverse'
510 },
511 {
512 lang: 'nl',
513 number: 987654321.654321,
514 result: '987654321,654321',
515 description: 'formatnum test for Nederlands, decimal seperator'
516 },
517 {
518 lang: 'nl',
519 number: -12.89,
520 result: '-12,89',
521 description: 'formatnum test for Nederlands, negative number'
522 },
523 {
524 lang: 'nl',
525 number: 'invalidnumber',
526 result: 'invalidnumber',
527 description: 'formatnum test for Nederlands, invalid number'
528 }
529 ];
530
531 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
532 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
533 $.each( formatnumTests, function ( i, test ) {
534 QUnit.stop();
535 getMwLanguage( test.lang, function ( langClass ) {
536 QUnit.start();
537 if ( !langClass ) {
538 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
539 return;
540 }
541 mw.messages.set( test.message );
542 mw.config.set( 'wgUserLanguage', test.lang );
543 var parser = new mw.jqueryMsg.parser( { language: langClass } );
544 assert.equal(
545 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
546 [ test.number ] ).html(),
547 test.result,
548 test.description
549 );
550 } );
551 } );
552 } );
553
554 }( mediaWiki, jQuery ) );