2023-05-20 08:30:52 +00:00
const { Config } = require ( 'mmv.bootstrap' ) ;
const { MetadataPanel , License } = require ( 'mmv' ) ;
2022-05-13 21:10:05 +00:00
QUnit . module ( 'mmv.ui.metadataPanel' , QUnit . newMwEnvironment ( ) ) ;
QUnit . test ( '.empty()' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
const panel = new MetadataPanel (
2022-05-13 21:10:05 +00:00
$qf ,
$ ( '<div>' ) . appendTo ( $qf ) ,
mw . storage ,
2023-05-20 08:30:52 +00:00
new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage )
2022-05-13 21:10:05 +00:00
) ;
panel . empty ( ) ;
[
'$license' ,
'$title' ,
'$location' ,
2024-02-07 17:34:34 +00:00
'$datetimeCreated' ,
'$datetimeUpdated'
2022-05-13 21:10:05 +00:00
] . forEach ( function ( thing ) {
assert . strictEqual ( panel [ thing ] . text ( ) , '' , thing + ' empty text' ) ;
2014-02-07 14:47:00 +00:00
} ) ;
2022-05-13 21:10:05 +00:00
[
'$licenseLi' ,
'$credit' ,
'$locationLi' ,
2024-02-07 17:34:34 +00:00
'$datetimeCreatedLi' ,
'$datetimeUpdatedLi'
2022-05-13 21:10:05 +00:00
] . forEach ( function ( thing ) {
assert . true ( panel [ thing ] . hasClass ( 'empty' ) , thing + ' empty class' ) ;
2014-02-07 14:47:00 +00:00
} ) ;
2022-05-13 21:10:05 +00:00
} ) ;
QUnit . test ( '.setLocationData()' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
const panel = new MetadataPanel (
2022-05-13 21:10:05 +00:00
$qf ,
$ ( '<div>' ) . appendTo ( $qf ) ,
mw . storage ,
2023-05-20 08:30:52 +00:00
new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage )
2022-05-13 21:10:05 +00:00
) ;
2023-10-24 09:53:23 +00:00
const fileName = 'Foobar.jpg' ;
let latitude = 12.3456789 ;
let longitude = 98.7654321 ;
const imageData = {
2022-05-13 21:10:05 +00:00
latitude : latitude ,
longitude : longitude ,
2024-02-13 00:44:51 +00:00
hasCoords : function ( ) {
return true ;
} ,
2022-05-13 21:10:05 +00:00
title : mw . Title . newFromText ( 'File:Foobar.jpg' )
} ;
panel . setLocationData ( imageData ) ;
assert . strictEqual (
panel . $location . text ( ) ,
'(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))))' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
2023-04-10 14:57:35 +00:00
'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '¶ms=' + latitude + '_N_' + longitude + '_E_&language=qqx' ,
2022-05-13 21:10:05 +00:00
'Location URL is set as expected'
) ;
latitude = - latitude ;
longitude = - longitude ;
imageData . latitude = latitude ;
imageData . longitude = longitude ;
panel . setLocationData ( imageData ) ;
assert . strictEqual (
panel . $location . text ( ) ,
'(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))))' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
2023-04-10 14:57:35 +00:00
'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '¶ms=' + ( - latitude ) + '_S_' + ( - longitude ) + '_W_&language=qqx' ,
2022-05-13 21:10:05 +00:00
'Location URL is set as expected'
) ;
latitude = 0 ;
longitude = 0 ;
imageData . latitude = latitude ;
imageData . longitude = longitude ;
panel . setLocationData ( imageData ) ;
assert . strictEqual (
panel . $location . text ( ) ,
'(multimediaviewer-geolocation: (multimediaviewer-geoloc-coords: (multimediaviewer-geoloc-coord: 0, 0, 0, (multimediaviewer-geoloc-north)), (multimediaviewer-geoloc-coord: 0, 0, 0, (multimediaviewer-geoloc-east))))' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
2023-04-10 14:57:35 +00:00
'https://geohack.toolforge.org/geohack.php?pagename=File:' + fileName + '¶ms=' + latitude + '_N_' + longitude + '_E_&language=qqx' ,
2022-05-13 21:10:05 +00:00
'Location URL is set as expected'
) ;
} ) ;
QUnit . test ( '.setImageInfo()' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
const panel = new MetadataPanel (
2022-05-13 21:10:05 +00:00
$qf ,
$ ( '<div>' ) . appendTo ( $qf ) ,
mw . storage ,
2023-05-20 08:30:52 +00:00
new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage )
2022-05-13 21:10:05 +00:00
) ;
2023-10-24 09:53:23 +00:00
const title = 'Foo bar' ;
const image = {
2022-05-13 21:10:05 +00:00
filePageTitle : mw . Title . newFromText ( 'File:' + title + '.jpg' )
} ;
2023-10-24 09:53:23 +00:00
const imageData = {
2022-05-13 21:10:05 +00:00
title : image . filePageTitle ,
url : 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg' ,
descriptionUrl : 'https://commons.wikimedia.org/wiki/File:Foobar.jpg' ,
2024-02-13 00:44:51 +00:00
hasCoords : function ( ) {
return false ;
}
2022-05-13 21:10:05 +00:00
} ;
2023-10-24 09:53:23 +00:00
const repoData = {
2024-02-13 00:44:51 +00:00
getArticlePath : function ( ) {
return 'Foo' ;
} ,
isCommons : function ( ) {
return false ;
}
2022-05-13 21:10:05 +00:00
} ;
2023-10-24 09:53:23 +00:00
const clock = this . sandbox . useFakeTimers ( ) ;
2022-05-13 21:10:05 +00:00
panel . setImageInfo ( image , imageData , repoData ) ;
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
2022-05-20 00:18:43 +00:00
assert . notStrictEqual ( panel . $credit . text ( ) , '' , 'Default credit is shown' ) ;
2023-05-19 13:06:18 +00:00
assert . strictEqual ( panel . $license . prop ( 'href' ) ,
imageData . descriptionUrl + '?uselang=qqx#(license-header)' ,
2022-05-13 21:10:05 +00:00
'User is directed to file page for license information' ) ;
2022-05-20 00:18:43 +00:00
assert . strictEqual ( panel . $license . prop ( 'target' ) , '' , 'License information opens in same window' ) ;
2024-02-07 17:34:34 +00:00
assert . true ( panel . $datetimeCreatedLi . hasClass ( 'empty' ) , 'Date/Time is empty' ) ;
assert . true ( panel . $datetimeUpdatedLi . hasClass ( 'empty' ) , 'Date/Time is empty' ) ;
2022-05-13 21:10:05 +00:00
assert . true ( panel . $locationLi . hasClass ( 'empty' ) , 'Location is empty' ) ;
imageData . creationDateTime = '2013-08-26T14:41:02Z' ;
imageData . uploadDateTime = '2013-08-25T14:41:02Z' ;
imageData . source = '<b>Lost</b><a href="foo">Bar</a>' ;
imageData . author = 'Bob' ;
2023-05-20 08:30:52 +00:00
imageData . license = new License ( 'CC-BY-2.0' , 'cc-by-2.0' ,
2022-05-13 21:10:05 +00:00
'Creative Commons Attribution - Share Alike 2.0' ,
'http://creativecommons.org/licenses/by-sa/2.0/' ) ;
imageData . restrictions = [ 'trademarked' , 'default' , 'insignia' ] ;
panel . setImageInfo ( image , imageData , repoData ) ;
2023-10-24 09:53:23 +00:00
const creditPopupText = panel . creditField . $element . attr ( 'original-title' ) ;
2022-05-13 21:10:05 +00:00
clock . tick ( 10 ) ;
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
assert . false ( panel . $credit . hasClass ( 'empty' ) , 'Credit is not empty' ) ;
2024-02-07 17:34:34 +00:00
assert . false ( panel . $datetimeCreatedLi . hasClass ( 'empty' ) , 'Date/Time is not empty' ) ;
2022-05-13 21:10:05 +00:00
assert . strictEqual ( panel . creditField . $element . find ( '.mw-mmv-author' ) . text ( ) , imageData . author , 'Author text is correctly set' ) ;
assert . strictEqual ( panel . creditField . $element . find ( '.mw-mmv-source' ) . html ( ) , '<b>Lost</b><a href="foo">Bar</a>' , 'Source text is correctly set' ) ;
// Either multimediaviewer-credit-popup-text or multimediaviewer-credit-popup-text-more.
2022-05-20 00:18:43 +00:00
assert . true ( creditPopupText === '(multimediaviewer-credit-popup-text)' || creditPopupText === '(multimediaviewer-credit-popup-text-more)' , 'Source tooltip is correctly set' ) ;
2024-02-07 17:34:34 +00:00
assert . strictEqual ( panel . $datetimeCreated . text ( ) , '(multimediaviewer-datetime-created: 26 August 2013)' , 'Correct date is displayed' ) ;
2022-05-13 21:10:05 +00:00
assert . strictEqual ( panel . $license . text ( ) , '(multimediaviewer-license-cc-by-2.0)' , 'License is correctly set' ) ;
2022-05-20 00:18:43 +00:00
assert . strictEqual ( panel . $license . prop ( 'target' ) , '_blank' , 'License information opens in new window' ) ;
2022-05-13 21:10:05 +00:00
assert . true ( panel . $restrictions . children ( ) . last ( ) . children ( ) . hasClass ( 'mw-mmv-restriction-default' ) , 'Default restriction is correctly displayed last' ) ;
imageData . creationDateTime = undefined ;
panel . setImageInfo ( image , imageData , repoData ) ;
clock . tick ( 10 ) ;
2024-02-07 17:34:34 +00:00
assert . false ( panel . $datetimeUpdatedLi . hasClass ( 'empty' ) , 'Date/Time is not empty' ) ;
assert . strictEqual ( panel . $datetimeUpdated . text ( ) , '(multimediaviewer-datetime-uploaded: 25 August 2013)' , 'Correct date is displayed' ) ;
2022-05-13 21:10:05 +00:00
clock . restore ( ) ;
} ) ;
2023-05-20 08:30:52 +00:00
// FIXME: test broken since migrating to require/packageFiles
QUnit . skip ( 'Setting permission information works as expected' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
const panel = new MetadataPanel (
2022-05-13 21:10:05 +00:00
$qf ,
$ ( '<div>' ) . appendTo ( $qf ) ,
mw . storage ,
2023-05-20 08:30:52 +00:00
new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage )
2022-05-13 21:10:05 +00:00
) ;
// make sure license is visible as it contains the permission
panel . setLicense ( null , 'http://example.com' ) ;
panel . setPermission ( 'Look at me, I am a permission!' ) ;
assert . true ( panel . $permissionLink . is ( ':visible' ) ) ;
} ) ;
QUnit . test ( 'Date formatting' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
const panel = new MetadataPanel (
2022-05-13 21:10:05 +00:00
$qf ,
$ ( '<div>' ) . appendTo ( $qf ) ,
mw . storage ,
2023-05-20 08:30:52 +00:00
new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage )
2022-05-13 21:10:05 +00:00
) ;
2023-10-24 09:53:23 +00:00
const date1 = 'Garbage' ;
const result = panel . formatDate ( date1 ) ;
2022-05-13 21:10:05 +00:00
assert . strictEqual ( result , date1 , 'Invalid date is correctly ignored' ) ;
} ) ;
QUnit . test ( 'About links' , function ( assert ) {
2023-10-24 09:53:23 +00:00
const $qf = $ ( '#qunit-fixture' ) ;
2022-05-13 21:10:05 +00:00
this . sandbox . stub ( mw . user , 'isAnon' ) ;
// eslint-disable-next-line no-new
2023-05-20 08:30:52 +00:00
new MetadataPanel ( $qf . empty ( ) , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ;
2022-05-13 21:10:05 +00:00
assert . strictEqual ( $qf . find ( '.mw-mmv-about-link' ) . length , 1 , 'About link is created.' ) ;
} ) ;