81fda7e3b2daa264fa3b8aac9baaebad2f17ad3e
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.util.test.js
1 ( function ( mw, $ ) {
2 QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment( {
3 setup: function () {
4 $.fn.updateTooltipAccessKeys.setTestMode( true );
5 },
6 teardown: function () {
7 $.fn.updateTooltipAccessKeys.setTestMode( false );
8 }
9 } ) );
10
11 QUnit.test( 'rawurlencode', 1, function ( assert ) {
12 assert.equal( mw.util.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
13 } );
14
15 QUnit.test( 'wikiUrlencode', 1, function ( assert ) {
16 assert.equal( mw.util.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
17 } );
18
19 QUnit.test( 'getUrl', 4, function ( assert ) {
20 // Not part of startUp module
21 mw.config.set( 'wgArticlePath', '/wiki/$1' );
22 mw.config.set( 'wgPageName', 'Foobar' );
23
24 var href = mw.util.getUrl( 'Sandbox' );
25 assert.equal( href, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
26
27 href = mw.util.getUrl( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
28 assert.equal( href, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
29 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
30
31 href = mw.util.getUrl();
32 assert.equal( href, '/wiki/Foobar', 'Default title; Get link for current page ("Foobar")' );
33
34 href = mw.util.getUrl( 'Sandbox', { action: 'edit' } );
35 assert.equal( href, '/wiki/Sandbox?action=edit',
36 'Simple title with query string; Get link for "Sandbox" with action=edit' );
37 } );
38
39 QUnit.test( 'wikiScript', 4, function ( assert ) {
40 mw.config.set( {
41 'wgScript': '/w/i.php', // customized wgScript for bug 39103
42 'wgLoadScript': '/w/l.php', // customized wgLoadScript for bug 39103
43 'wgScriptPath': '/w',
44 'wgScriptExtension': '.php'
45 } );
46
47 assert.equal( mw.util.wikiScript(), mw.config.get( 'wgScript' ),
48 'wikiScript() returns wgScript'
49 );
50 assert.equal( mw.util.wikiScript( 'index' ), mw.config.get( 'wgScript' ),
51 'wikiScript( index ) returns wgScript'
52 );
53 assert.equal( mw.util.wikiScript( 'load' ), mw.config.get( 'wgLoadScript' ),
54 'wikiScript( load ) returns wgLoadScript'
55 );
56 assert.equal( mw.util.wikiScript( 'api' ), '/w/api.php', 'API path' );
57 } );
58
59 QUnit.test( 'addCSS', 3, function ( assert ) {
60 var $el, style;
61 $el = $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
62
63 style = mw.util.addCSS( '#mw-addcsstest { visibility: hidden; }' );
64 assert.equal( typeof style, 'object', 'addCSS returned an object' );
65 assert.strictEqual( style.disabled, false, 'property "disabled" is available and set to false' );
66
67 assert.equal( $el.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
68
69 // Clean up
70 $( style.ownerNode ).remove();
71 } );
72
73 QUnit.test( 'getParamValue', 5, function ( assert ) {
74 var url;
75
76 url = 'http://example.org/?foo=wrong&foo=right#&foo=bad';
77 assert.equal( mw.util.getParamValue( 'foo', url ), 'right', 'Use latest one, ignore hash' );
78 assert.strictEqual( mw.util.getParamValue( 'bar', url ), null, 'Return null when not found' );
79
80 url = 'http://example.org/#&foo=bad';
81 assert.strictEqual( mw.util.getParamValue( 'foo', url ), null, 'Ignore hash if param is not in querystring but in hash (bug 27427)' );
82
83 url = 'example.org?' + $.param( { 'TEST': 'a b+c' } );
84 assert.strictEqual( mw.util.getParamValue( 'TEST', url ), 'a b+c', 'Bug 30441: getParamValue must understand "+" encoding of space' );
85
86 url = 'example.org?' + $.param( { 'TEST': 'a b+c d' } ); // check for sloppy code from r95332 :)
87 assert.strictEqual( mw.util.getParamValue( 'TEST', url ), 'a b+c d', 'Bug 30441: getParamValue must understand "+" encoding of space (multiple spaces)' );
88 } );
89
90 QUnit.test( 'tooltipAccessKey', 4, function ( assert ) {
91 this.suppressWarnings();
92
93 assert.equal( typeof mw.util.tooltipAccessKeyPrefix, 'string', 'tooltipAccessKeyPrefix must be a string' );
94 assert.equal( $.type( mw.util.tooltipAccessKeyRegexp ), 'regexp', 'tooltipAccessKeyRegexp is a regexp' );
95 assert.ok( mw.util.updateTooltipAccessKeys, 'updateTooltipAccessKeys is non-empty' );
96
97 'Example [a]'.replace( mw.util.tooltipAccessKeyRegexp, function ( sub, m1, m2, m3, m4, m5, m6 ) {
98 assert.equal( m6, 'a', 'tooltipAccessKeyRegexp finds the accesskey hint' );
99 } );
100
101 this.restoreWarnings();
102 } );
103
104 QUnit.test( '$content', 2, function ( assert ) {
105 assert.ok( mw.util.$content instanceof jQuery, 'mw.util.$content instance of jQuery' );
106 assert.strictEqual( mw.util.$content.length, 1, 'mw.util.$content must have length of 1' );
107 } );
108
109 /**
110 * Portlet names are prefixed with 'p-test' to avoid conflict with core
111 * when running the test suite under a wiki page.
112 * Previously, test elements where invisible to the selector since only
113 * one element can have a given id.
114 */
115 QUnit.test( 'addPortletLink', 13, function ( assert ) {
116 var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo,
117 addedAfter, tbRLDMnonexistentid, tbRLDMemptyjquery;
118
119 pTestTb = '\
120 <div class="portlet" id="p-test-tb">\
121 <h3>Toolbox</h3>\
122 <ul class="body"></ul>\
123 </div>';
124 pCustom = '\
125 <div class="portlet" id="p-test-custom">\
126 <h3>Views</h3>\
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 <h3>Views</h3>\
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
148 assert.ok( $.isDomElement( tbRL ), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
149
150 tbMW = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/',
151 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org', 'm', tbRL );
152 $tbMW = $( tbMW );
153
154 assert.propEqual(
155 $tbMW.getAttrs(),
156 {
157 id: 't-mworg'
158 },
159 'Validate attributes of created element'
160 );
161
162 assert.propEqual(
163 $tbMW.find( 'a' ).getAttrs(),
164 {
165 href: '//mediawiki.org/',
166 title: 'Go to MediaWiki.org [test-m]',
167 accesskey: 'm'
168 },
169 'Validate attributes of anchor tag in created element'
170 );
171
172 assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
173 assert.strictEqual( $tbMW.next()[0], tbRL, 'Link is in the correct position (by passing nextnode)' );
174
175 cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
176 $cuQuux = $( cuQuux );
177
178 assert.equal( $cuQuux.find( 'a' ).attr( 'title' ), 'Example [test-q]', 'Existing accesskey is stripped and updated' );
179
180 assert.equal(
181 $( '#p-test-custom #c-barmenu ul li' ).length,
182 1,
183 'addPortletLink did not add the item to all <ul> elements in the portlet (bug 35082)'
184 );
185
186 tbRLDM = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
187 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
188
189 assert.equal( $( tbRLDM ).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
190
191 caFoo = mw.util.addPortletLink( 'p-test-views', '#', 'Foo' );
192
193 assert.strictEqual( $tbMW.find( 'span' ).length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
194 assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
195
196 addedAfter = mw.util.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL ) );
197 assert.strictEqual( $( addedAfter ).next()[0], tbRL, 'Link is in the correct position (by passing a jQuery object as nextnode)' );
198
199 // test case - nonexistent id as next node
200 tbRLDMnonexistentid = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
201 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
202
203 assert.equal( tbRLDMnonexistentid, $( '#p-test-tb li:last' )[0], 'Nonexistent id as nextnode adds the portlet at end' );
204
205 // test case - empty jquery object as next node
206 tbRLDMemptyjquery = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
207 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
208
209 assert.equal( tbRLDMemptyjquery, $( '#p-test-tb li:last' )[0], 'Empty jquery as nextnode adds the portlet at end' );
210 } );
211
212 QUnit.test( 'jsMessage', 1, function ( assert ) {
213 this.suppressWarnings();
214 var a = mw.util.jsMessage( 'MediaWiki is <b>Awesome</b>.' );
215 this.restoreWarnings();
216 assert.ok( a, 'Basic checking of return value' );
217
218 // Clean up
219 $( '#mw-js-message' ).remove();
220 } );
221
222 QUnit.test( 'validateEmail', 6, function ( assert ) {
223 assert.strictEqual( mw.util.validateEmail( '' ), null, 'Should return null for empty string ' );
224 assert.strictEqual( mw.util.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
225
226 // testEmailWithCommasAreInvalids
227 assert.strictEqual( mw.util.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
228 assert.strictEqual( mw.util.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
229
230 // testEmailWithHyphens
231 assert.strictEqual( mw.util.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
232 assert.strictEqual( mw.util.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
233 } );
234
235 QUnit.test( 'isIPv6Address', 40, function ( assert ) {
236 // Shortcuts
237 function assertFalseIPv6( addy, summary ) {
238 return assert.strictEqual( mw.util.isIPv6Address( addy ), false, summary );
239 }
240
241 function assertTrueIPv6( addy, summary ) {
242 return assert.strictEqual( mw.util.isIPv6Address( addy ), true, summary );
243 }
244
245 // Based on IPTest.php > testisIPv6
246 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
247 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
248 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
249 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
250
251 $.each(
252 ['fc:100::',
253 'fc:100:a::',
254 'fc:100:a:d::',
255 'fc:100:a:d:1::',
256 'fc:100:a:d:1:e::',
257 'fc:100:a:d:1:e:ac::'], function ( i, addy ) {
258 assertTrueIPv6( addy, addy + ' is a valid IP' );
259 } );
260
261 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
262 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
263
264 assertFalseIPv6( ':::' );
265 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
266
267 assertTrueIPv6( '::', 'IPv6 zero address' );
268 $.each(
269 ['::0',
270 '::fc',
271 '::fc:100',
272 '::fc:100:a',
273 '::fc:100:a:d',
274 '::fc:100:a:d:1',
275 '::fc:100:a:d:1:e',
276 '::fc:100:a:d:1:e:ac',
277
278 'fc:100:a:d:1:e:ac:0'], function ( i, addy ) {
279 assertTrueIPv6( addy, addy + ' is a valid IP' );
280 } );
281
282 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
283 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
284
285 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
286 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
287 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
288
289 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
290 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
291 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
292 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
293 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
294 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
295 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
296 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
297 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
298
299 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
300 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
301 } );
302
303 QUnit.test( 'isIPv4Address', 11, function ( assert ) {
304 // Shortcuts
305 function assertFalseIPv4( addy, summary ) {
306 assert.strictEqual( mw.util.isIPv4Address( addy ), false, summary );
307 }
308
309 function assertTrueIPv4( addy, summary ) {
310 assert.strictEqual( mw.util.isIPv4Address( addy ), true, summary );
311 }
312
313 // Based on IPTest.php > testisIPv4
314 assertFalseIPv4( false, 'Boolean false is not an IP' );
315 assertFalseIPv4( true, 'Boolean true is not an IP' );
316 assertFalseIPv4( '', 'Empty string is not an IP' );
317 assertFalseIPv4( 'abc', '"abc" is not an IP' );
318 assertFalseIPv4( ':', 'Colon is not an IP' );
319 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
320 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
321 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
322
323 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
324 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
325 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );
326 } );
327 }( mediaWiki, jQuery ) );