2 QUnit
.module( 'mediawiki.util', QUnit
.newMwEnvironment( {
4 $.fn
.updateTooltipAccessKeys
.setTestMode( true );
6 teardown: function () {
7 $.fn
.updateTooltipAccessKeys
.setTestMode( false );
10 // Used by accessKeyLabel in test for addPortletLink
16 QUnit
.test( 'rawurlencode', 1, function ( assert
) {
17 assert
.equal( mw
.util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
20 QUnit
.test( 'wikiUrlencode', 10, function ( assert
) {
21 assert
.equal( mw
.util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
22 // See also wfUrlencodeTest.php#provideURLS
28 ';@$-_.!*': ';@$-_.!*',
33 }, function ( input
, output
) {
34 assert
.equal( mw
.util
.wikiUrlencode( input
), output
);
38 QUnit
.test( 'getUrl', 4, function ( assert
) {
39 // Not part of startUp module
40 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
41 mw
.config
.set( 'wgPageName', 'Foobar' );
43 var href
= mw
.util
.getUrl( 'Sandbox' );
44 assert
.equal( href
, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
46 href
= mw
.util
.getUrl( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
47 assert
.equal( href
, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_!_(test)/subpage',
48 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
50 href
= mw
.util
.getUrl();
51 assert
.equal( href
, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
53 href
= mw
.util
.getUrl( 'Sandbox', { action
: 'edit' } );
54 assert
.equal( href
, '/wiki/Sandbox?action=edit',
55 'Simple title with query string; Get link for "Sandbox" with action=edit' );
58 QUnit
.test( 'wikiScript', 4, function ( assert
) {
60 'wgScript': '/w/i.php', // customized wgScript for bug 39103
61 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103
63 'wgScriptExtension': '.php'
66 assert
.equal( mw
.util
.wikiScript(), mw
.config
.get( 'wgScript' ),
67 'wikiScript() returns wgScript'
69 assert
.equal( mw
.util
.wikiScript( 'index' ), mw
.config
.get( 'wgScript' ),
70 'wikiScript( index ) returns wgScript'
72 assert
.equal( mw
.util
.wikiScript( 'load' ), mw
.config
.get( 'wgLoadScript' ),
73 'wikiScript( load ) returns wgLoadScript'
75 assert
.equal( mw
.util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
78 QUnit
.test( 'addCSS', 3, function ( assert
) {
80 $el
= $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
82 style
= mw
.util
.addCSS( '#mw-addcsstest { visibility: hidden; }' );
83 assert
.equal( typeof style
, 'object', 'addCSS returned an object' );
84 assert
.strictEqual( style
.disabled
, false, 'property "disabled" is available and set to false' );
86 assert
.equal( $el
.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
89 $( style
.ownerNode
).remove();
92 QUnit
.test( 'getParamValue', 5, function ( assert
) {
95 url
= 'http://example.org/?foo=wrong&foo=right#&foo=bad';
96 assert
.equal( mw
.util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
97 assert
.strictEqual( mw
.util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
99 url
= 'http://example.org/#&foo=bad';
100 assert
.strictEqual( mw
.util
.getParamValue( 'foo', url
), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
102 url
= 'example.org?' + $.param( { 'TEST': 'a b+c' } );
103 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url
), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
105 url
= 'example.org?' + $.param( { 'TEST': 'a b+c d' } ); // check for sloppy code from r95332 :)
106 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url
), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
109 QUnit
.test( 'tooltipAccessKey', 4, function ( assert
) {
110 this.suppressWarnings();
112 assert
.equal( typeof mw
.util
.tooltipAccessKeyPrefix
, 'string', 'tooltipAccessKeyPrefix must be a string' );
113 assert
.equal( $.type( mw
.util
.tooltipAccessKeyRegexp
), 'regexp', 'tooltipAccessKeyRegexp is a regexp' );
114 assert
.ok( mw
.util
.updateTooltipAccessKeys
, 'updateTooltipAccessKeys is non-empty' );
116 'Example [a]'.replace( mw
.util
.tooltipAccessKeyRegexp
, function ( sub
, m1
, m2
, m3
, m4
, m5
, m6
) {
117 assert
.equal( m6
, 'a', 'tooltipAccessKeyRegexp finds the accesskey hint' );
120 this.restoreWarnings();
123 QUnit
.test( '$content', 2, function ( assert
) {
124 assert
.ok( mw
.util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
125 assert
.strictEqual( mw
.util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
129 * Portlet names are prefixed with 'p-test' to avoid conflict with core
130 * when running the test suite under a wiki page.
131 * Previously, test elements where invisible to the selector since only
132 * one element can have a given id.
134 QUnit
.test( 'addPortletLink', 13, function ( assert
) {
135 var pTestTb
, pCustom
, vectorTabs
, tbRL
, cuQuux
, $cuQuux
, tbMW
, $tbMW
, tbRLDM
, caFoo
,
136 addedAfter
, tbRLDMnonexistentid
, tbRLDMemptyjquery
;
139 <div class="portlet" id="p-test-tb">\
141 <ul class="body"></ul>\
144 <div class="portlet" id="p-test-custom">\
147 <li id="c-foo"><a href="#">Foo</a></li>\
150 <li id="c-bar-baz"><a href="#">Baz</a></a>\
156 <div id="p-test-views" class="vectorTabs">\
161 $( '#qunit-fixture' ).append( pTestTb
, pCustom
, vectorTabs
);
163 tbRL
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
164 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l'
167 assert
.ok( $.isDomElement( tbRL
), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
169 tbMW
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/',
170 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org', 'm', tbRL
);
178 'Validate attributes of created element'
182 $tbMW
.find( 'a' ).getAttrs(),
184 href
: '//mediawiki.org/',
185 title
: 'Go to MediaWiki.org [test-m]',
188 'Validate attributes of anchor tag in created element'
191 assert
.equal( $tbMW
.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
192 assert
.strictEqual( $tbMW
.next()[0], tbRL
, 'Link is in the correct position (nextnode as Node object)' );
194 cuQuux
= mw
.util
.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
195 $cuQuux
= $( cuQuux
);
197 assert
.equal( $cuQuux
.find( 'a' ).attr( 'title' ), 'Example [test-q]', 'Existing accesskey is stripped and updated' );
200 $( '#p-test-custom #c-barmenu ul li' ).length
,
202 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
205 tbRLDM
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
206 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
208 assert
.strictEqual( $( tbRLDM
).next()[0], tbRL
, 'Link is in the correct position (CSS selector as nextnode)' );
210 caFoo
= mw
.util
.addPortletLink( 'p-test-views', '#', 'Foo' );
212 assert
.strictEqual( $tbMW
.find( 'span' ).length
, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
213 assert
.strictEqual( $( caFoo
).find( 'span' ).length
, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
215 addedAfter
= mw
.util
.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL
) );
216 assert
.strictEqual( $( addedAfter
).next()[0], tbRL
, 'Link is in the correct position (jQuery object as nextnode)' );
218 // test case - nonexistent id as next node
219 tbRLDMnonexistentid
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
220 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
222 assert
.equal( tbRLDMnonexistentid
, $( '#p-test-tb li:last' )[0], 'Fallback to adding at the end (nextnode non-matching CSS selector)' );
224 // test case - empty jquery object as next node
225 tbRLDMemptyjquery
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
226 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
228 assert
.equal( tbRLDMemptyjquery
, $( '#p-test-tb li:last' )[0], 'Fallback to adding at the end (nextnode as empty jQuery object)' );
231 QUnit
.test( 'validateEmail', 6, function ( assert
) {
232 assert
.strictEqual( mw
.util
.validateEmail( '' ), null, 'Should return null for empty string ' );
233 assert
.strictEqual( mw
.util
.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
235 // testEmailWithCommasAreInvalids
236 assert
.strictEqual( mw
.util
.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
237 assert
.strictEqual( mw
.util
.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
239 // testEmailWithHyphens
240 assert
.strictEqual( mw
.util
.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
241 assert
.strictEqual( mw
.util
.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
244 QUnit
.test( 'isIPv6Address', 40, function ( assert
) {
246 function assertFalseIPv6( addy
, summary
) {
247 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), false, summary
);
250 function assertTrueIPv6( addy
, summary
) {
251 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), true, summary
);
254 // Based on IPTest.php > testisIPv6
255 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
256 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
257 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
258 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
266 'fc:100:a:d:1:e:ac::'], function ( i
, addy
) {
267 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
270 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
271 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
273 assertFalseIPv6( ':::' );
274 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
276 assertTrueIPv6( '::', 'IPv6 zero address' );
285 '::fc:100:a:d:1:e:ac',
287 'fc:100:a:d:1:e:ac:0'], function ( i
, addy
) {
288 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
291 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
292 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
294 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
295 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
296 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
298 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
299 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
300 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
301 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
302 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
303 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
304 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
305 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
306 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
308 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
309 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
312 QUnit
.test( 'isIPv4Address', 11, function ( assert
) {
314 function assertFalseIPv4( addy
, summary
) {
315 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), false, summary
);
318 function assertTrueIPv4( addy
, summary
) {
319 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), true, summary
);
322 // Based on IPTest.php > testisIPv4
323 assertFalseIPv4( false, 'Boolean false is not an IP' );
324 assertFalseIPv4( true, 'Boolean true is not an IP' );
325 assertFalseIPv4( '', 'Empty string is not an IP' );
326 assertFalseIPv4( 'abc', '"abc" is not an IP' );
327 assertFalseIPv4( ':', 'Colon is not an IP' );
328 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
329 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
330 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
332 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
333 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
334 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
336 }( mediaWiki
, jQuery
) );