8bb004f1e48c89b7180697359e1ec900f109f87a
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(){
39 'wgScript': '/w/index.php',
41 'wgScriptExtension': '.php',
44 equal( mw
.util
.wikiScript(), mw
.config
.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript.' );
48 test( 'addCSS', function(){
50 var a
= mw
.util
.addCSS( '#bodyContent { visibility: hidden; }' );
51 ok( a
, 'function works' );
52 deepEqual( a
.disabled
, false, 'property "disabled" is available and set to false' );
54 var $b
= $('#bodyContent');
55 equal( $b
.css('visibility'), 'hidden', 'Added style properties are in effect.' );
60 test( 'toggleToc', function(){
62 ok( mw
.util
.toggleToc
);
66 test( 'getParamValue', function(){
68 var url
= 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
70 equal( mw
.util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
71 deepEqual( mw
.util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
75 test( 'getActionFrom', function(){
78 var urlA
= 'http://mediawiki.org/wiki/Article',
79 urlB
= 'http://mediawiki.org/w/index.php?title=Article&action=edit',
80 urlC
= 'http://mediawiki.org/edit/Article',
81 urlD
= 'http://mediawiki.org/w/index.php/Article';
86 'wgArticlePath': '/wiki/$1'
89 equal( mw
.util
.getActionFrom( urlA
), 'view', 'wgArticlePath (/wiki/$1) support' );
90 equal( mw
.util
.getActionFrom( urlB
), 'edit', 'action-parameter support' );
93 mw
.config
.set( 'wgActionPaths', {
98 equal( mw
.util
.getActionFrom( urlB
), 'edit', 'action-parameter support with action path' );
99 equal( mw
.util
.getActionFrom( urlC
), 'edit', 'wgActionPaths support' );
104 'wgArticlePath': '/w/index.php/$1'
106 equal( mw
.util
.getActionFrom( urlB
), 'edit', 'action-parameter support with default settings' );
107 equal( mw
.util
.getActionFrom( urlD
), 'view', 'wgArticlePath (/index.php/$1) support' );
111 test( 'getTitleFrom', function(){
114 var urlA
= 'http://mediawiki.org/wiki/Article',
115 urlB
= 'http://mediawiki.org/w/index.php?title=Article&action=edit',
116 urlC
= 'http://mediawiki.org/edit/Article',
117 urlD
= 'http://mediawiki.org/w/index.php/Article';
122 'wgArticlePath': '/wiki/$1'
125 equal( mw
.util
.getTitleFrom( urlA
), 'Article', 'wgArticlePath (/wiki/$1) support' );
126 equal( mw
.util
.getTitleFrom( urlB
), 'Article', 'title-parameter support' );
129 mw
.config
.set( 'wgActionPaths', {
134 equal( mw
.util
.getTitleFrom( urlB
), 'Article', 'title-parameter support with action path' );
135 equal( mw
.util
.getTitleFrom( urlC
), 'Article', 'wgActionPaths support' );
140 'wgArticlePath': '/w/index.php/$1'
143 equal( mw
.util
.getTitleFrom( urlB
), 'Article', 'title-parameter support with default settings' );
144 equal( mw
.util
.getTitleFrom( urlD
), 'Article', 'wgArticlePath (/index.php/$1) support' );
148 test( 'tooltipAccessKey', function(){
150 equal( typeof mw
.util
.tooltipAccessKeyPrefix
, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
151 ok( mw
.util
.tooltipAccessKeyRegexp
instanceof RegExp
, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
152 ok( mw
.util
.updateTooltipAccessKeys
, 'mw.util.updateTooltipAccessKeys' );
156 test( '$content', function(){
158 ok( mw
.util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
159 deepEqual( mw
.util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
163 test( 'addPortletLink', function(){
165 var A
= mw
.util
.addPortletLink( 'p-tb', 'http://mediawiki.org/wiki/ResourceLoader',
166 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l', '#t-specialpages' );
168 ok( $.isDomElement( A
), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
170 var B
= mw
.util
.addPortletLink( "p-tb", "http://mediawiki.org/",
171 "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", A
);
173 equal( $( B
).attr( 'id' ), 't-mworg', 'Link has correct ID set' );
174 equal( $( B
).closest( '.portal' ).attr( 'id' ), 'p-tb', 'Link was inserted within correct portlet' );
175 equal( $( B
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
177 var C
= mw
.util
.addPortletLink( "p-tb", "http://mediawiki.org/wiki/RL/DM",
178 "Default modules", "t-rldm", "List of all default modules ", "d", "#t-rl" );
180 equal( $( C
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
183 $( [A
, B
, C
] ).remove();
187 test( 'jsMessage', function(){
189 var a
= mw
.util
.jsMessage( "MediaWiki is <b>Awesome</b>." );
191 ok( a
, 'Basic checking of return value' );
194 $( '#mw-js-message' ).remove();
198 test( 'validateEmail', function(){
200 deepEqual( mw
.util
.validateEmail( "" ), null, 'Empty string should return null' );
201 deepEqual( mw
.util
.validateEmail( "user@localhost" ), true );
203 // testEmailWithCommasAreInvalids
204 deepEqual( mw
.util
.validateEmail( "user,foo@example.org" ), false, 'Comma' );
205 deepEqual( mw
.util
.validateEmail( "userfoo@ex,ample.org" ), false, 'Comma' );
207 // testEmailWithHyphens
208 deepEqual( mw
.util
.validateEmail( "user-foo@example.org" ), true, 'Hyphen' );
209 deepEqual( mw
.util
.validateEmail( "userfoo@ex-ample.org" ), true, 'Hyphen' );
213 test( 'isIPv6Address', function(){
215 // Based on IPTest.php > IPv6
216 deepEqual( mw
.util
.isIPv6Address( "" ), false, 'Empty string is not an IP' );
217 deepEqual( mw
.util
.isIPv6Address( ":fc:100::" ), false, 'IPv6 starting with lone ":"' );
218 deepEqual( mw
.util
.isIPv6Address( "fc:100::" ), true );
219 deepEqual( mw
.util
.isIPv6Address( "fc:100:a:d:1:e:ac::" ), true );
220 deepEqual( mw
.util
.isIPv6Address( ":::" ), false );
221 deepEqual( mw
.util
.isIPv6Address( "::0:" ), false );
225 test( 'isIPv4Address', function(){
227 // Based on IPTest.php > IPv4
228 deepEqual( mw
.util
.isIPv4Address( "" ), false, 'Empty string is not an IP' );
229 deepEqual( mw
.util
.isIPv4Address( "...." ), false );
230 deepEqual( mw
.util
.isIPv4Address( "1.24.52.13" ), true );