adding links to other skins in mediaWiki JS test suite. (Previously making sure every...
[mediawiki.git] / resources / mediawiki.util / mediawiki.util.test.js
blobef47716a972bbf7e63ac6e58b3c7842b4944f25b
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  */
9 ( function( $, mw ) {
11         mediaWiki.test = {
13                 /* Variables */
14                 '$table' : null,
15                 'addedTests' : [],
17                 /* Functions */
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                 },
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                 },
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' ) {
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                                                 for ( skin in availableSkins ) {
68                                                         skinLinks.push( mw.html.element( 'a', {
69                                                                 'href': mw.util.wikiGetlink( wgPageName ) + '?action=mwutiltest&debug=true&useskin=' + encodeURIComponent( skin )
70                                                                 }, availableSkins[skin] ) );
71                                                 }
72                                                 skinLinksText += skinLinks.join( ' | ' ) + '.';
73                                                 mw.util.$content.html(
74                                                         '<p>Below is a list of tests to confirm proper functionality of the mediaWiki JavaScript library</p>'
75                                                         + '<p>' + skinLinksText + '</p>'
76                                                         + '<hr />'
77                                                         + '<table id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break; font-family:monospace,\'Courier New\'">'
78                                                         + '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>'
79                                                         + '</table>'
80                                                 );
81                                                 mw.test.$table = $( 'table#mw-mwutiltest-table' );
83                                                 /* Populate tests */
85                                                 // Main modules and their aliases
86                                                 mw.test.addHead( 'Main modules and their aliases' );
88                                                 mw.test.addTest( 'typeof mediaWiki',
89                                                         'object (string)' );
91                                                 mw.test.addTest( 'typeof mw',
92                                                         'object (string)' );
94                                                 mw.test.addTest( 'typeof jQuery',
95                                                         'function (string)' );
97                                                 mw.test.addTest( 'typeof $',
98                                                         'function (string)' );
100                                                 // Prototype functions added by MediaWiki
101                                                 mw.test.addHead( 'Prototype functions added by MediaWiki' );
103                                                 mw.test.addTest( 'typeof $.trimLeft',
104                                                         'function (string)' );
106                                                 mw.test.addTest( '$.trimLeft( "  foo bar  " )',
107                                                         'foo bar   (string)' );
109                                                 mw.test.addTest( 'typeof $.trimRight',
110                                                         'function (string)' );
112                                                 mw.test.addTest( '$.trimRight( "  foo bar  " )',
113                                                         '  foo bar (string)' );
115                                                 mw.test.addTest( 'typeof $.ucFirst',
116                                                         'function (string)' );
118                                                 mw.test.addTest( '$.ucFirst( "mediawiki" )',
119                                                         'Mediawiki (string)' );
121                                                 mw.test.addTest( 'typeof $.escapeRE',
122                                                         'function (string)' );
124                                                 mw.test.addTest( '$.escapeRE( ".st{e}$st" )',
125                                                         '\\.st\\{e\\}\\$st (string)' );
127                                                 mw.test.addTest( 'typeof $.isEmpty',
128                                                         'function (string)' );
130                                                 mw.test.addTest( '$.isEmpty( "string" )',
131                                                         'false (boolean)' );
133                                                 mw.test.addTest( '$.isEmpty( "0" )',
134                                                         'true (boolean)' );
136                                                 mw.test.addTest( '$.isEmpty([])',
137                                                         'true (boolean)' );
139                                                 mw.test.addTest( 'typeof $.compareArray',
140                                                         'function (string)' );
142                                                 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
143                                                         'true (boolean)' );
145                                                 mw.test.addTest( '$.compareArray( [1], [2] )',
146                                                         'false (boolean)' );
148                                                 mw.test.addTest( 'typeof $.compareObject',
149                                                         'function (string)' );
151                                                 // mediawiki.js
152                                                 mw.test.addHead( 'mediawiki.js' );
154                                                 mw.test.addTest( 'mw.config instanceof mw.Map',
155                                                         'true (boolean)' );
157                                                 mw.test.addTest( 'mw.config.exists()',
158                                                         'true (boolean)' );
160                                                 mw.test.addTest( 'mw.config.exists( "wgSomeName" )',
161                                                         'false (boolean)' );
163                                                 mw.test.addTest( 'mw.config.exists( ["wgCanonicalNamespace", "wgTitle"] )',
164                                                         'true (boolean)' );
166                                                 mw.test.addTest( 'mw.config.exists( ["wgSomeName", "wgTitle"] )',
167                                                         'false (boolean)' );
169                                                 mw.test.addTest( 'mw.config.get( "wgTitle" )',
170                                                         'BlankPage (string)' );
172                                                 mw.test.addTest( 'var a = mw.config.get( ["wgTitle"] ); a.wgTitle',
173                                                         'BlankPage (string)' );
175                                                 mw.test.addTest( 'typeof mw.html',
176                                                         'object (string)' );
178                                                 mw.test.addTest( 'mw.html.escape( \'<mw awesome="awesome">\' )',
179                                                         '&lt;mw awesome=&quot;awesome&quot;&gt; (string)' );
181                                                 mw.test.addTest( 'mw.html.element( "hr" )',
182                                                         '<hr/> (string)' );
184                                                 mw.test.addTest( 'mw.html.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
185                                                         '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)' );
187                                                 mw.test.addTest( 'typeof mw.loader',
188                                                         'object (string)' );
190                                                 mw.test.addTest( 'typeof mw.loader.using',
191                                                         'function (string)' );
193                                                 mw.test.addTest( 'typeof mw.Map',
194                                                         'function (string)' );
196                                                 mw.test.addTest( 'typeof mw.user',
197                                                         'object (string)' );
199                                                 mw.test.addTest( 'typeof mw.user.anonymous()',
200                                                         'boolean (string)' );
202                                                 // mediawiki.util.js
203                                                 mw.test.addHead( 'mediawiki.util.js' );
205                                                 mw.test.addTest( 'typeof mw.util',
206                                                         'object (string)' );
208                                                 mw.test.addTest( 'typeof mw.util.rawurlencode',
209                                                         'function (string)' );
211                                                 mw.test.addTest( 'mw.util.rawurlencode( "Test:A & B/Here" )',
212                                                         'Test%3AA%20%26%20B%2FHere (string)' );
214                                                 mw.test.addTest( 'typeof mw.util.wikiUrlencode',
215                                                         'function (string)' );
217                                                 mw.test.addTest( 'mw.util.wikiUrlencode( "Test:A & B/Here" )',
218                                                         'Test:A_%26_B/Here (string)' );
220                                                 mw.test.addTest( 'typeof mw.util.addCSS',
221                                                         'function (string)' );
223                                                 mw.test.addTest( 'var a = mw.util.addCSS( ".plainlinks { color:green; }" ); a.disabled;',
224                                                         'false (boolean)',
225                                                         '(boolean)' );
227                                                 mw.test.addTest( 'typeof mw.util.wikiGetlink',
228                                                         'function (string)' );
230                                                 mw.test.addTest( 'typeof mw.util.getParamValue',
231                                                         'function (string)' );
233                                                 mw.test.addTest( 'mw.util.getParamValue( "action" )',
234                                                         'mwutiltest (string)' );
236                                                 mw.test.addTest( 'mw.util.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
237                                                         'right (string)' );
239                                                 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
240                                                         'RegExp (string)' );
242                                                 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
243                                                         'function (string)' );
245                                                 mw.test.addTest( 'mw.util.$content instanceof jQuery',
246                                                         'true (boolean)' );
248                                                 mw.test.addTest( 'mw.util.$content.size()',
249                                                         '1 (number)' );
251                                                 mw.test.addTest( 'typeof mw.util.addPortletLink',
252                                                         'function (string)' );
254                                                 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" )',
255                                                         'object (string)' );
257                                                 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();',
258                                                         'MediaWiki.org (string)' );
260                                                 mw.test.addTest( 'typeof mw.util.jsMessage',
261                                                         'function (string)' );
263                                                 mw.test.addTest( 'mw.util.jsMessage( mw.config.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
264                                                         'true (boolean)' );
266                                                 // jQuery plugins
267                                                 mw.test.addHead( 'jQuery plugins' );
269                                                 mw.test.addTest( 'typeof $.client',
270                                                         'object (string)' );
272                                                 mw.test.addTest( 'typeof $.client.profile',
273                                                         'function (string)' );
275                                                 mw.test.addTest( 'var a = $.client.profile(); typeof a.name',
276                                                         'string (string)' );
278                                                 mw.test.addTest( 'typeof $.fn.makeCollapsible',
279                                                         'function (string)' );
281                                                 // Run tests and compare results
282                                                 var     exec,
283                                                         result,
284                                                         resulttype,
285                                                         numberoftests = 0,
286                                                         numberofpasseds = 0,
287                                                         numberofpartials = 0,
288                                                         numberoferrors = 0,
289                                                         $testrows = mw.test.$table.find( 'tr:has(td)' );
291                                                 $.each( mw.test.addedTests, function( i ) {
292                                                         numberoftests++;
294                                                         exec = mw.test.addedTests[i][0];
295                                                         shouldreturn = mw.test.addedTests[i][1];
296                                                         shouldcontain = mw.test.addedTests[i][2];
297                                                         doesreturn = eval( exec );
298                                                         doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
299                                                         $thisrow = $testrows.eq( i );
300                                                         $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesreturn ).replace(/  /g, '&nbsp;&nbsp;' ) );
302                                                         if ( doesreturn.indexOf( shouldcontain ) !== -1 ) {
303                                                                 if ( doesreturn == shouldreturn ) {
304                                                                         $thisrow.find( '> td' ).eq(3).css( 'background', '#EFE' ).text( 'OK' );
305                                                                         numberofpasseds++;
306                                                                 } else {
307                                                                         $thisrow.find( '> td' ).eq(3).css( 'background', '#FFE' ).html( '<small>PARTIALLY</small>' );
308                                                                         numberofpartials++;
309                                                                 }
310                                                         } else {
311                                                                 $thisrow.find( '> td' ).eq(3).css( 'background', '#FEE' ).text( 'ERROR' );
312                                                                 numberoferrors++;
313                                                         }
315                                                 } );
316                                                 mw.test.$table.before( '<p><strong>Ran ' + numberoftests + ' tests. ' +
317                                                         numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' +
318                                                         numberofpartials + ' partially passed test(s). </p>' );
320                                         }
321                                 } );
322                         }
323                 }
324         };
326         mediaWiki.test.init();
328 } )(jQuery, mediaWiki);