Merge "Fix ULS QUnit tests"
This commit is contained in:
@@ -102,7 +102,7 @@ class UniversalLanguageSelectorHooks {
|
|||||||
public static function addTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
|
public static function addTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
|
||||||
$testModules['qunit']['ext.uls.tests'] = array(
|
$testModules['qunit']['ext.uls.tests'] = array(
|
||||||
'scripts' => array( 'tests/qunit/ext.uls.tests.js' ),
|
'scripts' => array( 'tests/qunit/ext.uls.tests.js' ),
|
||||||
'dependencies' => array( 'ext.uls.init', 'ext.uls.interface' ),
|
'dependencies' => array( 'jquery.uls', 'ext.uls.preferences' ),
|
||||||
'localBasePath' => __DIR__,
|
'localBasePath' => __DIR__,
|
||||||
'remoteExtPath' => 'UniversalLanguageSelector',
|
'remoteExtPath' => 'UniversalLanguageSelector',
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,86 +20,7 @@
|
|||||||
( function ( $, mw ) {
|
( function ( $, mw ) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ULSPreferences,
|
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
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for localStorage, falls back to cookie
|
* Wrapper for localStorage, falls back to cookie
|
||||||
@@ -225,15 +146,14 @@
|
|||||||
preferenceStore().set( this.preferenceName, this.preferences );
|
preferenceStore().set( this.preferenceName, this.preferences );
|
||||||
callback.call( this, true );
|
callback.call( this, true );
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Logged in user. Use MW APIs to change preferences
|
// Logged in user. Use MW APIs to change preferences
|
||||||
saveOptionsWithToken( {
|
new mw.Api().postWithToken( 'options', {
|
||||||
action: 'options',
|
action: 'options',
|
||||||
optionname: ulsPreferences.preferenceName,
|
optionname: ulsPreferences.preferenceName,
|
||||||
optionvalue: JSON.stringify( ulsPreferences.preferences )
|
optionvalue: JSON.stringify( ulsPreferences.preferences )
|
||||||
}, function () {
|
} ).done( function () {
|
||||||
callback.call( this, true );
|
callback.call( this, true );
|
||||||
}, function () {
|
} ).fail( function () {
|
||||||
callback.call( this, false );
|
callback.call( this, false );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,18 +22,24 @@
|
|||||||
|
|
||||||
QUnit.module( 'ext.uls', QUnit.newMwEnvironment() );
|
QUnit.module( 'ext.uls', QUnit.newMwEnvironment() );
|
||||||
|
|
||||||
QUnit.test( '-- Initial check', 1, function ( assert ) {
|
QUnit.test( 'Initial check', function ( assert ) {
|
||||||
|
QUnit.expect( 1 );
|
||||||
|
|
||||||
assert.ok( $.fn.uls, '$.fn.uls is defined' );
|
assert.ok( $.fn.uls, '$.fn.uls is defined' );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( '-- Custom langdb', 1, function ( assert ) {
|
QUnit.test( 'Custom langdb', function ( assert ) {
|
||||||
|
QUnit.expect( 1 );
|
||||||
|
|
||||||
// This is a custom non-standard language code used in MW.
|
// This is a custom non-standard language code used in MW.
|
||||||
// If it's not defined, then, for example,
|
// If it's not defined, then, for example,
|
||||||
// its direction cannot be acquired using the langdb utils.
|
// its direction cannot be acquired using the langdb utils.
|
||||||
assert.strictEqual( $.uls.data.getDir( 'als' ), 'ltr', 'The direction of custom MW language als is ltr.' );
|
assert.strictEqual( $.uls.data.getDir( 'als' ), 'ltr', 'The direction of custom MW language als is ltr.' );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.asyncTest( '-- User preferences', 2, function ( assert ) {
|
QUnit.test( 'User preferences', function ( assert ) {
|
||||||
|
QUnit.expect( 2 );
|
||||||
|
|
||||||
// 'gofanim' means "fonts" in Hebrew.
|
// 'gofanim' means "fonts" in Hebrew.
|
||||||
// Here it's used as a meaningless word, to test
|
// Here it's used as a meaningless word, to test
|
||||||
// the preferences without changing anything useful.
|
// the preferences without changing anything useful.
|
||||||
@@ -43,30 +49,39 @@
|
|||||||
readPrefs;
|
readPrefs;
|
||||||
|
|
||||||
prefsToSave[prefName] = {
|
prefsToSave[prefName] = {
|
||||||
'fonts': {
|
fonts: {
|
||||||
'qqy': 'Megafont'
|
qqy: 'Megafont'
|
||||||
},
|
}
|
||||||
'webfonts-enabled': true
|
|
||||||
};
|
};
|
||||||
|
|
||||||
prefs.set( prefName, prefsToSave );
|
prefs.set( prefName, prefsToSave );
|
||||||
|
|
||||||
|
readPrefs = prefs.get( prefName );
|
||||||
|
assert.strictEqual(
|
||||||
|
readPrefs[prefName].fonts.qqy,
|
||||||
|
'Megafont',
|
||||||
|
'Correct value for the font name'
|
||||||
|
);
|
||||||
|
|
||||||
QUnit.stop();
|
QUnit.stop();
|
||||||
prefs.save( function ( successSave ) {
|
prefs.save( function ( successSave ) {
|
||||||
QUnit.start();
|
QUnit.start();
|
||||||
assert.ok( successSave, 'Options saving API did not produce an error.' );
|
assert.ok( successSave, 'Options saving API did not produce an error.' );
|
||||||
|
|
||||||
|
// Delete old options
|
||||||
|
prefs.set( prefName, undefined );
|
||||||
|
QUnit.stop();
|
||||||
|
prefs.save( function () {
|
||||||
|
QUnit.start();
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
|
|
||||||
readPrefs = prefs.get( prefName );
|
|
||||||
assert.strictEqual( readPrefs[prefName].fonts.qqy, 'Megafont', 'Correct value for the font name' );
|
|
||||||
|
|
||||||
// Delete old options
|
|
||||||
prefs.set( prefName, undefined );
|
|
||||||
prefs.save();
|
|
||||||
} );
|
} );
|
||||||
|
|
||||||
QUnit.test( '-- Common languages', 1, function ( assert ) {
|
QUnit.test( 'Common languages', function ( assert ) {
|
||||||
var i, foundTagalog, languagesInPH;
|
var i, foundTagalog, languagesInPH;
|
||||||
|
|
||||||
|
QUnit.expect( 1 );
|
||||||
|
|
||||||
// Bug 49847
|
// Bug 49847
|
||||||
foundTagalog = false;
|
foundTagalog = false;
|
||||||
languagesInPH = mw.uls.getFrequentLanguageList( 'PH' );
|
languagesInPH = mw.uls.getFrequentLanguageList( 'PH' );
|
||||||
|
|||||||
Reference in New Issue
Block a user