1 module( 'mediawiki.util.js' );
3 test( '-- Initial check', function(){
5 ok( mw.util, 'mw.util defined' );
9 test( 'rawurlencode', function(){
11 equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
15 test( 'wikiUrlencode', function(){
17 equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
21 test( 'wikiGetlink', function(){
23 // Not part of startUp module
24 mw.config.set( 'wgArticlePath', '/wiki/$1' );
26 var hrefA = mw.util.wikiGetlink( 'Sandbox' );
28 equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
30 var hrefB = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
32 equal( hrefB, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage', 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
36 test( 'wikiScript', function(){
40 'wgScript': '/w/index.php',
42 'wgScriptExtension': '.php'
45 equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript' );
46 deepEqual( mw.util.wikiScript( 'api' ), '/w/api.php' );
50 test( 'addCSS', function(){
53 var a = mw.util.addCSS( '#bodyContent { visibility: hidden; }' );
54 ok( a, 'function works' );
55 deepEqual( a.disabled, false, 'property "disabled" is available and set to false' );
57 var $b = $('#bodyContent');
58 equal( $b.css('visibility'), 'hidden', 'Added style properties are in effect' );
62 test( 'toggleToc', function(){
65 deepEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
68 '<table id="toc" class="toc"><tr><td>' +
69 '<div id="toctitle">' +
71 '<span class="toctoggle"> [<a href="#" class="internal" id="togglelink">Hide</a> ]</span>' +
73 '<ul><li></li></ul>' +
75 var $toc = $(tocHtml).appendTo( 'body' );
76 var $toggleLink = $( '#togglelink' );
78 // Toggle animation is asynchronous
79 // QUnit should not finish this test() untill they are all done
82 var actionC = function(){
88 var actionB = function(){
89 deepEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' );
91 var actionA = function(){
92 deepEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' );
100 test( 'getParamValue', function(){
102 var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
104 equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
105 deepEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
109 test( 'tooltipAccessKey', function(){
111 equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
112 ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
113 ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' );
117 test( '$content', function(){
119 ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
120 deepEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
124 test( 'addPortletLink', function(){
126 var A = mw.util.addPortletLink( 'p-tb', 'http://mediawiki.org/wiki/ResourceLoader',
127 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l', '#t-specialpages' );
129 ok( $.isDomElement( A ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
131 var B = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/",
132 "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", A );
134 equal( $( B ).attr( 'id' ), 't-mworg', 'Link has correct ID set' );
135 equal( $( B ).closest( '.portal' ).attr( 'id' ), 'p-tb', 'Link was inserted within correct portlet' );
136 equal( $( B ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
138 var C = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/wiki/RL/DM",
139 "Default modules", "t-rldm", "List of all default modules ", "d", "#t-rl" );
141 equal( $( C ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
144 $( [A, B, C] ).remove();
148 test( 'jsMessage', function(){
150 var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
152 ok( a, 'Basic checking of return value' );
155 $( '#mw-js-message' ).remove();
159 test( 'validateEmail', function(){
162 deepEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' );
163 deepEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
165 // testEmailWithCommasAreInvalids
166 deepEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
167 deepEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
169 // testEmailWithHyphens
170 deepEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
171 deepEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
175 test( 'isIPv6Address', function(){
178 // Based on IPTest.php > IPv6
179 deepEqual( mw.util.isIPv6Address( "" ), false, 'Empty string is not an IP' );
180 deepEqual( mw.util.isIPv6Address( ":fc:100::" ), false, 'IPv6 starting with lone ":"' );
181 deepEqual( mw.util.isIPv6Address( "fc:100::" ), true );
182 deepEqual( mw.util.isIPv6Address( "fc:100:a:d:1:e:ac::" ), true );
183 deepEqual( mw.util.isIPv6Address( ":::" ), false );
184 deepEqual( mw.util.isIPv6Address( "::0:" ), false );
188 test( 'isIPv4Address', function(){
191 // Based on IPTest.php > IPv4
192 deepEqual( mw.util.isIPv4Address( "" ), false, 'Empty string is not an IP' );
193 deepEqual( mw.util.isIPv4Address( "...." ), false );
194 deepEqual( mw.util.isIPv4Address( "1.24.52.13" ), true );