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