Localisation updates from http://translatewiki.net.
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
blobd396b04c1dd89a88470936d674160fbc0f320253
1 module( 'mediawiki.util', QUnit.newMwEnvironment() );
3 test( '-- Initial check', function() {
4         expect(1);
6         ok( mw.util, 'mw.util defined' );
7 });
9 test( 'rawurlencode', function() {
10         expect(1);
12         equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
13 });
15 test( 'wikiUrlencode', function() {
16         expect(1);
18         equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
19 });
21 test( 'wikiGetlink', function() {
22         expect(3);
24         // Not part of startUp module
25         mw.config.set( 'wgArticlePath', '/wiki/$1' );
26         mw.config.set( 'wgPageName', 'Foobar' );
28         var hrefA = mw.util.wikiGetlink( 'Sandbox' );
29         equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
31         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',
33                 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
35         var hrefC = mw.util.wikiGetlink();
36         equal( hrefC, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
37 });
39 test( 'wikiScript', function() {
40         expect(2);
42         mw.config.set({
43                 'wgScript': '/w/index.php',
44                 'wgScriptPath': '/w',
45                 'wgScriptExtension': '.php'
46         });
48         equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript' );
49         equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' );
50 });
52 test( 'addCSS', function() {
53         expect(3);
55         var $testEl = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
57         var style = mw.util.addCSS( '#mw-addcsstest { visibility: hidden; }' );
58         equal( typeof style, 'object', 'addCSS returned an object' );
59         strictEqual( style.disabled, false, 'property "disabled" is available and set to false' );
61         equal( $testEl.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
63         // Clean up
64         $( style.ownerNode ).remove();
65 });
67 test( 'toggleToc', function() {
68         expect(4);
70         strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
72         var     tocHtml =
73         '<table id="toc" class="toc"><tr><td>' +
74                 '<div id="toctitle">' +
75                         '<h2>Contents</h2>' +
76                         '<span class="toctoggle">&nbsp;[<a href="#" class="internal" id="togglelink">Hide</a>&nbsp;]</span>' +
77                 '</div>' +
78                 '<ul><li></li></ul>' +
79         '</td></tr></table>',
80                 $toc = $(tocHtml).appendTo( '#qunit-fixture' ),
81                 $toggleLink = $( '#togglelink' );
83         strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' );
85         // Toggle animation is asynchronous
86         // QUnit should not finish this test() untill they are all done
87         stop();
89         var actionC = function() {
90                 start();
91         };
92         var actionB = function() {
93                 start(); stop();
94                 strictEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' );
95         };
96         var actionA = function() {
97                 strictEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' );
98         };
100         actionA();
103 test( 'getParamValue', function() {
104         expect(5);
106         var     url1 = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
108         equal( mw.util.getParamValue( 'foo', url1 ), 'right', 'Use latest one, ignore hash' );
109         strictEqual( mw.util.getParamValue( 'bar', url1 ), null, 'Return null when not found' );
111         var url2 = 'http://example.org/#&foo=bad';
112         strictEqual( mw.util.getParamValue( 'foo', url2 ), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
114         var url3 = 'example.org?' + $.param({ 'TEST': 'a b+c' });
115         strictEqual( mw.util.getParamValue( 'TEST', url3 ), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
117         var url4 = 'example.org?' + $.param({ 'TEST': 'a b+c d' }); // check for sloppy code from r95332 :)
118         strictEqual( mw.util.getParamValue( 'TEST', url4 ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
121 test( 'tooltipAccessKey', function() {
122         expect(3);
124         equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
125         ok( mw.util.tooltipAccessKeyRegexp instanceof RegExp, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
126         ok( mw.util.updateTooltipAccessKeys, 'mw.util.updateTooltipAccessKeys' );
129 test( '$content', function() {
130         expect(2);
132         ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
133         strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
138  * Portlet names are prefixed with 'p-test' to avoid conflict with core
139  * when running the test suite under a wiki page.
140  * Previously, test elements where invisible to the selector since only
141  * one element can have a given id. 
142  */
143 test( 'addPortletLink', function () {
144         var pTestTb, vectorTabs, tbRL, tbMW, $tbMW, tbRLDM, caFoo;
145         expect(7);
147         pTestTb = '\
148         <div class="portlet" id="p-test-tb">\
149                 <ul class="body"></ul>\
150         </div>';
151         vectorTabs = '\
152         <div id="p-test-views" class="vectorTabs">\
153                 <h5>Views</h5>\
154                 <ul></ul>\
155         </div>';
157         $('#qunit-fixture').append(pTestTb, vectorTabs);
159         tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
160                 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' );
162         ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
164         tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/',
165                 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL );
166         $tbMW = $( tbMW );
169         equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' );
170         equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
171         equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
173         tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
174                 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
176         equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
178         caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
180         strictEqual( $tbMW.find( 'span').length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
181         strictEqual( $( caFoo ).find( 'span').length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
184 test( 'jsMessage', function() {
185         expect(1);
187         var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
188         ok( a, 'Basic checking of return value' );
190         // Clean up
191         $( '#mw-js-message' ).remove();
194 test( 'validateEmail', function() {
195         expect(6);
197         strictEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' );
198         strictEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
200         // testEmailWithCommasAreInvalids
201         strictEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
202         strictEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
204         // testEmailWithHyphens
205         strictEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
206         strictEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
209 test( 'isIPv6Address', function() {
210         expect(40);
212         // Shortcuts
213         var     assertFalseIPv6 = function( addy, summary ) {
214                         return strictEqual( mw.util.isIPv6Address( addy ), false, summary );
215                 },
216                 assertTrueIPv6 = function( addy, summary ) {
217                         return strictEqual( mw.util.isIPv6Address( addy ), true, summary );
218                 };
220         // Based on IPTest.php > testisIPv6
221         assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
222         assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
223         assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
224         assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
226         $.each(
227         ['fc:100::',
228         'fc:100:a::',
229         'fc:100:a:d::',
230         'fc:100:a:d:1::',
231         'fc:100:a:d:1:e::',
232         'fc:100:a:d:1:e:ac::'], function( i, addy ){
233                 assertTrueIPv6( addy, addy + ' is a valid IP' );
234         });
236         assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
237         assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
239         assertFalseIPv6( ':::' );
240         assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
242         assertTrueIPv6( '::', 'IPv6 zero address' );
243         $.each(
244         ['::0',
245         '::fc',
246         '::fc:100',
247         '::fc:100:a',
248         '::fc:100:a:d',
249         '::fc:100:a:d:1',
250         '::fc:100:a:d:1:e',
251         '::fc:100:a:d:1:e:ac',
253         'fc:100:a:d:1:e:ac:0'], function( i, addy ){
254                 assertTrueIPv6( addy, addy + ' is a valid IP' );
255         });
257         assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
258         assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
260         assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
261         assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
262         assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
264         assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
265         assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
266         assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
267         assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
268         assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
269         assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
270         assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
271         assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
272         assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
274         assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
275         assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
278 test( 'isIPv4Address', function() {
279         expect(11);
281         // Shortcuts
282         var     assertFalseIPv4 = function( addy, summary ) {
283                         return strictEqual( mw.util.isIPv4Address( addy ), false, summary );
284                 },
285                 assertTrueIPv4 = function( addy, summary ) {
286                         return strictEqual( mw.util.isIPv4Address( addy ), true, summary );
287                 };
289         // Based on IPTest.php > testisIPv4
290         assertFalseIPv4( false, 'Boolean false is not an IP' );
291         assertFalseIPv4( true, 'Boolean true is not an IP' );
292         assertFalseIPv4( '', 'Empty string is not an IP' );
293         assertFalseIPv4( 'abc', '"abc" is not an IP' );
294         assertFalseIPv4( ':', 'Colon is not an IP' );
295         assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
296         assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
297         assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
299         assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
300         assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
301         assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );