2018-11-12 16:33:24 +00:00
( function ( ) {
2014-02-07 14:47:00 +00:00
var thingsShouldBeEmptied = [
'$license' ,
'$title' ,
'$location' ,
'$datetime'
] ,
thingsShouldHaveEmptyClass = [
2014-10-27 14:03:16 +00:00
'$licenseLi' ,
2014-02-07 14:47:00 +00:00
'$credit' ,
'$locationLi' ,
2014-05-28 22:43:35 +00:00
'$datetimeLi'
2014-02-07 14:47:00 +00:00
] ;
QUnit . module ( 'mmv.ui.metadataPanel' , QUnit . newMwEnvironment ( ) ) ;
2017-07-26 00:01:07 +00:00
QUnit . test ( 'The panel is emptied properly when necessary' , function ( assert ) {
2014-02-07 14:47:00 +00:00
var i ,
$qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ;
2014-02-07 14:47:00 +00:00
panel . empty ( ) ;
for ( i = 0 ; i < thingsShouldBeEmptied . length ; i ++ ) {
2016-07-18 13:49:27 +00:00
assert . strictEqual ( panel [ thingsShouldBeEmptied [ i ] ] . text ( ) , '' , 'We successfully emptied the ' + thingsShouldBeEmptied [ i ] + ' element' ) ;
2014-02-07 14:47:00 +00:00
}
for ( i = 0 ; i < thingsShouldHaveEmptyClass . length ; i ++ ) {
2016-07-18 13:49:27 +00:00
assert . strictEqual ( panel [ thingsShouldHaveEmptyClass [ i ] ] . hasClass ( 'empty' ) , true , 'We successfully applied the empty class to the ' + thingsShouldHaveEmptyClass [ i ] + ' element' ) ;
2014-02-07 14:47:00 +00:00
}
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Setting location information works as expected' , function ( assert ) {
2014-02-07 14:47:00 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ,
2014-02-07 14:47:00 +00:00
fileName = 'Foobar.jpg' ,
latitude = 12.3456789 ,
longitude = 98.7654321 ,
imageData = {
latitude : latitude ,
longitude : longitude ,
2015-01-23 12:48:27 +00:00
hasCoords : function ( ) { return true ; } ,
2014-02-07 14:47:00 +00:00
title : mw . Title . newFromText ( 'File:Foobar.jpg' )
} ;
panel . setLocationData ( imageData ) ;
assert . strictEqual (
panel . $location . text ( ) ,
'Location: 12° 20′ 44.44″ N, 98° 45′ 55.56″ E' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
'http://tools.wmflabs.org/geohack/geohack.php?pagename=File:' + fileName + '¶ms=' + latitude + '_N_' + longitude + '_E_&language=en' ,
'Location URL is set as expected'
) ;
latitude = - latitude ;
longitude = - longitude ;
imageData . latitude = latitude ;
imageData . longitude = longitude ;
panel . setLocationData ( imageData ) ;
assert . strictEqual (
panel . $location . text ( ) ,
'Location: 12° 20′ 44.44″ S, 98° 45′ 55.56″ W' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
2016-07-18 13:49:27 +00:00
'http://tools.wmflabs.org/geohack/geohack.php?pagename=File:' + fileName + '¶ms=' + ( - latitude ) + '_S_' + ( - longitude ) + '_W_&language=en' ,
2014-02-07 14:47:00 +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 ( ) ,
'Location: 0° 0′ 0″ N, 0° 0′ 0″ E' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
panel . $location . prop ( 'href' ) ,
'http://tools.wmflabs.org/geohack/geohack.php?pagename=File:' + fileName + '¶ms=' + latitude + '_N_' + longitude + '_E_&language=en' ,
'Location URL is set as expected'
) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Setting image information works as expected' , function ( assert ) {
2015-05-20 10:22:25 +00:00
var creditPopupText ,
2014-02-07 14:47:00 +00:00
$qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ,
2014-02-07 14:47:00 +00:00
title = 'Foo bar' ,
image = {
2015-01-23 12:48:27 +00:00
filePageTitle : mw . Title . newFromText ( 'File:' + title + '.jpg' )
2014-02-07 14:47:00 +00:00
} ,
imageData = {
2014-03-17 08:07:53 +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' ,
2015-01-23 12:48:27 +00:00
hasCoords : function ( ) { return false ; }
2014-02-07 14:47:00 +00:00
} ,
repoData = {
2015-01-23 12:48:27 +00:00
getArticlePath : function ( ) { return 'Foo' ; } ,
isCommons : function ( ) { return false ; }
2014-02-07 14:47:00 +00:00
} ,
2017-05-15 12:26:42 +00:00
oldMoment = window . moment ,
// custom clock will give MPP.formatDate some time to load moment.js
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-07 14:47:00 +00:00
2016-12-14 13:09:10 +00:00
/ * w i n d o w . m o m e n t = f u n c t i o n ( d a t e ) {
2014-02-07 14:47:00 +00:00
// This has no effect for now, since writing this test revealed that our moment.js
// doesn't have any language configuration
return oldMoment ( date ) . lang ( 'fr' ) ;
} ; * /
2015-12-24 15:01:56 +00:00
panel . setImageInfo ( image , imageData , repoData ) ;
2014-02-07 14:47:00 +00:00
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
2014-10-29 13:36:08 +00:00
assert . ok ( panel . $credit . text ( ) , 'Default credit is shown' ) ;
2014-04-19 00:39:46 +00:00
assert . strictEqual ( panel . $license . prop ( 'href' ) , imageData . descriptionUrl ,
'User is directed to file page for license information' ) ;
2018-06-06 18:23:25 +00:00
assert . notOk ( panel . $license . prop ( 'target' ) , 'License information opens in same window' ) ;
assert . strictEqual ( panel . $datetimeLi . hasClass ( 'empty' ) , true , 'Date/Time is empty' ) ;
assert . strictEqual ( panel . $locationLi . hasClass ( 'empty' ) , true , 'Location is empty' ) ;
2014-02-07 14:47:00 +00:00
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' ;
2014-04-19 00:29:04 +00:00
imageData . license = new mw . mmv . model . License ( 'CC-BY-2.0' , 'cc-by-2.0' ,
'Creative Commons Attribution - Share Alike 2.0' ,
'http://creativecommons.org/licenses/by-sa/2.0/' ) ;
2015-06-20 05:03:28 +00:00
imageData . restrictions = [ 'trademarked' , 'default' , 'insignia' ] ;
2014-02-07 14:47:00 +00:00
2015-12-24 15:01:56 +00:00
panel . setImageInfo ( image , imageData , repoData ) ;
2015-05-20 10:22:25 +00:00
creditPopupText = panel . creditField . $element . attr ( 'original-title' ) ;
2017-05-15 12:26:42 +00:00
clock . tick ( 10 ) ;
2014-02-07 14:47:00 +00:00
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( panel . $credit . hasClass ( 'empty' ) , false , 'Credit is not empty' ) ;
assert . strictEqual ( panel . $datetimeLi . hasClass ( 'empty' ) , false , 'Date/Time is not empty' ) ;
2014-05-31 00:18:12 +00:00
assert . strictEqual ( panel . creditField . $element . find ( '.mw-mmv-author' ) . text ( ) , imageData . author , 'Author text is correctly set' ) ;
2014-12-30 22:00:37 +00:00
assert . strictEqual ( panel . creditField . $element . find ( '.mw-mmv-source' ) . html ( ) , '<b>Lost</b><a href="foo">Bar</a>' , 'Source text is correctly set' ) ;
2015-05-20 10:22:25 +00:00
// Either multimediaviewer-credit-popup-text or multimediaviewer-credit-popup-text-more.
assert . ok ( creditPopupText === 'Author and source information' || creditPopupText === 'View full author and source' , 'Source tooltip is correctly set' ) ;
2015-07-30 12:13:27 +00:00
assert . ok ( panel . $datetime . text ( ) . indexOf ( '26 August 2013' ) > 0 , 'Correct date is displayed' ) ;
2014-02-07 14:47:00 +00:00
assert . strictEqual ( panel . $license . text ( ) , 'CC BY 2.0' , 'License is correctly set' ) ;
2014-04-19 00:29:04 +00:00
assert . ok ( panel . $license . prop ( 'target' ) , 'License information opens in new window' ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( panel . $restrictions . children ( ) . last ( ) . children ( ) . hasClass ( 'mw-mmv-restriction-default' ) , true , 'Default restriction is correctly displayed last' ) ;
2014-02-07 14:47:00 +00:00
imageData . creationDateTime = undefined ;
2015-12-24 15:01:56 +00:00
panel . setImageInfo ( image , imageData , repoData ) ;
2017-05-15 12:26:42 +00:00
clock . tick ( 10 ) ;
2014-02-07 14:47:00 +00:00
2015-07-30 12:13:27 +00:00
assert . ok ( panel . $datetime . text ( ) . indexOf ( '25 August 2013' ) > 0 , 'Correct date is displayed' ) ;
2014-02-07 14:47:00 +00:00
window . moment = oldMoment ;
2017-05-15 12:26:42 +00:00
clock . restore ( ) ;
2014-02-07 14:47:00 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Setting permission information works as expected' , function ( assert ) {
2014-02-14 00:36:10 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ;
2014-02-14 00:36:10 +00:00
2014-10-27 14:03:16 +00:00
panel . setLicense ( null , 'http://example.com' ) ; // make sure license is visible as it contains the permission
2014-02-14 00:36:10 +00:00
panel . setPermission ( 'Look at me, I am a permission!' ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( panel . $permissionLink . is ( ':visible' ) , true ) ;
2014-02-14 00:36:10 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Date formatting' , function ( assert ) {
2014-02-07 14:47:00 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ,
2014-02-07 14:47:00 +00:00
date1 = 'Garbage' ,
2014-04-11 14:58:37 +00:00
promise = panel . formatDate ( date1 ) ;
2014-02-07 14:47:00 +00:00
2017-07-26 00:01:07 +00:00
return promise . then ( function ( result ) {
2014-04-11 14:58:37 +00:00
assert . strictEqual ( result , date1 , 'Invalid date is correctly ignored' ) ;
} ) ;
2014-02-07 14:47:00 +00:00
} ) ;
2014-02-25 13:43:22 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'About links' , function ( assert ) {
2019-05-08 18:21:45 +00:00
var $qf = $ ( '#qunit-fixture' ) ;
2014-04-23 20:31:43 +00:00
this . sandbox . stub ( mw . user , 'isAnon' ) ;
2016-12-14 13:09:10 +00:00
// eslint-disable-next-line no-new
2017-06-02 09:29:41 +00:00
new mw . mmv . ui . MetadataPanel ( $qf . empty ( ) , $ ( '<div>' ) . appendTo ( $qf ) , mw . storage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , mw . storage ) ) ;
2014-04-23 20:31:43 +00:00
assert . strictEqual ( $qf . find ( '.mw-mmv-about-link' ) . length , 1 , 'About link is created.' ) ;
assert . strictEqual ( $qf . find ( '.mw-mmv-discuss-link' ) . length , 1 , 'Discuss link is created.' ) ;
assert . strictEqual ( $qf . find ( '.mw-mmv-help-link' ) . length , 1 , 'Help link is created.' ) ;
2014-06-18 00:17:45 +00:00
} ) ;
2018-11-12 16:33:24 +00:00
} ( ) ) ;