Per Nikerabbit, fixes for r89176:
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.util / mediawiki.util.js
1 module( 'mediawiki.util.js' );
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(2);
23
24 // Not part of startUp module
25 mw.config.set( 'wgArticlePath', '/wiki/$1' );
26
27 var hrefA = mw.util.wikiGetlink( 'Sandbox' );
28 equal( hrefA, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
29
30 var hrefB = mw.util.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
31 equal( hrefB, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
32 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
33 });
34
35 test( 'wikiScript', function() {
36 expect(2);
37
38 mw.config.set({
39 'wgScript': '/w/index.php',
40 'wgScriptPath': '/w',
41 'wgScriptExtension': '.php'
42 });
43
44 equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript' );
45 equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' );
46
47 });
48
49 test( 'addCSS', function() {
50 expect(3);
51
52 var a = mw.util.addCSS( '#bodyContent { visibility: hidden; }' );
53 equal( typeof a, 'object', 'addCSS returned an object' );
54 strictEqual( a.disabled, false, 'property "disabled" is available and set to false' );
55
56 var $b = $('#bodyContent');
57 equal( $b.css('visibility'), 'hidden', 'Added style properties are in effect' );
58
59 // Clean up
60 $( a.ownerNode ).remove();
61 });
62
63 test( 'toggleToc', function() {
64 expect(3);
65
66 strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
67
68 var tocHtml =
69 '<table id="toc" class="toc"><tr><td>' +
70 '<div id="toctitle">' +
71 '<h2>Contents</h2>' +
72 '<span class="toctoggle">&nbsp;[<a href="#" class="internal" id="togglelink">Hide</a>&nbsp;]</span>' +
73 '</div>' +
74 '<ul><li></li></ul>' +
75 '</td></tr></table>';
76 var $toc = $(tocHtml).appendTo( 'body' );
77 var $toggleLink = $( '#togglelink' );
78
79 // Toggle animation is asynchronous
80 // QUnit should not finish this test() untill they are all done
81 stop();
82
83 var actionC = function() {
84 start();
85
86 // Clean up
87 $toc.remove();
88 };
89 var actionB = function() {
90 strictEqual( mw.util.toggleToc( $toggleLink, actionC ), true, 'Return boolean true if the TOC is now visible.' );
91 };
92 var actionA = function() {
93 strictEqual( mw.util.toggleToc( $toggleLink, actionB ), false, 'Return boolean false if the TOC is now hidden.' );
94 };
95
96 actionA();
97 });
98
99 test( 'getParamValue', function() {
100 expect(2);
101
102 var url = 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
103
104 equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
105 strictEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
106 });
107
108 test( 'tooltipAccessKey', function() {
109 expect(3);
110
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' );
114 });
115
116 test( '$content', function() {
117 expect(2);
118
119 ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
120 strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
121 });
122
123 test( 'addPortletLink', function() {
124 expect(5);
125
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' );
128
129 ok( $.isDomElement( A ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
130
131 var B = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/",
132 "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", A );
133
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)' );
137
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" );
140
141 equal( $( C ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
142
143 // Clean up
144 $( [A, B, C] ).remove();
145 });
146
147 test( 'jsMessage', function() {
148 expect(1);
149
150 var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
151 ok( a, 'Basic checking of return value' );
152
153 // Clean up
154 $( '#mw-js-message' ).remove();
155 });
156
157 test( 'validateEmail', function() {
158 expect(6);
159
160 strictEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' );
161 strictEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
162
163 // testEmailWithCommasAreInvalids
164 strictEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
165 strictEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
166
167 // testEmailWithHyphens
168 strictEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
169 strictEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
170 });
171
172 test( 'isIPv6Address', function() {
173 expect(40);
174
175 // Shortcuts
176 var assertFalseIPv6 = function( addy, summary ) {
177 return strictEqual( mw.util.isIPv6Address( addy ), false, summary );
178 },
179 assertTrueIPv6 = function( addy, summary ) {
180 return strictEqual( mw.util.isIPv6Address( addy ), true, summary );
181 };
182
183 // Based on IPTest.php > testisIPv6
184 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
185 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
186 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
187 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
188
189 $.each(
190 ['fc:100::',
191 'fc:100:a::',
192 'fc:100:a:d::',
193 'fc:100:a:d:1::',
194 'fc:100:a:d:1:e::',
195 'fc:100:a:d:1:e:ac::'], function( i, addy ){
196 assertTrueIPv6( addy, addy + ' is a valid IP' );
197 });
198
199 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
200 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
201
202 assertFalseIPv6( ':::' );
203 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
204
205 assertTrueIPv6( '::', 'IPv6 zero address' );
206 $.each(
207 ['::0',
208 '::fc',
209 '::fc:100',
210 '::fc:100:a',
211 '::fc:100:a:d',
212 '::fc:100:a:d:1',
213 '::fc:100:a:d:1:e',
214 '::fc:100:a:d:1:e:ac',
215
216 'fc:100:a:d:1:e:ac:0'], function( i, addy ){
217 assertTrueIPv6( addy, addy + ' is a valid IP' );
218 });
219
220 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
221 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
222
223 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
224 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
225 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
226
227 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
228 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
229 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
230 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
231 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
232 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
233 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
234 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
235 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
236
237 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
238 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
239 });
240
241 test( 'isIPv4Address', function() {
242 expect(11);
243
244 // Shortcuts
245 var assertFalseIPv4 = function( addy, summary ) {
246 return strictEqual( mw.util.isIPv4Address( addy ), false, summary );
247 },
248 assertTrueIPv4 = function( addy, summary ) {
249 return strictEqual( mw.util.isIPv4Address( addy ), true, summary );
250 };
251
252 // Based on IPTest.php > testisIPv4
253 assertFalseIPv4( false, 'Boolean false is not an IP' );
254 assertFalseIPv4( true, 'Boolean true is not an IP' );
255 assertFalseIPv4( '', 'Empty string is not an IP' );
256 assertFalseIPv4( 'abc', '"abc" is not an IP' );
257 assertFalseIPv4( ':', 'Colon is not an IP' );
258 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
259 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
260 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
261
262 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
263 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
264 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
265 });