moving tests to the mediawiki.util section + note about order
[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 'addedTests' : [],
16
17 /* Functions */
18
19 /**
20 * Adds a row to the test-table
21 *
22 * @param code String Code of the test to be executed
23 * @param result String Expected result in 'var (vartype)' form
24 * @param contain String Important part of the result,
25 * if result is different but does contain this it will not return ERROR but PARTIALLY
26 */
27 'addTest' : function( code, result, contain ) {
28 if ( !contain ) {
29 contain = result;
30 }
31 this.addedTests.push( [code, result, contain] );
32 this.$table.append( '<tr><td>' + mw.html.escape( code ).replace( / /g, '&nbsp;&nbsp;' )
33 + '</td><td>' + mw.html.escape( result ).replace( / /g, '&nbsp;&nbsp;' )
34 + '</td><td></td><td>?</td></tr>' );
35 return true;
36 },
37
38 /**
39 * Adds a heading to the test-table
40 *
41 * @param title String Title of the section
42 */
43 'addHead' : function( title ) {
44 if ( !title ) {
45 return false;
46 }
47 this.$table.append( '<tr><th colspan="4">' + mw.html.escape( title ).replace( / /g, '&nbsp;&nbsp;' ) + '</th></tr>' );
48 return true;
49 },
50
51 /* Initialisation */
52 'initialised' : false,
53 'init' : function() {
54 if ( this.initialised === false ) {
55 this.initialised = true;
56 // jQuery document ready
57 $( function() {
58 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Blankpage'
59 && mw.util.getParamValue( 'action' ) === 'mwutiltest' ) {
60
61 // Build page
62 document.title = 'mediaWiki JavaScript library test suite - ' + mw.config.get( 'wgSiteName' );
63 $( '#firstHeading' ).text( 'mediaWiki JavaScript library test suite' );
64 var skinLinksText = 'Test in: ';
65 skinLinks = [],
66 availableSkins = mw.config.get( 'wgAvailableSkins' ),
67 skincode = '';
68 for ( skincode in availableSkins ) {
69 skinLinks.push( mw.html.element( 'a', {
70 'href': mw.util.wikiGetlink( wgPageName ) + '?action=mwutiltest&debug=true&useskin=' + encodeURIComponent( skincode )
71 }, availableSkins[skincode] ) );
72 }
73 skinLinksText += skinLinks.join( ' | ' ) + '.';
74 mw.util.$content.html(
75 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki JavaScript library</p>'
76 + '<p>' + skinLinksText + '</p>'
77 + '<hr />'
78 + '<table id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break; font-family:monospace,\'Courier New\'">'
79 + '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>'
80 + '</table>'
81 );
82 mw.test.$table = $( 'table#mw-mwutiltest-table' );
83
84 /* Populate tests */
85 // Try to roughly keep the order similar to the order in the files
86 // or alphabetical (depending on the context)
87
88 // Main modules and their aliases
89 mw.test.addHead( 'Main modules and their aliases' );
90
91 mw.test.addTest( 'typeof mediaWiki',
92 'object (string)' );
93
94 mw.test.addTest( 'typeof mw',
95 'object (string)' );
96
97 mw.test.addTest( 'typeof jQuery',
98 'function (string)' );
99
100 mw.test.addTest( 'typeof $',
101 'function (string)' );
102
103 // Prototype functions added by MediaWiki
104 mw.test.addHead( 'Prototype functions added by MediaWiki' );
105
106 mw.test.addTest( 'typeof $.trimLeft',
107 'function (string)' );
108
109 mw.test.addTest( '$.trimLeft( " foo bar " )',
110 'foo bar (string)' );
111
112 mw.test.addTest( 'typeof $.trimRight',
113 'function (string)' );
114
115 mw.test.addTest( '$.trimRight( " foo bar " )',
116 ' foo bar (string)' );
117
118 mw.test.addTest( 'typeof $.ucFirst',
119 'function (string)' );
120
121 mw.test.addTest( '$.ucFirst( "mediawiki" )',
122 'Mediawiki (string)' );
123
124 mw.test.addTest( 'typeof $.escapeRE',
125 'function (string)' );
126
127 mw.test.addTest( '$.escapeRE( ".st{e}$st" )',
128 '\\.st\\{e\\}\\$st (string)' );
129
130 mw.test.addTest( 'typeof $.isEmpty',
131 'function (string)' );
132
133 mw.test.addTest( '$.isEmpty( "string" )',
134 'false (boolean)' );
135
136 mw.test.addTest( '$.isEmpty( "0" )',
137 'true (boolean)' );
138
139 mw.test.addTest( '$.isEmpty([])',
140 'true (boolean)' );
141
142 mw.test.addTest( 'typeof $.compareArray',
143 'function (string)' );
144
145 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
146 'true (boolean)' );
147
148 mw.test.addTest( '$.compareArray( [1], [2] )',
149 'false (boolean)' );
150
151 mw.test.addTest( 'typeof $.compareObject',
152 'function (string)' );
153
154 // mediawiki.js
155 mw.test.addHead( 'mediawiki.js' );
156
157 mw.test.addTest( 'mw.config instanceof mw.Map',
158 'true (boolean)' );
159
160 mw.test.addTest( 'mw.config.exists()',
161 'true (boolean)' );
162
163 mw.test.addTest( 'mw.config.exists( "wgSomeName" )',
164 'false (boolean)' );
165
166 mw.test.addTest( 'mw.config.exists( ["wgCanonicalNamespace", "wgTitle"] )',
167 'true (boolean)' );
168
169 mw.test.addTest( 'mw.config.exists( ["wgSomeName", "wgTitle"] )',
170 'false (boolean)' );
171
172 mw.test.addTest( 'mw.config.get( "wgTitle" )',
173 'BlankPage (string)' );
174
175 mw.test.addTest( 'var a = mw.config.get( ["wgTitle"] ); a.wgTitle',
176 'BlankPage (string)' );
177
178 mw.test.addTest( 'typeof mw.html',
179 'object (string)' );
180
181 mw.test.addTest( 'mw.html.escape( \'<mw awesome="awesome">\' )',
182 '&lt;mw awesome=&quot;awesome&quot;&gt; (string)' );
183
184 mw.test.addTest( 'mw.html.element( "hr" )',
185 '<hr/> (string)' );
186
187 mw.test.addTest( 'mw.html.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
188 '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)' );
189
190 mw.test.addTest( 'typeof mw.loader',
191 'object (string)' );
192
193 mw.test.addTest( 'typeof mw.loader.using',
194 'function (string)' );
195
196 mw.test.addTest( 'typeof mw.Map',
197 'function (string)' );
198
199 mw.test.addTest( 'typeof mw.user',
200 'object (string)' );
201
202 mw.test.addTest( 'typeof mw.user.anonymous()',
203 'boolean (string)' );
204
205 // mediawiki.util.js
206 mw.test.addHead( 'mediawiki.util.js' );
207
208 mw.test.addTest( 'typeof mw.util',
209 'object (string)' );
210
211 mw.test.addTest( 'typeof mw.util.rawurlencode',
212 'function (string)' );
213
214 mw.test.addTest( 'mw.util.rawurlencode( "Test:A & B/Here" )',
215 'Test%3AA%20%26%20B%2FHere (string)' );
216
217 mw.test.addTest( 'typeof mw.util.wikiUrlencode',
218 'function (string)' );
219
220 mw.test.addTest( 'mw.util.wikiUrlencode( "Test:A & B/Here" )',
221 'Test:A_%26_B/Here (string)' );
222
223 mw.test.addTest( 'typeof mw.util.addCSS',
224 'function (string)' );
225
226 mw.test.addTest( 'var a = mw.util.addCSS( ".plainlinks { color:green; }" ); a.disabled;',
227 'false (boolean)',
228 '(boolean)' );
229
230 mw.test.addTest( 'typeof mw.util.wikiGetlink',
231 'function (string)' );
232
233 mw.test.addTest( 'typeof mw.util.getParamValue',
234 'function (string)' );
235
236 mw.test.addTest( 'mw.util.getParamValue( "action" )',
237 'mwutiltest (string)' );
238
239 mw.test.addTest( 'mw.util.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
240 'right (string)' );
241
242 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
243 'RegExp (string)' );
244
245 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
246 'function (string)' );
247
248 mw.test.addTest( 'mw.util.$content instanceof jQuery',
249 'true (boolean)' );
250
251 mw.test.addTest( 'mw.util.$content.size()',
252 '1 (number)' );
253
254 mw.test.addTest( 'typeof mw.util.addPortletLink',
255 'function (string)' );
256
257 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" )',
258 'object (string)' );
259
260 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();',
261 'MediaWiki.org (string)' );
262
263 mw.test.addTest( 'typeof mw.util.jsMessage',
264 'function (string)' );
265
266 mw.test.addTest( 'mw.util.jsMessage( mw.config.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
267 'true (boolean)' );
268
269 // TODO: Import tests from PHPUnit test suite for user::isValidEmailAddr
270 mw.test.addTest( 'mw.util.validateEmail( "" )',
271 'null (object)' );
272
273 mw.test.addTest( 'mw.util.validateEmail( "user@localhost" )',
274 'true (boolean)' );
275
276 // jQuery plugins
277 mw.test.addHead( 'jQuery plugins' );
278
279 mw.test.addTest( 'typeof $.client',
280 'object (string)' );
281
282 mw.test.addTest( 'typeof $.client.profile',
283 'function (string)' );
284
285 mw.test.addTest( 'var a = $.client.profile(); typeof a.name',
286 'string (string)' );
287
288 mw.test.addTest( 'typeof $.fn.makeCollapsible',
289 'function (string)' );
290
291 // Run tests and compare results
292 var exec,
293 result,
294 resulttype,
295 numberoftests = 0,
296 numberofpasseds = 0,
297 numberofpartials = 0,
298 numberoferrors = 0,
299 $testrows = mw.test.$table.find( 'tr:has(td)' );
300
301 $.each( mw.test.addedTests, function( i ) {
302 numberoftests++;
303
304 exec = mw.test.addedTests[i][0];
305 shouldreturn = mw.test.addedTests[i][1];
306 shouldcontain = mw.test.addedTests[i][2];
307 doesreturn = eval( exec );
308 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
309 $thisrow = $testrows.eq( i );
310 $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesreturn ).replace(/ /g, '&nbsp;&nbsp;' ) );
311
312 if ( doesreturn.indexOf( shouldcontain ) !== -1 ) {
313 if ( doesreturn == shouldreturn ) {
314 $thisrow.find( '> td' ).eq(3).css( 'background', '#EFE' ).text( 'OK' );
315 numberofpasseds++;
316 } else {
317 $thisrow.find( '> td' ).eq(3).css( 'background', '#FFE' ).html( '<small>PARTIALLY</small>' );
318 numberofpartials++;
319 }
320 } else {
321 $thisrow.find( '> td' ).eq(3).css( 'background', '#FEE' ).text( 'ERROR' );
322 numberoferrors++;
323 }
324
325 } );
326 mw.test.$table.before( '<p><strong>Ran ' + numberoftests + ' tests. ' +
327 numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' +
328 numberofpartials + ' partially passed test(s). </p>' );
329
330 }
331 } );
332 }
333 }
334 };
335
336 mediaWiki.test.init();
337
338 } )(jQuery, mediaWiki);