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