2014-03-21 20:29:55 +00:00
|
|
|
( function ( mw ) {
|
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 ) {
|
|
|
|
var license = options.licenseShortName ? new mw.mmv.model.License( options.licenseShortName,
|
|
|
|
options.licenseInternalName, options.licenseLongName, options.licenseUrl ) : undefined,
|
2015-12-24 15:01:56 +00:00
|
|
|
imageInfo = new mw.mmv.model.Image(
|
|
|
|
|
|
|
|
options.title,
|
|
|
|
options.title.getNameText(),
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
options.imgUrl,
|
|
|
|
options.filePageUrl,
|
2015-12-24 20:35:52 +00:00
|
|
|
options.shortFilePageUrl,
|
|
|
|
42,
|
2015-12-24 15:01:56 +00:00
|
|
|
'repo',
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
options.source,
|
2016-07-18 13:49:27 +00:00
|
|
|
options.author,
|
2015-12-24 15:01:56 +00:00
|
|
|
options.authorCount,
|
|
|
|
license ),
|
2014-03-21 20:29:55 +00:00
|
|
|
repoInfo = { displayName: options.siteName, getSiteLink:
|
|
|
|
function () { return options.siteUrl; } };
|
|
|
|
|
|
|
|
return new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, options.caption );
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
QUnit.test( 'EmbedFileFormatter constructor sanity check', 1, function ( assert ) {
|
|
|
|
var formatter = new mw.mmv.EmbedFileFormatter();
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( formatter, 'constructor with no argument works' );
|
2014-03-19 02:00:26 +00:00
|
|
|
} );
|
|
|
|
|
2014-12-28 21:04:22 +00:00
|
|
|
QUnit.test( 'getByline():', 5, function ( assert ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var formatter = new mw.mmv.EmbedFileFormatter(),
|
2014-03-31 21:33:12 +00:00
|
|
|
author = '<span class="mw-mmv-author">Homer</span>',
|
|
|
|
source = '<span class="mw-mmv-source">Iliad</span>',
|
2014-12-28 21:04:22 +00:00
|
|
|
attribution = '<span class="mw-mmv-attr">Cat</span>',
|
2014-03-21 20:29:55 +00:00
|
|
|
byline;
|
2014-03-19 03:05:36 +00:00
|
|
|
|
|
|
|
// Works with no arguments
|
2014-03-21 20:29:55 +00:00
|
|
|
byline = formatter.getByline();
|
|
|
|
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 );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( byline.match( /Cat/ ), '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 );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( byline.match( /Homer|Iliad/ ), '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 );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( byline.match( /Homer/ ), '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 );
|
|
|
|
assert.ok( byline.match( /Iliad/ ), 'Source found in bylines.' );
|
|
|
|
} );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
QUnit.test( 'getSiteLink():', 2, function ( assert ) {
|
|
|
|
var repoInfo = new mw.mmv.model.Repo( 'Wikipedia', '//wikipedia.org/favicon.ico', true ),
|
|
|
|
info = new mw.mmv.model.EmbedFileInfo( {}, repoInfo ),
|
|
|
|
formatter = new mw.mmv.EmbedFileFormatter(),
|
|
|
|
siteUrl = repoInfo.getSiteLink(),
|
|
|
|
siteLink = formatter.getSiteLink( info );
|
|
|
|
|
|
|
|
assert.ok( siteLink.match( 'Wikipedia' ), 'Site name is present in site link' );
|
2014-12-08 21:07:41 +00:00
|
|
|
assert.ok( siteLink.indexOf( siteUrl ) !== -1, 'Site URL is present in site link' );
|
2014-03-19 03:05:36 +00:00
|
|
|
} );
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
QUnit.test( 'getThumbnailHtml():', 36, function ( assert ) {
|
2014-03-19 03:05:36 +00:00
|
|
|
var formatter = new mw.mmv.EmbedFileFormatter(),
|
2014-03-21 20:29:55 +00:00
|
|
|
titleText = 'Music Room',
|
|
|
|
title = mw.Title.newFromText( titleText ),
|
2014-03-19 03:05:36 +00:00
|
|
|
imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
|
|
|
|
filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
|
2015-12-24 20:35:52 +00:00
|
|
|
filePageShortUrl = 'https://commons.wikimedia.org/wiki/index.php?curid=42',
|
2014-03-19 03:05:36 +00:00
|
|
|
siteName = 'Site Name',
|
2014-03-21 20:29:55 +00:00
|
|
|
siteUrl = '//site.url/',
|
|
|
|
licenseShortName = 'Public License',
|
|
|
|
licenseInternalName = '-',
|
|
|
|
licenseLongName = 'Public Domain, copyrights have lapsed',
|
|
|
|
licenseUrl = '//example.com/pd',
|
2014-03-31 21:33:12 +00:00
|
|
|
author = '<span class="mw-mmv-author">Homer</span>',
|
|
|
|
source = '<span class="mw-mmv-source">Iliad</span>',
|
2014-03-19 03:05:36 +00:00
|
|
|
thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg',
|
|
|
|
width = 700,
|
|
|
|
height = 500,
|
|
|
|
info,
|
|
|
|
generatedHtml;
|
|
|
|
|
|
|
|
// Bylines, 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,
|
|
|
|
licenseShortName: licenseShortName, licenseInternalName: licenseInternalName,
|
|
|
|
licenseLongName: licenseLongName, licenseUrl: licenseUrl, author: author, source: source } );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
generatedHtml = formatter.getThumbnailHtml( info, thumbUrl, width, height );
|
2014-03-21 20:29:55 +00:00
|
|
|
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
|
|
|
|
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( 'Public License' ), 'License appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( 'Homer' ), 'Author appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( 'Iliad' ), 'Source appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( height ), '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.
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( generatedHtml.includes( filePageShortUrl ), '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
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
|
|
|
|
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( !generatedHtml.match( 'Public License' ), 'License should not appear in generated HTML' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( 'Homer' ), 'Author appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( 'Iliad' ), 'Source appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( generatedHtml.includes( filePageShortUrl ), '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
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
|
|
|
|
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( 'Public License' ), 'License appears in generated HTML' );
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( !generatedHtml.match( 'Homer' ), 'Author should not appear in generated HTML' );
|
|
|
|
assert.ok( !generatedHtml.match( 'Iliad' ), 'Source should not appear in generated HTML' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( generatedHtml.includes( filePageShortUrl ), '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
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( generatedHtml.match( titleText ), 'Title appears in generated HTML.' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( filePageUrl ), 'Page url appears in generated HTML.' );
|
|
|
|
assert.ok( generatedHtml.match( thumbUrl ), 'Thumbnail url appears in generated HTML' );
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( !generatedHtml.match( 'Public License' ), 'License should not appear in generated HTML' );
|
|
|
|
assert.ok( !generatedHtml.match( 'Homer' ), 'Author should not appear in generated HTML' );
|
|
|
|
assert.ok( !generatedHtml.match( 'Iliad' ), 'Source should not appear in generated HTML' );
|
2014-03-19 03:05:36 +00:00
|
|
|
assert.ok( generatedHtml.match( width ), 'Width appears in generated HTML' );
|
|
|
|
assert.ok( generatedHtml.match( height ), 'Height appears in generated HTML' );
|
2016-07-18 13:49:27 +00:00
|
|
|
assert.ok( generatedHtml.includes( filePageShortUrl ), 'Short URL appears in generated HTML' );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
QUnit.test( 'getThumbnailWikitext():', 3, function ( assert ) {
|
|
|
|
var formatter = new mw.mmv.EmbedFileFormatter(),
|
|
|
|
title = mw.Title.newFromText( 'File:Foobar.jpg' ),
|
|
|
|
imgUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
|
|
|
|
filePageUrl = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
|
|
|
|
caption = 'Foobar caption.',
|
|
|
|
width = 700,
|
|
|
|
info,
|
|
|
|
wikitext;
|
|
|
|
|
|
|
|
// Title, width and caption
|
2014-03-21 20:29:55 +00:00
|
|
|
info = createEmbedFileInfo( { title: title, imgUrl: imgUrl, filePageUrl: filePageUrl,
|
|
|
|
caption: caption } );
|
2014-03-19 02:00:26 +00:00
|
|
|
wikitext = formatter.getThumbnailWikitextFromEmbedFileInfo( info, width );
|
|
|
|
|
|
|
|
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.' );
|
|
|
|
} );
|
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
QUnit.test( 'getCreditText():', 2, function ( assert ) {
|
|
|
|
var txt, formatter = new mw.mmv.EmbedFileFormatter();
|
|
|
|
|
|
|
|
txt = formatter.getCreditText( {
|
|
|
|
repoInfo: {
|
|
|
|
displayName: 'Localcommons'
|
|
|
|
},
|
|
|
|
|
|
|
|
imageInfo: {
|
|
|
|
author: 'Author',
|
|
|
|
source: 'Source',
|
2015-12-24 20:35:52 +00:00
|
|
|
descriptionShortUrl: 'link',
|
2014-06-12 23:59:06 +00:00
|
|
|
title: {
|
|
|
|
getNameText: function () { return 'Image Title'; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
assert.strictEqual( txt, 'By Author - Source, link', 'Sanity check' );
|
2014-06-12 23:59:06 +00:00
|
|
|
|
|
|
|
txt = formatter.getCreditText( {
|
|
|
|
repoInfo: {
|
|
|
|
displayName: 'Localcommons'
|
|
|
|
},
|
|
|
|
|
|
|
|
imageInfo: {
|
|
|
|
author: 'Author',
|
|
|
|
source: 'Source',
|
2015-12-24 20:35:52 +00:00
|
|
|
descriptionShortUrl: 'link',
|
2014-06-12 23:59:06 +00:00
|
|
|
title: {
|
|
|
|
getNameText: function () { return 'Image Title'; }
|
|
|
|
},
|
|
|
|
license: {
|
2015-12-24 20:35:52 +00:00
|
|
|
getShortName: function () { return 'WTFPL v2'; },
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
assert.strictEqual( txt, 'By Author - Source, WTFPL v2, link', 'License message works' );
|
2014-12-01 20:15:17 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'getCreditHtml():', 2, function ( assert ) {
|
|
|
|
var html, formatter = new mw.mmv.EmbedFileFormatter();
|
|
|
|
|
|
|
|
html = formatter.getCreditHtml( {
|
|
|
|
repoInfo: {
|
|
|
|
displayName: 'Localcommons',
|
|
|
|
getSiteLink: function () { return 'quux'; }
|
|
|
|
},
|
|
|
|
|
|
|
|
imageInfo: {
|
|
|
|
author: 'Author',
|
|
|
|
source: 'Source',
|
2016-09-04 04:49:58 +00:00
|
|
|
descriptionShortUrl: 'some link',
|
2014-12-01 20:15:17 +00:00
|
|
|
title: {
|
|
|
|
getNameText: function () { return 'Image Title'; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2016-09-04 04:49:58 +00:00
|
|
|
assert.strictEqual( html, 'By Author - Source, <a href="some link">Link</a>', 'Sanity check' );
|
2014-12-01 20:15:17 +00:00
|
|
|
|
|
|
|
html = formatter.getCreditHtml( {
|
|
|
|
repoInfo: {
|
|
|
|
displayName: 'Localcommons',
|
|
|
|
getSiteLink: function () { return 'quux'; }
|
|
|
|
},
|
|
|
|
|
|
|
|
imageInfo: {
|
|
|
|
author: 'Author',
|
|
|
|
source: 'Source',
|
2016-09-04 04:49:58 +00:00
|
|
|
descriptionShortUrl: 'some link',
|
2014-12-01 20:15:17 +00:00
|
|
|
title: {
|
|
|
|
getNameText: function () { return 'Image Title'; }
|
|
|
|
},
|
|
|
|
license: {
|
2015-12-24 20:35:52 +00:00
|
|
|
getShortLink: function () { return '<a href="http://www.wtfpl.net/">WTFPL v2</a>'; },
|
|
|
|
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 )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2016-09-04 04:49:58 +00:00
|
|
|
assert.strictEqual( html, 'By Author - Source, <a href="http://www.wtfpl.net/">WTFPL v2</a>, <a href="some link">Link</a>', 'Sanity check' );
|
2014-06-12 23:59:06 +00:00
|
|
|
} );
|
2014-03-21 20:29:55 +00:00
|
|
|
}( mediaWiki ) );
|