Merge "Improve "selfmove" message's wording"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.jqueryMsg.test.js
1 ( function ( mw, $ ) {
2 /* eslint-disable camelcase */
3 var formatText, formatParse, formatnumTests, specialCharactersPageName, expectedListUsers,
4 expectedListUsersSitename, expectedLinkPagenamee, expectedEntrypoints,
5 mwLanguageCache = {},
6 hasOwn = Object.hasOwnProperty;
7
8 // When the expected result is the same in both modes
9 function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) {
10 assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
11 assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
12 }
13
14 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
15 setup: function () {
16 this.originalMwLanguage = mw.language;
17 this.parserDefaults = mw.jqueryMsg.getParserDefaults();
18 mw.jqueryMsg.setParserDefaults( {
19 magic: {
20 PAGENAME: '2 + 2',
21 PAGENAMEE: mw.util.wikiUrlencode( '2 + 2' ),
22 SITENAME: 'Wiki'
23 }
24 } );
25
26 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
27
28 expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
29 expectedListUsersSitename = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
30 'Wiki</a>';
31 expectedLinkPagenamee = '<a href="https://example.org/wiki/Foo?bar=baz#val/2_%2B_2">Test</a>';
32
33 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
34
35 formatText = mw.jqueryMsg.getMessageFunction( {
36 format: 'text'
37 } );
38
39 formatParse = mw.jqueryMsg.getMessageFunction( {
40 format: 'parse'
41 } );
42 },
43 teardown: function () {
44 mw.language = this.originalMwLanguage;
45 mw.jqueryMsg.setParserDefaults( this.parserDefaults );
46 },
47 config: {
48 wgArticlePath: '/wiki/$1',
49 wgNamespaceIds: {
50 template: 10,
51 template_talk: 11,
52 // Localised
53 szablon: 10,
54 dyskusja_szablonu: 11
55 },
56 wgFormattedNamespaces: {
57 // Localised
58 10: 'Szablon',
59 11: 'Dyskusja szablonu'
60 }
61 },
62 // Messages that are reused in multiple tests
63 messages: {
64 // The values for gender are not significant,
65 // what matters is which of the values is choosen by the parser
66 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
67 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
68
69 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
70 // See https://phabricator.wikimedia.org/T71993
71 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
72 // Assume the grammar form grammar_case_foo is not valid in any language
73 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
74
75 'formatnum-msg': '{{formatnum:$1}}',
76
77 'portal-url': 'Project:Community portal',
78 'see-portal-url': '{{Int:portal-url}} is an important community page.',
79
80 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
81 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
82 'jquerymsg-test-link-pagenamee': '[https://example.org/wiki/Foo?bar=baz#val/{{PAGENAMEE}} Test]',
83
84 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
85
86 'external-link-replace': 'Foo [$1 bar]',
87 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
88 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
89 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
90 }
91 } ) );
92
93 /**
94 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
95 * to transport the module back to the test. It musn't be overwritten concurrentely.
96 *
97 * This function caches the mw.language data to avoid having to request the same module
98 * multiple times. There is more than one test case for any given language.
99 */
100 function getMwLanguage( langCode ) {
101 if ( !hasOwn.call( mwLanguageCache, langCode ) ) {
102 mwLanguageCache[ langCode ] = $.ajax( {
103 url: mw.util.wikiScript( 'load' ),
104 data: {
105 skin: mw.config.get( 'skin' ),
106 lang: langCode,
107 debug: mw.config.get( 'debug' ),
108 modules: [
109 'mediawiki.language.data',
110 'mediawiki.language'
111 ].join( '|' ),
112 only: 'scripts'
113 },
114 dataType: 'script',
115 cache: true
116 } ).then( function () {
117 return mw.language;
118 } );
119 }
120 return mwLanguageCache[ langCode ];
121 }
122
123 /**
124 * @param {Function[]} tasks List of functions that perform tasks
125 * that may be asynchronous. Invoke the callback parameter when done.
126 */
127 function process( tasks ) {
128 function abort() {
129 tasks.splice( 0, tasks.length );
130 // eslint-disable-next-line no-use-before-define
131 next();
132 }
133 function next() {
134 var task;
135 if ( !tasks ) {
136 // This happens if after the process is completed, one of our callbacks is
137 // invoked. This can happen if a test timed out but the process was still
138 // running. In that case, ignore it. Don't invoke complete() a second time.
139 return;
140 }
141 task = tasks.shift();
142 if ( task ) {
143 task( next, abort );
144 } else {
145 // Remove tasks list to indicate the process is final.
146 tasks = null;
147 }
148 }
149 next();
150 }
151
152 QUnit.test( 'Replace', function ( assert ) {
153 mw.messages.set( 'simple', 'Foo $1 baz $2' );
154
155 assert.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
156 assert.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
157 assert.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
158
159 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y&lt;</foo>z' );
160
161 assert.equal(
162 formatParse( 'plain-input', 'bar' ),
163 '&lt;foo foo="foo"&gt;xbary&amp;lt;&lt;/foo&gt;z',
164 'Input is not considered html'
165 );
166
167 mw.messages.set( 'plain-replace', 'Foo $1' );
168
169 assert.equal(
170 formatParse( 'plain-replace', '<bar bar="bar">&gt;</bar>' ),
171 'Foo &lt;bar bar="bar"&gt;&amp;gt;&lt;/bar&gt;',
172 'Replacement is not considered html'
173 );
174
175 mw.messages.set( 'object-replace', 'Foo $1' );
176
177 assert.equal(
178 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ) ),
179 'Foo <div class="bar">&gt;</div>',
180 'jQuery objects are preserved as raw html'
181 );
182
183 assert.equal(
184 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
185 'Foo <div class="bar">&gt;</div>',
186 'HTMLElement objects are preserved as raw html'
187 );
188
189 assert.equal(
190 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).toArray() ),
191 'Foo <div class="bar">&gt;</div>',
192 'HTMLElement[] arrays are preserved as raw html'
193 );
194
195 assert.equal(
196 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
197 'Foo <a href="http://example.org/?x=y&amp;z">bar</a>',
198 'Href is not double-escaped in wikilink function'
199 );
200 assert.equal(
201 formatParse( 'external-link-plural', 1, 'http://example.org' ),
202 'Foo is <a href="http://example.org">one</a> things.',
203 'Link is expanded inside plural and is not escaped html'
204 );
205 assert.equal(
206 formatParse( 'external-link-plural', 2, 'http://example.org' ),
207 'Foo <a href="http://example.org">two</a> things.',
208 'Link is expanded inside an explicit plural form and is not escaped html'
209 );
210 assert.equal(
211 formatParse( 'external-link-plural', 3 ),
212 'Foo three things.',
213 'A simple explicit plural form co-existing with complex explicit plural forms'
214 );
215 assert.equal(
216 formatParse( 'external-link-plural', 4, 'http://example.org' ),
217 'Foo a=b things.',
218 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
219 );
220 assert.equal(
221 formatParse( 'external-link-plural', 6, 'http://example.org' ),
222 'Foo are <a href="http://example.org">some</a> things.',
223 'Plural fallback to the "other" plural form'
224 );
225 assert.equal(
226 formatParse( 'plural-only-explicit-forms', 2 ),
227 'It is a double room.',
228 'Plural with explicit forms alone.'
229 );
230 } );
231
232 QUnit.test( 'Plural', function ( assert ) {
233 assert.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
234 assert.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
235 assert.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
236 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
237 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in Wiki', 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
238 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
239 assert.equal( formatParse( 'plural-empty-explicit-form', 0 ), 'There is me.' );
240 assert.equal( formatParse( 'plural-empty-explicit-form', 1 ), 'There is me and other people.' );
241 assert.equal( formatParse( 'plural-empty-explicit-form', 2 ), 'There is me and other people.' );
242 } );
243
244 QUnit.test( 'Gender', function ( assert ) {
245 var originalGender = mw.user.options.get( 'gender' );
246
247 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
248 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
249 mw.user.options.set( 'gender', 'male' );
250 assert.equal(
251 formatParse( 'gender-msg', 'Bob', 'male' ),
252 'Bob: blue',
253 'Masculine from string "male"'
254 );
255 assert.equal(
256 formatParse( 'gender-msg', 'Bob', mw.user ),
257 'Bob: blue',
258 'Masculine from mw.user object'
259 );
260 assert.equal(
261 formatParse( 'gender-msg-currentuser' ),
262 'blue',
263 'Masculine for current user'
264 );
265
266 mw.user.options.set( 'gender', 'female' );
267 assert.equal(
268 formatParse( 'gender-msg', 'Alice', 'female' ),
269 'Alice: pink',
270 'Feminine from string "female"' );
271 assert.equal(
272 formatParse( 'gender-msg', 'Alice', mw.user ),
273 'Alice: pink',
274 'Feminine from mw.user object'
275 );
276 assert.equal(
277 formatParse( 'gender-msg-currentuser' ),
278 'pink',
279 'Feminine for current user'
280 );
281
282 mw.user.options.set( 'gender', 'unknown' );
283 assert.equal(
284 formatParse( 'gender-msg', 'Foo', mw.user ),
285 'Foo: green',
286 'Neutral from mw.user object' );
287 assert.equal(
288 formatParse( 'gender-msg', 'User' ),
289 'User: green',
290 'Neutral when no parameter given' );
291 assert.equal(
292 formatParse( 'gender-msg', 'User', 'unknown' ),
293 'User: green',
294 'Neutral from string "unknown"'
295 );
296 assert.equal(
297 formatParse( 'gender-msg-currentuser' ),
298 'green',
299 'Neutral for current user'
300 );
301
302 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
303
304 assert.equal(
305 formatParse( 'gender-msg-one-form', 'male', 10 ),
306 'User: 10 edits',
307 'Gender neutral and plural form'
308 );
309 assert.equal(
310 formatParse( 'gender-msg-one-form', 'female', 1 ),
311 'User: 1 edit',
312 'Gender neutral and singular form'
313 );
314
315 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
316 assert.equal(
317 formatParse( 'gender-msg-lowercase', 'male' ),
318 'he is awesome',
319 'Gender masculine'
320 );
321 assert.equal(
322 formatParse( 'gender-msg-lowercase', 'female' ),
323 'she is awesome',
324 'Gender feminine'
325 );
326
327 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
328 assert.equal(
329 formatParse( 'gender-msg-wrong', 'female' ),
330 ' test',
331 'Invalid syntax should result in {{gender}} simply being stripped away'
332 );
333
334 mw.user.options.set( 'gender', originalGender );
335 } );
336
337 QUnit.test( 'Case changing', function ( assert ) {
338 mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
339 assert.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
340
341 mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
342 assert.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
343
344 mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
345 mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
346 assert.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
347 assert.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
348
349 mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
350 mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
351 assert.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
352 assert.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
353
354 mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
355 assert.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
356 mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
357 assert.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
358 } );
359
360 QUnit.test( 'Grammar', function ( assert ) {
361 assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );
362
363 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
364 assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
365 } );
366
367 QUnit.test( 'Match PHP parser', function ( assert ) {
368 var tasks;
369 mw.messages.set( mw.libs.phpParserData.messages );
370 tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
371 var done = assert.async();
372 return function ( next, abort ) {
373 getMwLanguage( test.lang )
374 .then( function ( langClass ) {
375 var parser;
376 mw.config.set( 'wgUserLanguage', test.lang );
377 // eslint-disable-next-line new-cap
378 parser = new mw.jqueryMsg.parser( { language: langClass } );
379 assert.equal(
380 parser.parse( test.key, test.args ).html(),
381 test.result,
382 test.name
383 );
384 }, function () {
385 assert.ok( false, 'Language "' + test.lang + '" failed to load.' );
386 } )
387 .then( done, done )
388 .then( next, abort );
389 };
390 } );
391
392 process( tasks );
393 } );
394
395 QUnit.test( 'Links', function ( assert ) {
396 var testCases,
397 expectedDisambiguationsText,
398 expectedMultipleBars,
399 expectedSpecialCharacters;
400
401 // The below three are all identical to or based on real messages. For disambiguations-text,
402 // the bold was removed because it is not yet implemented.
403
404 assert.htmlEqual(
405 formatParse( 'jquerymsg-test-statistics-users' ),
406 expectedListUsers,
407 'Piped wikilink'
408 );
409
410 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 ' +
411 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
412
413 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]].' );
414 assert.htmlEqual(
415 formatParse( 'disambiguations-text' ),
416 expectedDisambiguationsText,
417 'Wikilink without pipe'
418 );
419
420 assert.htmlEqual(
421 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
422 expectedEntrypoints,
423 'External link'
424 );
425
426 // Pipe trick is not supported currently, but should not parse as text either.
427 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
428 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
429 mw.messages.set( 'empty-link', '[[]]' );
430 this.suppressWarnings();
431 assert.equal(
432 formatParse( 'pipe-trick' ),
433 '[[Tampa, Florida|]]',
434 'Pipe trick should not be parsed.'
435 );
436 assert.equal(
437 formatParse( 'reverse-pipe-trick' ),
438 '[[|Tampa, Florida]]',
439 'Reverse pipe trick should not be parsed.'
440 );
441 assert.equal(
442 formatParse( 'empty-link' ),
443 '[[]]',
444 'Empty link should not be parsed.'
445 );
446 this.restoreWarnings();
447
448 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
449 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
450 assert.htmlEqual(
451 formatParse( 'multiple-bars' ),
452 expectedMultipleBars,
453 'Bar in anchor'
454 );
455
456 expectedSpecialCharacters = '<a title="&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?</a>';
457
458 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
459 assert.htmlEqual(
460 formatParse( 'special-characters' ),
461 expectedSpecialCharacters,
462 'Special characters'
463 );
464
465 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
466 assert.htmlEqual(
467 formatParse( 'leading-colon' ),
468 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
469 'Leading colon in links is stripped'
470 );
471
472 assert.htmlEqual(
473 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
474 expectedListUsersSitename,
475 'Piped wikilink with parser function in the text'
476 );
477
478 assert.htmlEqual(
479 formatParse( 'jquerymsg-test-link-pagenamee' ),
480 expectedLinkPagenamee,
481 'External link with parser function in the URL'
482 );
483
484 testCases = [
485 [
486 'extlink-html-full',
487 'asd [http://example.org <strong>Example</strong>] asd',
488 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
489 ],
490 [
491 'extlink-html-partial',
492 'asd [http://example.org foo <strong>Example</strong> bar] asd',
493 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
494 ],
495 [
496 'wikilink-html-full',
497 'asd [[Example|<strong>Example</strong>]] asd',
498 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
499 ],
500 [
501 'wikilink-html-partial',
502 'asd [[Example|foo <strong>Example</strong> bar]] asd',
503 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
504 ]
505 ];
506
507 $.each( testCases, function () {
508 var
509 key = this[ 0 ],
510 input = this[ 1 ],
511 output = this[ 2 ];
512 mw.messages.set( key, input );
513 assert.htmlEqual(
514 formatParse( key ),
515 output,
516 'HTML in links: ' + key
517 );
518 } );
519 } );
520
521 QUnit.test( 'Replacements in links', function ( assert ) {
522 var testCases = [
523 [
524 'extlink-param-href-full',
525 'asd [$1 Example] asd',
526 'asd <a href="http://example.com">Example</a> asd'
527 ],
528 [
529 'extlink-param-href-partial',
530 'asd [$1/example Example] asd',
531 'asd <a href="http://example.com/example">Example</a> asd'
532 ],
533 [
534 'extlink-param-text-full',
535 'asd [http://example.org $2] asd',
536 'asd <a href="http://example.org">Text</a> asd'
537 ],
538 [
539 'extlink-param-text-partial',
540 'asd [http://example.org Example $2] asd',
541 'asd <a href="http://example.org">Example Text</a> asd'
542 ],
543 [
544 'extlink-param-both-full',
545 'asd [$1 $2] asd',
546 'asd <a href="http://example.com">Text</a> asd'
547 ],
548 [
549 'extlink-param-both-partial',
550 'asd [$1/example Example $2] asd',
551 'asd <a href="http://example.com/example">Example Text</a> asd'
552 ],
553 [
554 'wikilink-param-href-full',
555 'asd [[$1|Example]] asd',
556 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
557 ],
558 [
559 'wikilink-param-href-partial',
560 'asd [[$1/Test|Example]] asd',
561 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
562 ],
563 [
564 'wikilink-param-text-full',
565 'asd [[Example|$2]] asd',
566 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
567 ],
568 [
569 'wikilink-param-text-partial',
570 'asd [[Example|Example $2]] asd',
571 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
572 ],
573 [
574 'wikilink-param-both-full',
575 'asd [[$1|$2]] asd',
576 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
577 ],
578 [
579 'wikilink-param-both-partial',
580 'asd [[$1/Test|Example $2]] asd',
581 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
582 ],
583 [
584 'wikilink-param-unpiped-full',
585 'asd [[$1]] asd',
586 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
587 ],
588 [
589 'wikilink-param-unpiped-partial',
590 'asd [[$1/Test]] asd',
591 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
592 ]
593 ];
594
595 $.each( testCases, function () {
596 var
597 key = this[ 0 ],
598 input = this[ 1 ],
599 output = this[ 2 ],
600 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
601 paramText = 'Text';
602 mw.messages.set( key, input );
603 assert.htmlEqual(
604 formatParse( key, paramHref, paramText ),
605 output,
606 'Replacements in links: ' + key
607 );
608 } );
609 } );
610
611 // Tests that {{-transformation vs. general parsing are done as requested
612 QUnit.test( 'Curly brace transformation', function ( assert ) {
613 var oldUserLang = mw.config.get( 'wgUserLanguage' );
614
615 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
616
617 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
618
619 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
620
621 mw.config.set( 'wgUserLanguage', 'en' );
622 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
623
624 // Test non-{{ wikitext, where behavior differs
625
626 // Wikilink
627 assert.equal(
628 formatText( 'jquerymsg-test-statistics-users' ),
629 mw.messages.get( 'jquerymsg-test-statistics-users' ),
630 'Internal link message unchanged when format is \'text\''
631 );
632 assert.htmlEqual(
633 formatParse( 'jquerymsg-test-statistics-users' ),
634 expectedListUsers,
635 'Internal link message parsed when format is \'parse\''
636 );
637
638 // External link
639 assert.equal(
640 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
641 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
642 'External link message unchanged when format is \'text\''
643 );
644 assert.htmlEqual(
645 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
646 expectedEntrypoints,
647 'External link message processed when format is \'parse\''
648 );
649
650 // External link with parameter
651 assert.equal(
652 formatText( 'external-link-replace', 'http://example.com' ),
653 'Foo [http://example.com bar]',
654 'External link message only substitutes parameter when format is \'text\''
655 );
656 assert.htmlEqual(
657 formatParse( 'external-link-replace', 'http://example.com' ),
658 'Foo <a href="http://example.com">bar</a>',
659 'External link message processed when format is \'parse\''
660 );
661 assert.htmlEqual(
662 formatParse( 'external-link-replace', $( '<i>' ) ),
663 'Foo <i>bar</i>',
664 'External link message processed as jQuery object when format is \'parse\''
665 );
666 assert.htmlEqual(
667 formatParse( 'external-link-replace', function () {} ),
668 'Foo <a role="button" tabindex="0">bar</a>',
669 'External link message processed as function when format is \'parse\''
670 );
671
672 mw.config.set( 'wgUserLanguage', oldUserLang );
673 } );
674
675 QUnit.test( 'Int', function ( assert ) {
676 var 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:Foobar}}|foobar]] for more info). If you are here by mistake, click your browser\'s back button.',
677 expectedNewarticletext,
678 helpPageTitle = 'Help:Foobar';
679
680 mw.messages.set( 'foobar', helpPageTitle );
681
682 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 ' +
683 '<a title="Help:Foobar" href="/wiki/Help:Foobar">foobar</a> for more info). If you are here by mistake, click your browser\'s back button.';
684
685 mw.messages.set( 'newarticletext', newarticletextSource );
686
687 assert.htmlEqual(
688 formatParse( 'newarticletext' ),
689 expectedNewarticletext,
690 'Link with nested message'
691 );
692
693 assert.equal(
694 formatParse( 'see-portal-url' ),
695 'Project:Community portal is an important community page.',
696 'Nested message'
697 );
698
699 mw.messages.set( 'newarticletext-lowercase',
700 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
701
702 assert.htmlEqual(
703 formatParse( 'newarticletext-lowercase' ),
704 expectedNewarticletext,
705 'Link with nested message, lowercase include'
706 );
707
708 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
709
710 assert.equal(
711 formatParse( 'uses-missing-int' ),
712 '⧼doesnt-exist⧽',
713 'int: where nested message does not exist'
714 );
715 } );
716
717 QUnit.test( 'Ns', function ( assert ) {
718 mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
719 assert.equal(
720 formatParse( 'ns-template-talk' ),
721 'Dyskusja szablonu',
722 'ns: returns localised namespace when used with a canonical namespace name'
723 );
724
725 mw.messages.set( 'ns-10', '{{ns:10}}' );
726 assert.equal(
727 formatParse( 'ns-10' ),
728 'Szablon',
729 'ns: returns localised namespace when used with a namespace number'
730 );
731
732 mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
733 assert.equal(
734 formatParse( 'ns-unknown' ),
735 '',
736 'ns: returns empty string for unknown namespace name'
737 );
738
739 mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
740 assert.equal(
741 formatParse( 'ns-in-a-link' ),
742 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
743 'ns: works when used inside a wikilink'
744 );
745 } );
746
747 // Tests that getMessageFunction is used for non-plain messages with curly braces or
748 // square brackets, but not otherwise.
749 QUnit.test( 'mw.Message.prototype.parser monkey-patch', function ( assert ) {
750 var oldGMF, outerCalled, innerCalled;
751
752 mw.messages.set( {
753 'curly-brace': '{{int:message}}',
754 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
755 'double-square-bracket': '[[Some page]]',
756 regular: 'Other message'
757 } );
758
759 oldGMF = mw.jqueryMsg.getMessageFunction;
760
761 mw.jqueryMsg.getMessageFunction = function () {
762 outerCalled = true;
763 return function () {
764 innerCalled = true;
765 };
766 };
767
768 function verifyGetMessageFunction( key, format, shouldCall ) {
769 var message;
770 outerCalled = false;
771 innerCalled = false;
772 message = mw.message( key );
773 message[ format ]();
774 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
775 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
776 delete mw.messages[ format ];
777 }
778
779 verifyGetMessageFunction( 'curly-brace', 'parse', true );
780 verifyGetMessageFunction( 'curly-brace', 'plain', false );
781
782 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
783 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
784
785 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
786 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
787
788 verifyGetMessageFunction( 'regular', 'parse', false );
789 verifyGetMessageFunction( 'regular', 'plain', false );
790
791 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
792 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
793 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
794
795 mw.jqueryMsg.getMessageFunction = oldGMF;
796 } );
797
798 formatnumTests = [
799 {
800 lang: 'en',
801 number: 987654321.654321,
802 result: '987,654,321.654',
803 description: 'formatnum test for English, decimal separator'
804 },
805 {
806 lang: 'ar',
807 number: 987654321.654321,
808 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
809 description: 'formatnum test for Arabic, with decimal separator'
810 },
811 {
812 lang: 'ar',
813 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
814 result: 987654321,
815 integer: true,
816 description: 'formatnum test for Arabic, with decimal separator, reverse'
817 },
818 {
819 lang: 'ar',
820 number: -12.89,
821 result: '-١٢٫٨٩',
822 description: 'formatnum test for Arabic, negative number'
823 },
824 {
825 lang: 'ar',
826 number: '-١٢٫٨٩',
827 result: -12,
828 integer: true,
829 description: 'formatnum test for Arabic, negative number, reverse'
830 },
831 {
832 lang: 'nl',
833 number: 987654321.654321,
834 result: '987.654.321,654',
835 description: 'formatnum test for Nederlands, decimal separator'
836 },
837 {
838 lang: 'nl',
839 number: -12.89,
840 result: '-12,89',
841 description: 'formatnum test for Nederlands, negative number'
842 },
843 {
844 lang: 'nl',
845 number: '.89',
846 result: '0,89',
847 description: 'formatnum test for Nederlands'
848 },
849 {
850 lang: 'nl',
851 number: 'invalidnumber',
852 result: 'invalidnumber',
853 description: 'formatnum test for Nederlands, invalid number'
854 },
855 {
856 lang: 'ml',
857 number: '1000000000',
858 result: '1,00,00,00,000',
859 description: 'formatnum test for Malayalam'
860 },
861 {
862 lang: 'ml',
863 number: '-1000000000',
864 result: '-1,00,00,00,000',
865 description: 'formatnum test for Malayalam, negative number'
866 },
867 /*
868 * This will fail because of wrong pattern for ml in MW(different from CLDR)
869 {
870 lang: 'ml',
871 number: '1000000000.000',
872 result: '1,00,00,00,000.000',
873 description: 'formatnum test for Malayalam with decimal place'
874 },
875 */
876 {
877 lang: 'hi',
878 number: '123456789.123456789',
879 result: '१२,३४,५६,७८९',
880 description: 'formatnum test for Hindi'
881 },
882 {
883 lang: 'hi',
884 number: '१२,३४,५६,७८९',
885 result: '१२,३४,५६,७८९',
886 description: 'formatnum test for Hindi, Devanagari digits passed'
887 },
888 {
889 lang: 'hi',
890 number: '१,२३,४५६',
891 result: '123456',
892 integer: true,
893 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
894 }
895 ];
896
897 QUnit.test( 'formatnum', function ( assert ) {
898 var queue;
899 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
900 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
901 queue = $.map( formatnumTests, function ( test ) {
902 var done = assert.async();
903 return function ( next, abort ) {
904 getMwLanguage( test.lang )
905 .then( function ( langClass ) {
906 var parser;
907 mw.config.set( 'wgUserLanguage', test.lang );
908 // eslint-disable-next-line new-cap
909 parser = new mw.jqueryMsg.parser( { language: langClass } );
910 assert.equal(
911 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
912 [ test.number ] ).html(),
913 test.result,
914 test.description
915 );
916 }, function () {
917 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
918 } )
919 .then( done, done )
920 .then( next, abort );
921 };
922 } );
923 process( queue );
924 } );
925
926 // HTML in wikitext
927 QUnit.test( 'HTML', function ( assert ) {
928 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
929
930 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
931
932 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
933 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
934
935 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
936 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
937
938 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
939 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
940
941 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
942
943 assert.htmlEqual(
944 formatParse( 'jquerymsg-italics-with-link' ),
945 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
946 'Italics with link inside in parse mode'
947 );
948
949 assert.equal(
950 formatText( 'jquerymsg-italics-with-link' ),
951 mw.messages.get( 'jquerymsg-italics-with-link' ),
952 'Italics with link unchanged in text mode'
953 );
954
955 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
956 assert.htmlEqual(
957 formatParse( 'jquerymsg-italics-id-class' ),
958 mw.messages.get( 'jquerymsg-italics-id-class' ),
959 'ID and class are allowed'
960 );
961
962 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
963 assert.htmlEqual(
964 formatParse( 'jquerymsg-italics-onclick' ),
965 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
966 'element with onclick is escaped because it is not allowed'
967 );
968
969 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
970 assert.htmlEqual(
971 formatParse( 'jquerymsg-script-msg' ),
972 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
973 'Tag outside whitelist escaped in parse mode'
974 );
975
976 assert.equal(
977 formatText( 'jquerymsg-script-msg' ),
978 mw.messages.get( 'jquerymsg-script-msg' ),
979 'Tag outside whitelist unchanged in text mode'
980 );
981
982 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
983 assert.htmlEqual(
984 formatParse( 'jquerymsg-script-link-msg' ),
985 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
986 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
987 );
988
989 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
990 assert.htmlEqual(
991 formatParse( 'jquerymsg-mismatched-html' ),
992 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
993 'Mismatched HTML start and end tag treated as text'
994 );
995
996 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
997 assert.htmlEqual(
998 formatParse( 'jquerymsg-script-and-external-link' ),
999 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com"><i>Foo</i> bar</a>',
1000 'HTML tags in external links not interfering with escaping of other tags'
1001 );
1002
1003 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
1004 assert.htmlEqual(
1005 formatParse( 'jquerymsg-link-script' ),
1006 '<a href="http://example.com">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</a>',
1007 'Non-whitelisted HTML tag in external link anchor treated as text'
1008 );
1009
1010 // Intentionally not using htmlEqual for the quote tests
1011 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
1012 assert.equal(
1013 formatParse( 'jquerymsg-double-quotes-preserved' ),
1014 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
1015 'Attributes with double quotes are preserved as such'
1016 );
1017
1018 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1019 assert.equal(
1020 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1021 '<i id="single">Single</i>',
1022 'Attributes with single quotes are normalized to double'
1023 );
1024
1025 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
1026 assert.htmlEqual(
1027 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1028 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1029 'Escaped attributes are parsed correctly'
1030 );
1031
1032 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
1033 assert.htmlEqual(
1034 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1035 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1036 'Escaped attributes are parsed correctly'
1037 );
1038
1039 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1040 assert.htmlEqual(
1041 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1042 '<i><a href="http://example.com">Example</a></i>',
1043 'Contents of valid tag are treated as wikitext, so external link is parsed'
1044 );
1045
1046 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1047 assert.htmlEqual(
1048 formatParse( 'jquerymsg-wikitext-contents-script' ),
1049 '<i>&lt;script&gt;Script inside&lt;/script&gt;</i>',
1050 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1051 );
1052
1053 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1054 assert.htmlEqual(
1055 formatParse( 'jquerymsg-unclosed-tag' ),
1056 'Foo&lt;tag&gt;bar',
1057 'Nonsupported unclosed tags are escaped'
1058 );
1059
1060 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1061 assert.htmlEqual(
1062 formatParse( 'jquerymsg-self-closing-tag' ),
1063 'Foo&lt;tag/&gt;bar',
1064 'Self-closing tags don\'t cause a parse error'
1065 );
1066
1067 mw.messages.set( 'jquerymsg-asciialphabetliteral-regression', '<b >>>="dir">asd</b>' );
1068 assert.htmlEqual(
1069 formatParse( 'jquerymsg-asciialphabetliteral-regression' ),
1070 '<b>&gt;&gt;="dir"&gt;asd</b>',
1071 'Regression test for bad "asciiAlphabetLiteral" definition'
1072 );
1073
1074 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
1075 mw.messages.set( 'jquerymsg-entities2', 'A&gt;B' );
1076 mw.messages.set( 'jquerymsg-entities3', 'A&rarr;B' );
1077 assert.htmlEqual(
1078 formatParse( 'jquerymsg-entities1' ),
1079 'A&amp;B',
1080 'Lone "&" is escaped in text'
1081 );
1082 assert.htmlEqual(
1083 formatParse( 'jquerymsg-entities2' ),
1084 'A&amp;gt;B',
1085 '"&gt;" entity is double-escaped in text' // (WHY?)
1086 );
1087 assert.htmlEqual(
1088 formatParse( 'jquerymsg-entities3' ),
1089 'A&amp;rarr;B',
1090 '"&rarr;" entity is double-escaped in text'
1091 );
1092
1093 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1094 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A&gt;B"></i>' );
1095 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A&rarr;B"></i>' );
1096 assert.htmlEqual(
1097 formatParse( 'jquerymsg-entities-attr1' ),
1098 '<i title="A&amp;B"></i>',
1099 'Lone "&" is escaped in attribute'
1100 );
1101 assert.htmlEqual(
1102 formatParse( 'jquerymsg-entities-attr2' ),
1103 '<i title="A&gt;B"></i>',
1104 '"&gt;" entity is not double-escaped in attribute' // (WHY?)
1105 );
1106 assert.htmlEqual(
1107 formatParse( 'jquerymsg-entities-attr3' ),
1108 '<i title="A&amp;rarr;B"></i>',
1109 '"&rarr;" entity is double-escaped in attribute'
1110 );
1111 } );
1112
1113 QUnit.test( 'Nowiki', function ( assert ) {
1114 mw.messages.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1115 assert.equal(
1116 formatParse( 'jquerymsg-nowiki-link' ),
1117 'Foo [[bar]] baz.',
1118 'Link inside nowiki is not parsed'
1119 );
1120
1121 mw.messages.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1122 assert.equal(
1123 formatParse( 'jquerymsg-nowiki-htmltag' ),
1124 'Foo &lt;b&gt;bar&lt;/b&gt; baz.',
1125 'HTML inside nowiki is not parsed and escaped'
1126 );
1127
1128 mw.messages.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1129 assert.equal(
1130 formatParse( 'jquerymsg-nowiki-template' ),
1131 'Foo {{bar}} baz.',
1132 'Template inside nowiki is not parsed and does not cause a parse error'
1133 );
1134 } );
1135
1136 QUnit.test( 'Behavior in case of invalid wikitext', function ( assert ) {
1137 var logSpy;
1138 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1139
1140 this.suppressWarnings();
1141 logSpy = this.sandbox.spy( mw.log, 'warn' );
1142
1143 assert.equal(
1144 formatParse( 'invalid-wikitext' ),
1145 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
1146 'Invalid wikitext: \'parse\' format'
1147 );
1148
1149 assert.equal(
1150 formatText( 'invalid-wikitext' ),
1151 '<b>{{FAIL}}</b>',
1152 'Invalid wikitext: \'text\' format'
1153 );
1154
1155 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
1156 } );
1157
1158 QUnit.test( 'Integration', function ( assert ) {
1159 var expected, logSpy, msg;
1160
1161 expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1162 mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
1163
1164 this.suppressWarnings();
1165 logSpy = this.sandbox.spy( mw.log, 'warn' );
1166 assert.equal(
1167 window.gM( 'integration-test' ),
1168 expected,
1169 'Global function gM() works correctly'
1170 );
1171 assert.equal( logSpy.callCount, 1, 'mw.log.warn called' );
1172 this.restoreWarnings();
1173
1174 assert.equal(
1175 mw.message( 'integration-test' ).parse(),
1176 expected,
1177 'mw.message().parse() works correctly'
1178 );
1179
1180 assert.equal(
1181 $( '<span>' ).msg( 'integration-test' ).html(),
1182 expected,
1183 'jQuery plugin $.fn.msg() works correctly'
1184 );
1185
1186 mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
1187 msg = mw.message(
1188 'integration-test-extlink',
1189 $( '<a>' ).attr( 'href', 'http://example.com/' )
1190 );
1191 msg.parse(); // Not a no-op
1192 assert.equal(
1193 msg.parse(),
1194 '<a href="http://example.com/">Link</a>',
1195 'Calling .parse() multiple times does not duplicate link contents'
1196 );
1197 } );
1198
1199 QUnit.test( 'setParserDefaults', function ( assert ) {
1200 mw.jqueryMsg.setParserDefaults( {
1201 magic: {
1202 FOO: 'foo',
1203 BAR: 'bar'
1204 }
1205 } );
1206
1207 assert.deepEqual(
1208 mw.jqueryMsg.getParserDefaults().magic,
1209 {
1210 FOO: 'foo',
1211 BAR: 'bar'
1212 },
1213 'setParserDefaults is shallow by default'
1214 );
1215
1216 mw.jqueryMsg.setParserDefaults(
1217 {
1218 magic: {
1219 BAZ: 'baz'
1220 }
1221 },
1222 true
1223 );
1224
1225 assert.deepEqual(
1226 mw.jqueryMsg.getParserDefaults().magic,
1227 {
1228 FOO: 'foo',
1229 BAR: 'bar',
1230 BAZ: 'baz'
1231 },
1232 'setParserDefaults is deep if requested'
1233 );
1234 } );
1235 }( mediaWiki, jQuery ) );