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/>.
|
|
|
|
*/
|
|
|
|
|
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 ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-01-31 02:03:08 +00:00
|
|
|
options = {},
|
|
|
|
apiProvider = new mw.mmv.provider.Api( api, options ),
|
|
|
|
ApiProviderWithNoOptions = new mw.mmv.provider.Api( api );
|
|
|
|
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( apiProvider instanceof mw.mmv.provider.Api );
|
|
|
|
assert.true( ApiProviderWithNoOptions instanceof mw.mmv.provider.Api );
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'apiGetWithMaxAge()', function ( assert ) {
|
2014-04-19 01:59:17 +00:00
|
|
|
var api = {},
|
|
|
|
options = {},
|
|
|
|
apiProvider = new mw.mmv.provider.Api( api, options );
|
|
|
|
|
|
|
|
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 };
|
|
|
|
apiProvider = new mw.mmv.provider.Api( api, options );
|
|
|
|
|
|
|
|
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 ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-03-05 01:15:28 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
|
|
|
oldMwLog = mw.log,
|
|
|
|
promiseSource,
|
|
|
|
promiseShouldBeCached = false;
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
promiseSource = function ( result ) {
|
|
|
|
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 ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-03-05 01:15:28 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
|
|
|
oldMwLog = mw.log,
|
|
|
|
promiseSource,
|
|
|
|
promiseShouldBeCached = false;
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
promiseSource = function ( result ) {
|
|
|
|
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 ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
|
|
|
errorMessage;
|
|
|
|
|
|
|
|
errorMessage = apiProvider.getErrorMessage( {
|
|
|
|
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( 'getNormalizedTitle', function ( assert ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
|
|
|
title = new mw.Title( 'Image:Stuff.jpg' ),
|
|
|
|
normalizedTitle;
|
|
|
|
|
|
|
|
normalizedTitle = apiProvider.getNormalizedTitle( title, {} );
|
|
|
|
assert.strictEqual( normalizedTitle, title, 'missing normalization block is handled' );
|
|
|
|
|
|
|
|
normalizedTitle = apiProvider.getNormalizedTitle( title, {
|
|
|
|
query: {
|
|
|
|
normalized: [
|
|
|
|
{
|
|
|
|
from: 'Image:Foo.jpg',
|
|
|
|
to: 'File:Foo.jpg'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
assert.strictEqual( normalizedTitle, title, 'irrelevant normalization info is skipped' );
|
|
|
|
|
|
|
|
normalizedTitle = apiProvider.getNormalizedTitle( title, {
|
|
|
|
query: {
|
|
|
|
normalized: [
|
|
|
|
{
|
|
|
|
from: 'Image:Stuff.jpg',
|
|
|
|
to: 'File:Stuff.jpg'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
assert.strictEqual( normalizedTitle.getPrefixedDb(), 'File:Stuff.jpg', 'normalization happens' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getQueryField', function ( assert ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
2017-07-26 00:01:07 +00:00
|
|
|
done = assert.async( 3 ),
|
2014-01-31 02:03:08 +00:00
|
|
|
data;
|
|
|
|
|
|
|
|
data = {
|
|
|
|
query: {
|
|
|
|
imageusage: [
|
|
|
|
{
|
|
|
|
pageid: 736,
|
|
|
|
ns: 0,
|
|
|
|
title: 'Albert Einstein'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
2017-07-26 00:01:07 +00:00
|
|
|
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider.getQueryField( 'imageusage', data ).then( function ( field ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.strictEqual( field, data.query.imageusage, 'specified field is found' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
apiProvider.getQueryField( 'imageusage', {} ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
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
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getQueryField( 'imageusage', { data: { query: {} } } ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'promise rejected when field is missing' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getQueryPage', function ( assert ) {
|
2015-01-23 12:48:27 +00:00
|
|
|
var api = { get: function () {} },
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider = new mw.mmv.provider.Api( api ),
|
|
|
|
title = new mw.Title( 'File:Stuff.jpg' ),
|
|
|
|
titleWithNamespaceAlias = new mw.Title( 'Image:Stuff.jpg' ),
|
|
|
|
otherTitle = new mw.Title( 'File:Foo.jpg' ),
|
2017-07-26 00:01:07 +00:00
|
|
|
done = assert.async( 6 ),
|
2014-01-31 02:03:08 +00:00
|
|
|
data;
|
|
|
|
|
|
|
|
data = {
|
|
|
|
normalized: [
|
|
|
|
{
|
|
|
|
from: 'Image:Stuff.jpg',
|
|
|
|
to: 'File:Stuff.jpg'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
query: {
|
|
|
|
pages: {
|
|
|
|
'-1': {
|
|
|
|
title: 'File:Stuff.jpg'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-07-26 00:01:07 +00:00
|
|
|
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider.getQueryPage( title, data ).then( function ( field ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.strictEqual( field, data.query.pages[ '-1' ], '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
|
|
|
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider.getQueryPage( titleWithNamespaceAlias, data ).then( function ( field ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.strictEqual( field, data.query.pages[ '-1' ],
|
2015-01-23 12:48:27 +00:00
|
|
|
'specified page is found even if its title was normalized' );
|
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
|
|
|
|
2014-01-31 02:03:08 +00:00
|
|
|
apiProvider.getQueryPage( otherTitle, {} ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'promise rejected when page has different title' );
|
2017-07-26 00:01:07 +00:00
|
|
|
done();
|
2014-01-31 02:03:08 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getQueryPage( title, {} ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
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
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getQueryPage( title, { data: { query: {} } } ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
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
|
|
|
} );
|
|
|
|
|
|
|
|
apiProvider.getQueryPage( title, { data: { query: { pages: {} } } } ).fail( function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
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
|
|
|
} );
|
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|