Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/model/mmv.model.Image.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer 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 * MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { ImageModel, License } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.model.Image', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'Image model constructor sense check', ( assert ) => {
24 const title = mw.Title.newFromText( 'File:Foobar.jpg' );
25 const name = 'Foo bar';
26 const size = 100;
27 const width = 10;
28 const height = 15;
29 const mime = 'image/jpeg';
30 const url = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
31 const pageID = 42;
32 const descurl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
33 const descShortUrl = '';
34 const repo = 'wikimediacommons';
35 const datetime = '2011-07-04T23:31:14Z';
36 const anondatetime = '20110704000000';
37 const origdatetime = '2010-07-04T23:31:14Z';
38 const description = 'This is a test file.';
39 const source = 'WMF';
40 const author = 'Ryan Kaldari';
41 const authorCount = 1;
42 const permission = 'only use for good, not evil';
43 const deletionReason = 'poor quality';
44 const license = new License( 'cc0' );
45 const attribution = 'Created by my cats on a winter morning';
46 const latitude = 39.12381283;
47 const longitude = 100.983829;
48 const restrictions = [ 'trademarked' ];
49 const imageData = new ImageModel(
50 title, name, size, width, height, mime, url,
51 descurl, descShortUrl, pageID, repo, datetime, anondatetime, origdatetime,
52 description, source, author, authorCount, license, permission, attribution,
53 deletionReason, latitude, longitude, restrictions );
54
55 assert.strictEqual( imageData.title, title, 'Title is set correctly' );
56 assert.strictEqual( imageData.name, name, 'Name is set correctly' );
57 assert.strictEqual( imageData.size, size, 'Size is set correctly' );
58 assert.strictEqual( imageData.width, width, 'Width is set correctly' );
59 assert.strictEqual( imageData.height, height, 'Height is set correctly' );
60 assert.strictEqual( imageData.mimeType, mime, 'MIME type is set correctly' );
61 assert.strictEqual( imageData.url, url, 'URL for original image is set correctly' );
62 assert.strictEqual( imageData.descriptionUrl, descurl, 'URL for image description page is set correctly' );
63 assert.strictEqual( imageData.pageID, pageID, 'Page ID of image description is set correctly' );
64 assert.strictEqual( imageData.repo, repo, 'Repository name is set correctly' );
65 assert.strictEqual( imageData.uploadDateTime, datetime, 'Date and time of last upload is set correctly' );
66 assert.strictEqual( imageData.anonymizedUploadDateTime, anondatetime, 'Anonymized date and time of last upload is set correctly' );
67 assert.strictEqual( imageData.creationDateTime, origdatetime, 'Date and time of original upload is set correctly' );
68 assert.strictEqual( imageData.description, description, 'Description is set correctly' );
69 assert.strictEqual( imageData.source, source, 'Source is set correctly' );
70 assert.strictEqual( imageData.author, author, 'Author is set correctly' );
71 assert.strictEqual( imageData.authorCount, authorCount, 'Author is set correctly' );
72 assert.strictEqual( imageData.license, license, 'License is set correctly' );
73 assert.strictEqual( imageData.permission, permission, 'Permission is set correctly' );
74 assert.strictEqual( imageData.attribution, attribution, 'Attribution is set correctly' );
75 assert.strictEqual( imageData.deletionReason, deletionReason, 'Deletion reason is set correctly' );
76 assert.strictEqual( imageData.latitude, latitude, 'Latitude is set correctly' );
77 assert.strictEqual( imageData.longitude, longitude, 'Longitude is set correctly' );
78 assert.deepEqual( imageData.restrictions, restrictions, 'Restrictions is set correctly' );
79 assert.true( $.isPlainObject( imageData.thumbUrls ), 'Thumb URL cache is set up properly' );
80 } );
81
82 QUnit.test( 'hasCoords()', ( assert ) => {
83 const firstImageData = new ImageModel(
84 mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), 'Foo bar',
85 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com', 42,
86 'example', 'tester', '2013-11-10', '20131110', '2013-11-09', 'Blah blah blah',
87 'A person', 'Another person', 1, 'CC-BY-SA-3.0', 'Permitted', 'My cat'
88 );
89 const secondImageData = new ImageModel(
90 mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), 'Foo bar',
91 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com', 42,
92 'example', 'tester', '2013-11-10', '20131110', '2013-11-09', 'Blah blah blah',
93 'A person', 'Another person', 1, 'CC-BY-SA-3.0', 'Permitted', 'My cat',
94 undefined, '39.91820938', '78.09812938'
95 );
96
97 assert.strictEqual( firstImageData.hasCoords(), false, 'No coordinates present means hasCoords returns false.' );
98 assert.strictEqual( secondImageData.hasCoords(), true, 'Coordinates present means hasCoords returns true.' );
99 } );
100
101 QUnit.test( 'parseExtmeta()', ( assert ) => {
102 const stringData = { value: 'foo' };
103 const plaintextData = { value: 'fo<b>o</b>' };
104 const integerData = { value: 3 };
105 const integerStringData = { value: '3' };
106 const zeroPrefixedIntegerStringData = { value: '03' };
107 const floatData = { value: 1.23 };
108 const floatStringData = { value: '1.23' };
109 const booleanData = { value: 'yes' };
110 const wrongBooleanData = { value: 'blah' };
111 const listDataEmpty = { value: '' };
112 const listDataSingle = { value: 'foo' };
113 const listDataMultiple = { value: 'foo|bar|baz' };
114 const missingData = undefined;
115
116 assert.strictEqual( ImageModel.parseExtmeta( stringData, 'string' ), 'foo',
117 'Extmeta string parsed correctly.' );
118 assert.strictEqual( ImageModel.parseExtmeta( plaintextData, 'plaintext' ), 'foo',
119 'Extmeta plaintext parsed correctly.' );
120 assert.strictEqual( ImageModel.parseExtmeta( floatData, 'float' ), 1.23,
121 'Extmeta float parsed correctly.' );
122 assert.strictEqual( ImageModel.parseExtmeta( floatStringData, 'float' ), 1.23,
123 'Extmeta float string parsed correctly.' );
124 assert.strictEqual( ImageModel.parseExtmeta( booleanData, 'boolean' ), true,
125 'Extmeta boolean string parsed correctly.' );
126 assert.strictEqual( ImageModel.parseExtmeta( wrongBooleanData, 'boolean' ), undefined,
127 'Extmeta boolean string with error ignored.' );
128 assert.strictEqual( ImageModel.parseExtmeta( integerData, 'integer' ), 3,
129 'Extmeta integer parsed correctly.' );
130 assert.strictEqual( ImageModel.parseExtmeta( integerStringData, 'integer' ), 3,
131 'Extmeta integer string parsed correctly.' );
132 assert.strictEqual( ImageModel.parseExtmeta( zeroPrefixedIntegerStringData, 'integer' ), 3,
133 'Extmeta zero-prefixed integer string parsed correctly.' );
134 assert.deepEqual( ImageModel.parseExtmeta( listDataEmpty, 'list' ), [],
135 'Extmeta empty list parsed correctly.' );
136 assert.deepEqual( ImageModel.parseExtmeta( listDataSingle, 'list' ), [ 'foo' ],
137 'Extmeta list with single element parsed correctly.' );
138 assert.deepEqual( ImageModel.parseExtmeta( listDataMultiple, 'list' ), [ 'foo', 'bar', 'baz' ],
139 'Extmeta list with multiple elements parsed correctly.' );
140 assert.strictEqual( ImageModel.parseExtmeta( missingData, 'string' ), undefined,
141 'Extmeta missing data parsed correctly.' );
142
143 assert.strictEqual( ImageModel.parseExtmeta( { value: '1960-03-14' }, 'datetime' ), '1960-03-14',
144 'Extmeta date is parsed correctly.' );
145 assert.strictEqual( ImageModel.parseExtmeta( { value: '1960' }, 'datetime' ), '1960',
146 'Extmeta year is parsed correctly.' );
147 assert.strictEqual( ImageModel.parseExtmeta( { value: '1926<div style="display: none;">date QS:P571,+1926-00-00T00:00:00Z/9</div>' }, 'datetime' ), '1926',
148 'Extmeta year is extracted from hidden div.' );
149
150 assert.throws( () => {
151 ImageModel.parseExtmeta( stringData, 'strong' );
152 }, 'Exception is thrown on invalid argument' );
153 } );
154
155 }() );