Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js

raw symbols SCIP
1 const { Config } = require( 'mmv.bootstrap' );
2 const { MetadataPanel, License } = require( 'mmv' );
3
4 const mwMessagesExists = mw.messages.exists;
5 QUnit.module( 'mmv.ui.metadataPanel', QUnit.newMwEnvironment( {
6 beforeEach: () => {
7 // mock mw.messages.exists for License.getShortLink (multimediaviewer-license-cc-by-2.0)
8 mw.messages.exists = () => true;
9 },
10 afterEach: () => {
11 mw.messages.exists = mwMessagesExists;
12 }
13 } ) );
14
15 QUnit.test( '.empty()', ( assert ) => {
16 const $qf = $( '#qunit-fixture' );
17 const panel = new MetadataPanel(
18 $qf,
19 $( '<div>' ).appendTo( $qf ),
20 new Config()
21 );
22 panel.empty();
23
24 [
25 '$license',
26 '$title',
27 '$location',
28 '$datetimeCreated',
29 '$datetimeUpdated'
30 ].forEach( ( thing ) => {
31 assert.strictEqual( panel[ thing ].text(), '', thing + ' empty text' );
32 } );
33
34 [
35 '$licenseLi',
36 '$credit',
37 '$locationLi',
38 '$datetimeCreatedLi',
39 '$datetimeUpdatedLi'
40 ].forEach( ( thing ) => {
41 assert.true( panel[ thing ].hasClass( 'empty' ), thing + ' empty class' );
42 } );
43 } );
44
45 QUnit.test( '.setLocationData()', ( assert ) => {
46 const $qf = $( '#qunit-fixture' );
47 const panel = new MetadataPanel(
48 $qf,
49 $( '<div>' ).appendTo( $qf ),
50 new Config()
51 );
52 const fileName = 'Foobar.jpg';
53 let latitude = 12.3456789;
54 let longitude = 98.7654321;
55 const imageData = {
56 latitude: latitude,
57 longitude: longitude,
58 hasCoords: () => true,
59 title: mw.Title.newFromText( 'File:Foobar.jpg' )
60 };
61
62 panel.setLocationData( imageData );
63
64 assert.strictEqual(
65 panel.$location.text(),
66 '(multimediaviewer-geolocation: (multimediaviewer-geoloc-coords: (multimediaviewer-geoloc-coord: 12, 20, 44.44, (multimediaviewer-geoloc-north)), (multimediaviewer-geoloc-coord: 98, 45, 55.56, (multimediaviewer-geoloc-east))))',
67 'Location text is set as expected - if this fails it may be due to i18n issues.'
68 );
69 assert.strictEqual(
70 panel.$location.prop( 'href' ),
71 'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '&params=' + latitude + '_N_' + longitude + '_E_&language=qqx',
72 'Location URL is set as expected'
73 );
74
75 latitude = -latitude;
76 longitude = -longitude;
77 imageData.latitude = latitude;
78 imageData.longitude = longitude;
79 panel.setLocationData( imageData );
80
81 assert.strictEqual(
82 panel.$location.text(),
83 '(multimediaviewer-geolocation: (multimediaviewer-geoloc-coords: (multimediaviewer-geoloc-coord: 12, 20, 44.44, (multimediaviewer-geoloc-south)), (multimediaviewer-geoloc-coord: 98, 45, 55.56, (multimediaviewer-geoloc-west))))',
84 'Location text is set as expected - if this fails it may be due to i18n issues.'
85 );
86 assert.strictEqual(
87 panel.$location.prop( 'href' ),
88 'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '&params=' + ( -latitude ) + '_S_' + ( -longitude ) + '_W_&language=qqx',
89 'Location URL is set as expected'
90 );
91
92 latitude = 0;
93 longitude = 0;
94 imageData.latitude = latitude;
95 imageData.longitude = longitude;
96 panel.setLocationData( imageData );
97
98 assert.strictEqual(
99 panel.$location.text(),
100 '(multimediaviewer-geolocation: (multimediaviewer-geoloc-coords: (multimediaviewer-geoloc-coord: 0, 0, 0, (multimediaviewer-geoloc-north)), (multimediaviewer-geoloc-coord: 0, 0, 0, (multimediaviewer-geoloc-east))))',
101 'Location text is set as expected - if this fails it may be due to i18n issues.'
102 );
103 assert.strictEqual(
104 panel.$location.prop( 'href' ),
105 'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '&params=' + latitude + '_N_' + longitude + '_E_&language=qqx',
106 'Location URL is set as expected'
107 );
108 } );
109
110 QUnit.test( '.setImageInfo()', function ( assert ) {
111 const $qf = $( '#qunit-fixture' );
112 const panel = new MetadataPanel(
113 $qf,
114 $( '<div>' ).appendTo( $qf ),
115 new Config()
116 );
117 const title = 'Foo bar';
118 const image = {
119 filePageTitle: mw.Title.newFromText( 'File:' + title + '.jpg' ),
120 src: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'
121 };
122 const imageData = {
123 title: image.filePageTitle,
124 url: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
125 descriptionUrl: 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
126 hasCoords: () => false
127 };
128 const clock = this.sandbox.useFakeTimers();
129
130 panel.setImageInfo( image, imageData );
131
132 assert.strictEqual( panel.$title.text(), title, 'Title is correctly set' );
133 assert.notStrictEqual( panel.$credit.text(), '', 'Default credit is shown' );
134 assert.strictEqual( panel.$license.prop( 'href' ),
135 imageData.descriptionUrl + '?uselang=qqx#(license-header)',
136 'User is directed to file page for license information' );
137 assert.strictEqual( panel.$license.prop( 'target' ), '', 'License information opens in same window' );
138 assert.true( panel.$datetimeCreatedLi.hasClass( 'empty' ), 'Date/Time is empty' );
139 assert.true( panel.$datetimeUpdatedLi.hasClass( 'empty' ), 'Date/Time is empty' );
140 assert.true( panel.$locationLi.hasClass( 'empty' ), 'Location is empty' );
141
142 imageData.creationDateTime = '2013-08-26T14:41:02Z';
143 imageData.uploadDateTime = '2013-08-25T14:41:02Z';
144 imageData.source = '<b>Lost</b><a href="foo">Bar</a>';
145 imageData.author = 'Bob';
146 imageData.license = new License( 'CC-BY-2.0', 'cc-by-2.0',
147 'Creative Commons Attribution - Share Alike 2.0',
148 'http://creativecommons.org/licenses/by-sa/2.0/' );
149 imageData.restrictions = [ 'trademarked', 'default', 'insignia' ];
150
151 panel.setImageInfo( image, imageData );
152 const creditPopupText = panel.creditField.$element.attr( 'original-title' );
153 clock.tick( 10 );
154
155 assert.strictEqual( panel.$title.text(), title, 'Title is correctly set' );
156 assert.false( panel.$credit.hasClass( 'empty' ), 'Credit is not empty' );
157 assert.false( panel.$datetimeCreatedLi.hasClass( 'empty' ), 'Date/Time is not empty' );
158 assert.strictEqual( panel.creditField.$element.find( '.mw-mmv-author' ).text(), imageData.author, 'Author text is correctly set' );
159 assert.strictEqual( panel.creditField.$element.find( '.mw-mmv-source' ).html(), '<b>Lost</b><a href="foo">Bar</a>', 'Source text is correctly set' );
160 // Either multimediaviewer-credit-popup-text or multimediaviewer-credit-popup-text-more.
161 assert.true( creditPopupText === '(multimediaviewer-credit-popup-text)' || creditPopupText === '(multimediaviewer-credit-popup-text-more)', 'Source tooltip is correctly set' );
162 assert.strictEqual( panel.$datetimeCreated.text(), '(multimediaviewer-datetime-created: 26 August 2013)', 'Correct date is displayed' );
163 assert.strictEqual( panel.$license.text(), '(multimediaviewer-license-cc-by-2.0)', 'License is correctly set' );
164 assert.strictEqual( panel.$license.prop( 'target' ), '_blank', 'License information opens in new window' );
165 assert.true( panel.$restrictions.children().last().children().hasClass( 'mw-mmv-restriction-default' ), 'Default restriction is correctly displayed last' );
166
167 imageData.creationDateTime = undefined;
168 panel.setImageInfo( image, imageData );
169 clock.tick( 10 );
170
171 assert.false( panel.$datetimeUpdatedLi.hasClass( 'empty' ), 'Date/Time is not empty' );
172 assert.strictEqual( panel.$datetimeUpdated.text(), '(multimediaviewer-datetime-uploaded: 25 August 2013)', 'Correct date is displayed' );
173
174 clock.restore();
175 } );
176
177 // FIXME: test broken since migrating to require/packageFiles
178 QUnit.skip( 'Setting permission information works as expected', ( assert ) => {
179 const $qf = $( '#qunit-fixture' );
180 const panel = new MetadataPanel(
181 $qf,
182 $( '<div>' ).appendTo( $qf ),
183 new Config()
184 );
185
186 // make sure license is visible as it contains the permission
187 panel.setLicense( null, 'http://example.com' );
188 panel.setPermission( 'Look at me, I am a permission!' );
189 assert.true( panel.$permissionLink.is( ':visible' ) );
190 } );
191
192 QUnit.test( 'Date formatting', ( assert ) => {
193 const $qf = $( '#qunit-fixture' );
194 const panel = new MetadataPanel(
195 $qf,
196 $( '<div>' ).appendTo( $qf ),
197 new Config()
198 );
199 const date1 = 'Garbage';
200 const result = panel.formatDate( date1 );
201
202 assert.strictEqual( result, date1, 'Invalid date is correctly ignored' );
203 } );