mediawiki.util.test: Set wgFragmentMode before testing getUrl
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
1 ( function ( mw, $ ) {
2 var util = require( 'mediawiki.util' ),
3 // Based on IPTest.php > testisIPv4
4 IPV4_CASES = [
5 [ false, false, 'Boolean false is not an IP' ],
6 [ false, true, 'Boolean true is not an IP' ],
7 [ false, '', 'Empty string is not an IP' ],
8 [ false, 'abc', '"abc" is not an IP' ],
9 [ false, ':', 'Colon is not an IP' ],
10 [ false, '124.24.52', 'IPv4 not enough quads' ],
11 [ false, '24.324.52.13', 'IPv4 out of range' ],
12 [ false, '.24.52.13', 'IPv4 starts with period' ],
13
14 [ true, '124.24.52.13', '124.24.52.134 is a valid IP' ],
15 [ true, '1.24.52.13', '1.24.52.13 is a valid IP' ],
16 [ false, '74.24.52.13/20', 'IPv4 ranges are not recognized as valid IPs' ]
17 ],
18
19 // Based on IPTest.php > testisIPv6
20 IPV6_CASES = [
21 [ false, ':fc:100::', 'IPv6 starting with lone ":"' ],
22 [ false, 'fc:100:::', 'IPv6 ending with a ":::"' ],
23 [ false, 'fc:300', 'IPv6 with only 2 words' ],
24 [ false, 'fc:100:300', 'IPv6 with only 3 words' ],
25
26 [ false, 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' ],
27 [ false, 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' ],
28
29 [ false, ':::' ],
30 [ false, '::0:', 'IPv6 ending in a lone ":"' ],
31
32 [ true, '::', 'IPv6 zero address' ],
33
34 [ false, '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' ],
35 [ false, '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' ],
36
37 [ false, ':fc::100', 'IPv6 starting with lone ":"' ],
38 [ false, 'fc::100:', 'IPv6 ending with lone ":"' ],
39 [ false, 'fc:::100', 'IPv6 with ":::" in the middle' ],
40
41 [ true, 'fc::100', 'IPv6 with "::" and 2 words' ],
42 [ true, 'fc::100:a', 'IPv6 with "::" and 3 words' ],
43 [ true, 'fc::100:a:d', 'IPv6 with "::" and 4 words' ],
44 [ true, 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' ],
45 [ true, 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' ],
46 [ true, 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' ],
47 [ true, '2001::df', 'IPv6 with "::" and 2 words' ],
48 [ true, '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' ],
49 [ true, '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' ],
50
51 [ false, 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' ],
52 [ false, 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' ]
53 ];
54
55 Array.prototype.push.apply( IPV6_CASES,
56 [
57 'fc:100::',
58 'fc:100:a::',
59 'fc:100:a:d::',
60 'fc:100:a:d:1::',
61 'fc:100:a:d:1:e::',
62 'fc:100:a:d:1:e:ac::',
63 '::0',
64 '::fc',
65 '::fc:100',
66 '::fc:100:a',
67 '::fc:100:a:d',
68 '::fc:100:a:d:1',
69 '::fc:100:a:d:1:e',
70 '::fc:100:a:d:1:e:ac',
71 'fc:100:a:d:1:e:ac:0'
72 ].map( function ( el ) {
73 return [ true, el, el + ' is a valid IP' ];
74 } )
75 );
76
77 QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment( {
78 setup: function () {
79 $.fn.updateTooltipAccessKeys.setTestMode( true );
80 },
81 teardown: function () {
82 $.fn.updateTooltipAccessKeys.setTestMode( false );
83 },
84 messages: {
85 // Used by accessKeyLabel in test for addPortletLink
86 brackets: '[$1]',
87 'word-separator': ' '
88 }
89 } ) );
90
91 QUnit.test( 'rawurlencode', function ( assert ) {
92 assert.equal( util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
93 } );
94
95 QUnit.test( 'escapeId', function ( assert ) {
96 mw.config.set( 'wgFragmentMode', [ 'legacy' ] );
97 $.each( {
98 '+': '.2B',
99 '&': '.26',
100 '=': '.3D',
101 ':': ':',
102 ';': '.3B',
103 '@': '.40',
104 $: '.24',
105 '-_.': '-_.',
106 '!': '.21',
107 '*': '.2A',
108 '/': '.2F',
109 '[]': '.5B.5D',
110 '<>': '.3C.3E',
111 '\'': '.27',
112 '§': '.C2.A7',
113 'Test:A & B/Here': 'Test:A_.26_B.2FHere',
114 'A&B&amp;C&amp;amp;D&amp;amp;amp;E': 'A.26B.26amp.3BC.26amp.3Bamp.3BD.26amp.3Bamp.3Bamp.3BE'
115 }, function ( input, output ) {
116 assert.equal( util.escapeId( input ), output );
117 } );
118 } );
119
120 QUnit.test( 'escapeIdForAttribute', function ( assert ) {
121 // Test cases are kept in sync with SanitizerTest.php
122 var text = 'foo тест_#%!\'()[]:<>',
123 legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
124 html5Encoded = 'foo_тест_#%!\'()[]:<>',
125 html5Experimental = 'foo_тест_!_()[]:<>',
126 // Settings: this is $wgFragmentMode
127 legacy = [ 'legacy' ],
128 legacyNew = [ 'legacy', 'html5' ],
129 newLegacy = [ 'html5', 'legacy' ],
130 allNew = [ 'html5' ],
131 experimentalLegacy = [ 'html5-legacy', 'legacy' ],
132 newExperimental = [ 'html5', 'html5-legacy' ];
133
134 // Test cases are kept in sync with SanitizerTest.php
135 [
136 // Pure legacy: how MW worked before 2017
137 [ legacy, text, legacyEncoded ],
138 // Transition to a new world: legacy links with HTML5 fallback
139 [ legacyNew, text, legacyEncoded ],
140 // New world: HTML5 links, legacy fallbacks
141 [ newLegacy, text, html5Encoded ],
142 // Distant future: no legacy fallbacks
143 [ allNew, text, html5Encoded ],
144 // Someone flipped $wgExperimentalHtmlIds on
145 [ experimentalLegacy, text, html5Experimental ],
146 // Migration from $wgExperimentalHtmlIds to modern HTML5
147 [ newExperimental, text, html5Encoded ]
148 ].forEach( function ( testCase ) {
149 mw.config.set( 'wgFragmentMode', testCase[ 0 ] );
150
151 assert.equal( util.escapeIdForAttribute( testCase[ 1 ] ), testCase[ 2 ] );
152 } );
153 } );
154
155 QUnit.test( 'escapeIdForLink', function ( assert ) {
156 // Test cases are kept in sync with SanitizerTest.php
157 var text = 'foo тест_#%!\'()[]:<>',
158 legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
159 html5Encoded = 'foo_тест_#%!\'()[]:<>',
160 html5Experimental = 'foo_тест_!_()[]:<>',
161 // Settings: this is wgFragmentMode
162 legacy = [ 'legacy' ],
163 legacyNew = [ 'legacy', 'html5' ],
164 newLegacy = [ 'html5', 'legacy' ],
165 allNew = [ 'html5' ],
166 experimentalLegacy = [ 'html5-legacy', 'legacy' ],
167 newExperimental = [ 'html5', 'html5-legacy' ];
168
169 [
170 // Pure legacy: how MW worked before 2017
171 [ legacy, text, legacyEncoded ],
172 // Transition to a new world: legacy links with HTML5 fallback
173 [ legacyNew, text, legacyEncoded ],
174 // New world: HTML5 links, legacy fallbacks
175 [ newLegacy, text, html5Encoded ],
176 // Distant future: no legacy fallbacks
177 [ allNew, text, html5Encoded ],
178 // Someone flipped wgExperimentalHtmlIds on
179 [ experimentalLegacy, text, html5Experimental ],
180 // Migration from wgExperimentalHtmlIds to modern HTML5
181 [ newExperimental, text, html5Encoded ]
182 ].forEach( function ( testCase ) {
183 mw.config.set( 'wgFragmentMode', testCase[ 0 ] );
184
185 assert.equal( util.escapeIdForLink( testCase[ 1 ] ), testCase[ 2 ] );
186 } );
187 } );
188
189 QUnit.test( 'wikiUrlencode', function ( assert ) {
190 assert.equal( util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
191 // See also wfUrlencodeTest.php#provideURLS
192 $.each( {
193 '+': '%2B',
194 '&': '%26',
195 '=': '%3D',
196 ':': ':',
197 ';@$-_.!*': ';@$-_.!*',
198 '/': '/',
199 '~': '~',
200 '[]': '%5B%5D',
201 '<>': '%3C%3E',
202 '\'': '%27'
203 }, function ( input, output ) {
204 assert.equal( util.wikiUrlencode( input ), output );
205 } );
206 } );
207
208 QUnit.test( 'getUrl', function ( assert ) {
209 var href;
210 mw.config.set( {
211 wgScript: '/w/index.php',
212 wgArticlePath: '/wiki/$1',
213 wgPageName: 'Foobar'
214 } );
215
216 href = util.getUrl( 'Sandbox' );
217 assert.equal( href, '/wiki/Sandbox', 'simple title' );
218
219 href = util.getUrl( 'Foo:Sandbox? 5+5=10! (test)/sub ' );
220 assert.equal( href, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'complex title' );
221
222 // T149767
223 href = util.getUrl( 'My$$test$$$$$title' );
224 assert.equal( href, '/wiki/My$$test$$$$$title', 'title with multiple consecutive dollar signs' );
225
226 href = util.getUrl();
227 assert.equal( href, '/wiki/Foobar', 'default title' );
228
229 href = util.getUrl( null, { action: 'edit' } );
230 assert.equal( href, '/w/index.php?title=Foobar&action=edit', 'default title with query string' );
231
232 href = util.getUrl( 'Sandbox', { action: 'edit' } );
233 assert.equal( href, '/w/index.php?title=Sandbox&action=edit', 'simple title with query string' );
234
235 // Test fragments
236 href = util.getUrl( 'Foo:Sandbox#Fragment', { action: 'edit' } );
237 assert.equal( href, '/w/index.php?title=Foo:Sandbox&action=edit#Fragment', 'namespaced title with query string and fragment' );
238
239 href = util.getUrl( 'Sandbox#', { action: 'edit' } );
240 assert.equal( href, '/w/index.php?title=Sandbox&action=edit', 'title with query string and empty fragment' );
241
242 href = util.getUrl( 'Sandbox', {} );
243 assert.equal( href, '/wiki/Sandbox', 'title with empty query string' );
244
245 href = util.getUrl( '#Fragment' );
246 assert.equal( href, '/wiki/#Fragment', 'empty title with fragment' );
247
248 href = util.getUrl( '#Fragment', { action: 'edit' } );
249 assert.equal( href, '/w/index.php?action=edit#Fragment', 'empty title with query string and fragment' );
250
251 mw.config.set( 'wgFragmentMode', [ 'legacy' ] );
252 href = util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
253 assert.equal( href, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
254
255 mw.config.set( 'wgFragmentMode', [ 'html5' ] );
256 href = util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
257 assert.equal( href, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_Ä', 'title with query string, fragment, and special characters' );
258
259 href = util.getUrl( 'Foo:%23#Fragment', { action: 'edit' } );
260 assert.equal( href, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
261
262 mw.config.set( 'wgFragmentMode', [ 'legacy' ] );
263 href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } );
264 assert.equal( href, '/w/index.php?action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' );
265
266 mw.config.set( 'wgFragmentMode', [ 'html5' ] );
267 href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } );
268 assert.equal( href, '/w/index.php?action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' );
269 } );
270
271 QUnit.test( 'wikiScript', function ( assert ) {
272 mw.config.set( {
273 // customized wgScript for T41103
274 wgScript: '/w/i.php',
275 // customized wgLoadScript for T41103
276 wgLoadScript: '/w/l.php',
277 wgScriptPath: '/w'
278 } );
279
280 assert.equal( util.wikiScript(), mw.config.get( 'wgScript' ),
281 'wikiScript() returns wgScript'
282 );
283 assert.equal( util.wikiScript( 'index' ), mw.config.get( 'wgScript' ),
284 'wikiScript( index ) returns wgScript'
285 );
286 assert.equal( util.wikiScript( 'load' ), mw.config.get( 'wgLoadScript' ),
287 'wikiScript( load ) returns wgLoadScript'
288 );
289 assert.equal( util.wikiScript( 'api' ), '/w/api.php', 'API path' );
290 } );
291
292 QUnit.test( 'addCSS', function ( assert ) {
293 var $el, style;
294 $el = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
295
296 style = util.addCSS( '#mw-addcsstest { visibility: hidden; }' );
297 assert.equal( typeof style, 'object', 'addCSS returned an object' );
298 assert.strictEqual( style.disabled, false, 'property "disabled" is available and set to false' );
299
300 assert.equal( $el.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
301
302 // Clean up
303 $( style.ownerNode ).remove();
304 } );
305
306 QUnit.test( 'getParamValue', function ( assert ) {
307 var url;
308
309 url = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
310 assert.equal( util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
311 assert.strictEqual( util.getParamValue( 'bar', url ), null, 'Return null when not found' );
312
313 url = 'http://example.org/#&foo=bad';
314 assert.strictEqual( util.getParamValue( 'foo', url ), null, 'Ignore hash if param is not in querystring but in hash (T29427)' );
315
316 url = 'example.org?' + $.param( { TEST: 'a b+c' } );
317 assert.strictEqual( util.getParamValue( 'TEST', url ), 'a b+c', 'T32441: getParamValue must understand "+" encoding of space' );
318
319 url = 'example.org?' + $.param( { TEST: 'a b+c d' } ); // check for sloppy code from r95332 :)
320 assert.strictEqual( util.getParamValue( 'TEST', url ), 'a b+c d', 'T32441: getParamValue must understand "+" encoding of space (multiple spaces)' );
321 } );
322
323 QUnit.test( '$content', function ( assert ) {
324 assert.ok( util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
325 assert.strictEqual( util.$content.length, 1, 'mw.util.$content must have length of 1' );
326 } );
327
328 /**
329 * Portlet names are prefixed with 'p-test' to avoid conflict with core
330 * when running the test suite under a wiki page.
331 * Previously, test elements where invisible to the selector since only
332 * one element can have a given id.
333 */
334 QUnit.test( 'addPortletLink', function ( assert ) {
335 var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo,
336 addedAfter, tbRLDMnonexistentid, tbRLDMemptyjquery;
337
338 pTestTb =
339 '<div class="portlet" id="p-test-tb">' +
340 '<h3>Toolbox</h3>' +
341 '<ul class="body"></ul>' +
342 '</div>';
343 pCustom =
344 '<div class="portlet" id="p-test-custom">' +
345 '<h3>Views</h3>' +
346 '<ul class="body">' +
347 '<li id="c-foo"><a href="#">Foo</a></li>' +
348 '<li id="c-barmenu">' +
349 '<ul>' +
350 '<li id="c-bar-baz"><a href="#">Baz</a></a>' +
351 '</ul>' +
352 '</li>' +
353 '</ul>' +
354 '</div>';
355 vectorTabs =
356 '<div id="p-test-views" class="vectorTabs">' +
357 '<h3>Views</h3>' +
358 '<ul></ul>' +
359 '</div>';
360
361 $( '#qunit-fixture' ).append( pTestTb, pCustom, vectorTabs );
362
363 tbRL = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
364 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l'
365 );
366
367 assert.ok( tbRL && tbRL.nodeType, 'addPortletLink returns a DOM Node' );
368
369 tbMW = util.addPortletLink( 'p-test-tb', '//mediawiki.org/',
370 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org', 'm', tbRL );
371 $tbMW = $( tbMW );
372
373 assert.propEqual(
374 $tbMW.getAttrs(),
375 {
376 id: 't-mworg'
377 },
378 'Validate attributes of created element'
379 );
380
381 assert.propEqual(
382 $tbMW.find( 'a' ).getAttrs(),
383 {
384 href: '//mediawiki.org/',
385 title: 'Go to MediaWiki.org [test-m]',
386 accesskey: 'm'
387 },
388 'Validate attributes of anchor tag in created element'
389 );
390
391 assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
392 assert.strictEqual( $tbMW.next()[ 0 ], tbRL, 'Link is in the correct position (nextnode as Node object)' );
393
394 cuQuux = util.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
395 $cuQuux = $( cuQuux );
396
397 assert.equal( $cuQuux.find( 'a' ).attr( 'title' ), 'Example [test-q]', 'Existing accesskey is stripped and updated' );
398
399 assert.equal(
400 $( '#p-test-custom #c-barmenu ul li' ).length,
401 1,
402 'addPortletLink did not add the item to all <ul> elements in the portlet (T37082)'
403 );
404
405 tbRLDM = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
406 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
407
408 assert.strictEqual( $( tbRLDM ).next()[ 0 ], tbRL, 'Link is in the correct position (CSS selector as nextnode)' );
409
410 caFoo = util.addPortletLink( 'p-test-views', '#', 'Foo' );
411
412 assert.strictEqual( $tbMW.find( 'span' ).length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
413 assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
414
415 addedAfter = util.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL ) );
416 assert.strictEqual( $( addedAfter ).next()[ 0 ], tbRL, 'Link is in the correct position (jQuery object as nextnode)' );
417
418 // test case - nonexistent id as next node
419 tbRLDMnonexistentid = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
420 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
421
422 assert.equal( tbRLDMnonexistentid, $( '#p-test-tb li:last' )[ 0 ], 'Fallback to adding at the end (nextnode non-matching CSS selector)' );
423
424 // test case - empty jquery object as next node
425 tbRLDMemptyjquery = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
426 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
427
428 assert.equal( tbRLDMemptyjquery, $( '#p-test-tb li:last' )[ 0 ], 'Fallback to adding at the end (nextnode as empty jQuery object)' );
429 } );
430
431 QUnit.test( 'validateEmail', function ( assert ) {
432 assert.strictEqual( util.validateEmail( '' ), null, 'Should return null for empty string ' );
433 assert.strictEqual( util.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
434
435 // testEmailWithCommasAreInvalids
436 assert.strictEqual( util.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
437 assert.strictEqual( util.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
438
439 // testEmailWithHyphens
440 assert.strictEqual( util.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
441 assert.strictEqual( util.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
442 } );
443
444 QUnit.test( 'isIPv6Address', function ( assert ) {
445 IPV6_CASES.forEach( function ( ipCase ) {
446 assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
447 } );
448 } );
449
450 QUnit.test( 'isIPv4Address', function ( assert ) {
451 IPV4_CASES.forEach( function ( ipCase ) {
452 assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
453 } );
454 } );
455
456 QUnit.test( 'isIPAddress', function ( assert ) {
457 IPV4_CASES.forEach( function ( ipCase ) {
458 assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
459 } );
460
461 IPV6_CASES.forEach( function ( ipCase ) {
462 assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
463 } );
464 } );
465 }( mediaWiki, jQuery ) );