be06a632fc08dbc83a12a1222349512e23e5da87
[lhc/web/wiklou.git] / resources / mediawiki.util / mediawiki.util.test.js
1 /**
2 * mediaWiki JavaScript library test suite
3 *
4 * Available on Special:BlankPage?action=mwutiltest&debug=true
5 *
6 * @author Krinkle <krinklemail@gmail.com>
7 */
8
9 ( function( $, mw ) {
10
11 mw.test = {
12
13 /* Variables */
14 '$table' : null,
15 // contains either a header or a test
16 // test: [ code, result, contain ] see addTest
17 // header: [ 'HEADER', escapedtitle, id ] see addHead
18 'addedTests' : [],
19 'headResults' : [],
20 'numberOfHeader' : 0,
21
22 /* Functions */
23
24 /**
25 * Adds a row to the test-table
26 *
27 * @param code String Code of the test to be executed
28 * @param result String Expected result in 'var (vartype)' form
29 * @param contain String Important part of the result,
30 * if result is different but does contain this it will not return ERROR but PARTIALLY
31 */
32 'addTest' : function( code, result, contain ) {
33 if ( !contain ) {
34 contain = result;
35 }
36 this.addedTests.push( [code, result, contain] );
37 this.$table.append( '<tr class="mw-mwutiltest-test">'
38 + '<td>' + mw.html.escape( code ).replace( / /g, '&nbsp;&nbsp;' )
39 + '</td><td>' + mw.html.escape( result ).replace( / /g, '&nbsp;&nbsp;' )
40 + '</td><td></td><td>?</td></tr>' );
41 return true;
42 },
43
44 /**
45 * Adds a heading to the test-table
46 *
47 * @param title String Title of the section
48 */
49 'addHead' : function( title ) {
50 if ( !title ) {
51 return false;
52 }
53 var escapedtitle = mw.html.escape( title ).replace( / /g, '&nbsp;&nbsp;' );
54 this.addedTests.push( [ 'HEADER', escapedtitle, mw.test.numberOfHeader++ ] );
55 this.$table.append( '<tr class="mw-mwutiltest-head" id="mw-mwutiltest-head'+mw.test.numberOfHeader+'"><th colspan="4">' + escapedtitle + '</th></tr>' );
56 return true;
57 },
58
59 /* Initialisation */
60 'initialised' : false,
61 'init' : function() {
62 if ( this.initialised === false ) {
63 this.initialised = true;
64 // jQuery document ready
65 $( function() {
66 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Blankpage'
67 && mw.util.getParamValue( 'action' ) === 'mwutiltest' ) {
68
69 // Build page
70 document.title = 'mediaWiki JavaScript library test suite - ' + mw.config.get( 'wgSiteName' );
71 $( '#firstHeading' ).text( 'mediaWiki JavaScript library test suite' );
72 var skinLinksText = 'Test in: ',
73 skinLinks = [],
74 availableSkins = mw.config.get( 'wgAvailableSkins' ),
75 skincode = '';
76 for ( skincode in availableSkins ) {
77 skinLinks.push( mw.html.element( 'a', {
78 'href': mw.util.wikiGetlink( wgPageName ) + '?action=mwutiltest&debug=true&useskin=' + encodeURIComponent( skincode )
79 }, availableSkins[skincode] ) );
80 }
81 skinLinksText += skinLinks.join( ' | ' ) + '.';
82 mw.util.$content.html(
83 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki JavaScript library</p>'
84 + '<p>' + skinLinksText + '</p>'
85 + '<hr />'
86 + '<table id="mw-mwutiltest-table" class="wikitable" style="white-space:break; font-family:monospace,\'Courier New\'; width:100%;">'
87 + '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>'
88 + '</table>'
89 );
90
91 mw.util.addCSS(
92 '#mw-mwutiltest-table tr td { padding:0 !important; }' // Override wikitable padding for <td>
93 );
94
95 mw.test.$table = $( 'table#mw-mwutiltest-table' );
96
97 /* Populate tests */
98 // Try to roughly keep the order similar to the order in the files
99 // or alphabetical (depending on the context)
100
101 /** Main modules and their aliases **/
102 mw.test.addHead( 'Main modules and their aliases' );
103
104 mw.test.addTest( 'typeof mediaWiki',
105 'object (string)' );
106
107 mw.test.addTest( 'typeof mw',
108 'object (string)' );
109
110 mw.test.addTest( 'typeof jQuery',
111 'function (string)' );
112
113 mw.test.addTest( 'typeof $',
114 'function (string)' );
115
116 /** Prototype functions added by MediaWiki **/
117 mw.test.addHead( 'Prototype functions added by MediaWiki' );
118
119 mw.test.addTest( 'typeof $.trimLeft',
120 'function (string)' );
121
122 mw.test.addTest( '$.trimLeft( " foo bar " )',
123 'foo bar (string)' );
124
125 mw.test.addTest( 'typeof $.trimRight',
126 'function (string)' );
127
128 mw.test.addTest( '$.trimRight( " foo bar " )',
129 ' foo bar (string)' );
130
131 mw.test.addTest( 'typeof $.ucFirst',
132 'function (string)' );
133
134 mw.test.addTest( '$.ucFirst( "mediawiki" )',
135 'Mediawiki (string)' );
136
137 mw.test.addTest( 'typeof $.escapeRE',
138 'function (string)' );
139
140 mw.test.addTest( '$.escapeRE( "<!-- ([{+mW+}]) $^|?>" )',
141 '<!\\-\\- \\(\\[\\{\\+mW\\+\\}\\]\\) \\$\\^\\|\\?> (string)' ); // double escaped
142
143 mw.test.addTest( '$.escapeRE( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" )',
144 'ABCDEFGHIJKLMNOPQRSTUVWXYZ (string)' );
145
146 mw.test.addTest( '$.escapeRE( "abcdefghijklmnopqrstuvwxyz" )',
147 'abcdefghijklmnopqrstuvwxyz (string)' );
148
149 mw.test.addTest( '$.escapeRE( "0123456789" )',
150 '0123456789 (string)' );
151
152 mw.test.addTest( '$.isDomElement( document.getElementById("mw-mwutiltest-table") )',
153 'true (boolean)' );
154
155 mw.test.addTest( '$.isDomElement( document.getElementById("not-existant-id") )',
156 'false (boolean)' ); // returns null
157
158 mw.test.addTest( '$.isDomElement( document.getElementsByClassName("wikitable") )',
159 'false (boolean)' ); // returns an array
160
161 mw.test.addTest( '$.isDomElement( document.getElementsByClassName("wikitable")[0] )',
162 'true (boolean)' );
163
164 mw.test.addTest( '$.isDomElement( jQuery( "#mw-mwutiltest-table" ) )',
165 'false (boolean)' ); // returns jQuery object
166
167 mw.test.addTest( '$.isDomElement( jQuery( "#mw-mwutiltest-table" ).get(0) )',
168 'true (boolean)' );
169
170 mw.test.addTest( '$.isDomElement( document.createElement( "div" ) )',
171 'true (boolean)' );
172
173 mw.test.addTest( '$.isDomElement( {some: "thing" } )',
174 'false (boolean)' );
175
176 mw.test.addTest( 'typeof $.isEmpty',
177 'function (string)' );
178
179 mw.test.addTest( '$.isEmpty( "string" )',
180 'false (boolean)' );
181
182 mw.test.addTest( '$.isEmpty( "0" )',
183 'true (boolean)' );
184
185 mw.test.addTest( '$.isEmpty([])',
186 'true (boolean)' );
187
188 mw.test.addTest( 'typeof $.compareArray',
189 'function (string)' );
190
191 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
192 'true (boolean)' );
193
194 mw.test.addTest( '$.compareArray( [1], [2] )',
195 'false (boolean)' );
196
197 mw.test.addTest( 'typeof $.compareObject',
198 'function (string)' );
199
200 /** mediawiki.js **/
201 mw.test.addHead( 'mediawiki.js' );
202
203 mw.test.addTest( 'mw.config instanceof mw.Map',
204 'true (boolean)' );
205
206 mw.test.addTest( 'mw.config.exists()',
207 'true (boolean)' );
208
209 mw.test.addTest( 'mw.config.exists( "wgSomeName" )',
210 'false (boolean)' );
211
212 mw.test.addTest( 'mw.config.exists( ["wgCanonicalNamespace", "wgTitle"] )',
213 'true (boolean)' );
214
215 mw.test.addTest( 'mw.config.exists( ["wgSomeName", "wgTitle"] )',
216 'false (boolean)' );
217
218 mw.test.addTest( 'mw.config.get( "wgCanonicalNamespace" )',
219 'Special (string)' );
220
221 mw.test.addTest( 'var a = mw.config.get( ["wgCanonicalNamespace"] ); a.wgCanonicalNamespace',
222 'Special (string)' );
223
224 mw.test.addTest( 'typeof mw.html',
225 'object (string)' );
226
227 mw.test.addTest( 'mw.html.escape( \'<mw awesome="awesome">\' )',
228 '&lt;mw awesome=&quot;awesome&quot;&gt; (string)' );
229
230 mw.test.addTest( 'mw.html.element( "hr" )',
231 '<hr/> (string)' );
232
233 mw.test.addTest( 'mw.html.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
234 '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)' );
235
236 mw.test.addTest( 'typeof mw.loader',
237 'object (string)' );
238
239 mw.test.addTest( 'typeof mw.loader.using',
240 'function (string)' );
241
242 mw.test.addTest( 'typeof mw.Map',
243 'function (string)' );
244
245 mw.test.addTest( 'typeof mw.user',
246 'object (string)' );
247
248 mw.test.addTest( 'typeof mw.user.anonymous()',
249 'boolean (string)' );
250
251 /** mediawiki.util.js **/
252 mw.test.addHead( 'mediawiki.util.js' );
253
254 mw.test.addTest( 'typeof mw.util',
255 'object (string)' );
256
257 mw.test.addTest( 'typeof mw.util.rawurlencode',
258 'function (string)' );
259
260 mw.test.addTest( 'mw.util.rawurlencode( "Test:A & B/Here" )',
261 'Test%3AA%20%26%20B%2FHere (string)' );
262
263 mw.test.addTest( 'typeof mw.util.wikiUrlencode',
264 'function (string)' );
265
266 mw.test.addTest( 'mw.util.wikiUrlencode( "Test:A & B/Here" )',
267 'Test:A_%26_B/Here (string)' );
268
269 mw.test.addTest( 'typeof mw.util.addCSS',
270 'function (string)' );
271
272 mw.test.addTest( 'var a = mw.util.addCSS( "#mw-js-message { background-color: #AFA !important; }" ); a.disabled;',
273 'false (boolean)',
274 '(boolean)' );
275
276 mw.test.addTest( 'typeof mw.util.toggleToc',
277 'function (string)' );
278
279 mw.test.addTest( 'typeof mw.util.wikiGetlink',
280 'function (string)' );
281
282 mw.test.addTest( 'typeof mw.util.getParamValue',
283 'function (string)' );
284
285 mw.test.addTest( 'mw.util.getParamValue( "action" )',
286 'mwutiltest (string)' );
287
288 mw.test.addTest( 'mw.util.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
289 'right (string)' );
290
291 mw.test.addTest( 'typeof mw.util.tooltipAccessKeyPrefix',
292 'string (string)' );
293
294 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
295 'RegExp (string)' );
296
297 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
298 'function (string)' );
299
300 mw.test.addTest( 'mw.util.$content instanceof jQuery',
301 'true (boolean)' );
302
303 mw.test.addTest( 'mw.util.$content.size()',
304 '1 (number)' );
305
306 mw.test.addTest( 'mw.util.isMainPage()',
307 'false (boolean)' );
308
309 mw.test.addTest( 'typeof mw.util.addPortletLink',
310 'function (string)' );
311
312 mw.test.addTest( 'typeof mw.util.addPortletLink( "p-tb", "http://mediawiki.org/wiki/ResourceLoader", "ResourceLoader", "t-rl", "More info about ResourceLoader on MediaWiki.org ", "l", "#t-specialpages" )',
313 'object (string)' );
314
315 mw.test.addTest( 'var a = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-rl" ); $(a).text();',
316 'MediaWiki.org (string)' );
317
318 mw.test.addTest( 'typeof mw.util.jsMessage',
319 'function (string)' );
320
321 mw.test.addTest( 'mw.util.jsMessage( mw.config.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
322 'true (boolean)' );
323
324 // TODO: Import tests from PHPUnit test suite for user::isValidEmailAddr
325 mw.test.addTest( 'mw.util.validateEmail( "" )',
326 'null (object)' );
327
328 mw.test.addTest( 'mw.util.validateEmail( "user@localhost" )',
329 'true (boolean)' );
330
331 // testEmailWithCommasAreInvalids
332 mw.test.addTest( 'mw.util.validateEmail( "user,foo@example.org" )',
333 'false (boolean)' );
334 mw.test.addTest( 'mw.util.validateEmail( "userfoo@ex,ample.org" )',
335 'false (boolean)' );
336 // testEmailWithHyphens
337 mw.test.addTest( 'mw.util.validateEmail( "user-foo@example.org" )',
338 'true (boolean)' );
339 mw.test.addTest( 'mw.util.validateEmail( "userfoo@ex-ample.org" )',
340 'true (boolean)' );
341
342 // From IPTest.php IPv6
343 mw.test.addTest( 'mw.util.isIPv6Address( "" )',
344 'false (boolean)' );
345 mw.test.addTest( 'mw.util.isIPv6Address( ":fc:100::" )',
346 'false (boolean)' );
347 mw.test.addTest( 'mw.util.isIPv6Address( "fc:100::" )',
348 'true (boolean)' );
349 mw.test.addTest( 'mw.util.isIPv6Address( "fc:100:a:d:1:e:ac::" )',
350 'true (boolean)' );
351 mw.test.addTest( 'mw.util.isIPv6Address( ":::" )',
352 'false (boolean)' );
353 mw.test.addTest( 'mw.util.isIPv6Address( "::0:" )',
354 'false (boolean)' );
355
356 // From IPTest.php IPv4
357 mw.test.addTest( 'mw.util.isIPv4Address( "" )',
358 'false (boolean)' );
359 mw.test.addTest( 'mw.util.isIPv4Address( "...." )',
360 'false (boolean)' );
361 mw.test.addTest( 'mw.util.isIPv4Address( "abc" )',
362 'false (boolean)' );
363 mw.test.addTest( 'mw.util.isIPv4Address( "124.24.52" )',
364 'false (boolean)' );
365 mw.test.addTest( 'mw.util.isIPv4Address( ".24.52.13" )',
366 'false (boolean)' );
367 mw.test.addTest( 'mw.util.isIPv4Address( "124.24.52.13" )',
368 'true (boolean)' );
369 mw.test.addTest( 'mw.util.isIPv4Address( "1.24.52.13" )',
370 'true (boolean)' );
371 mw.test.addTest( 'mw.util.isIPv4Address( "74.24.52.13/20" )', // Range
372 'false (boolean)' );
373 // @FIXME: The regex that's been in MW JS has never supported ranges but it should
374 // The regex is expected to return false for that reason
375
376 // jQuery plugins
377 mw.test.addHead( 'jQuery plugins' );
378
379 mw.test.addTest( 'typeof $.client',
380 'object (string)' );
381
382 mw.test.addTest( 'typeof $.client.profile',
383 'function (string)' );
384
385 mw.test.addTest( 'var a = $.client.profile(); typeof a.name',
386 'string (string)' );
387
388 mw.test.addTest( 'typeof $.fn.makeCollapsible',
389 'function (string)' );
390
391
392 // End of tests.
393 mw.test.addHead( '*** End of tests ***' );
394
395 // Run tests and compare results
396 var exec,
397 result,
398 resulttype,
399 numberOfTests = 0,
400 numberOfPasseds = 0,
401 numberOfPartials = 0,
402 numberOfErrors = 0,
403 headNumberOfTests = 0,
404 headNumberOfPasseds = 0,
405 headNumberOfPartials = 0,
406 headNumberOfErrors = 0,
407 numberOfHeaders = 0,
408 previousHeadTitle = '',
409 $testrows = mw.test.$table.find( 'tr:has(td)' );
410
411 $.each( mw.test.addedTests, function( i, item ) {
412
413 // New header
414 if( item[0] == 'HEADER' ) {
415
416 // update current header with its tests results
417 mw.test.$table.find( 'tr#mw-mwutiltest-head' + numberOfHeaders +' > th' )
418 .html( previousHeadTitle + ' <span style="float:right">('
419 + 'Tests: ' + headNumberOfTests
420 + ' OK: ' + headNumberOfPasseds
421 + ' Partial: ' + headNumberOfPartials
422 + ' Error: ' + headNumberOfErrors
423 + ')</span>' );
424
425 numberOfHeaders++;
426 // Reset values for the new header;
427 headNumberOfTests = 0;
428 headNumberOfPasseds = 0;
429 headNumberOfPartials = 0;
430 headNumberOfErrors = 0;
431
432 previousHeadTitle = item[1];
433
434 return true;
435 }
436
437 exec = item[0];
438 var shouldreturn = item[1];
439 var shouldcontain = item[2];
440
441 numberOfTests++;
442 headNumberOfTests++;
443 try {
444 var doesReturn = eval( exec );
445 } catch (e){
446 mw.log ('mw.util.test> ' + e );
447 }
448 doesReturn = doesReturn + ' (' + typeof doesReturn + ')';
449 var $thisrow = $testrows.eq( i - numberOfHeaders ); // since headers are rows as well
450 $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesReturn ).replace(/ /g, '&nbsp;&nbsp;' ) );
451
452 if ( doesReturn.indexOf( shouldcontain ) !== -1 ) {
453 if ( doesReturn == shouldreturn ) {
454 $thisrow.find( '> td' ).eq(3).css( 'background', '#AFA' ).text( 'OK' );
455 numberOfPasseds++;
456 headNumberOfPasseds++;
457 } else {
458 $thisrow.find( '> td' ).eq(3).css( 'background', '#FFA' ).html( '<small>PARTIALLY</small>' );
459 numberOfPartials++;
460 headNumberOfPartials++;
461 }
462 } else {
463 $thisrow.css( 'background', '#FAA' ).find( '> td' ).eq(3).text( 'ERROR' );
464 numberOfErrors++;
465 headNumberOfErrors++;
466 }
467
468 } );
469 mw.test.$table.before( '<p><strong>Ran ' + numberOfTests + ' tests. ' +
470 numberOfPasseds + ' passed test(s). ' + numberOfErrors + ' error(s). ' +
471 numberOfPartials + ' partially passed test(s). </p>' );
472
473 }
474 } );
475 }
476 }
477 };
478
479 mw.test.init();
480
481 } )(jQuery, mediaWiki);