1 QUnit
.module( 'mediawiki.util', QUnit
.newMwEnvironment() );
3 QUnit
.test( 'rawurlencode', 1, function ( assert
) {
4 assert
.equal( mw
.util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
7 QUnit
.test( 'wikiUrlencode', 1, function ( assert
) {
8 assert
.equal( mw
.util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
11 QUnit
.test( 'wikiGetlink', 3, function ( assert
) {
12 // Not part of startUp module
13 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
14 mw
.config
.set( 'wgPageName', 'Foobar' );
16 var hrefA
= mw
.util
.wikiGetlink( 'Sandbox' );
17 assert
.equal( hrefA
, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
19 var hrefB
= mw
.util
.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
20 assert
.equal( hrefB
, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
21 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
23 var hrefC
= mw
.util
.wikiGetlink();
24 assert
.equal( hrefC
, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
27 QUnit
.test( 'wikiScript', 4, function ( assert
) {
29 'wgScript': '/w/i.php', // customized wgScript for bug 39103
30 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103
32 'wgScriptExtension': '.php'
35 assert
.equal( mw
.util
.wikiScript(), mw
.config
.get( 'wgScript' ), 'wikiScript() returns wgScript' );
36 assert
.equal( mw
.util
.wikiScript( 'index' ), mw
.config
.get( 'wgScript' ), "wikiScript( 'index' ) returns wgScript" );
37 assert
.equal( mw
.util
.wikiScript( 'load' ), mw
.config
.get( 'wgLoadScript' ), "wikiScript( 'load' ) returns wgLoadScript" );
38 assert
.equal( mw
.util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
41 QUnit
.test( 'addCSS', 3, function ( assert
) {
42 var $testEl
= $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
44 var style
= mw
.util
.addCSS( '#mw-addcsstest { visibility: hidden; }' );
45 assert
.equal( typeof style
, 'object', 'addCSS returned an object' );
46 assert
.strictEqual( style
.disabled
, false, 'property "disabled" is available and set to false' );
48 assert
.equal( $testEl
.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
51 $( style
.ownerNode
).remove();
54 QUnit
.asyncTest( 'toggleToc', 4, function ( assert
) {
55 assert
.strictEqual( mw
.util
.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
58 '<table id="toc" class="toc"><tr><td>' +
59 '<div id="toctitle">' +
61 '<span class="toctoggle"> [<a href="#" class="internal" id="togglelink">Hide</a> ]</span>' +
63 '<ul><li></li></ul>' +
65 $toc
= $(tocHtml
).appendTo( '#qunit-fixture' ),
66 $toggleLink
= $( '#togglelink' );
68 assert
.strictEqual( $toggleLink
.length
, 1, 'Toggle link is appended to the page.' );
70 var actionC = function() {
73 var actionB = function() {
74 assert
.strictEqual( mw
.util
.toggleToc( $toggleLink
, actionC
), true, 'Return boolean true if the TOC is now visible.' );
76 var actionA = function() {
77 assert
.strictEqual( mw
.util
.toggleToc( $toggleLink
, actionB
), false, 'Return boolean false if the TOC is now hidden.' );
83 QUnit
.test( 'getParamValue', 5, function ( assert
) {
84 var url1
= 'http://example.org/?foo=wrong&foo=right#&foo=bad';
86 assert
.equal( mw
.util
.getParamValue( 'foo', url1
), 'right', 'Use latest one, ignore hash' );
87 assert
.strictEqual( mw
.util
.getParamValue( 'bar', url1
), null, 'Return null when not found' );
89 var url2
= 'http://example.org/#&foo=bad';
90 assert
.strictEqual( mw
.util
.getParamValue( 'foo', url2
), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
92 var url3
= 'example.org?' + $.param({ 'TEST': 'a b+c' });
93 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url3
), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
95 var url4
= 'example.org?' + $.param({ 'TEST': 'a b+c d' }); // check for sloppy code from r95332 :)
96 assert
.strictEqual( mw
.util
.getParamValue( 'TEST', url4
), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
99 QUnit
.test( 'tooltipAccessKey', 3, function ( assert
) {
100 assert
.equal( typeof mw
.util
.tooltipAccessKeyPrefix
, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
101 assert
.ok( mw
.util
.tooltipAccessKeyRegexp
instanceof RegExp
, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
102 assert
.ok( mw
.util
.updateTooltipAccessKeys
, 'mw.util.updateTooltipAccessKeys' );
105 QUnit
.test( '$content', 2, function ( assert
) {
106 assert
.ok( mw
.util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
107 assert
.strictEqual( mw
.util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
112 * Portlet names are prefixed with 'p-test' to avoid conflict with core
113 * when running the test suite under a wiki page.
114 * Previously, test elements where invisible to the selector since only
115 * one element can have a given id.
117 QUnit
.test( 'addPortletLink', 8, function ( assert
) {
118 var pTestTb
, pCustom
, vectorTabs
, tbRL
, cuQuux
, $cuQuux
, tbMW
, $tbMW
, tbRLDM
, caFoo
;
120 <div class="portlet" id="p-test-tb">\
122 <ul class="body"></ul>\
125 <div class="portlet" id="p-test-custom">\
128 <li id="c-foo"><a href="#">Foo</a></li>\
131 <li id="c-bar-baz"><a href="#">Baz</a></a>\
137 <div id="p-test-views" class="vectorTabs">\
142 $( '#qunit-fixture' ).append( pTestTb
, pCustom
, vectorTabs
);
144 tbRL
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
145 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' );
147 assert
.ok( $.isDomElement( tbRL
), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
149 tbMW
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/',
150 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL
);
154 assert
.equal( $tbMW
.attr( 'id' ), 't-mworg', 'Link has correct ID set' );
155 assert
.equal( $tbMW
.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
156 assert
.equal( $tbMW
.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
158 cuQuux
= mw
.util
.addPortletLink( 'p-test-custom', '#', 'Quux' );
162 $( '#p-test-custom #c-barmenu ul li' ).length
,
164 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
167 tbRLDM
= mw
.util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
168 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
170 assert
.equal( $( tbRLDM
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
172 caFoo
= mw
.util
.addPortletLink( 'p-test-views', '#', 'Foo' );
174 assert
.strictEqual( $tbMW
.find( 'span').length
, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
175 assert
.strictEqual( $( caFoo
).find( 'span').length
, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
178 QUnit
.test( 'jsMessage', 1, function ( assert
) {
179 var a
= mw
.util
.jsMessage( "MediaWiki is <b>Awesome</b>." );
180 assert
.ok( a
, 'Basic checking of return value' );
183 $( '#mw-js-message' ).remove();
186 QUnit
.test( 'validateEmail', 6, function ( assert
) {
187 assert
.strictEqual( mw
.util
.validateEmail( "" ), null, 'Should return null for empty string ' );
188 assert
.strictEqual( mw
.util
.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
190 // testEmailWithCommasAreInvalids
191 assert
.strictEqual( mw
.util
.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
192 assert
.strictEqual( mw
.util
.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
194 // testEmailWithHyphens
195 assert
.strictEqual( mw
.util
.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
196 assert
.strictEqual( mw
.util
.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
199 QUnit
.test( 'isIPv6Address', 40, function ( assert
) {
201 function assertFalseIPv6( addy
, summary
) {
202 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), false, summary
);
204 function assertTrueIPv6( addy
, summary
) {
205 return assert
.strictEqual( mw
.util
.isIPv6Address( addy
), true, summary
);
208 // Based on IPTest.php > testisIPv6
209 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
210 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
211 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
212 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
220 'fc:100:a:d:1:e:ac::'], function ( i
, addy
){
221 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
224 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
225 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
227 assertFalseIPv6( ':::' );
228 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
230 assertTrueIPv6( '::', 'IPv6 zero address' );
239 '::fc:100:a:d:1:e:ac',
241 'fc:100:a:d:1:e:ac:0'], function ( i
, addy
){
242 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
245 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
246 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
248 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
249 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
250 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
252 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
253 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
254 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
255 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
256 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
257 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
258 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
259 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
260 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
262 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
263 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
266 QUnit
.test( 'isIPv4Address', 11, function ( assert
) {
268 function assertFalseIPv4( addy
, summary
) {
269 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), false, summary
);
271 function assertTrueIPv4( addy
, summary
) {
272 assert
.strictEqual( mw
.util
.isIPv4Address( addy
), true, summary
);
275 // Based on IPTest.php > testisIPv4
276 assertFalseIPv4( false, 'Boolean false is not an IP' );
277 assertFalseIPv4( true, 'Boolean true is not an IP' );
278 assertFalseIPv4( '', 'Empty string is not an IP' );
279 assertFalseIPv4( 'abc', '"abc" is not an IP' );
280 assertFalseIPv4( ':', 'Colon is not an IP' );
281 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
282 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
283 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
285 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
286 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
287 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );