Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/provider/mmv.provider.ImageInfo.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 { ImageInfo } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.provider.ImageInfo', 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( 'ImageInfo constructor sense check', ( assert ) => {
81 const api = { get: function () {} };
82 const imageInfoProvider = new ImageInfo( api );
83
84 assert.true( imageInfoProvider instanceof ImageInfo );
85 } );
86
87 QUnit.test( 'ImageInfo get test', ( assert ) => {
88 let apiCallCount = 0;
89 const api = { get: function () {
90 apiCallCount++;
91 return $.Deferred().resolve( {
92 query: {
93 pages: [
94 {
95 ns: 6,
96 title: 'File:Stuff.jpg',
97 missing: true,
98 imagerepository: 'shared',
99 imageinfo: [
100 {
101 timestamp: '2013-08-25T14:41:02Z',
102 userid: '3053121',
103 size: 346684,
104 width: 720,
105 height: 1412,
106 comment: 'User created page with UploadWizard',
107 url: 'https://upload.wikimedia.org/wikipedia/commons/1/19/Stuff.jpg',
108 descriptionurl: 'https://commons.wikimedia.org/wiki/File:Stuff.jpg',
109 sha1: 'a1ba23d471f4dad208b71c143e2e105a0e3032db',
110 metadata: [],
111 extmetadata: {
112 ObjectName: {
113 value: 'Some stuff',
114 source: 'commons-templates'
115 },
116 License: {
117 value: 'cc0',
118 source: 'commons-templates',
119 hidden: ''
120 },
121 LicenseShortName: {
122 value: 'CC0',
123 source: 'commons-templates'
124 },
125 UsageTerms: {
126 value: 'Creative Commons Public Domain Dedication',
127 source: 'commons-templates'
128 },
129 LicenseUrl: {
130 value: 'http://creativecommons.org/publicdomain/zero/1.0/',
131 source: 'commons-templates'
132 },
133 GPSLatitude: {
134 value: '90.000000',
135 source: 'commons-desc-page'
136 },
137 GPSLongitude: {
138 value: ' 180.000000',
139 source: 'commons-desc-page'
140 },
141 ImageDescription: {
142 value: 'Wikis stuff',
143 source: 'commons-desc-page'
144 },
145 DateTimeOriginal: {
146 value: '<time class="dtstart" datetime="2009-02-18">18 February 2009</time>\u00a0(according to <a href="//en.wikipedia.org/wiki/Exchangeable_image_file_format" class="extiw" title="en:Exchangeable image file format">EXIF</a> data)',
147 source: 'commons-desc-page'
148 },
149 DateTime: {
150 value: '2013-08-25T14:41:02Z',
151 source: 'commons-desc-page'
152 },
153 Credit: {
154 value: 'Wikipedia',
155 source: 'commons-desc-page',
156 hidden: ''
157 },
158 Artist: {
159 value: 'John Smith',
160 source: 'commons-desc-page'
161 },
162 AuthorCount: {
163 value: '2',
164 source: 'commons-desc-page'
165 },
166 Attribution: {
167 value: 'By John Smith',
168 source: 'commons-desc-page'
169 },
170 Permission: {
171 value: 'Do not use. Ever.',
172 source: 'commons-desc-page'
173 },
174 AttributionRequired: {
175 value: 'no',
176 source: 'commons-desc-page'
177 },
178 NonFree: {
179 value: 'yes',
180 source: 'commons-desc-page'
181 },
182 Restrictions: {
183 value: 'trademarked|insignia',
184 source: 'commons-desc-page'
185 },
186 DeletionReason: {
187 value: 'copyvio',
188 source: 'commons-desc-page'
189 }
190 },
191 mime: 'image/jpeg',
192 mediatype: 'BITMAP'
193 }
194 ]
195 }
196 ]
197 }
198 } );
199 } };
200 const file = new mw.Title( 'File:Stuff.jpg' );
201 const imageInfoProvider = new ImageInfo( api );
202
203 return imageInfoProvider.get( file ).then( ( image ) => {
204 assert.strictEqual( image.title.getPrefixedDb(), 'File:Stuff.jpg', 'title is set correctly' );
205 assert.strictEqual( image.name, 'Some stuff', 'name is set correctly' );
206 assert.strictEqual( image.size, 346684, 'size is set correctly' );
207 assert.strictEqual( image.width, 720, 'width is set correctly' );
208 assert.strictEqual( image.height, 1412, 'height is set correctly' );
209 assert.strictEqual( image.mimeType, 'image/jpeg', 'mimeType is set correctly' );
210 assert.strictEqual( image.url, 'https://upload.wikimedia.org/wikipedia/commons/1/19/Stuff.jpg', 'url is set correctly' );
211 assert.strictEqual( image.descriptionUrl, 'https://commons.wikimedia.org/wiki/File:Stuff.jpg', 'descriptionUrl is set correctly' );
212 assert.strictEqual( image.repo, 'shared', 'repo is set correctly' );
213 assert.strictEqual( image.uploadDateTime, '2013-08-25T14:41:02Z', 'uploadDateTime is set correctly' );
214 assert.strictEqual( image.anonymizedUploadDateTime, '20130825000000', 'anonymizedUploadDateTime is set correctly' );
215 assert.strictEqual( image.creationDateTime, '2009-02-18', 'creationDateTime is set correctly' );
216 assert.strictEqual( image.description, 'Wikis stuff', 'description is set correctly' );
217 assert.strictEqual( image.source, 'Wikipedia', 'source is set correctly' );
218 assert.strictEqual( image.author, 'John Smith', 'author is set correctly' );
219 assert.strictEqual( image.authorCount, 2, 'author count is set correctly' );
220 assert.strictEqual( image.attribution, 'By John Smith', 'attribution is set correctly' );
221 assert.strictEqual( image.license.shortName, 'CC0', 'license short name is set correctly' );
222 assert.strictEqual( image.license.internalName, 'cc0', 'license internal name is set correctly' );
223 assert.strictEqual( image.license.longName, 'Creative Commons Public Domain Dedication', 'license long name is set correctly' );
224 assert.strictEqual( image.license.deedUrl, 'http://creativecommons.org/publicdomain/zero/1.0/', 'license URL is set correctly' );
225 assert.strictEqual( image.license.attributionRequired, false, 'Attribution required flag is honored' );
226 assert.strictEqual( image.license.nonFree, true, 'Non-free flag is honored' );
227 assert.strictEqual( image.permission, 'Do not use. Ever.', 'permission is set correctly' );
228 assert.strictEqual( image.deletionReason, 'copyvio', 'permission is set correctly' );
229 assert.strictEqual( image.latitude, 90, 'latitude is set correctly' );
230 assert.strictEqual( image.longitude, 180, 'longitude is set correctly' );
231 assert.deepEqual( image.restrictions, [ 'trademarked', 'insignia' ], 'restrictions is set correctly' );
232 } ).then(
233 // call the data provider a second time to check caching
234 () => imageInfoProvider.get( file )
235 ).then( () => {
236 assert.strictEqual( apiCallCount, 1 );
237 } );
238 } );
239
240 QUnit.test( 'ImageInfo fail test', ( assert ) => {
241 const api = { get: function () {
242 return $.Deferred().resolve( {} );
243 } };
244 const file = new mw.Title( 'File:Stuff.jpg' );
245 const done = assert.async();
246 const imageInfoProvider = new ImageInfo( api );
247
248 imageInfoProvider.get( file ).fail( () => {
249 assert.true( true, 'promise rejected when no data is returned' );
250 done();
251 } );
252 } );
253
254 QUnit.test( 'ImageInfo fail test 2', ( assert ) => {
255 const api = { get: function () {
256 return $.Deferred().resolve( {
257 query: {
258 pages: [
259 {
260 title: 'File:Stuff.jpg'
261 }
262 ]
263 }
264 } );
265 } };
266 const file = new mw.Title( 'File:Stuff.jpg' );
267 const done = assert.async();
268 const imageInfoProvider = new ImageInfo( api );
269
270 imageInfoProvider.get( file ).fail( () => {
271 assert.true( true, 'promise rejected when imageinfo is missing' );
272 done();
273 } );
274 } );
275
276 QUnit.test( 'ImageInfo missing page test', ( assert ) => {
277 const api = { get: function () {
278 return $.Deferred().resolve( {
279 query: {
280 pages: [
281 {
282 title: 'File:Stuff.jpg',
283 missing: true,
284 imagerepository: ''
285 }
286 ]
287 }
288 } );
289 } };
290 const file = new mw.Title( 'File:Stuff.jpg' );
291 const done = assert.async();
292 const imageInfoProvider = new ImageInfo( api );
293
294 imageInfoProvider.get( file ).fail( ( errorMessage ) => {
295 assert.strictEqual( errorMessage, 'file does not exist: File:Stuff.jpg',
296 'error message is set correctly for missing file' );
297 done();
298 } );
299 } );
300 }() );