Merge "Rank aliases in search in order they appear in the messages file."
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.jqueryMsg.test.js
1 ( function ( mw, $ ) {
2 var formatText, formatParse, formatnumTests, specialCharactersPageName, expectedListUsers,
3 expectedListUsersSitename, expectedLinkPagenamee, expectedEntrypoints,
4 mwLanguageCache = {},
5 hasOwn = Object.hasOwnProperty;
6
7 // When the expected result is the same in both modes
8 function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) {
9 assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
10 assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
11 }
12
13 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
14 setup: function () {
15 this.originalMwLanguage = mw.language;
16 this.parserDefaults = mw.jqueryMsg.getParserDefaults();
17 mw.jqueryMsg.setParserDefaults( {
18 magic: {
19 PAGENAME: '2 + 2',
20 PAGENAMEE: mw.util.wikiUrlencode( '2 + 2' ),
21 SITENAME: 'Wiki'
22 }
23 } );
24
25 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
26
27 expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
28 expectedListUsersSitename = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
29 'Wiki</a>';
30 expectedLinkPagenamee = '<a href="https://example.org/wiki/Foo?bar=baz#val/2_%2B_2">Test</a>';
31
32 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
33
34 formatText = mw.jqueryMsg.getMessageFunction( {
35 format: 'text'
36 } );
37
38 formatParse = mw.jqueryMsg.getMessageFunction( {
39 format: 'parse'
40 } );
41 },
42 teardown: function () {
43 mw.language = this.originalMwLanguage;
44 mw.jqueryMsg.setParserDefaults( this.parserDefaults );
45 },
46 config: {
47 wgArticlePath: '/wiki/$1',
48 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
49 wgNamespaceIds: {
50 template: 10,
51 template_talk: 11,
52 // Localised
53 szablon: 10,
54 dyskusja_szablonu: 11
55 },
56 // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
57 wgFormattedNamespaces: {
58 // Localised
59 10: 'Szablon',
60 11: 'Dyskusja szablonu'
61 }
62 },
63 // Messages that are reused in multiple tests
64 messages: {
65 // The values for gender are not significant,
66 // what matters is which of the values is choosen by the parser
67 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
68 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
69
70 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
71 // See https://phabricator.wikimedia.org/T71993
72 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
73 // Assume the grammar form grammar_case_foo is not valid in any language
74 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
75
76 'formatnum-msg': '{{formatnum:$1}}',
77
78 'portal-url': 'Project:Community portal',
79 'see-portal-url': '{{Int:portal-url}} is an important community page.',
80
81 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
82 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
83 'jquerymsg-test-link-pagenamee': '[https://example.org/wiki/Foo?bar=baz#val/{{PAGENAMEE}} Test]',
84
85 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
86
87 'external-link-replace': 'Foo [$1 bar]',
88 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
89 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
90 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
91 }
92 } ) );
93
94 /**
95 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
96 * to transport the module back to the test. It musn't be overwritten concurrentely.
97 *
98 * This function caches the mw.language data to avoid having to request the same module
99 * multiple times. There is more than one test case for any given language.
100 */
101 function getMwLanguage( langCode ) {
102 if ( !hasOwn.call( mwLanguageCache, langCode ) ) {
103 mwLanguageCache[ langCode ] = $.ajax( {
104 url: mw.util.wikiScript( 'load' ),
105 data: {
106 skin: mw.config.get( 'skin' ),
107 lang: langCode,
108 debug: mw.config.get( 'debug' ),
109 modules: [
110 'mediawiki.language.data',
111 'mediawiki.language'
112 ].join( '|' ),
113 only: 'scripts'
114 },
115 dataType: 'script',
116 cache: true
117 } ).then( function () {
118 return mw.language;
119 } );
120 }
121 return mwLanguageCache[ langCode ];
122 }
123
124 /**
125 * @param {Function[]} tasks List of functions that perform tasks
126 * that may be asynchronous. Invoke the callback parameter when done.
127 */
128 function process( tasks ) {
129 /*jshint latedef:false */
130 function abort() {
131 tasks.splice( 0, tasks.length );
132 next();
133 }
134 function next() {
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 var 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', 15, 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', 9, 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', 15, 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', 8, 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', 2, 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', mw.libs.phpParserData.tests.length, function ( assert ) {
368 mw.messages.set( mw.libs.phpParserData.messages );
369 var tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
370 var done = assert.async();
371 return function ( next, abort ) {
372 getMwLanguage( test.lang )
373 .then( function ( langClass ) {
374 mw.config.set( 'wgUserLanguage', test.lang );
375 var parser = new mw.jqueryMsg.parser( { language: langClass } );
376 assert.equal(
377 parser.parse( test.key, test.args ).html(),
378 test.result,
379 test.name
380 );
381 }, function () {
382 assert.ok( false, 'Language "' + test.lang + '" failed to load.' );
383 } )
384 .then( done, done )
385 .then( next, abort );
386 };
387 } );
388
389 process( tasks );
390 } );
391
392 QUnit.test( 'Links', 15, function ( assert ) {
393 var testCases,
394 expectedDisambiguationsText,
395 expectedMultipleBars,
396 expectedSpecialCharacters;
397
398 // The below three are all identical to or based on real messages. For disambiguations-text,
399 // the bold was removed because it is not yet implemented.
400
401 assert.htmlEqual(
402 formatParse( 'jquerymsg-test-statistics-users' ),
403 expectedListUsers,
404 'Piped wikilink'
405 );
406
407 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 ' +
408 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
409
410 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]].' );
411 assert.htmlEqual(
412 formatParse( 'disambiguations-text' ),
413 expectedDisambiguationsText,
414 'Wikilink without pipe'
415 );
416
417 assert.htmlEqual(
418 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
419 expectedEntrypoints,
420 'External link'
421 );
422
423 // Pipe trick is not supported currently, but should not parse as text either.
424 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
425 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
426 mw.messages.set( 'empty-link', '[[]]' );
427 this.suppressWarnings();
428 assert.equal(
429 formatParse( 'pipe-trick' ),
430 '[[Tampa, Florida|]]',
431 'Pipe trick should not be parsed.'
432 );
433 assert.equal(
434 formatParse( 'reverse-pipe-trick' ),
435 '[[|Tampa, Florida]]',
436 'Reverse pipe trick should not be parsed.'
437 );
438 assert.equal(
439 formatParse( 'empty-link' ),
440 '[[]]',
441 'Empty link should not be parsed.'
442 );
443 this.restoreWarnings();
444
445 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
446 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
447 assert.htmlEqual(
448 formatParse( 'multiple-bars' ),
449 expectedMultipleBars,
450 'Bar in anchor'
451 );
452
453 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>';
454
455 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
456 assert.htmlEqual(
457 formatParse( 'special-characters' ),
458 expectedSpecialCharacters,
459 'Special characters'
460 );
461
462 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
463 assert.htmlEqual(
464 formatParse( 'leading-colon' ),
465 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
466 'Leading colon in links is stripped'
467 );
468
469 assert.htmlEqual(
470 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
471 expectedListUsersSitename,
472 'Piped wikilink with parser function in the text'
473 );
474
475 assert.htmlEqual(
476 formatParse( 'jquerymsg-test-link-pagenamee' ),
477 expectedLinkPagenamee,
478 'External link with parser function in the URL'
479 );
480
481 testCases = [
482 [
483 'extlink-html-full',
484 'asd [http://example.org <strong>Example</strong>] asd',
485 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
486 ],
487 [
488 'extlink-html-partial',
489 'asd [http://example.org foo <strong>Example</strong> bar] asd',
490 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
491 ],
492 [
493 'wikilink-html-full',
494 'asd [[Example|<strong>Example</strong>]] asd',
495 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
496 ],
497 [
498 'wikilink-html-partial',
499 'asd [[Example|foo <strong>Example</strong> bar]] asd',
500 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
501 ]
502 ];
503
504 $.each( testCases, function () {
505 var
506 key = this[ 0 ],
507 input = this[ 1 ],
508 output = this[ 2 ];
509 mw.messages.set( key, input );
510 assert.htmlEqual(
511 formatParse( key ),
512 output,
513 'HTML in links: ' + key
514 );
515 } );
516 } );
517
518 QUnit.test( 'Replacements in links', 14, function ( assert ) {
519 var testCases = [
520 [
521 'extlink-param-href-full',
522 'asd [$1 Example] asd',
523 'asd <a href="http://example.com">Example</a> asd'
524 ],
525 [
526 'extlink-param-href-partial',
527 'asd [$1/example Example] asd',
528 'asd <a href="http://example.com/example">Example</a> asd'
529 ],
530 [
531 'extlink-param-text-full',
532 'asd [http://example.org $2] asd',
533 'asd <a href="http://example.org">Text</a> asd'
534 ],
535 [
536 'extlink-param-text-partial',
537 'asd [http://example.org Example $2] asd',
538 'asd <a href="http://example.org">Example Text</a> asd'
539 ],
540 [
541 'extlink-param-both-full',
542 'asd [$1 $2] asd',
543 'asd <a href="http://example.com">Text</a> asd'
544 ],
545 [
546 'extlink-param-both-partial',
547 'asd [$1/example Example $2] asd',
548 'asd <a href="http://example.com/example">Example Text</a> asd'
549 ],
550 [
551 'wikilink-param-href-full',
552 'asd [[$1|Example]] asd',
553 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
554 ],
555 [
556 'wikilink-param-href-partial',
557 'asd [[$1/Test|Example]] asd',
558 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
559 ],
560 [
561 'wikilink-param-text-full',
562 'asd [[Example|$2]] asd',
563 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
564 ],
565 [
566 'wikilink-param-text-partial',
567 'asd [[Example|Example $2]] asd',
568 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
569 ],
570 [
571 'wikilink-param-both-full',
572 'asd [[$1|$2]] asd',
573 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
574 ],
575 [
576 'wikilink-param-both-partial',
577 'asd [[$1/Test|Example $2]] asd',
578 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
579 ],
580 [
581 'wikilink-param-unpiped-full',
582 'asd [[$1]] asd',
583 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
584 ],
585 [
586 'wikilink-param-unpiped-partial',
587 'asd [[$1/Test]] asd',
588 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
589 ]
590 ];
591
592 $.each( testCases, function () {
593 var
594 key = this[ 0 ],
595 input = this[ 1 ],
596 output = this[ 2 ],
597 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
598 paramText = 'Text';
599 mw.messages.set( key, input );
600 assert.htmlEqual(
601 formatParse( key, paramHref, paramText ),
602 output,
603 'Replacements in links: ' + key
604 );
605 } );
606 } );
607
608 // Tests that {{-transformation vs. general parsing are done as requested
609 QUnit.test( 'Curly brace transformation', 16, function ( assert ) {
610 var oldUserLang = mw.config.get( 'wgUserLanguage' );
611
612 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
613
614 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
615
616 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
617
618 mw.config.set( 'wgUserLanguage', 'en' );
619 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
620
621 // Test non-{{ wikitext, where behavior differs
622
623 // Wikilink
624 assert.equal(
625 formatText( 'jquerymsg-test-statistics-users' ),
626 mw.messages.get( 'jquerymsg-test-statistics-users' ),
627 'Internal link message unchanged when format is \'text\''
628 );
629 assert.htmlEqual(
630 formatParse( 'jquerymsg-test-statistics-users' ),
631 expectedListUsers,
632 'Internal link message parsed when format is \'parse\''
633 );
634
635 // External link
636 assert.equal(
637 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
638 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
639 'External link message unchanged when format is \'text\''
640 );
641 assert.htmlEqual(
642 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
643 expectedEntrypoints,
644 'External link message processed when format is \'parse\''
645 );
646
647 // External link with parameter
648 assert.equal(
649 formatText( 'external-link-replace', 'http://example.com' ),
650 'Foo [http://example.com bar]',
651 'External link message only substitutes parameter when format is \'text\''
652 );
653 assert.htmlEqual(
654 formatParse( 'external-link-replace', 'http://example.com' ),
655 'Foo <a href="http://example.com">bar</a>',
656 'External link message processed when format is \'parse\''
657 );
658 assert.htmlEqual(
659 formatParse( 'external-link-replace', $( '<i>' ) ),
660 'Foo <i>bar</i>',
661 'External link message processed as jQuery object when format is \'parse\''
662 );
663 assert.htmlEqual(
664 formatParse( 'external-link-replace', function () {} ),
665 'Foo <a role="button" tabindex="0">bar</a>',
666 'External link message processed as function when format is \'parse\''
667 );
668
669 mw.config.set( 'wgUserLanguage', oldUserLang );
670 } );
671
672 QUnit.test( 'Int', 4, function ( assert ) {
673 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.',
674 expectedNewarticletext,
675 helpPageTitle = 'Help:Foobar';
676
677 mw.messages.set( 'foobar', helpPageTitle );
678
679 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 ' +
680 '<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.';
681
682 mw.messages.set( 'newarticletext', newarticletextSource );
683
684 assert.htmlEqual(
685 formatParse( 'newarticletext' ),
686 expectedNewarticletext,
687 'Link with nested message'
688 );
689
690 assert.equal(
691 formatParse( 'see-portal-url' ),
692 'Project:Community portal is an important community page.',
693 'Nested message'
694 );
695
696 mw.messages.set( 'newarticletext-lowercase',
697 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
698
699 assert.htmlEqual(
700 formatParse( 'newarticletext-lowercase' ),
701 expectedNewarticletext,
702 'Link with nested message, lowercase include'
703 );
704
705 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
706
707 assert.equal(
708 formatParse( 'uses-missing-int' ),
709 '⧼doesnt-exist⧽',
710 'int: where nested message does not exist'
711 );
712 } );
713
714 QUnit.test( 'Ns', 4, function ( assert ) {
715 mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
716 assert.equal(
717 formatParse( 'ns-template-talk' ),
718 'Dyskusja szablonu',
719 'ns: returns localised namespace when used with a canonical namespace name'
720 );
721
722 mw.messages.set( 'ns-10', '{{ns:10}}' );
723 assert.equal(
724 formatParse( 'ns-10' ),
725 'Szablon',
726 'ns: returns localised namespace when used with a namespace number'
727 );
728
729 mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
730 assert.equal(
731 formatParse( 'ns-unknown' ),
732 '',
733 'ns: returns empty string for unknown namespace name'
734 );
735
736 mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
737 assert.equal(
738 formatParse( 'ns-in-a-link' ),
739 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
740 'ns: works when used inside a wikilink'
741 );
742 } );
743
744 // Tests that getMessageFunction is used for non-plain messages with curly braces or
745 // square brackets, but not otherwise.
746 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
747 var oldGMF, outerCalled, innerCalled;
748
749 mw.messages.set( {
750 'curly-brace': '{{int:message}}',
751 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
752 'double-square-bracket': '[[Some page]]',
753 regular: 'Other message'
754 } );
755
756 oldGMF = mw.jqueryMsg.getMessageFunction;
757
758 mw.jqueryMsg.getMessageFunction = function () {
759 outerCalled = true;
760 return function () {
761 innerCalled = true;
762 };
763 };
764
765 function verifyGetMessageFunction( key, format, shouldCall ) {
766 var message;
767 outerCalled = false;
768 innerCalled = false;
769 message = mw.message( key );
770 message[ format ]();
771 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
772 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
773 delete mw.messages[ format ];
774 }
775
776 verifyGetMessageFunction( 'curly-brace', 'parse', true );
777 verifyGetMessageFunction( 'curly-brace', 'plain', false );
778
779 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
780 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
781
782 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
783 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
784
785 verifyGetMessageFunction( 'regular', 'parse', false );
786 verifyGetMessageFunction( 'regular', 'plain', false );
787
788 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
789 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
790 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
791
792 mw.jqueryMsg.getMessageFunction = oldGMF;
793 } );
794
795 formatnumTests = [
796 {
797 lang: 'en',
798 number: 987654321.654321,
799 result: '987,654,321.654',
800 description: 'formatnum test for English, decimal separator'
801 },
802 {
803 lang: 'ar',
804 number: 987654321.654321,
805 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
806 description: 'formatnum test for Arabic, with decimal separator'
807 },
808 {
809 lang: 'ar',
810 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
811 result: 987654321,
812 integer: true,
813 description: 'formatnum test for Arabic, with decimal separator, reverse'
814 },
815 {
816 lang: 'ar',
817 number: -12.89,
818 result: '-١٢٫٨٩',
819 description: 'formatnum test for Arabic, negative number'
820 },
821 {
822 lang: 'ar',
823 number: '-١٢٫٨٩',
824 result: -12,
825 integer: true,
826 description: 'formatnum test for Arabic, negative number, reverse'
827 },
828 {
829 lang: 'nl',
830 number: 987654321.654321,
831 result: '987.654.321,654',
832 description: 'formatnum test for Nederlands, decimal separator'
833 },
834 {
835 lang: 'nl',
836 number: -12.89,
837 result: '-12,89',
838 description: 'formatnum test for Nederlands, negative number'
839 },
840 {
841 lang: 'nl',
842 number: '.89',
843 result: '0,89',
844 description: 'formatnum test for Nederlands'
845 },
846 {
847 lang: 'nl',
848 number: 'invalidnumber',
849 result: 'invalidnumber',
850 description: 'formatnum test for Nederlands, invalid number'
851 },
852 {
853 lang: 'ml',
854 number: '1000000000',
855 result: '1,00,00,00,000',
856 description: 'formatnum test for Malayalam'
857 },
858 {
859 lang: 'ml',
860 number: '-1000000000',
861 result: '-1,00,00,00,000',
862 description: 'formatnum test for Malayalam, negative number'
863 },
864 /*
865 * This will fail because of wrong pattern for ml in MW(different from CLDR)
866 {
867 lang: 'ml',
868 number: '1000000000.000',
869 result: '1,00,00,00,000.000',
870 description: 'formatnum test for Malayalam with decimal place'
871 },
872 */
873 {
874 lang: 'hi',
875 number: '123456789.123456789',
876 result: '१२,३४,५६,७८९',
877 description: 'formatnum test for Hindi'
878 },
879 {
880 lang: 'hi',
881 number: '१२,३४,५६,७८९',
882 result: '१२,३४,५६,७८९',
883 description: 'formatnum test for Hindi, Devanagari digits passed'
884 },
885 {
886 lang: 'hi',
887 number: '१,२३,४५६',
888 result: '123456',
889 integer: true,
890 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
891 }
892 ];
893
894 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
895 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
896 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
897 var queue = $.map( formatnumTests, function ( test ) {
898 var done = assert.async();
899 return function ( next, abort ) {
900 getMwLanguage( test.lang )
901 .then( function ( langClass ) {
902 mw.config.set( 'wgUserLanguage', test.lang );
903 var parser = new mw.jqueryMsg.parser( { language: langClass } );
904 assert.equal(
905 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
906 [ test.number ] ).html(),
907 test.result,
908 test.description
909 );
910 }, function () {
911 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
912 } )
913 .then( done, done )
914 .then( next, abort );
915 };
916 } );
917 process( queue );
918 } );
919
920 // HTML in wikitext
921 QUnit.test( 'HTML', 33, function ( assert ) {
922 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
923
924 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
925
926 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
927 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
928
929 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
930 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
931
932 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
933 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
934
935 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
936
937 assert.htmlEqual(
938 formatParse( 'jquerymsg-italics-with-link' ),
939 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
940 'Italics with link inside in parse mode'
941 );
942
943 assert.equal(
944 formatText( 'jquerymsg-italics-with-link' ),
945 mw.messages.get( 'jquerymsg-italics-with-link' ),
946 'Italics with link unchanged in text mode'
947 );
948
949 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
950 assert.htmlEqual(
951 formatParse( 'jquerymsg-italics-id-class' ),
952 mw.messages.get( 'jquerymsg-italics-id-class' ),
953 'ID and class are allowed'
954 );
955
956 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
957 assert.htmlEqual(
958 formatParse( 'jquerymsg-italics-onclick' ),
959 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
960 'element with onclick is escaped because it is not allowed'
961 );
962
963 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
964 assert.htmlEqual(
965 formatParse( 'jquerymsg-script-msg' ),
966 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
967 'Tag outside whitelist escaped in parse mode'
968 );
969
970 assert.equal(
971 formatText( 'jquerymsg-script-msg' ),
972 mw.messages.get( 'jquerymsg-script-msg' ),
973 'Tag outside whitelist unchanged in text mode'
974 );
975
976 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
977 assert.htmlEqual(
978 formatParse( 'jquerymsg-script-link-msg' ),
979 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
980 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
981 );
982
983 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
984 assert.htmlEqual(
985 formatParse( 'jquerymsg-mismatched-html' ),
986 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
987 'Mismatched HTML start and end tag treated as text'
988 );
989
990 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
991 assert.htmlEqual(
992 formatParse( 'jquerymsg-script-and-external-link' ),
993 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com"><i>Foo</i> bar</a>',
994 'HTML tags in external links not interfering with escaping of other tags'
995 );
996
997 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
998 assert.htmlEqual(
999 formatParse( 'jquerymsg-link-script' ),
1000 '<a href="http://example.com">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</a>',
1001 'Non-whitelisted HTML tag in external link anchor treated as text'
1002 );
1003
1004 // Intentionally not using htmlEqual for the quote tests
1005 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
1006 assert.equal(
1007 formatParse( 'jquerymsg-double-quotes-preserved' ),
1008 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
1009 'Attributes with double quotes are preserved as such'
1010 );
1011
1012 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1013 assert.equal(
1014 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1015 '<i id="single">Single</i>',
1016 'Attributes with single quotes are normalized to double'
1017 );
1018
1019 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
1020 assert.htmlEqual(
1021 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1022 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1023 'Escaped attributes are parsed correctly'
1024 );
1025
1026 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
1027 assert.htmlEqual(
1028 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1029 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1030 'Escaped attributes are parsed correctly'
1031 );
1032
1033 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1034 assert.htmlEqual(
1035 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1036 '<i><a href="http://example.com">Example</a></i>',
1037 'Contents of valid tag are treated as wikitext, so external link is parsed'
1038 );
1039
1040 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1041 assert.htmlEqual(
1042 formatParse( 'jquerymsg-wikitext-contents-script' ),
1043 '<i>&lt;script&gt;Script inside&lt;/script&gt;</i>',
1044 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1045 );
1046
1047 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1048 assert.htmlEqual(
1049 formatParse( 'jquerymsg-unclosed-tag' ),
1050 'Foo&lt;tag&gt;bar',
1051 'Nonsupported unclosed tags are escaped'
1052 );
1053
1054 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1055 assert.htmlEqual(
1056 formatParse( 'jquerymsg-self-closing-tag' ),
1057 'Foo&lt;tag/&gt;bar',
1058 'Self-closing tags don\'t cause a parse error'
1059 );
1060
1061 mw.messages.set( 'jquerymsg-asciialphabetliteral-regression', '<b >>>="dir">asd</b>' );
1062 assert.htmlEqual(
1063 formatParse( 'jquerymsg-asciialphabetliteral-regression' ),
1064 '<b>&gt;&gt;="dir"&gt;asd</b>',
1065 'Regression test for bad "asciiAlphabetLiteral" definition'
1066 );
1067
1068 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
1069 mw.messages.set( 'jquerymsg-entities2', 'A&gt;B' );
1070 mw.messages.set( 'jquerymsg-entities3', 'A&rarr;B' );
1071 assert.htmlEqual(
1072 formatParse( 'jquerymsg-entities1' ),
1073 'A&amp;B',
1074 'Lone "&" is escaped in text'
1075 );
1076 assert.htmlEqual(
1077 formatParse( 'jquerymsg-entities2' ),
1078 'A&amp;gt;B',
1079 '"&gt;" entity is double-escaped in text' // (WHY?)
1080 );
1081 assert.htmlEqual(
1082 formatParse( 'jquerymsg-entities3' ),
1083 'A&amp;rarr;B',
1084 '"&rarr;" entity is double-escaped in text'
1085 );
1086
1087 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1088 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A&gt;B"></i>' );
1089 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A&rarr;B"></i>' );
1090 assert.htmlEqual(
1091 formatParse( 'jquerymsg-entities-attr1' ),
1092 '<i title="A&amp;B"></i>',
1093 'Lone "&" is escaped in attribute'
1094 );
1095 assert.htmlEqual(
1096 formatParse( 'jquerymsg-entities-attr2' ),
1097 '<i title="A&gt;B"></i>',
1098 '"&gt;" entity is not double-escaped in attribute' // (WHY?)
1099 );
1100 assert.htmlEqual(
1101 formatParse( 'jquerymsg-entities-attr3' ),
1102 '<i title="A&amp;rarr;B"></i>',
1103 '"&rarr;" entity is double-escaped in attribute'
1104 );
1105 } );
1106
1107 QUnit.test( 'Nowiki', 3, function ( assert ) {
1108 mw.messages.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1109 assert.equal(
1110 formatParse( 'jquerymsg-nowiki-link' ),
1111 'Foo [[bar]] baz.',
1112 'Link inside nowiki is not parsed'
1113 );
1114
1115 mw.messages.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1116 assert.equal(
1117 formatParse( 'jquerymsg-nowiki-htmltag' ),
1118 'Foo &lt;b&gt;bar&lt;/b&gt; baz.',
1119 'HTML inside nowiki is not parsed and escaped'
1120 );
1121
1122 mw.messages.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1123 assert.equal(
1124 formatParse( 'jquerymsg-nowiki-template' ),
1125 'Foo {{bar}} baz.',
1126 'Template inside nowiki is not parsed and does not cause a parse error'
1127 );
1128 } );
1129
1130 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
1131 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1132
1133 this.suppressWarnings();
1134 var logSpy = this.sandbox.spy( mw.log, 'warn' );
1135
1136 assert.equal(
1137 formatParse( 'invalid-wikitext' ),
1138 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
1139 'Invalid wikitext: \'parse\' format'
1140 );
1141
1142 assert.equal(
1143 formatText( 'invalid-wikitext' ),
1144 '<b>{{FAIL}}</b>',
1145 'Invalid wikitext: \'text\' format'
1146 );
1147
1148 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
1149 } );
1150
1151 QUnit.test( 'Integration', 5, function ( assert ) {
1152 var expected, logSpy, msg;
1153
1154 expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1155 mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
1156
1157 this.suppressWarnings();
1158 logSpy = this.sandbox.spy( mw.log, 'warn' );
1159 assert.equal(
1160 window.gM( 'integration-test' ),
1161 expected,
1162 'Global function gM() works correctly'
1163 );
1164 assert.equal( logSpy.callCount, 1, 'mw.log.warn called' );
1165 this.restoreWarnings();
1166
1167 assert.equal(
1168 mw.message( 'integration-test' ).parse(),
1169 expected,
1170 'mw.message().parse() works correctly'
1171 );
1172
1173 assert.equal(
1174 $( '<span>' ).msg( 'integration-test' ).html(),
1175 expected,
1176 'jQuery plugin $.fn.msg() works correctly'
1177 );
1178
1179 mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
1180 msg = mw.message(
1181 'integration-test-extlink',
1182 $( '<a>' ).attr( 'href', 'http://example.com/' )
1183 );
1184 msg.parse(); // Not a no-op
1185 assert.equal(
1186 msg.parse(),
1187 '<a href="http://example.com/">Link</a>',
1188 'Calling .parse() multiple times does not duplicate link contents'
1189 );
1190 } );
1191
1192 }( mediaWiki, jQuery ) );