Merge "Notice: Undefined index: page_is_redirect in \includes\api\ApiPageSet.php...
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
1 module( 'mediawiki.util', QUnit.newMwEnvironment() );
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 mw.config.set({
43 'wgScript': '/w/index.php',
44 'wgScriptPath': '/w',
45 'wgScriptExtension': '.php'
46 });
47
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 });
51
52 test( 'addCSS', function() {
53 expect(3);
54
55 var $testEl = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
56
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' );
60
61 equal( $testEl.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
62
63 // Clean up
64 $( style.ownerNode ).remove();
65 });
66
67 test( 'toggleToc', function() {
68 expect(4);
69
70 strictEqual( mw.util.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
71
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' );
82
83 strictEqual( $toggleLink.length, 1, 'Toggle link is appended to the page.' );
84
85 // Toggle animation is asynchronous
86 // QUnit should not finish this test() untill they are all done
87 stop();
88
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 };
99
100 actionA();
101 });
102
103 test( 'getParamValue', function() {
104 expect(5);
105
106 var url1 = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
107
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' );
110
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)' );
113
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' );
116
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)' );
119 });
120
121 test( 'tooltipAccessKey', function() {
122 expect(3);
123
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' );
127 });
128
129 test( '$content', function() {
130 expect(2);
131
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' );
134 });
135
136
137 /**
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, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo;
145 expect( 8 );
146
147 pTestTb = '\
148 <div class="portlet" id="p-test-tb">\
149 <h5>Toolbox</h5>\
150 <ul class="body"></ul>\
151 </div>';
152 pCustom = '\
153 <div class="portlet" id="p-test-custom">\
154 <h5>Views</h5>\
155 <ul class="body">\
156 <li id="c-foo"><a href="#">Foo</a></li>\
157 <li id="c-barmenu">\
158 <ul>\
159 <li id="c-bar-baz"><a href="#">Baz</a></a>\
160 </ul>\
161 </li>\
162 </ul>\
163 </div>';
164 vectorTabs = '\
165 <div id="p-test-views" class="vectorTabs">\
166 <h5>Views</h5>\
167 <ul></ul>\
168 </div>';
169
170 $( '#qunit-fixture' ).append( pTestTb, pCustom, vectorTabs );
171
172 tbRL = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
173 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l' );
174
175 ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
176
177 tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/',
178 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org ', 'm', tbRL );
179 $tbMW = $( tbMW );
180
181
182 equal( $tbMW.attr( 'id' ), 't-mworg', 'Link has correct ID set' );
183 equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
184 equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
185
186 cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux' );
187 $cuQuux = $(cuQuux);
188
189 equal(
190 $( '#p-test-custom #c-barmenu ul li' ).length,
191 1,
192 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
193 );
194
195 tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
196 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
197
198 equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
199
200 caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
201
202 strictEqual( $tbMW.find( 'span').length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
203 strictEqual( $( caFoo ).find( 'span').length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
204 });
205
206 test( 'jsMessage', function() {
207 expect(1);
208
209 var a = mw.util.jsMessage( "MediaWiki is <b>Awesome</b>." );
210 ok( a, 'Basic checking of return value' );
211
212 // Clean up
213 $( '#mw-js-message' ).remove();
214 });
215
216 test( 'validateEmail', function() {
217 expect(6);
218
219 strictEqual( mw.util.validateEmail( "" ), null, 'Should return null for empty string ' );
220 strictEqual( mw.util.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
221
222 // testEmailWithCommasAreInvalids
223 strictEqual( mw.util.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
224 strictEqual( mw.util.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
225
226 // testEmailWithHyphens
227 strictEqual( mw.util.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
228 strictEqual( mw.util.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
229 });
230
231 test( 'isIPv6Address', function() {
232 expect(40);
233
234 // Shortcuts
235 var assertFalseIPv6 = function( addy, summary ) {
236 return strictEqual( mw.util.isIPv6Address( addy ), false, summary );
237 },
238 assertTrueIPv6 = function( addy, summary ) {
239 return strictEqual( mw.util.isIPv6Address( addy ), true, summary );
240 };
241
242 // Based on IPTest.php > testisIPv6
243 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
244 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
245 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
246 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
247
248 $.each(
249 ['fc:100::',
250 'fc:100:a::',
251 'fc:100:a:d::',
252 'fc:100:a:d:1::',
253 'fc:100:a:d:1:e::',
254 'fc:100:a:d:1:e:ac::'], function( i, addy ){
255 assertTrueIPv6( addy, addy + ' is a valid IP' );
256 });
257
258 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
259 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
260
261 assertFalseIPv6( ':::' );
262 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
263
264 assertTrueIPv6( '::', 'IPv6 zero address' );
265 $.each(
266 ['::0',
267 '::fc',
268 '::fc:100',
269 '::fc:100:a',
270 '::fc:100:a:d',
271 '::fc:100:a:d:1',
272 '::fc:100:a:d:1:e',
273 '::fc:100:a:d:1:e:ac',
274
275 'fc:100:a:d:1:e:ac:0'], function( i, addy ){
276 assertTrueIPv6( addy, addy + ' is a valid IP' );
277 });
278
279 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
280 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
281
282 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
283 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
284 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
285
286 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
287 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
288 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
289 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
290 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
291 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
292 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
293 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
294 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
295
296 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
297 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
298 });
299
300 test( 'isIPv4Address', function() {
301 expect(11);
302
303 // Shortcuts
304 var assertFalseIPv4 = function( addy, summary ) {
305 return strictEqual( mw.util.isIPv4Address( addy ), false, summary );
306 },
307 assertTrueIPv4 = function( addy, summary ) {
308 return strictEqual( mw.util.isIPv4Address( addy ), true, summary );
309 };
310
311 // Based on IPTest.php > testisIPv4
312 assertFalseIPv4( false, 'Boolean false is not an IP' );
313 assertFalseIPv4( true, 'Boolean true is not an IP' );
314 assertFalseIPv4( '', 'Empty string is not an IP' );
315 assertFalseIPv4( 'abc', '"abc" is not an IP' );
316 assertFalseIPv4( ':', 'Colon is not an IP' );
317 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
318 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
319 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
320
321 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
322 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
323 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
324 });