Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
1 ( function () {
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 this.origConfig = mw.util.setOptionsForTest( {
81 FragmentMode: [ 'legacy', 'html5' ],
82 LoadScript: '/w/load.php'
83 } );
84 },
85 teardown: function () {
86 $.fn.updateTooltipAccessKeys.setTestMode( false );
87 mw.util.setOptionsForTest( this.origConfig );
88 },
89 messages: {
90 // Used by accessKeyLabel in test for addPortletLink
91 brackets: '[$1]',
92 'word-separator': ' '
93 }
94 } ) );
95
96 QUnit.test( 'rawurlencode', function ( assert ) {
97 assert.strictEqual( util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
98 } );
99
100 QUnit.test( 'escapeIdForAttribute', function ( assert ) {
101 // Test cases are kept in sync with SanitizerTest.php
102 var text = 'foo тест_#%!\'()[]:<>',
103 legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
104 html5Encoded = 'foo_тест_#%!\'()[]:<>',
105 // Settings: this is $wgFragmentMode
106 legacy = [ 'legacy' ],
107 legacyNew = [ 'legacy', 'html5' ],
108 newLegacy = [ 'html5', 'legacy' ],
109 allNew = [ 'html5' ];
110
111 // Test cases are kept in sync with SanitizerTest.php
112 [
113 // Pure legacy: how MW worked before 2017
114 [ legacy, text, legacyEncoded ],
115 // Transition to a new world: legacy links with HTML5 fallback
116 [ legacyNew, text, legacyEncoded ],
117 // New world: HTML5 links, legacy fallbacks
118 [ newLegacy, text, html5Encoded ],
119 // Distant future: no legacy fallbacks
120 [ allNew, text, html5Encoded ]
121 ].forEach( function ( testCase ) {
122 mw.util.setOptionsForTest( { FragmentMode: testCase[ 0 ] } );
123
124 assert.strictEqual( util.escapeIdForAttribute( testCase[ 1 ] ), testCase[ 2 ] );
125 } );
126 } );
127
128 QUnit.test( 'escapeIdForLink', function ( assert ) {
129 // Test cases are kept in sync with SanitizerTest.php
130 var text = 'foo тест_#%!\'()[]:<>',
131 legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
132 html5Encoded = 'foo_тест_#%!\'()[]:<>',
133 // Settings: this is wgFragmentMode
134 legacy = [ 'legacy' ],
135 legacyNew = [ 'legacy', 'html5' ],
136 newLegacy = [ 'html5', 'legacy' ],
137 allNew = [ 'html5' ];
138
139 [
140 // Pure legacy: how MW worked before 2017
141 [ legacy, text, legacyEncoded ],
142 // Transition to a new world: legacy links with HTML5 fallback
143 [ legacyNew, text, legacyEncoded ],
144 // New world: HTML5 links, legacy fallbacks
145 [ newLegacy, text, html5Encoded ],
146 // Distant future: no legacy fallbacks
147 [ allNew, text, html5Encoded ]
148 ].forEach( function ( testCase ) {
149 mw.util.setOptionsForTest( { FragmentMode: testCase[ 0 ] } );
150
151 assert.strictEqual( util.escapeIdForLink( testCase[ 1 ] ), testCase[ 2 ] );
152 } );
153 } );
154
155 QUnit.test( 'wikiUrlencode', function ( assert ) {
156 assert.strictEqual( util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
157 // See also wfUrlencodeTest.php#provideURLS
158 // eslint-disable-next-line no-jquery/no-each-util
159 $.each( {
160 '+': '%2B',
161 '&': '%26',
162 '=': '%3D',
163 ':': ':',
164 ';@$-_.!*': ';@$-_.!*',
165 '/': '/',
166 '~': '~',
167 '[]': '%5B%5D',
168 '<>': '%3C%3E',
169 '\'': '%27'
170 }, function ( input, output ) {
171 assert.strictEqual( util.wikiUrlencode( input ), output );
172 } );
173 } );
174
175 QUnit.test( 'getUrl', function ( assert ) {
176 var href;
177 mw.config.set( {
178 wgScript: '/w/index.php',
179 wgArticlePath: '/wiki/$1',
180 wgPageName: 'Foobar'
181 } );
182
183 href = util.getUrl( 'Sandbox' );
184 assert.strictEqual( href, '/wiki/Sandbox', 'simple title' );
185
186 href = util.getUrl( 'Foo:Sandbox? 5+5=10! (test)/sub ' );
187 assert.strictEqual( href, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'complex title' );
188
189 // T149767
190 href = util.getUrl( 'My$$test$$$$$title' );
191 assert.strictEqual( href, '/wiki/My$$test$$$$$title', 'title with multiple consecutive dollar signs' );
192
193 href = util.getUrl();
194 assert.strictEqual( href, '/wiki/Foobar', 'default title' );
195
196 href = util.getUrl( null, { action: 'edit' } );
197 assert.strictEqual( href, '/w/index.php?title=Foobar&action=edit', 'default title with query string' );
198
199 href = util.getUrl( 'Sandbox', { action: 'edit' } );
200 assert.strictEqual( href, '/w/index.php?title=Sandbox&action=edit', 'simple title with query string' );
201
202 // Test fragments
203 href = util.getUrl( 'Foo:Sandbox#Fragment', { action: 'edit' } );
204 assert.strictEqual( href, '/w/index.php?title=Foo:Sandbox&action=edit#Fragment', 'namespaced title with query string and fragment' );
205
206 href = util.getUrl( 'Sandbox#', { action: 'edit' } );
207 assert.strictEqual( href, '/w/index.php?title=Sandbox&action=edit', 'title with query string and empty fragment' );
208
209 href = util.getUrl( 'Sandbox', {} );
210 assert.strictEqual( href, '/wiki/Sandbox', 'title with empty query string' );
211
212 href = util.getUrl( '#Fragment' );
213 assert.strictEqual( href, '/wiki/#Fragment', 'empty title with fragment' );
214
215 href = util.getUrl( '#Fragment', { action: 'edit' } );
216 assert.strictEqual( href, '/w/index.php?action=edit#Fragment', 'empty title with query string and fragment' );
217
218 mw.util.setOptionsForTest( { FragmentMode: [ 'legacy' ] } );
219 href = util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
220 assert.strictEqual( href, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
221
222 mw.util.setOptionsForTest( { FragmentMode: [ 'html5' ] } );
223 href = util.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action: 'edit' } );
224 assert.strictEqual( href, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_Ä', 'title with query string, fragment, and special characters' );
225
226 href = util.getUrl( 'Foo:%23#Fragment', { action: 'edit' } );
227 assert.strictEqual( href, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
228
229 mw.util.setOptionsForTest( { FragmentMode: [ 'legacy' ] } );
230 href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } );
231 assert.strictEqual( 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' );
232
233 mw.util.setOptionsForTest( { FragmentMode: [ 'html5' ] } );
234 href = util.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action: 'edit' } );
235 assert.strictEqual( href, '/w/index.php?action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' );
236 } );
237
238 QUnit.test( 'wikiScript', function ( assert ) {
239 mw.util.setOptionsForTest( {
240 LoadScript: '/w/l.php'
241 } );
242 mw.config.set( {
243 // customized wgScript for T41103
244 wgScript: '/w/i.php',
245 wgScriptPath: '/w'
246 } );
247
248 assert.strictEqual( util.wikiScript(), mw.config.get( 'wgScript' ),
249 'wikiScript() returns wgScript'
250 );
251 assert.strictEqual( util.wikiScript( 'index' ), mw.config.get( 'wgScript' ),
252 'wikiScript( index ) returns wgScript'
253 );
254 assert.strictEqual( util.wikiScript( 'load' ), '/w/l.php',
255 'wikiScript( load ) returns /w/l.php'
256 );
257 assert.strictEqual( util.wikiScript( 'api' ), '/w/api.php', 'API path' );
258 } );
259
260 QUnit.test( 'addCSS', function ( assert ) {
261 var $el, style;
262 $el = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
263
264 style = util.addCSS( '#mw-addcsstest { visibility: hidden; }' );
265 assert.strictEqual( typeof style, 'object', 'addCSS returned an object' );
266 assert.strictEqual( style.disabled, false, 'property "disabled" is available and set to false' );
267
268 assert.strictEqual( $el.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
269
270 // Clean up
271 $( style.ownerNode ).remove();
272 } );
273
274 QUnit.test( 'getParamValue', function ( assert ) {
275 var url;
276
277 url = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
278 assert.strictEqual( util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
279 assert.strictEqual( util.getParamValue( 'bar', url ), null, 'Return null when not found' );
280
281 url = 'http://example.org/#&foo=bad';
282 assert.strictEqual( util.getParamValue( 'foo', url ), null, 'Ignore hash if param is not in querystring but in hash (T29427)' );
283
284 url = 'example.org?' + $.param( { TEST: 'a b+c' } );
285 assert.strictEqual( util.getParamValue( 'TEST', url ), 'a b+c', 'T32441: getParamValue must understand "+" encoding of space' );
286
287 url = 'example.org?' + $.param( { TEST: 'a b+c d' } ); // check for sloppy code from r95332 :)
288 assert.strictEqual( util.getParamValue( 'TEST', url ), 'a b+c d', 'T32441: getParamValue must understand "+" encoding of space (multiple spaces)' );
289 } );
290
291 QUnit.test( '$content', function ( assert ) {
292 assert.ok( util.$content instanceof $, 'mw.util.$content instance of jQuery' );
293 assert.strictEqual( util.$content.length, 1, 'mw.util.$content must have length of 1' );
294 } );
295
296 /**
297 * Portlet names are prefixed with 'p-test' to avoid conflict with core
298 * when running the test suite under a wiki page.
299 * Previously, test elements where invisible to the selector since only
300 * one element can have a given id.
301 */
302 QUnit.test( 'addPortletLink', function ( assert ) {
303 var tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo,
304 addedAfter, tbRLDMnonexistentid, tbRLDMemptyjquery;
305
306 $( '#qunit-fixture' ).append(
307 '<div class="portlet" id="p-test-tb">' +
308 '<h3>Toolbox</h3>' +
309 '<ul class="body"></ul>' +
310 '</div>' +
311 '<div class="portlet" id="p-test-custom">' +
312 '<h3>Views</h3>' +
313 '<ul class="body">' +
314 '<li id="c-foo"><a href="#">Foo</a></li>' +
315 '<li id="c-barmenu">' +
316 '<ul>' +
317 '<li id="c-bar-baz"><a href="#">Baz</a></a>' +
318 '</ul>' +
319 '</li>' +
320 '</ul>' +
321 '</div>' +
322 '<div id="p-test-views" class="vectorTabs">' +
323 '<h3>Views</h3>' +
324 '<ul></ul>' +
325 '</div>'
326 );
327
328 tbRL = util.addPortletLink( 'p-test-tb', 'https://example.org/next',
329 'Next', 't-rl', 'More info about Example Next ', 'l'
330 );
331 assert.strictEqual( tbRL.nodeType, 1, 'returns a DOM Node' );
332 assert.strictEqual( tbRL.nodeName, 'LI', 'returns a list item element' );
333
334 tbMW = util.addPortletLink( 'p-test-tb', '//example.org/',
335 'Example.org', 't-xmp', 'Go to Example', 'x', tbRL );
336 $tbMW = $( tbMW );
337 assert.propEqual(
338 $tbMW.getAttrs(),
339 {
340 id: 't-xmp'
341 },
342 'List item attributes'
343 );
344 assert.propEqual(
345 $tbMW.find( 'a' ).getAttrs(),
346 {
347 href: '//example.org/',
348 title: 'Go to Example [test-x]',
349 accesskey: 'x'
350 },
351 'Anchor link attributes'
352 );
353 assert.strictEqual(
354 $tbMW.closest( '.portlet' ).attr( 'id' ),
355 'p-test-tb',
356 'Parent portlet ID'
357 );
358 assert.strictEqual(
359 $tbMW.next()[ 0 ],
360 tbRL,
361 'Next node (set as Node object)'
362 );
363 assert.strictEqual(
364 $tbMW.find( 'span' ).length,
365 0,
366 'No <span> wrap for porlets without vectorTabs class'
367 );
368
369 cuQuux = util.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
370 $cuQuux = $( cuQuux );
371 assert.strictEqual(
372 $cuQuux.find( 'a' ).attr( 'title' ),
373 'Example [test-q]',
374 'Title has new accesskey and label'
375 );
376 assert.strictEqual(
377 $( '#p-test-custom #c-barmenu ul li' ).length,
378 1,
379 'No items added to unrelated <ul> elsewhere in the portlet (T37082)'
380 );
381
382 tbRLDM = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
383 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
384 assert.strictEqual( $( tbRLDM ).next()[ 0 ], tbRL, 'Next node (set as CSS selector)' );
385
386 caFoo = util.addPortletLink( 'p-test-views', '#', 'Foo' );
387 assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'Added <span> element for porlet with vectorTabs class' );
388
389 addedAfter = util.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL ) );
390 assert.strictEqual( $( addedAfter ).next()[ 0 ], tbRL, 'Next node (set as jQuery object)' );
391
392 tbRLDMnonexistentid = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
393 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
394 assert.strictEqual(
395 tbRLDMnonexistentid,
396 $( '#p-test-tb li' ).last()[ 0 ],
397 'Next node as non-matching CSS selector falls back to appending'
398 );
399
400 tbRLDMemptyjquery = util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
401 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
402 assert.strictEqual(
403 tbRLDMemptyjquery,
404 $( '#p-test-tb li' ).last()[ 0 ],
405 'Next node as empty jQuery object falls back to appending'
406 );
407 } );
408
409 QUnit.test( 'validateEmail', function ( assert ) {
410 assert.strictEqual( util.validateEmail( '' ), null, 'Should return null for empty string ' );
411 assert.strictEqual( util.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
412
413 // testEmailWithCommasAreInvalids
414 assert.strictEqual( util.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
415 assert.strictEqual( util.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
416
417 // testEmailWithHyphens
418 assert.strictEqual( util.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
419 assert.strictEqual( util.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
420 } );
421
422 QUnit.test( 'isIPv6Address', function ( assert ) {
423 IPV6_CASES.forEach( function ( ipCase ) {
424 assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
425 } );
426 } );
427
428 QUnit.test( 'isIPv4Address', function ( assert ) {
429 IPV4_CASES.forEach( function ( ipCase ) {
430 assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
431 } );
432 } );
433
434 QUnit.test( 'isIPAddress', function ( assert ) {
435 IPV4_CASES.forEach( function ( ipCase ) {
436 assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
437 } );
438
439 IPV6_CASES.forEach( function ( ipCase ) {
440 assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
441 } );
442 } );
443 }() );