2 QUnit
.module( 'mediawiki.util', QUnit
.newMwEnvironment( {
4 this.taPrefix
= mw
.util
.tooltipAccessKeyPrefix
;
5 mw
.util
.tooltipAccessKeyPrefix
= 'ctrl-alt-';
7 teardown: function () {
8 mw
.util
.tooltipAccessKeyPrefix
= this.taPrefix
;
12 QUnit
.test( 'rawurlencode', 1, function ( assert
) {
13 assert
.equal( mw
.util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
16 QUnit
.test( 'wikiUrlencode', 1, function ( assert
) {
17 assert
.equal( mw
.util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
20 QUnit
.test( 'wikiGetlink', 4, function ( assert
) {
21 // Not part of startUp module
22 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
23 mw
.config
.set( 'wgPageName', 'Foobar' );
25 var href
= mw
.util
.wikiGetlink( 'Sandbox' );
26 assert
.equal( href
, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
28 href
= mw
.util
.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
29 assert
.equal( href
, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
30 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
32 href
= mw
.util
.wikiGetlink();
33 assert
.equal( href
, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
35 href
= mw
.util
.wikiGetlink( 'Sandbox', { action
: 'edit' } );
36 assert
.equal( href
, '/wiki/Sandbox?action=edit',
37 'Simple title with query string; Get link for "Sandbox" with action=edit' );
40 QUnit
.test( 'wikiScript', 4, function ( assert
) {
42 'wgScript': '/w/i.php', // customized wgScript for bug 39103
43 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103
45 'wgScriptExtension': '.php'
48 assert
.equal( mw
.util
.wikiScript(), mw
.config
.get( 'wgScript' ),
49 'wikiScript() returns wgScript'
51 assert
.equal( mw
.util
.wikiScript( 'index' ), mw
.config
.get( 'wgScript' ),
52 'wikiScript( index ) returns wgScript'
54 assert
.equal( mw
.util
.wikiScript( 'load' ), mw
.config
.get( 'wgLoadScript' ),
55 'wikiScript( load ) returns wgLoadScript'
57 assert
.equal( mw
.util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
60 QUnit
.test( 'addCSS', 3, function ( assert
) {
62 $el
= $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
64 style
= mw
.util
.addCSS( '#mw-addcsstest { visibility: hidden; }' );
65 assert
.equal( typeof style
, 'object', 'addCSS returned an object' );
66 assert
.strictEqual( style
.disabled
, false, 'property "disabled" is available and set to false' );
68 assert
.equal( $el
.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
71 $( style
.ownerNode
).remove();
74 QUnit
.asyncTest( 'toggleToc', 4, function ( assert
) {
75 var tocHtml
, $toggleLink
;
82 assert
.strictEqual( mw
.util
.toggleToc( $toggleLink
, actionC
), true, 'Return boolean true if the TOC is now visible.' );
86 assert
.strictEqual( mw
.util
.toggleToc( $toggleLink
, actionB
), false, 'Return boolean false if the TOC is now hidden.' );
89 assert
.strictEqual( mw
.util
.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
91 tocHtml
= '<div id="toc" class="toc">' +
92 '<div id="toctitle">' +
94 '<span class="toctoggle"> [<a href="#" class="internal" id="togglelink">Hide</a> ]</span>' +
96 '<ul><li></li></ul>' +
98 $( tocHtml
).appendTo( '#qunit-fixture' );
99 $toggleLink
= $( '#togglelink' );
101 assert
.strictEqual( $toggleLink
.length
, 1, 'Toggle link is appended to the page.' );
106 QUnit
.test( 'getParamValue', 5, function ( assert
) {
109 url
= 'http://example.org/?foo=wrong&foo=right#&foo=bad';
110 assert
.equal( mw
.util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
111 assert
.strictEqual( mw
.util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
113 url
= 'http://example.org/#&foo=bad';
114 assert
.strictEqual( mw
.util
.getParamValue( 'foo', url
), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
116 url
= 'example.org?' + $.param( { 'TEST': 'a b+c' } );
117 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url
), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
119 url
= 'example.org?' + $.param( { 'TEST': 'a b+c d' } ); // check for sloppy code from r95332 :)
120 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url
), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
123 QUnit
.test( 'tooltipAccessKey', 4, function ( assert
) {
124 assert
.equal( typeof mw
.util
.tooltipAccessKeyPrefix
, 'string', 'tooltipAccessKeyPrefix must be a string' );
125 assert
.equal( $.type( mw
.util
.tooltipAccessKeyRegexp
), 'regexp', 'tooltipAccessKeyRegexp is a regexp' );
126 assert
.ok( mw
.util
.updateTooltipAccessKeys
, 'updateTooltipAccessKeys is non-empty' );
128 'Example [a]'.replace( mw
.util
.tooltipAccessKeyRegexp
, function ( sub
, m1
, m2
, m3
, m4
, m5
, m6
) {
129 assert
.equal( m6
, 'a', 'tooltipAccessKeyRegexp finds the accesskey hint' );
133 QUnit
.test( '$content', 2, function ( assert
) {
134 assert
.ok( mw
.util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
135 assert
.strictEqual( mw
.util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
139 * Portlet names are prefixed with 'p-test' to avoid conflict with core
140 * when running the test suite under a wiki page.
141 * Previously, test elements where invisible to the selector since only
142 * one element can have a given id.
144 QUnit
.test( 'addPortletLink', 11, function ( assert
) {
145 var pTestTb
, pCustom
, vectorTabs
, tbRL
, cuQuux
, $cuQuux
, tbMW
, $tbMW
, tbRLDM
, caFoo
, addedAfter
;
148 <div class="portlet" id="p-test-tb">\
150 <ul class="body"></ul>\
153 <div class="portlet" id="p-test-custom">\
156 <li id="c-foo"><a href="#">Foo</a></li>\
159 <li id="c-bar-baz"><a href="#">Baz</a></a>\
165 <div id="p-test-views" class="vectorTabs">\
170 $( '#qunit-fixture' ).append( pTestTb
, pCustom
, vectorTabs
);
172 tbRL
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
173 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l'
176 assert
.ok( $.isDomElement( tbRL
), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
178 tbMW
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/',
179 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL
);
187 'Validate attributes of created element'
191 $tbMW
.find( 'a' ).getAttrs(),
193 href
: '//mediawiki.org/',
194 title
: 'Go to MediaWiki.org [ctrl-alt-m]',
197 'Validate attributes of anchor tag in created element'
200 assert
.equal( $tbMW
.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
201 assert
.strictEqual( $tbMW
.next()[0], tbRL
, 'Link is in the correct position (by passing nextnode)' );
203 cuQuux
= mw
.util
.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
204 $cuQuux
= $( cuQuux
);
206 assert
.equal( $cuQuux
.find( 'a' ).attr( 'title' ), 'Example [ctrl-alt-q]', 'Existing accesskey is stripped and updated' );
209 $( '#p-test-custom #c-barmenu ul li' ).length
,
211 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
214 tbRLDM
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
215 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
217 assert
.equal( $( tbRLDM
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
219 caFoo
= mw
.util
.addPortletLink( 'p-test-views', '#', 'Foo' );
221 assert
.strictEqual( $tbMW
.find( 'span' ).length
, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
222 assert
.strictEqual( $( caFoo
).find( 'span' ).length
, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
224 addedAfter
= mw
.util
.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL
) );
225 assert
.strictEqual( $( addedAfter
).next()[0], tbRL
, 'Link is in the correct position (by passing a jQuery object as nextnode)' );
228 QUnit
.test( 'jsMessage', 1, function ( assert
) {
229 var a
= mw
.util
.jsMessage( 'MediaWiki is <b>Awesome</b>.' );
230 assert
.ok( a
, 'Basic checking of return value' );
233 $( '#mw-js-message' ).remove();
236 QUnit
.test( 'validateEmail', 6, function ( assert
) {
237 assert
.strictEqual( mw
.util
.validateEmail( '' ), null, 'Should return null for empty string ' );
238 assert
.strictEqual( mw
.util
.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
240 // testEmailWithCommasAreInvalids
241 assert
.strictEqual( mw
.util
.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
242 assert
.strictEqual( mw
.util
.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
244 // testEmailWithHyphens
245 assert
.strictEqual( mw
.util
.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
246 assert
.strictEqual( mw
.util
.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
249 QUnit
.test( 'isIPv6Address', 40, function ( assert
) {
251 function assertFalseIPv6( addy
, summary
) {
252 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), false, summary
);
255 function assertTrueIPv6( addy
, summary
) {
256 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), true, summary
);
259 // Based on IPTest.php > testisIPv6
260 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
261 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
262 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
263 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
271 'fc:100:a:d:1:e:ac::'], function ( i
, addy
) {
272 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
275 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
276 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
278 assertFalseIPv6( ':::' );
279 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
281 assertTrueIPv6( '::', 'IPv6 zero address' );
290 '::fc:100:a:d:1:e:ac',
292 'fc:100:a:d:1:e:ac:0'], function ( i
, addy
) {
293 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
296 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
297 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
299 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
300 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
301 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
303 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
304 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
305 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
306 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
307 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
308 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
309 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
310 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
311 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
313 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
314 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
317 QUnit
.test( 'isIPv4Address', 11, function ( assert
) {
319 function assertFalseIPv4( addy
, summary
) {
320 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), false, summary
);
323 function assertTrueIPv4( addy
, summary
) {
324 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), true, summary
);
327 // Based on IPTest.php > testisIPv4
328 assertFalseIPv4( false, 'Boolean false is not an IP' );
329 assertFalseIPv4( true, 'Boolean true is not an IP' );
330 assertFalseIPv4( '', 'Empty string is not an IP' );
331 assertFalseIPv4( 'abc', '"abc" is not an IP' );
332 assertFalseIPv4( ':', 'Colon is not an IP' );
333 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
334 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
335 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
337 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
338 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
339 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
341 }( mediaWiki
, jQuery
) );