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