2023-05-20 08:30:52 +00:00
const { License , ImageModel , Repo } = require ( 'mmv' ) ;
const { EmbedFileFormatter } = require ( 'mmv.ui.ondemandshareddependencies' ) ;
2018-11-12 16:33:24 +00:00
( function ( ) {
2014-03-19 02:00:26 +00:00
QUnit . module ( 'mmv.EmbedFileFormatter' , QUnit . newMwEnvironment ( ) ) ;
2014-03-21 20:29:55 +00:00
function createEmbedFileInfo ( options ) {
2023-10-24 09:53:23 +00:00
const license = options . licenseShortName ?
new License (
options . licenseShortName ,
options . licenseInternalName ,
options . licenseLongName ,
options . licenseUrl
) : undefined ;
const imageInfo = new ImageModel (
options . title ,
options . title . getNameText ( ) ,
undefined ,
undefined ,
undefined ,
undefined ,
options . imgUrl ,
options . filePageUrl ,
options . shortFilePageUrl ,
42 ,
'repo' ,
undefined ,
undefined ,
undefined ,
undefined ,
options . source ,
options . author ,
options . authorCount ,
license
) ;
const repoInfo = {
displayName : options . siteName ,
2024-02-13 00:44:51 +00:00
getSiteLink : function ( ) {
return options . siteUrl ;
}
2023-10-24 09:53:23 +00:00
} ;
2014-03-21 20:29:55 +00:00
2019-02-05 21:52:48 +00:00
return {
imageInfo : imageInfo ,
repoInfo : repoInfo ,
caption : options . caption
} ;
2014-03-21 20:29:55 +00:00
}
2021-12-10 00:43:28 +00:00
QUnit . test ( 'EmbedFileFormatter constructor sense check' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
2023-05-20 08:30:52 +00:00
assert . true ( formatter instanceof EmbedFileFormatter , 'constructor with no argument works' ) ;
2014-03-19 02:00:26 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getByline():' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
const author = '<span class="mw-mmv-author">Homer</span>' ;
const source = '<span class="mw-mmv-source">Iliad</span>' ;
const attribution = '<span class="mw-mmv-attr">Cat</span>' ;
2014-03-19 03:05:36 +00:00
// Works with no arguments
2023-10-24 09:53:23 +00:00
let byline = formatter . getByline ( ) ;
2014-03-21 20:29:55 +00:00
assert . strictEqual ( byline , undefined , 'No argument case handled correctly.' ) ;
2014-03-19 03:05:36 +00:00
2014-12-28 21:04:22 +00:00
// Attribution present
byline = formatter . getByline ( author , source , attribution ) ;
2024-05-05 08:09:40 +00:00
assert . strictEqual ( byline , '<span class="mw-mmv-attr">Cat</span>' , 'Attribution found in bylines' ) ;
2014-12-28 21:04:22 +00:00
2014-03-19 03:05:36 +00:00
// Author and source present
2014-03-21 20:29:55 +00:00
byline = formatter . getByline ( author , source ) ;
2024-05-05 08:09:40 +00:00
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' ) ;
2014-03-19 03:05:36 +00:00
// Only author present
2014-03-21 20:29:55 +00:00
byline = formatter . getByline ( author ) ;
2024-05-05 08:09:40 +00:00
assert . strictEqual ( byline , '<span class="mw-mmv-author">Homer</span>' , 'Author found in bylines.' ) ;
2014-03-19 03:05:36 +00:00
// Only source present
2014-03-21 20:29:55 +00:00
byline = formatter . getByline ( undefined , source ) ;
2024-05-05 08:09:40 +00:00
assert . strictEqual ( byline , '<span class="mw-mmv-source">Iliad</span>' , 'Source found in bylines.' ) ;
2014-03-21 20:29:55 +00:00
} ) ;
2014-03-19 03:05:36 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getSiteLink():' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const repoInfo = new Repo ( 'Wikipedia' , '//wikipedia.org/favicon.ico' , true ) ;
const info = { imageInfo : { } , repoInfo : repoInfo } ;
const formatter = new EmbedFileFormatter ( ) ;
const siteUrl = repoInfo . getSiteLink ( ) ;
const siteLink = formatter . getSiteLink ( info ) ;
2014-03-21 20:29:55 +00:00
2021-04-16 07:31:44 +00:00
assert . notStrictEqual ( siteLink . indexOf ( 'Wikipedia' ) , - 1 , 'Site name is present in site link' ) ;
assert . notStrictEqual ( siteLink . indexOf ( siteUrl ) , - 1 , 'Site URL is present in site link' ) ;
2014-03-19 03:05:36 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getThumbnailHtml():' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
const titleText = 'Music Room' ;
const title = mw . Title . newFromText ( titleText ) ;
const imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg' ;
const filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg' ;
const filePageShortUrl = 'https://commons.wikimedia.org/wiki/index.php?curid=42' ;
const siteName = 'Site Name' ;
const siteUrl = '//site.url/' ;
const licenseShortName = 'Public License' ;
const licenseInternalName = '-' ;
const licenseLongName = 'Public Domain, copyrights have lapsed' ;
const licenseUrl = '//example.com/pd' ;
const author = '<span class="mw-mmv-author">Homer</span>' ;
const source = '<span class="mw-mmv-source">Iliad</span>' ;
const thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg' ;
const width = 700 ;
const height = 500 ;
2014-03-19 03:05:36 +00:00
// Bylines, license and site
2023-10-24 09:53:23 +00:00
let info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl ,
2015-12-24 20:35:52 +00:00
shortFilePageUrl : filePageShortUrl , siteName : siteName , siteUrl : siteUrl ,
licenseShortName : licenseShortName , licenseInternalName : licenseInternalName ,
licenseLongName : licenseLongName , licenseUrl : licenseUrl , author : author , source : source } ) ;
2014-03-19 03:05:36 +00:00
2023-10-24 09:53:23 +00:00
let generatedHtml = formatter . getThumbnailHtml ( info , thumbUrl , width , height ) ;
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . match ( titleText ) , null , 'Title appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( filePageUrl ) , null , 'Page url appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( thumbUrl ) , null , 'Thumbnail url appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Public License' ) , null , 'License appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Homer' ) , null , 'Author appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Iliad' ) , null , 'Source appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( width ) , null , 'Width appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( height ) , null , 'Height appears in generated HTML' ) ;
2015-12-24 20:35:52 +00:00
// .includes() for checking the short url since it contains a ? (bad for regex). Could escape instead.
2023-06-27 20:10:10 +00:00
// eslint-disable-next-line es-x/no-array-prototype-includes
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . includes ( filePageShortUrl ) , null , 'Short URL appears in generated HTML' ) ;
2014-03-19 03:05:36 +00:00
// Bylines, no license and site
2014-03-21 20:29:55 +00:00
info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl ,
2015-12-24 20:35:52 +00:00
shortFilePageUrl : filePageShortUrl , siteName : siteName , siteUrl : siteUrl ,
2014-03-21 20:29:55 +00:00
author : author , source : source } ) ;
2015-01-23 12:48:27 +00:00
generatedHtml = formatter . getThumbnailHtml ( info , thumbUrl , width , height ) ;
2014-03-19 03:05:36 +00:00
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . match ( titleText ) , null , 'Title appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( filePageUrl ) , null , 'Page url appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( thumbUrl ) , null , 'Thumbnail url appears in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Public License' ) , null , 'License should not appear in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Homer' ) , null , 'Author appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Iliad' ) , null , 'Source appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( width ) , null , 'Width appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( height ) , null , 'Height appears in generated HTML' ) ;
2023-06-27 20:10:10 +00:00
// eslint-disable-next-line es-x/no-array-prototype-includes
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . includes ( filePageShortUrl ) , null , 'Short URL appears in generated HTML' ) ;
2014-03-19 03:05:36 +00:00
// No bylines, license and site
2014-03-21 20:29:55 +00:00
info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl ,
siteName : siteName , siteUrl : siteUrl , licenseShortName : licenseShortName ,
licenseInternalName : licenseInternalName , licenseLongName : licenseLongName ,
2015-12-24 20:35:52 +00:00
licenseUrl : licenseUrl , shortFilePageUrl : filePageShortUrl } ) ;
2015-01-23 12:48:27 +00:00
generatedHtml = formatter . getThumbnailHtml ( info , thumbUrl , width , height ) ;
2014-03-19 03:05:36 +00:00
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . match ( titleText ) , null , 'Title appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( filePageUrl ) , null , 'Page url appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( thumbUrl ) , null , 'Thumbnail url appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( 'Public License' ) , null , 'License appears in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Homer' ) , null , 'Author should not appear in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Iliad' ) , null , 'Source should not appear in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( width ) , null , 'Width appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( height ) , null , 'Height appears in generated HTML' ) ;
2023-06-27 20:10:10 +00:00
// eslint-disable-next-line es-x/no-array-prototype-includes
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . includes ( filePageShortUrl ) , null , 'Short URL appears in generated HTML' ) ;
2014-03-19 03:05:36 +00:00
// No bylines, no license and site
2014-03-21 20:29:55 +00:00
info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl ,
2015-12-24 20:35:52 +00:00
siteName : siteName , siteUrl : siteUrl , shortFilePageUrl : filePageShortUrl } ) ;
2015-01-23 12:48:27 +00:00
generatedHtml = formatter . getThumbnailHtml ( info , thumbUrl , width , height ) ;
2014-03-19 03:05:36 +00:00
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . match ( titleText ) , null , 'Title appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( filePageUrl ) , null , 'Page url appears in generated HTML.' ) ;
assert . notStrictEqual ( generatedHtml . match ( thumbUrl ) , null , 'Thumbnail url appears in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Public License' ) , null , 'License should not appear in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Homer' ) , null , 'Author should not appear in generated HTML' ) ;
assert . strictEqual ( generatedHtml . match ( 'Iliad' ) , null , 'Source should not appear in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( width ) , null , 'Width appears in generated HTML' ) ;
assert . notStrictEqual ( generatedHtml . match ( height ) , null , 'Height appears in generated HTML' ) ;
2023-06-27 20:10:10 +00:00
// eslint-disable-next-line es-x/no-array-prototype-includes
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( generatedHtml . includes ( filePageShortUrl ) , null , 'Short URL appears in generated HTML' ) ;
2014-03-19 03:05:36 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getThumbnailWikitext():' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
const title = mw . Title . newFromText ( 'File:Foobar.jpg' ) ;
const imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg' ;
const filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg' ;
const caption = 'Foobar caption.' ;
const width = 700 ;
2014-03-19 02:00:26 +00:00
// Title, width and caption
2023-10-24 09:53:23 +00:00
let info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl ,
2014-03-21 20:29:55 +00:00
caption : caption } ) ;
2023-10-24 09:53:23 +00:00
let wikitext = formatter . getThumbnailWikitextFromEmbedFileInfo ( info , width ) ;
2014-03-19 02:00:26 +00:00
assert . strictEqual (
wikitext ,
'[[File:Foobar.jpg|700px|thumb|Foobar caption.]]' ,
'Wikitext generated correctly.' ) ;
// Title, width and no caption
2014-03-21 20:29:55 +00:00
info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl } ) ;
2016-07-18 13:49:27 +00:00
wikitext = formatter . getThumbnailWikitextFromEmbedFileInfo ( info , width ) ;
2014-03-19 02:00:26 +00:00
assert . strictEqual (
wikitext ,
'[[File:Foobar.jpg|700px|thumb|Foobar]]' ,
'Wikitext generated correctly.' ) ;
// Title, no width and no caption
2014-03-21 20:29:55 +00:00
info = createEmbedFileInfo ( { title : title , imgUrl : imgUrl , filePageUrl : filePageUrl } ) ;
2014-03-19 02:00:26 +00:00
wikitext = formatter . getThumbnailWikitextFromEmbedFileInfo ( info ) ;
assert . strictEqual (
wikitext ,
'[[File:Foobar.jpg|thumb|Foobar]]' ,
'Wikitext generated correctly.' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getCreditText():' , function ( assert ) {
2024-05-05 08:09:40 +00:00
const author = '<span class="mw-mmv-author">Homer</span>' ;
const source = '<span class="mw-mmv-source">Iliad</span>' ;
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
2014-06-12 23:59:06 +00:00
2023-10-24 09:53:23 +00:00
let txt = formatter . getCreditText ( {
2014-06-12 23:59:06 +00:00
repoInfo : {
displayName : 'Localcommons'
} ,
imageInfo : {
2024-05-05 08:09:40 +00:00
author ,
source ,
2015-12-24 20:35:52 +00:00
descriptionShortUrl : 'link' ,
2014-06-12 23:59:06 +00:00
title : {
2024-02-13 00:44:51 +00:00
getNameText : function ( ) {
return 'Image Title' ;
}
2014-06-12 23:59:06 +00:00
}
}
} ) ;
2024-05-05 08:09:40 +00:00
assert . strictEqual ( txt , '(multimediaviewer-text-embed-credit-text-b: (multimediaviewer-credit: Homer, Iliad), link)' , 'Sense check' ) ;
2014-06-12 23:59:06 +00:00
txt = formatter . getCreditText ( {
repoInfo : {
displayName : 'Localcommons'
} ,
imageInfo : {
2024-05-05 08:09:40 +00:00
author ,
source ,
2015-12-24 20:35:52 +00:00
descriptionShortUrl : 'link' ,
2014-06-12 23:59:06 +00:00
title : {
2024-02-13 00:44:51 +00:00
getNameText : function ( ) {
return 'Image Title' ;
}
2014-06-12 23:59:06 +00:00
} ,
license : {
2024-02-13 00:44:51 +00:00
getShortName : function ( ) {
return 'WTFPL v2' ;
} ,
2015-12-24 20:35:52 +00:00
longName : 'Do What the Fuck You Want Public License Version 2' ,
2014-11-24 22:09:03 +00:00
isFree : this . sandbox . stub ( ) . returns ( true )
2014-06-12 23:59:06 +00:00
}
}
} ) ;
2024-05-05 08:09:40 +00:00
assert . strictEqual ( txt , '(multimediaviewer-text-embed-credit-text-bl: (multimediaviewer-credit: Homer, Iliad), WTFPL v2, link)' , 'License message works' ) ;
2014-12-01 20:15:17 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'getCreditHtml():' , function ( assert ) {
2024-05-05 08:09:40 +00:00
const author = '<span class="mw-mmv-author">Homer</span>' ;
const source = '<span class="mw-mmv-source">Iliad</span>' ;
2023-10-24 09:53:23 +00:00
const formatter = new EmbedFileFormatter ( ) ;
2014-12-01 20:15:17 +00:00
2023-10-24 09:53:23 +00:00
let html = formatter . getCreditHtml ( {
2014-12-01 20:15:17 +00:00
repoInfo : {
displayName : 'Localcommons' ,
2024-02-13 00:44:51 +00:00
getSiteLink : function ( ) {
return 'quux' ;
}
2014-12-01 20:15:17 +00:00
} ,
imageInfo : {
2024-05-05 08:09:40 +00:00
author ,
source ,
2016-09-04 04:49:58 +00:00
descriptionShortUrl : 'some link' ,
2014-12-01 20:15:17 +00:00
title : {
2024-02-13 00:44:51 +00:00
getNameText : function ( ) {
return 'Image Title' ;
}
2014-12-01 20:15:17 +00:00
}
}
} ) ;
2020-06-12 07:48:11 +00:00
assert . strictEqual (
html ,
2024-05-05 08:09:40 +00:00
'(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>)' ,
2021-12-10 00:43:28 +00:00
'Sense check'
2020-06-12 07:48:11 +00:00
) ;
2014-12-01 20:15:17 +00:00
html = formatter . getCreditHtml ( {
repoInfo : {
displayName : 'Localcommons' ,
2024-02-13 00:44:51 +00:00
getSiteLink : function ( ) {
return 'quux' ;
}
2014-12-01 20:15:17 +00:00
} ,
imageInfo : {
2024-05-05 08:09:40 +00:00
author ,
source ,
2016-09-04 04:49:58 +00:00
descriptionShortUrl : 'some link' ,
2014-12-01 20:15:17 +00:00
title : {
2024-02-13 00:44:51 +00:00
getNameText : function ( ) {
return 'Image Title' ;
}
2014-12-01 20:15:17 +00:00
} ,
license : {
2024-02-13 00:44:51 +00:00
getShortLink : function ( ) {
return '<a href="http://www.wtfpl.net/">WTFPL v2</a>' ;
} ,
2015-12-24 20:35:52 +00:00
longName : 'Do What the Fuck You Want Public License Version 2' ,
2014-12-01 20:15:17 +00:00
isFree : this . sandbox . stub ( ) . returns ( true )
}
}
} ) ;
2020-06-12 07:48:11 +00:00
assert . strictEqual (
html ,
2024-05-05 08:09:40 +00:00
'(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>)' ,
2021-12-10 00:43:28 +00:00
'Sense check'
2020-06-12 07:48:11 +00:00
) ;
2014-06-12 23:59:06 +00:00
} ) ;
2018-11-12 16:33:24 +00:00
} ( ) ) ;