Fix ULS QUnit tests

Tests were failing for two reasons: wrong modules as dependencies of the
test module and test timing out due to incorrect usage of QUnit.asyncTest
and QUnit.start() and QUnit.Stop.

While at it, did some small cleanups and converted the number of
assertions to use non-deprecated QUnit.expect() instead.

Options are saved using postWithToken of mw.API. This also rises minimum
MediaWiki version to 1.22. This allowed removing hundred lines of code.
MLEB is already 1.22 and above.

Change-Id: I46a1674e4ede48a0e331c8d201d1d847db51d9dd
This commit is contained in:
Niklas Laxström
2014-09-18 12:27:06 +02:00
committed by Santhosh
parent c5bca60536
commit d25f7f3307
3 changed files with 35 additions and 100 deletions

View File

@@ -20,86 +20,7 @@
( function ( $, mw ) {
'use strict';
var ULSPreferences,
cachedOptionsToken = null;
/**
* Post to options API with correct token.
* If we have no token, get one and try to post.
* If we have a cached token try using that,
* and if it fails, blank out the cached token and start over.
*
* @param params {Object} API parameters
* @param ok {Function} callback for success
* @param err {Function} [optional] error callback
* @return {jqXHR}
*/
function saveOptionsWithToken( params, ok, err ) {
if ( cachedOptionsToken === null ) {
// We don't have a valid cached token, so get a fresh one and try posting.
// We do not trap any 'badtoken' or 'notoken' errors, because we don't want
// an infinite loop. If this fresh token is bad, something else is very wrong.
return getOptionsToken( function ( token ) {
params.token = token;
new mw.Api().post( params, ok, err );
}, err );
} else {
params.token = cachedOptionsToken;
return new mw.Api().post( params, {
ok: ok,
err: function ( code, result ) {
// We do have a token, but it might be expired.
// So if it is 'bad', then start over with a new token.
if ( code === 'badtoken' ) {
// force a new token, clear any old one
cachedOptionsToken = null;
saveOptionsWithToken( params, ok, err );
} else {
err( code, result );
}
}
} );
}
}
/**
* Api helper to grab an options token
*
* token callback has signature ( String token )
* error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
* Note that xhr and exception are only available for 'http_*' errors
* code may be any http_* error code (see mw.Api), or 'token_missing'
*
* @param tokenCallback {Function} received token callback
* @param err {Function} error callback
* @return {jqXHR}
*/
function getOptionsToken( tokenCallback, err ) {
return new mw.Api().get( {
action: 'tokens',
type: 'options'
}, {
ok: function ( data ) {
var token;
// If token type is not available for this user,
// key 'translationreviewtoken' is missing or can contain Boolean false
if ( data.tokens && data.tokens.optionstoken ) {
token = data.tokens.optionstoken;
cachedOptionsToken = token;
tokenCallback( token );
} else {
err( 'token-missing', data );
}
},
err: err,
// Due to the API assuming we're logged out if we pass the callback-parameter,
// we have to disable jQuery's callback system, and instead parse JSON string,
// by setting 'jsonp' to false.
jsonp: false
} );
}
var ULSPreferences;
/**
* Wrapper for localStorage, falls back to cookie
@@ -225,15 +146,14 @@
preferenceStore().set( this.preferenceName, this.preferences );
callback.call( this, true );
} else {
// Logged in user. Use MW APIs to change preferences
saveOptionsWithToken( {
new mw.Api().postWithToken( 'options', {
action: 'options',
optionname: ulsPreferences.preferenceName,
optionvalue: JSON.stringify( ulsPreferences.preferences )
}, function () {
} ).done( function () {
callback.call( this, true );
}, function () {
} ).fail( function () {
callback.call( this, false );
} );
}