mediawiki.log: Introduce mw.log.error
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 /*global CompletenessTest, sinon */
2 /*jshint evil: true */
3 ( function ( $, mw, QUnit ) {
4 'use strict';
5
6 var mwTestIgnore, mwTester,
7 addons,
8 ELEMENT_NODE = 1,
9 TEXT_NODE = 3;
10
11 /**
12 * Add bogus to url to prevent IE crazy caching
13 *
14 * @param value {String} a relative path (eg. 'data/foo.js'
15 * or 'data/test.php?foo=bar').
16 * @return {String} Such as 'data/foo.js?131031765087663960'
17 */
18 QUnit.fixurl = function ( value ) {
19 return value + (/\?/.test( value ) ? '&' : '?')
20 + String( new Date().getTime() )
21 + String( parseInt( Math.random() * 100000, 10 ) );
22 };
23
24 /**
25 * Configuration
26 */
27
28 // When a test() indicates asynchronicity with stop(),
29 // allow 30 seconds to pass before killing the test(),
30 // and assuming failure.
31 QUnit.config.testTimeout = 30 * 1000;
32
33 QUnit.config.requireExpects = true;
34
35 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
36 QUnit.config.urlConfig.push( {
37 id: 'debug',
38 label: 'Enable ResourceLoaderDebug',
39 tooltip: 'Enable debug mode in ResourceLoader',
40 value: 'true'
41 } );
42
43 /**
44 * CompletenessTest
45 *
46 * Adds toggle checkbox to header
47 */
48 QUnit.config.urlConfig.push( {
49 id: 'completenesstest',
50 label: 'Run CompletenessTest',
51 tooltip: 'Run the completeness test'
52 } );
53
54 /**
55 * SinonJS
56 *
57 * Glue code for nicer integration with QUnit setup/teardown
58 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
59 * Fixes:
60 * - Work properly with asynchronous QUnit by using module setup/teardown
61 * instead of synchronously wrapping QUnit.test.
62 */
63 sinon.assert.fail = function ( msg ) {
64 QUnit.assert.ok( false, msg );
65 };
66 sinon.assert.pass = function ( msg ) {
67 QUnit.assert.ok( true, msg );
68 };
69 sinon.config = {
70 injectIntoThis: true,
71 injectInto: null,
72 properties: ['spy', 'stub', 'mock', 'sandbox'],
73 // Don't fake timers by default
74 useFakeTimers: false,
75 useFakeServer: false
76 };
77 ( function () {
78 var orgModule = QUnit.module;
79
80 QUnit.module = function ( name, localEnv ) {
81 localEnv = localEnv || {};
82 orgModule( name, {
83 setup: function () {
84 var config = sinon.getConfig( sinon.config );
85 config.injectInto = this;
86 sinon.sandbox.create( config );
87
88 if ( localEnv.setup ) {
89 localEnv.setup.call( this );
90 }
91 },
92 teardown: function () {
93 this.sandbox.verifyAndRestore();
94
95 if ( localEnv.teardown ) {
96 localEnv.teardown.call( this );
97 }
98 }
99 } );
100 };
101 }() );
102
103 // Extend QUnit.module to provide a fixture element.
104 ( function () {
105 var orgModule = QUnit.module;
106
107 QUnit.module = function ( name, localEnv ) {
108 var fixture;
109 localEnv = localEnv || {};
110 orgModule( name, {
111 setup: function () {
112 fixture = document.createElement( 'div' );
113 fixture.id = 'qunit-fixture';
114 document.body.appendChild( fixture );
115
116 if ( localEnv.setup ) {
117 localEnv.setup.call( this );
118 }
119 },
120 teardown: function () {
121 if ( localEnv.teardown ) {
122 localEnv.teardown.call( this );
123 }
124
125 fixture.parentNode.removeChild( fixture );
126 }
127 } );
128 };
129 }() );
130
131 // Initiate when enabled
132 if ( QUnit.urlParams.completenesstest ) {
133
134 // Return true to ignore
135 mwTestIgnore = function ( val, tester ) {
136
137 // Don't record methods of the properties of constructors,
138 // to avoid getting into a loop (prototype.constructor.prototype..).
139 // Since we're therefor skipping any injection for
140 // "new mw.Foo()", manually set it to true here.
141 if ( val instanceof mw.Map ) {
142 tester.methodCallTracker.Map = true;
143 return true;
144 }
145 if ( val instanceof mw.Title ) {
146 tester.methodCallTracker.Title = true;
147 return true;
148 }
149
150 // Don't record methods of the properties of a jQuery object
151 if ( val instanceof $ ) {
152 return true;
153 }
154
155 // Don't iterate over the module registry (the 'script' references would
156 // be listed as untested methods otherwise)
157 if ( val === mw.loader.moduleRegistry ) {
158 return true;
159 }
160
161 return false;
162 };
163
164 mwTester = new CompletenessTest( mw, mwTestIgnore );
165 }
166
167 /**
168 * Test environment recommended for all QUnit test modules
169 *
170 * Whether to log environment changes to the console
171 */
172 QUnit.config.urlConfig.push( 'mwlogenv' );
173
174 /**
175 * Reset mw.config and others to a fresh copy of the live config for each test(),
176 * and restore it back to the live one afterwards.
177 * @param localEnv {Object} [optional]
178 * @example (see test suite at the bottom of this file)
179 * </code>
180 */
181 QUnit.newMwEnvironment = ( function () {
182 var warn, error, log, liveConfig, liveMessages,
183 ajaxRequests = [];
184
185 liveConfig = mw.config.values;
186 liveMessages = mw.messages.values;
187
188 function suppressWarnings() {
189 warn = mw.log.warn;
190 error = mw.log.error;
191 mw.log.warn = mw.log.error = $.noop;
192 }
193
194 function restoreWarnings() {
195 // Guard against calls not balanced with suppressWarnings()
196 if ( warn !== undefined ) {
197 mw.log.warn = warn;
198 mw.log.error = error;
199 warn = error = undefined;
200 }
201 }
202
203 function freshConfigCopy( custom ) {
204 var copy;
205 // Tests should mock all factors that directly influence the tested code.
206 // For backwards compatibility though we set mw.config to a fresh copy of the live
207 // config. This way any modifications made to mw.config during the test will not
208 // affect other tests, nor the global scope outside the test runner.
209 // This is a shallow copy, since overriding an array or object value via "custom"
210 // should replace it. Setting a config property means you override it, not extend it.
211 // NOTE: It is important that we suppress warnings because extend() will also access
212 // deprecated properties and trigger deprecation warnings from mw.log#deprecate.
213 suppressWarnings();
214 copy = $.extend( {}, liveConfig, custom );
215 restoreWarnings();
216
217 return copy;
218 }
219
220 function freshMessagesCopy( custom ) {
221 return $.extend( /*deep=*/true, {}, liveMessages, custom );
222 }
223
224 /**
225 * @param {jQuery.Event} event
226 * @param {jqXHR} jqXHR
227 * @param {Object} ajaxOptions
228 */
229 function trackAjax( event, jqXHR, ajaxOptions ) {
230 ajaxRequests.push( { xhr: jqXHR, options: ajaxOptions } );
231 }
232
233 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
234
235 return function ( localEnv ) {
236 localEnv = $.extend( {
237 // QUnit
238 setup: $.noop,
239 teardown: $.noop,
240 // MediaWiki
241 config: {},
242 messages: {}
243 }, localEnv );
244
245 return {
246 setup: function () {
247 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
248 + ': ' + QUnit.config.current.testName + '"' );
249
250 // Greetings, mock environment!
251 mw.config.values = freshConfigCopy( localEnv.config );
252 mw.messages.values = freshMessagesCopy( localEnv.messages );
253 this.suppressWarnings = suppressWarnings;
254 this.restoreWarnings = restoreWarnings;
255
256 // Start tracking ajax requests
257 $( document ).on( 'ajaxSend', trackAjax );
258
259 localEnv.setup.call( this );
260 },
261
262 teardown: function () {
263 var timers, active;
264 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
265 + ': ' + QUnit.config.current.testName + '"' );
266
267 localEnv.teardown.call( this );
268
269 // Stop tracking ajax requests
270 $( document ).off( 'ajaxSend', trackAjax );
271
272 // Farewell, mock environment!
273 mw.config.values = liveConfig;
274 mw.messages.values = liveMessages;
275
276 // As a convenience feature, automatically restore warnings if they're
277 // still suppressed by the end of the test.
278 restoreWarnings();
279
280 // Tests should use fake timers or wait for animations to complete
281 // Check for incomplete animations/requests/etc and throw if there are any.
282 if ( $.timers && $.timers.length !== 0 ) {
283 timers = $.timers.length;
284 $.each( $.timers, function ( i, timer ) {
285 var node = timer.elem;
286 mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
287 mw.html.element( node.nodeName.toLowerCase(), $(node).getAttrs() )
288 );
289 } );
290 // Force animations to stop to give the next test a clean start
291 $.fx.stop();
292
293 throw new Error( 'Unfinished animations: ' + timers );
294 }
295
296 // Test should use fake XHR, wait for requests, or call abort()
297 if ( $.active !== undefined && $.active !== 0 ) {
298 active = $.grep( ajaxRequests, function ( ajax ) {
299 return ajax.xhr.state() === 'pending';
300 } );
301 if ( active.length !== $.active ) {
302 mw.log.warn( 'Pending requests does not match jQuery.active count' );
303 }
304 // Force requests to stop to give the next test a clean start
305 $.each( active, function ( i, ajax ) {
306 mw.log.warn( 'Unfinished AJAX request #' + i, ajax.options );
307 ajax.xhr.abort();
308 } );
309 ajaxRequests = [];
310
311 throw new Error( 'Unfinished AJAX requests: ' + active.length );
312 }
313 }
314 };
315 };
316 }() );
317
318 // $.when stops as soon as one fails, which makes sense in most
319 // practical scenarios, but not in a unit test where we really do
320 // need to wait until all of them are finished.
321 QUnit.whenPromisesComplete = function () {
322 var altPromises = [];
323
324 $.each( arguments, function ( i, arg ) {
325 var alt = $.Deferred();
326 altPromises.push( alt );
327
328 // Whether this one fails or not, forwards it to
329 // the 'done' (resolve) callback of the alternative promise.
330 arg.always( alt.resolve );
331 } );
332
333 return $.when.apply( $, altPromises );
334 };
335
336 /**
337 * Recursively convert a node to a plain object representing its structure.
338 * Only considers attributes and contents (elements and text nodes).
339 * Attribute values are compared strictly and not normalised.
340 *
341 * @param {Node} node
342 * @return {Object|string} Plain JavaScript value representing the node.
343 */
344 function getDomStructure( node ) {
345 var $node, children, processedChildren, i, len, el;
346 $node = $( node );
347 if ( node.nodeType === ELEMENT_NODE ) {
348 children = $node.contents();
349 processedChildren = [];
350 for ( i = 0, len = children.length; i < len; i++ ) {
351 el = children[i];
352 if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) {
353 processedChildren.push( getDomStructure( el ) );
354 }
355 }
356
357 return {
358 tagName: node.tagName,
359 attributes: $node.getAttrs(),
360 contents: processedChildren
361 };
362 } else {
363 // Should be text node
364 return $node.text();
365 }
366 }
367
368 /**
369 * Gets structure of node for this HTML.
370 *
371 * @param {string} html HTML markup for one or more nodes.
372 */
373 function getHtmlStructure( html ) {
374 var el = $( '<div>' ).append( html )[0];
375 return getDomStructure( el );
376 }
377
378 /**
379 * Add-on assertion helpers
380 */
381 // Define the add-ons
382 addons = {
383
384 // Expect boolean true
385 assertTrue: function ( actual, message ) {
386 QUnit.push( actual === true, actual, true, message );
387 },
388
389 // Expect boolean false
390 assertFalse: function ( actual, message ) {
391 QUnit.push( actual === false, actual, false, message );
392 },
393
394 // Expect numerical value less than X
395 lt: function ( actual, expected, message ) {
396 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
397 },
398
399 // Expect numerical value less than or equal to X
400 ltOrEq: function ( actual, expected, message ) {
401 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
402 },
403
404 // Expect numerical value greater than X
405 gt: function ( actual, expected, message ) {
406 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
407 },
408
409 // Expect numerical value greater than or equal to X
410 gtOrEq: function ( actual, expected, message ) {
411 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
412 },
413
414 /**
415 * Asserts that two HTML strings are structurally equivalent.
416 *
417 * @param {string} actualHtml Actual HTML markup.
418 * @param {string} expectedHtml Expected HTML markup
419 * @param {string} message Assertion message.
420 */
421 htmlEqual: function ( actualHtml, expectedHtml, message ) {
422 var actual = getHtmlStructure( actualHtml ),
423 expected = getHtmlStructure( expectedHtml );
424
425 QUnit.push(
426 QUnit.equiv(
427 actual,
428 expected
429 ),
430 actual,
431 expected,
432 message
433 );
434 },
435
436 /**
437 * Asserts that two HTML strings are not structurally equivalent.
438 *
439 * @param {string} actualHtml Actual HTML markup.
440 * @param {string} expectedHtml Expected HTML markup.
441 * @param {string} message Assertion message.
442 */
443 notHtmlEqual: function ( actualHtml, expectedHtml, message ) {
444 var actual = getHtmlStructure( actualHtml ),
445 expected = getHtmlStructure( expectedHtml );
446
447 QUnit.push(
448 !QUnit.equiv(
449 actual,
450 expected
451 ),
452 actual,
453 expected,
454 message
455 );
456 }
457 };
458
459 $.extend( QUnit.assert, addons );
460
461 /**
462 * Small test suite to confirm proper functionality of the utilities and
463 * initializations defined above in this file.
464 */
465 QUnit.module( 'test.mediawiki.qunit.testrunner', QUnit.newMwEnvironment( {
466 setup: function () {
467 this.mwHtmlLive = mw.html;
468 mw.html = {
469 escape: function () {
470 return 'mocked';
471 }
472 };
473 },
474 teardown: function () {
475 mw.html = this.mwHtmlLive;
476 },
477 config: {
478 testVar: 'foo'
479 },
480 messages: {
481 testMsg: 'Foo.'
482 }
483 } ) );
484
485 QUnit.test( 'Setup', 3, function ( assert ) {
486 assert.equal( mw.html.escape( 'foo' ), 'mocked', 'setup() callback was ran.' );
487 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
488 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
489
490 mw.config.set( 'testVar', 'bar' );
491 mw.messages.set( 'testMsg', 'Bar.' );
492 } );
493
494 QUnit.test( 'Teardown', 2, function ( assert ) {
495 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
496 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
497 } );
498
499 QUnit.test( 'Loader status', 2, function ( assert ) {
500 var i, len, state,
501 modules = mw.loader.getModuleNames(),
502 error = [],
503 missing = [];
504
505 for ( i = 0, len = modules.length; i < len; i++ ) {
506 state = mw.loader.getState( modules[i] );
507 if ( state === 'error' ) {
508 error.push( modules[i] );
509 } else if ( state === 'missing' ) {
510 missing.push( modules[i] );
511 }
512 }
513
514 assert.deepEqual( error, [], 'Modules in error state' );
515 assert.deepEqual( missing, [], 'Modules in missing state' );
516 } );
517
518 QUnit.test( 'htmlEqual', 8, function ( assert ) {
519 assert.htmlEqual(
520 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
521 '<div><p data-length=\'10\' class=\'some classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
522 'Attribute order, spacing and quotation marks (equal)'
523 );
524
525 assert.notHtmlEqual(
526 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
527 '<div><p data-length=\'10\' class=\'some more classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
528 'Attribute order, spacing and quotation marks (not equal)'
529 );
530
531 assert.htmlEqual(
532 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
533 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
534 'Multiple root nodes (equal)'
535 );
536
537 assert.notHtmlEqual(
538 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
539 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
540 'Multiple root nodes (not equal, last label node is different)'
541 );
542
543 assert.htmlEqual(
544 'fo&quot;o<br/>b&gt;ar',
545 'fo"o<br/>b>ar',
546 'Extra escaping is equal'
547 );
548 assert.notHtmlEqual(
549 'foo&lt;br/&gt;bar',
550 'foo<br/>bar',
551 'Text escaping (not equal)'
552 );
553
554 assert.htmlEqual(
555 'foo<a href="http://example.com">example</a>bar',
556 'foo<a href="http://example.com">example</a>bar',
557 'Outer text nodes are compared (equal)'
558 );
559
560 assert.notHtmlEqual(
561 'foo<a href="http://example.com">example</a>bar',
562 'foo<a href="http://example.com">example</a>quux',
563 'Outer text nodes are compared (last text node different)'
564 );
565
566 } );
567
568 QUnit.module( 'test.mediawiki.qunit.testrunner-after', QUnit.newMwEnvironment() );
569
570 QUnit.test( 'Teardown', 3, function ( assert ) {
571 assert.equal( mw.html.escape( '<' ), '&lt;', 'teardown() callback was ran.' );
572 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
573 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
574 } );
575
576 }( jQuery, mediaWiki, QUnit ) );