Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/mmv.EmbedFileFormatter.test.js

raw symbols SCIP
1 const { License, ImageModel } = require( 'mmv' );
2 const { EmbedFileFormatter } = require( 'mmv.ui.reuse' );
3
4 ( function () {
5 QUnit.module( 'mmv.EmbedFileFormatter', QUnit.newMwEnvironment() );
6
7 function createEmbedFileInfo( options ) {
8 const license = options.licenseShortName ?
9 new License(
10 options.licenseShortName,
11 options.licenseInternalName,
12 options.licenseLongName,
13 options.licenseUrl
14 ) : undefined;
15 const imageInfo = new ImageModel(
16 options.title,
17 options.title.getNameText(),
18 undefined,
19 undefined,
20 undefined,
21 undefined,
22 options.imgUrl,
23 options.filePageUrl,
24 options.shortFilePageUrl,
25 42,
26 'repo',
27 undefined,
28 undefined,
29 undefined,
30 undefined,
31 options.source,
32 options.author,
33 options.authorCount,
34 license
35 );
36 return {
37 imageInfo: imageInfo,
38 caption: options.caption
39 };
40 }
41
42 QUnit.test( 'EmbedFileFormatter constructor sense check', ( assert ) => {
43 const formatter = new EmbedFileFormatter();
44 assert.true( formatter instanceof EmbedFileFormatter, 'constructor with no argument works' );
45 } );
46
47 QUnit.test( 'getByline():', ( assert ) => {
48 const formatter = new EmbedFileFormatter();
49 const author = '<span class="mw-mmv-author">Homer</span>';
50 const source = '<span class="mw-mmv-source">Iliad</span>';
51 const attribution = '<span class="mw-mmv-attr">Cat</span>';
52
53 // Works with no arguments
54 let byline = formatter.getByline();
55 assert.strictEqual( byline, undefined, 'No argument case handled correctly.' );
56
57 // Attribution present
58 byline = formatter.getByline( author, source, attribution );
59 assert.strictEqual( byline, '<span class="mw-mmv-attr">Cat</span>', 'Attribution found in bylines' );
60
61 // Author and source present
62 byline = formatter.getByline( author, source );
63 assert.strictEqual( byline, '(multimediaviewer-credit: <span class="mw-mmv-author">Homer</span>, <span class="mw-mmv-source">Iliad</span>)', 'Author and source found in bylines' );
64
65 // Only author present
66 byline = formatter.getByline( author );
67 assert.strictEqual( byline, '<span class="mw-mmv-author">Homer</span>', 'Author found in bylines.' );
68
69 // Only source present
70 byline = formatter.getByline( undefined, source );
71 assert.strictEqual( byline, '<span class="mw-mmv-source">Iliad</span>', 'Source found in bylines.' );
72 } );
73
74 QUnit.test( 'getThumbnailHtml():', ( assert ) => {
75 const formatter = new EmbedFileFormatter();
76 const titleText = 'Music Room';
77 const title = mw.Title.newFromText( titleText );
78 const imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
79 const filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
80 const filePageShortUrl = 'https://commons.wikimedia.org/wiki/index.php?curid=42';
81 const siteName = 'Site Name';
82 const siteUrl = '//site.url/';
83 const licenseShortName = 'Public License';
84 const licenseInternalName = '-';
85 const licenseLongName = 'Public Domain, copyrights have lapsed';
86 const licenseUrl = '//example.com/pd';
87 const author = '<span class="mw-mmv-author">Homer</span>';
88 const source = '<span class="mw-mmv-source">Iliad</span>';
89 const thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg';
90 const width = 700;
91 const height = 500;
92
93 // Bylines, license and site
94 let info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
95 shortFilePageUrl: filePageShortUrl, siteName: siteName, siteUrl: siteUrl,
96 licenseShortName: licenseShortName, licenseInternalName: licenseInternalName,
97 licenseLongName: licenseLongName, licenseUrl: licenseUrl, author: author, source: source } );
98
99 let generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height );
100 assert.notStrictEqual( generatedHtml.match( titleText ), null, 'Title appears in generated HTML.' );
101 assert.notStrictEqual( generatedHtml.match( filePageUrl ), null, 'Page url appears in generated HTML.' );
102 assert.notStrictEqual( generatedHtml.match( thumbUrl ), null, 'Thumbnail url appears in generated HTML' );
103 assert.notStrictEqual( generatedHtml.match( 'Public License' ), null, 'License appears in generated HTML' );
104 assert.notStrictEqual( generatedHtml.match( 'Homer' ), null, 'Author appears in generated HTML' );
105 assert.notStrictEqual( generatedHtml.match( 'Iliad' ), null, 'Source appears in generated HTML' );
106 assert.notStrictEqual( generatedHtml.match( width ), null, 'Width appears in generated HTML' );
107 assert.notStrictEqual( generatedHtml.match( height ), null, 'Height appears in generated HTML' );
108 // .includes() for checking the short url since it contains a ? (bad for regex). Could escape instead.
109 // eslint-disable-next-line es-x/no-array-prototype-includes
110 assert.notStrictEqual( generatedHtml.includes( filePageShortUrl ), null, 'Short URL appears in generated HTML' );
111
112 // Bylines, no license and site
113 info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
114 shortFilePageUrl: filePageShortUrl, siteName: siteName, siteUrl: siteUrl,
115 author: author, source: source } );
116 generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height );
117
118 assert.notStrictEqual( generatedHtml.match( titleText ), null, 'Title appears in generated HTML.' );
119 assert.notStrictEqual( generatedHtml.match( filePageUrl ), null, 'Page url appears in generated HTML.' );
120 assert.notStrictEqual( generatedHtml.match( thumbUrl ), null, 'Thumbnail url appears in generated HTML' );
121 assert.strictEqual( generatedHtml.match( 'Public License' ), null, 'License should not appear in generated HTML' );
122 assert.notStrictEqual( generatedHtml.match( 'Homer' ), null, 'Author appears in generated HTML' );
123 assert.notStrictEqual( generatedHtml.match( 'Iliad' ), null, 'Source appears in generated HTML' );
124 assert.notStrictEqual( generatedHtml.match( width ), null, 'Width appears in generated HTML' );
125 assert.notStrictEqual( generatedHtml.match( height ), null, 'Height appears in generated HTML' );
126 // eslint-disable-next-line es-x/no-array-prototype-includes
127 assert.notStrictEqual( generatedHtml.includes( filePageShortUrl ), null, 'Short URL appears in generated HTML' );
128
129 // No bylines, license and site
130 info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
131 siteName: siteName, siteUrl: siteUrl, licenseShortName: licenseShortName,
132 licenseInternalName: licenseInternalName, licenseLongName: licenseLongName,
133 licenseUrl: licenseUrl, shortFilePageUrl: filePageShortUrl } );
134 generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height );
135
136 assert.notStrictEqual( generatedHtml.match( titleText ), null, 'Title appears in generated HTML.' );
137 assert.notStrictEqual( generatedHtml.match( filePageUrl ), null, 'Page url appears in generated HTML.' );
138 assert.notStrictEqual( generatedHtml.match( thumbUrl ), null, 'Thumbnail url appears in generated HTML' );
139 assert.notStrictEqual( generatedHtml.match( 'Public License' ), null, 'License appears in generated HTML' );
140 assert.strictEqual( generatedHtml.match( 'Homer' ), null, 'Author should not appear in generated HTML' );
141 assert.strictEqual( generatedHtml.match( 'Iliad' ), null, 'Source should not appear in generated HTML' );
142 assert.notStrictEqual( generatedHtml.match( width ), null, 'Width appears in generated HTML' );
143 assert.notStrictEqual( generatedHtml.match( height ), null, 'Height appears in generated HTML' );
144 // eslint-disable-next-line es-x/no-array-prototype-includes
145 assert.notStrictEqual( generatedHtml.includes( filePageShortUrl ), null, 'Short URL appears in generated HTML' );
146
147 // No bylines, no license and site
148 info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
149 siteName: siteName, siteUrl: siteUrl, shortFilePageUrl: filePageShortUrl } );
150 generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height );
151
152 assert.notStrictEqual( generatedHtml.match( titleText ), null, 'Title appears in generated HTML.' );
153 assert.notStrictEqual( generatedHtml.match( filePageUrl ), null, 'Page url appears in generated HTML.' );
154 assert.notStrictEqual( generatedHtml.match( thumbUrl ), null, 'Thumbnail url appears in generated HTML' );
155 assert.strictEqual( generatedHtml.match( 'Public License' ), null, 'License should not appear in generated HTML' );
156 assert.strictEqual( generatedHtml.match( 'Homer' ), null, 'Author should not appear in generated HTML' );
157 assert.strictEqual( generatedHtml.match( 'Iliad' ), null, 'Source should not appear in generated HTML' );
158 assert.notStrictEqual( generatedHtml.match( width ), null, 'Width appears in generated HTML' );
159 assert.notStrictEqual( generatedHtml.match( height ), null, 'Height appears in generated HTML' );
160 // eslint-disable-next-line es-x/no-array-prototype-includes
161 assert.notStrictEqual( generatedHtml.includes( filePageShortUrl ), null, 'Short URL appears in generated HTML' );
162
163 } );
164
165 QUnit.test( 'getThumbnailWikitext():', ( assert ) => {
166 const formatter = new EmbedFileFormatter();
167 const title = mw.Title.newFromText( 'File:Foobar.jpg' );
168 const imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
169 const filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
170 const caption = 'Foobar caption.';
171 const width = 700;
172
173 // Title, width and caption
174 let info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
175 caption: caption } );
176 let wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info, width );
177
178 assert.strictEqual(
179 wikitext,
180 '[[File:Foobar.jpg|700px|thumb|Foobar caption.]]',
181 'Wikitext generated correctly.' );
182
183 // Title, width and no caption
184 info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl } );
185 wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info, width );
186
187 assert.strictEqual(
188 wikitext,
189 '[[File:Foobar.jpg|700px|thumb|Foobar]]',
190 'Wikitext generated correctly.' );
191
192 // Title, no width and no caption
193 info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl } );
194 wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info );
195
196 assert.strictEqual(
197 wikitext,
198 '[[File:Foobar.jpg|thumb|Foobar]]',
199 'Wikitext generated correctly.' );
200 } );
201
202 QUnit.test( 'getCreditText():', function ( assert ) {
203 const author = '<span class="mw-mmv-author">Homer</span>';
204 const source = '<span class="mw-mmv-source">Iliad</span>';
205 const formatter = new EmbedFileFormatter();
206
207 let txt = formatter.getCreditText( {
208 author,
209 source,
210 descriptionShortUrl: 'link',
211 title: {
212 getNameText: function () {
213 return 'Image Title';
214 }
215 }
216 }
217 );
218
219 assert.strictEqual( txt, '(multimediaviewer-text-embed-credit-text-b: (multimediaviewer-credit: Homer, Iliad), link)', 'Sense check' );
220
221 txt = formatter.getCreditText( {
222 author,
223 source,
224 descriptionShortUrl: 'link',
225 title: {
226 getNameText: function () {
227 return 'Image Title';
228 }
229 },
230 license: {
231 getShortName: function () {
232 return 'WTFPL v2';
233 },
234 longName: 'Do What the Fuck You Want Public License Version 2',
235 isFree: this.sandbox.stub().returns( true )
236 }
237 }
238 );
239
240 assert.strictEqual( txt, '(multimediaviewer-text-embed-credit-text-bl: (multimediaviewer-credit: Homer, Iliad), WTFPL v2, link)', 'License message works' );
241 } );
242
243 QUnit.test( 'getCreditHtml():', function ( assert ) {
244 const author = '<span class="mw-mmv-author">Homer</span>';
245 const source = '<span class="mw-mmv-source">Iliad</span>';
246 const formatter = new EmbedFileFormatter();
247
248 let html = formatter.getCreditHtml( {
249 author,
250 source,
251 descriptionShortUrl: 'some link',
252 title: {
253 getNameText: function () {
254 return 'Image Title';
255 }
256 }
257 }
258 );
259
260 assert.strictEqual(
261 html,
262 '(multimediaviewer-html-embed-credit-text-b: (multimediaviewer-credit: <span class="mw-mmv-author">Homer</span>, <span class="mw-mmv-source">Iliad</span>), <a href="some link">(multimediaviewer-html-embed-credit-link-text)</a>)',
263 'Sense check'
264 );
265
266 html = formatter.getCreditHtml( {
267 author,
268 source,
269 descriptionShortUrl: 'some link',
270 title: {
271 getNameText: function () {
272 return 'Image Title';
273 }
274 },
275 license: {
276 getShortLink: function () {
277 return '<a href="http://www.wtfpl.net/">WTFPL v2</a>';
278 },
279 longName: 'Do What the Fuck You Want Public License Version 2',
280 isFree: this.sandbox.stub().returns( true )
281 }
282 }
283 );
284
285 assert.strictEqual(
286 html,
287 '(multimediaviewer-html-embed-credit-text-bl: (multimediaviewer-credit: <span class="mw-mmv-author">Homer</span>, <span class="mw-mmv-source">Iliad</span>), <a href="http://www.wtfpl.net/">WTFPL v2</a>, <a href="some link">(multimediaviewer-html-embed-credit-link-text)</a>)',
288 'Sense check'
289 );
290 } );
291 }() );