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