e70842cf10f3e2362a0a9cb825ec86fcba65e866
[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 mediaWiki.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 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 '.mw-mwutiltest-head:hover { cursor: pointer; } ' // Header-clicks hide/show the below rows
94 );
95
96 mw.test.$table = $( 'table#mw-mwutiltest-table' );
97
98 /* Populate tests */
99 // Try to roughly keep the order similar to the order in the files
100 // or alphabetical (depending on the context)
101
102 // Main modules and their aliases
103 mw.test.addHead( 'Main modules and their aliases' );
104
105 mw.test.addTest( 'typeof mediaWiki',
106 'object (string)' );
107
108 mw.test.addTest( 'typeof mw',
109 'object (string)' );
110
111 mw.test.addTest( 'typeof jQuery',
112 'function (string)' );
113
114 mw.test.addTest( 'typeof $',
115 'function (string)' );
116
117 // Prototype functions added by MediaWiki
118 mw.test.addHead( 'Prototype functions added by MediaWiki' );
119
120 mw.test.addTest( 'typeof $.trimLeft',
121 'function (string)' );
122
123 mw.test.addTest( '$.trimLeft( " foo bar " )',
124 'foo bar (string)' );
125
126 mw.test.addTest( 'typeof $.trimRight',
127 'function (string)' );
128
129 mw.test.addTest( '$.trimRight( " foo bar " )',
130 ' foo bar (string)' );
131
132 mw.test.addTest( 'typeof $.ucFirst',
133 'function (string)' );
134
135 mw.test.addTest( '$.ucFirst( "mediawiki" )',
136 'Mediawiki (string)' );
137
138 mw.test.addTest( 'typeof $.escapeRE',
139 'function (string)' );
140
141 mw.test.addTest( '$.escapeRE( ".st{e}$st" )',
142 '\\.st\\{e\\}\\$st (string)' );
143
144 mw.test.addTest( 'typeof $.isEmpty',
145 'function (string)' );
146
147 mw.test.addTest( '$.isEmpty( "string" )',
148 'false (boolean)' );
149
150 mw.test.addTest( '$.isEmpty( "0" )',
151 'true (boolean)' );
152
153 mw.test.addTest( '$.isEmpty([])',
154 'true (boolean)' );
155
156 mw.test.addTest( 'typeof $.compareArray',
157 'function (string)' );
158
159 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
160 'true (boolean)' );
161
162 mw.test.addTest( '$.compareArray( [1], [2] )',
163 'false (boolean)' );
164
165 mw.test.addTest( 'typeof $.compareObject',
166 'function (string)' );
167
168 // mediawiki.js
169 mw.test.addHead( 'mediawiki.js' );
170
171 mw.test.addTest( 'mw.config instanceof mw.Map',
172 'true (boolean)' );
173
174 mw.test.addTest( 'mw.config.exists()',
175 'true (boolean)' );
176
177 mw.test.addTest( 'mw.config.exists( "wgSomeName" )',
178 'false (boolean)' );
179
180 mw.test.addTest( 'mw.config.exists( ["wgCanonicalNamespace", "wgTitle"] )',
181 'true (boolean)' );
182
183 mw.test.addTest( 'mw.config.exists( ["wgSomeName", "wgTitle"] )',
184 'false (boolean)' );
185
186 mw.test.addTest( 'mw.config.get( "wgTitle" )',
187 'BlankPage (string)' );
188
189 mw.test.addTest( 'var a = mw.config.get( ["wgTitle"] ); a.wgTitle',
190 'BlankPage (string)' );
191
192 mw.test.addTest( 'typeof mw.html',
193 'object (string)' );
194
195 mw.test.addTest( 'mw.html.escape( \'<mw awesome="awesome">\' )',
196 '&lt;mw awesome=&quot;awesome&quot;&gt; (string)' );
197
198 mw.test.addTest( 'mw.html.element( "hr" )',
199 '<hr/> (string)' );
200
201 mw.test.addTest( 'mw.html.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
202 '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)' );
203
204 mw.test.addTest( 'typeof mw.loader',
205 'object (string)' );
206
207 mw.test.addTest( 'typeof mw.loader.using',
208 'function (string)' );
209
210 mw.test.addTest( 'typeof mw.Map',
211 'function (string)' );
212
213 mw.test.addTest( 'typeof mw.user',
214 'object (string)' );
215
216 mw.test.addTest( 'typeof mw.user.anonymous()',
217 'boolean (string)' );
218
219 // mediawiki.util.js
220 mw.test.addHead( 'mediawiki.util.js' );
221
222 mw.test.addTest( 'typeof mw.util',
223 'object (string)' );
224
225 mw.test.addTest( 'typeof mw.util.rawurlencode',
226 'function (string)' );
227
228 mw.test.addTest( 'mw.util.rawurlencode( "Test:A & B/Here" )',
229 'Test%3AA%20%26%20B%2FHere (string)' );
230
231 mw.test.addTest( 'typeof mw.util.wikiUrlencode',
232 'function (string)' );
233
234 mw.test.addTest( 'mw.util.wikiUrlencode( "Test:A & B/Here" )',
235 'Test:A_%26_B/Here (string)' );
236
237 mw.test.addTest( 'typeof mw.util.addCSS',
238 'function (string)' );
239
240 mw.test.addTest( 'var a = mw.util.addCSS( ".plainlinks { color:green; }" ); a.disabled;',
241 'false (boolean)',
242 '(boolean)' );
243
244 mw.test.addTest( 'typeof mw.util.wikiGetlink',
245 'function (string)' );
246
247 mw.test.addTest( 'typeof mw.util.getParamValue',
248 'function (string)' );
249
250 mw.test.addTest( 'mw.util.getParamValue( "action" )',
251 'mwutiltest (string)' );
252
253 mw.test.addTest( 'mw.util.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
254 'right (string)' );
255
256 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
257 'RegExp (string)' );
258
259 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
260 'function (string)' );
261
262 mw.test.addTest( 'mw.util.$content instanceof jQuery',
263 'true (boolean)' );
264
265 mw.test.addTest( 'mw.util.$content.size()',
266 '1 (number)' );
267
268 mw.test.addTest( 'typeof mw.util.addPortletLink',
269 'function (string)' );
270
271 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" )',
272 'object (string)' );
273
274 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();',
275 'MediaWiki.org (string)' );
276
277 mw.test.addTest( 'typeof mw.util.jsMessage',
278 'function (string)' );
279
280 mw.test.addTest( 'mw.util.jsMessage( mw.config.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
281 'true (boolean)' );
282
283 // TODO: Import tests from PHPUnit test suite for user::isValidEmailAddr
284 mw.test.addTest( 'mw.util.validateEmail( "" )',
285 'null (object)' );
286
287 mw.test.addTest( 'mw.util.validateEmail( "user@localhost" )',
288 'true (boolean)' );
289
290 // testEmailWithCommasAreInvalids
291 mw.test.addTest( 'mw.util.validateEmail( "user,foo@example.org" )',
292 'false (boolean)' );
293 mw.test.addTest( 'mw.util.validateEmail( "userfoo@ex,ample.org" )',
294 'false (boolean)' );
295 // testEmailWithHyphens
296 mw.test.addTest( 'mw.util.validateEmail( "user-foo@example.org" )',
297 'true (boolean)' );
298 mw.test.addTest( 'mw.util.validateEmail( "userfoo@ex-ample.org" )',
299 'true (boolean)' );
300
301 // jQuery plugins
302 mw.test.addHead( 'jQuery plugins' );
303
304 mw.test.addTest( 'typeof $.client',
305 'object (string)' );
306
307 mw.test.addTest( 'typeof $.client.profile',
308 'function (string)' );
309
310 mw.test.addTest( 'var a = $.client.profile(); typeof a.name',
311 'string (string)' );
312
313 mw.test.addTest( 'typeof $.fn.makeCollapsible',
314 'function (string)' );
315
316
317 // End of tests.
318 mw.test.addHead( '*** End of tests ***' );
319
320 // Run tests and compare results
321 var exec,
322 result,
323 resulttype,
324 numberOfTests = 0,
325 numberOfPasseds = 0,
326 numberOfPartials = 0,
327 numberOfErrors = 0,
328 headNumberOfTests = 0,
329 headNumberOfPasseds = 0,
330 headNumberOfPartials = 0,
331 headNumberOfErrors = 0,
332 numberOfHeaders = 0,
333 previousHeadTitle = '',
334 $testrows = mw.test.$table.find( 'tr:has(td)' );
335
336 $.each( mw.test.addedTests, function( i, item ) {
337
338 // New header
339 if( item[0] == 'HEADER' ) {
340
341 // update current header with its tests results
342 mw.test.$table.find( 'tr#mw-mwutiltest-head' + numberOfHeaders +' > th' )
343 .html( previousHeadTitle + ' <span style="float:right">('
344 + 'T: ' + headNumberOfTests
345 + ' ok: ' + headNumberOfPasseds
346 + ' partial: ' + headNumberOfPartials
347 + ' err: ' + headNumberOfErrors
348 + ')</span>' );
349
350 numberOfHeaders++;
351 // Reset values for the new header;
352 headNumberOfTests = 0;
353 headNumberOfPasseds = 0;
354 headNumberOfPartials = 0;
355 headNumberOfErrors = 0;
356
357 previousHeadTitle = item[1];
358
359 return true;
360 }
361
362 exec = item[0];
363 shouldreturn = item[1];
364 shouldcontain = item[2];
365
366 numberOfTests++;
367 headNumberOfTests++;
368 doesReturn = eval( exec );
369 doesReturn = doesReturn + ' (' + typeof doesReturn + ')';
370 $thisrow = $testrows.eq( i - numberOfHeaders ); // since headers are rows as well
371 $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesReturn ).replace(/ /g, '&nbsp;&nbsp;' ) );
372
373 if ( doesReturn.indexOf( shouldcontain ) !== -1 ) {
374 if ( doesReturn == shouldreturn ) {
375 $thisrow.find( '> td' ).eq(3).css( 'background', '#AFA' ).text( 'OK' );
376 numberOfPasseds++;
377 headNumberOfPasseds++;
378 } else {
379 $thisrow.find( '> td' ).eq(3).css( 'background', '#FFA' ).html( '<small>PARTIALLY</small>' );
380 numberOfPartials++;
381 headNumberOfPartials++;
382 }
383 } else {
384 $thisrow.find( '> td' ).eq(3).css( 'background', '#FAA' ).text( 'ERROR' );
385 numberOfErrors++;
386 headNumberOfErrors++;
387 }
388
389 } );
390 mw.test.$table.before( '<p><strong>Ran ' + numberOfTests + ' tests. ' +
391 numberOfPasseds + ' passed test(s). ' + numberOfErrors + ' error(s). ' +
392 numberOfPartials + ' partially passed test(s). </p>' );
393
394 // hide all tests. TODO hide only OK?
395 mw.test.$table.find( '.mw-mwutiltest-test' ).hide();
396 // clickable header to show/hide the tests
397 mw.test.$table.find( '.mw-mwutiltest-head' ).click(function() {
398 $(this).nextUntil( '.mw-mwutiltest-head' ).toggle();
399 });
400 }
401 } );
402 }
403 }
404 };
405
406 mediaWiki.test.init();
407
408 } )(jQuery, mediaWiki);