Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/provider/mmv.provider.ThumbnailInfo.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { ThumbnailInfo } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.provider.ThumbnailInfo', QUnit.newMwEnvironment( {
22 // mw.Title relies on these three config vars
23 // Restore them after each test run
24 config: {
25 wgFormattedNamespaces: {
26 '-2': 'Media',
27 '-1': 'Special',
28 0: '',
29 1: 'Talk',
30 2: 'User',
31 3: 'User talk',
32 4: 'Wikipedia',
33 5: 'Wikipedia talk',
34 6: 'File',
35 7: 'File talk',
36 8: 'MediaWiki',
37 9: 'MediaWiki talk',
38 10: 'Template',
39 11: 'Template talk',
40 12: 'Help',
41 13: 'Help talk',
42 14: 'Category',
43 15: 'Category talk',
44 // testing custom / localized namespace
45 100: 'Penguins'
46 },
47 wgNamespaceIds: {
48 /* eslint-disable camelcase */
49 media: -2,
50 special: -1,
51 '': 0,
52 talk: 1,
53 user: 2,
54 user_talk: 3,
55 wikipedia: 4,
56 wikipedia_talk: 5,
57 file: 6,
58 file_talk: 7,
59 mediawiki: 8,
60 mediawiki_talk: 9,
61 template: 10,
62 template_talk: 11,
63 help: 12,
64 help_talk: 13,
65 category: 14,
66 category_talk: 15,
67 image: 6,
68 image_talk: 7,
69 project: 4,
70 project_talk: 5,
71 // Testing custom namespaces and aliases
72 penguins: 100,
73 antarctic_waterfowl: 100
74 /* eslint-enable camelcase */
75 },
76 wgCaseSensitiveNamespaces: []
77 }
78 } ) );
79
80 QUnit.test( 'ThumbnailInfo constructor sense check', ( assert ) => {
81 const api = { get: function () {} };
82 const thumbnailInfoProvider = new ThumbnailInfo( api );
83
84 assert.true( thumbnailInfoProvider instanceof ThumbnailInfo );
85 } );
86
87 QUnit.test( 'ThumbnailInfo get test', function ( assert ) {
88 const api = { get: this.sandbox.stub().returns( $.Deferred().resolve( {
89 query: {
90 pages: [
91 {
92 ns: 6,
93 title: 'File:Stuff.jpg',
94 missing: true,
95 imagerepository: 'shared',
96 imageinfo: [
97 {
98 thumburl: 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Stuff.jpg/51px-Stuff.jpg',
99 thumbwidth: 95,
100 thumbheight: 200,
101 url: 'https://upload.wikimedia.org/wikipedia/commons/1/19/Stuff.jpg',
102 descriptionurl: 'https://commons.wikimedia.org/wiki/File:Stuff.jpg'
103 }
104 ]
105 }
106 ]
107 }
108 } ) ) };
109 const file = new mw.Title( 'File:Stuff.jpg' );
110 const thumbnailInfoProvider = new ThumbnailInfo( api );
111
112 return thumbnailInfoProvider.get( file, '', 100 ).then( ( thumbnail ) => {
113 assert.strictEqual( thumbnail.url,
114 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Stuff.jpg/51px-Stuff.jpg',
115 'URL is set correctly' );
116 assert.strictEqual( thumbnail.width, 95, 'actual width is set correctly' );
117 assert.strictEqual( thumbnail.height, 200, 'actual height is set correctly' );
118 } ).then( () => {
119 assert.strictEqual( api.get.calledOnce, true );
120 assert.deepEqual( api.get.getCall( 0 ).args[ 0 ], {
121 formatversion: 2,
122 action: 'query',
123 prop: 'imageinfo',
124 titles: 'File:Stuff.jpg',
125 iiprop: 'url',
126 iiurlheight: undefined,
127 iiurlparam: undefined,
128 iiurlwidth: 100
129 } );
130 // call the data provider a second time to check caching
131 return thumbnailInfoProvider.get( file, '', 100 );
132 } ).then( () => {
133 assert.strictEqual( api.get.calledOnce, true );
134 // call a third time with different size to check caching
135 return thumbnailInfoProvider.get( file, '', 110 );
136 } ).then( () => {
137 assert.strictEqual( api.get.calledTwice, true );
138 // call it again, with a height specified, to check caching
139 return thumbnailInfoProvider.get( file, '', 110, 100 );
140 } ).then( () => {
141 assert.strictEqual( api.get.calledThrice, true );
142 // call it again, with multi lingual SVG
143 const file2 = new mw.Title( 'File:Multilingual_SVG_example.svg' );
144 const sampleUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/1e/Multilingual_SVG_example.svg/langit-120px-Multilingual_SVG_example.svg.png';
145 return thumbnailInfoProvider.get( file2, sampleUrl, 100 );
146 } ).then( () => {
147 assert.deepEqual( api.get.getCall( api.get.callCount - 1 ).args[ 0 ], {
148 action: 'query',
149 formatversion: 2,
150 iiprop: 'url',
151 iiurlheight: undefined,
152 iiurlparam: 'langit-100px',
153 iiurlwidth: 100,
154 prop: 'imageinfo',
155 titles: 'File:Multilingual_SVG_example.svg'
156 } );
157 } );
158 } );
159
160 QUnit.test( 'ThumbnailInfo fail test', ( assert ) => {
161 const api = { get: function () {
162 return $.Deferred().resolve( {} );
163 } };
164 const file = new mw.Title( 'File:Stuff.jpg' );
165 const done = assert.async();
166 const thumbnailInfoProvider = new ThumbnailInfo( api );
167
168 thumbnailInfoProvider.get( file, '', 100 ).fail( () => {
169 assert.true( true, 'promise rejected when no data is returned' );
170 done();
171 } );
172 } );
173
174 QUnit.test( 'ThumbnailInfo fail test 2', ( assert ) => {
175 const api = { get: function () {
176 return $.Deferred().resolve( {
177 query: {
178 pages: [
179 {
180 title: 'File:Stuff.jpg'
181 }
182 ]
183 }
184 } );
185 } };
186 const file = new mw.Title( 'File:Stuff.jpg' );
187 const done = assert.async();
188 const thumbnailInfoProvider = new ThumbnailInfo( api );
189
190 thumbnailInfoProvider.get( file, '', 100 ).fail( () => {
191 assert.true( true, 'promise rejected when imageinfo is missing' );
192 done();
193 } );
194 } );
195
196 QUnit.test( 'ThumbnailInfo missing page test', ( assert ) => {
197 const api = { get: function () {
198 return $.Deferred().resolve( {
199 query: {
200 pages: [
201 {
202 title: 'File:Stuff.jpg',
203 missing: true,
204 imagerepository: ''
205 }
206 ]
207 }
208 } );
209 } };
210 const file = new mw.Title( 'File:Stuff.jpg' );
211 const done = assert.async();
212 const thumbnailInfoProvider = new ThumbnailInfo( api );
213
214 thumbnailInfoProvider.get( file, '' ).fail( ( errorMessage ) => {
215 assert.strictEqual( errorMessage, 'file does not exist: File:Stuff.jpg',
216 'error message is set correctly for missing file' );
217 done();
218 } );
219 } );
220
221 QUnit.test( 'ThumbnailInfo fail test 3', ( assert ) => {
222 const api = { get: function () {
223 return $.Deferred().resolve( {
224 query: {
225 pages: [
226 {
227 title: 'File:Stuff.jpg',
228 imageinfo: [
229 {}
230 ]
231 }
232 ]
233 }
234 } );
235 } };
236 const file = new mw.Title( 'File:Stuff.jpg' );
237 const done = assert.async();
238 const thumbnailInfoProvider = new ThumbnailInfo( api );
239
240 thumbnailInfoProvider.get( file, '', 100 ).fail( () => {
241 assert.true( true, 'promise rejected when thumbnail info is missing' );
242 done();
243 } );
244 } );
245 }() );