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