2014-01-31 02:03:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
const { Api } = require( 'mmv' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-01-31 02:03:08 +00:00
|
|
|
QUnit.module( 'mmv.provider.Api', QUnit.newMwEnvironment() );
|
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'Api constructor sense check', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = { get: function () {} };
|
|
|
|
const options = {};
|
|
|
|
const apiProvider = new Api( api, options );
|
|
|
|
const ApiProviderWithNoOptions = new Api( api );
|
2014-01-31 02:03:08 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
assert.true( apiProvider instanceof Api );
|
|
|
|
assert.true( ApiProviderWithNoOptions instanceof Api );
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'apiGetWithMaxAge()', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = {};
|
|
|
|
let options = {};
|
|
|
|
let apiProvider = new Api( api, options );
|
2014-04-19 01:59:17 +00:00
|
|
|
|
|
|
|
api.get = this.sandbox.stub();
|
|
|
|
apiProvider.apiGetWithMaxAge( {} );
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.false( 'maxage' in api.get.getCall( 0 ).args[ 0 ], 'maxage is not set by default' );
|
|
|
|
assert.false( 'smaxage' in api.get.getCall( 0 ).args[ 0 ], 'smaxage is not set by default' );
|
2014-04-19 01:59:17 +00:00
|
|
|
|
|
|
|
options = { maxage: 123 };
|
2023-05-20 08:30:52 +00:00
|
|
|
apiProvider = new Api( api, options );
|
2014-04-19 01:59:17 +00:00
|
|
|
|
|
|
|
api.get = this.sandbox.stub();
|
|
|
|
apiProvider.apiGetWithMaxAge( {} );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].maxage, 123, 'maxage falls back to provider default' );
|
|
|
|
assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].smaxage, 123, 'smaxage falls back to provider default' );
|
2014-04-19 01:59:17 +00:00
|
|
|
|
|
|
|
api.get = this.sandbox.stub();
|
|
|
|
apiProvider.apiGetWithMaxAge( {}, null, 456 );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].maxage, 456, 'maxage can be overridden' );
|
|
|
|
assert.strictEqual( api.get.getCall( 0 ).args[ 0 ].smaxage, 456, 'smaxage can be overridden' );
|
2014-04-19 01:59:17 +00:00
|
|
|
|
|
|
|
api.get = this.sandbox.stub();
|
|
|
|
apiProvider.apiGetWithMaxAge( {}, null, null );
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.false( 'maxage' in api.get.getCall( 0 ).args[ 0 ], 'maxage can be overridden to unset' );
|
|
|
|
assert.false( 'smaxage' in api.get.getCall( 0 ).args[ 0 ], 'smaxage can be overridden to unset' );
|
2014-04-19 01:59:17 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getCachedPromise success', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = { get: function () {} };
|
|
|
|
const apiProvider = new Api( api );
|
|
|
|
const oldMwLog = mw.log;
|
|
|
|
let promiseShouldBeCached = false;
|
2014-03-05 01:15:28 +00:00
|
|
|
|
|
|
|
mw.log = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'mw.log should not have been called' );
|
2014-03-05 01:15:28 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const promiseSource = function ( result ) {
|
2014-03-05 01:15:28 +00:00
|
|
|
return function () {
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( promiseShouldBeCached, false, 'promise was not cached' );
|
2014-03-05 01:15:28 +00:00
|
|
|
return $.Deferred().resolve( result );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).done( function ( result ) {
|
|
|
|
assert.strictEqual( result, 1, 'result comes from the promise source' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).done( function ( result ) {
|
|
|
|
assert.strictEqual( result, 2, 'result comes from the promise source' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
promiseShouldBeCached = true;
|
|
|
|
apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).done( function ( result ) {
|
|
|
|
assert.strictEqual( result, 1, 'result comes from cache' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.log = oldMwLog;
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getCachedPromise failure', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = { get: function () {} };
|
|
|
|
const apiProvider = new Api( api );
|
|
|
|
const oldMwLog = mw.log;
|
|
|
|
let promiseShouldBeCached = false;
|
2014-03-05 01:15:28 +00:00
|
|
|
|
|
|
|
mw.log = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'mw.log was called' );
|
2014-03-05 01:15:28 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const promiseSource = function ( result ) {
|
2014-03-05 01:15:28 +00:00
|
|
|
return function () {
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( promiseShouldBeCached, false, 'promise was not cached' );
|
2014-03-05 01:15:28 +00:00
|
|
|
return $.Deferred().reject( result );
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
apiProvider.getCachedPromise( 'foo', promiseSource( 1 ) ).fail( function ( result ) {
|
|
|
|
assert.strictEqual( result, 1, 'result comes from the promise source' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getCachedPromise( 'bar', promiseSource( 2 ) ).fail( function ( result ) {
|
|
|
|
assert.strictEqual( result, 2, 'result comes from the promise source' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
promiseShouldBeCached = true;
|
|
|
|
apiProvider.getCachedPromise( 'foo', promiseSource( 3 ) ).fail( function ( result ) {
|
|
|
|
assert.strictEqual( result, 1, 'result comes from cache' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.log = oldMwLog;
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getErrorMessage', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = { get: function () {} };
|
|
|
|
const apiProvider = new Api( api );
|
2014-01-31 02:03:08 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const errorMessage = apiProvider.getErrorMessage( {
|
2014-01-31 02:03:08 +00:00
|
|
|
servedby: 'mw1194',
|
|
|
|
error: {
|
|
|
|
code: 'unknown_action',
|
|
|
|
info: 'Unrecognized value for parameter \'action\': FOO'
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
assert.strictEqual( errorMessage,
|
|
|
|
'unknown_action: Unrecognized value for parameter \'action\': FOO',
|
2015-01-23 12:48:27 +00:00
|
|
|
'error message is parsed correctly' );
|
2014-01-31 02:03:08 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.strictEqual( apiProvider.getErrorMessage( {} ), 'unknown error', 'missing error message is handled' );
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getQueryPage', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const api = { get: function () {} };
|
|
|
|
const apiProvider = new Api( api );
|
|
|
|
const done = assert.async( 5 );
|
2014-01-31 02:03:08 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const data = {
|
2014-01-31 02:03:08 +00:00
|
|
|
query: {
|
2023-04-19 20:07:12 +00:00
|
|
|
pages: [
|
|
|
|
{
|
2014-01-31 02:03:08 +00:00
|
|
|
title: 'File:Stuff.jpg'
|
|
|
|
}
|
2023-04-19 20:07:12 +00:00
|
|
|
]
|
2014-01-31 02:03:08 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-26 00:01:07 +00:00
|
|
|
|
2023-04-19 20:07:12 +00:00
|
|
|
apiProvider.getQueryPage( data ).then( function ( field ) {
|
|
|
|
assert.strictEqual( field, data.query.pages[ 0 ], 'specified page is found' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
2017-07-26 00:01:07 +00:00
|
|
|
|
2023-04-19 20:07:12 +00:00
|
|
|
apiProvider.getQueryPage( {} ).fail( function () {
|
|
|
|
assert.true( true, 'promise rejected when data is missing' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
2017-07-26 00:01:07 +00:00
|
|
|
|
2023-04-19 20:07:12 +00:00
|
|
|
apiProvider.getQueryPage( { data: { query: {} } } ).fail( function () {
|
|
|
|
assert.true( true, 'promise rejected when pages are missing' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
2023-04-19 20:07:12 +00:00
|
|
|
apiProvider.getQueryPage( { data: { query: { pages: [] } } } ).fail( function () {
|
|
|
|
assert.true( true, 'promise rejected when pages are empty' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
2023-04-19 20:07:12 +00:00
|
|
|
apiProvider.getQueryPage( {
|
|
|
|
query: {
|
|
|
|
pages: [
|
|
|
|
{
|
|
|
|
title: 'File:Stuff.jpg'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'File:OtherStuff.jpg'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} ).fail( function () {
|
|
|
|
assert.true( true, 'promise rejected when data contains two entries' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|