2014-02-07 14:47:00 +00:00
( function ( mw , $ ) {
var thingsShouldBeEmptied = [
'$license' ,
'$title' ,
'$credit' ,
'$username' ,
'$location' ,
'$datetime'
] ,
thingsShouldHaveEmptyClass = [
'$license' ,
'$credit' ,
'$usernameLi' ,
'$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 ( ) ) ;
2014-05-05 21:44:27 +00:00
QUnit . test ( 'The panel is emptied properly when necessary' , thingsShouldBeEmptied . length + thingsShouldHaveEmptyClass . length , function ( assert ) {
2014-02-07 14:47:00 +00:00
var i ,
$qf = $ ( '#qunit-fixture' ) ,
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ;
2014-02-07 14:47:00 +00:00
panel . empty ( ) ;
for ( i = 0 ; i < thingsShouldBeEmptied . length ; i ++ ) {
assert . strictEqual ( panel [ thingsShouldBeEmptied [ i ] ] . text ( ) , '' , 'We successfully emptied the ' + thingsShouldBeEmptied [ i ] + ' element' ) ;
}
for ( i = 0 ; i < thingsShouldHaveEmptyClass . length ; i ++ ) {
assert . strictEqual ( panel [ thingsShouldHaveEmptyClass [ i ] ] . hasClass ( 'empty' ) , true , 'We successfully applied the empty class to the ' + thingsShouldHaveEmptyClass [ i ] + ' element' ) ;
}
} ) ;
QUnit . test ( 'Setting location information works as expected' , 6 , function ( assert ) {
var $qf = $ ( '#qunit-fixture' ) ,
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ,
2014-02-07 14:47:00 +00:00
fileName = 'Foobar.jpg' ,
latitude = 12.3456789 ,
longitude = 98.7654321 ,
imageData = {
latitude : latitude ,
longitude : longitude ,
hasCoords : function ( ) { return true ; } ,
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' ) ,
'http://tools.wmflabs.org/geohack/geohack.php?pagename=File:' + fileName + '¶ms=' + ( - latitude ) + '_S_' + ( - longitude ) + '_W_&language=en' ,
'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'
) ;
} ) ;
2014-07-03 19:12:59 +00:00
QUnit . test ( 'Setting image information works as expected' , 18 , function ( assert ) {
2014-02-07 14:47:00 +00:00
var gender ,
$qf = $ ( '#qunit-fixture' ) ,
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ,
2014-02-07 14:47:00 +00:00
title = 'Foo bar' ,
image = {
filePageTitle : mw . Title . newFromText ( 'File:' + title + '.jpg' )
} ,
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' ,
hasCoords : function ( ) { return false ; }
2014-02-07 14:47:00 +00:00
} ,
repoData = {
2014-04-08 22:43:54 +00:00
getArticlePath : function ( ) { return 'Foo' ; } ,
isCommons : function ( ) { return false ; }
2014-02-07 14:47:00 +00:00
} ,
oldMoment = window . moment ;
/ * w i n d o w . m o m e n t = f u n c t i o n ( d a t e ) {
// 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' ) ;
} ; * /
2014-09-05 06:14:52 +00:00
panel . setImageInfo ( image , imageData , repoData , gender ) ;
2014-02-07 14:47:00 +00:00
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
assert . ok ( panel . $credit . hasClass ( 'empty' ) , 'Credit is empty' ) ;
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' ) ;
2014-04-19 00:29:04 +00:00
assert . ok ( ! panel . $license . prop ( 'target' ) , 'License information opens in same window' ) ;
2014-02-07 14:47:00 +00:00
assert . ok ( panel . $usernameLi . hasClass ( 'empty' ) , 'Username is empty' ) ;
assert . ok ( panel . $datetimeLi . hasClass ( 'empty' ) , 'Date/Time is empty' ) ;
assert . ok ( 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' ;
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/' ) ;
2014-02-07 14:47:00 +00:00
gender = 'female' ;
imageData . lastUploader = 'Ursula' ;
2014-09-05 06:14:52 +00:00
panel . setImageInfo ( image , imageData , repoData , gender ) ;
2014-02-07 14:47:00 +00:00
assert . strictEqual ( panel . $title . text ( ) , title , 'Title is correctly set' ) ;
assert . ok ( ! panel . $credit . hasClass ( 'empty' ) , 'Credit is not empty' ) ;
assert . ok ( ! panel . $datetimeLi . hasClass ( 'empty' ) , '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' ) ;
assert . strictEqual ( panel . creditField . $element . find ( '.mw-mmv-source' ) . html ( ) , 'Lost<a href="foo">Bar</a>' , 'Source text is correctly set' ) ;
2014-07-03 19:12:59 +00:00
assert . strictEqual ( panel . creditField . $element . attr ( 'original-title' ) , 'Author and source information' , 'Source tooltip is correctly set' ) ;
2014-08-16 12:11:46 +00:00
assert . ok ( panel . $datetime . text ( ) . indexOf ( 'August 26, 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' ) ;
2014-02-07 14:47:00 +00:00
assert . ok ( panel . $username . text ( ) . indexOf ( imageData . lastUploader ) > 0 , 'Correct username is displayed' ) ;
imageData . creationDateTime = undefined ;
2014-09-05 06:14:52 +00:00
panel . setImageInfo ( image , imageData , repoData , gender ) ;
2014-02-07 14:47:00 +00:00
2014-08-16 12:11:46 +00:00
assert . ok ( panel . $datetime . text ( ) . indexOf ( 'August 25, 2013' ) > 0 , 'Correct date is displayed' ) ;
2014-02-07 14:47:00 +00:00
window . moment = oldMoment ;
} ) ;
2014-02-14 00:36:10 +00:00
QUnit . test ( 'Setting permission information works as expected' , 1 , function ( assert ) {
var $qf = $ ( '#qunit-fixture' ) ,
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ;
2014-02-14 00:36:10 +00:00
panel . setPermission ( 'Look at me, I am a permission!' ) ;
assert . ok ( panel . $permissionLink . is ( ':visible' ) ) ;
} ) ;
2014-02-07 14:47:00 +00:00
QUnit . test ( 'Date formatting' , 1 , function ( assert ) {
var $qf = $ ( '#qunit-fixture' ) ,
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ,
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
2014-04-11 14:58:37 +00:00
QUnit . stop ( ) ;
promise . then ( function ( result ) {
assert . strictEqual ( result , date1 , 'Invalid date is correctly ignored' ) ;
QUnit . start ( ) ;
} ) ;
2014-02-07 14:47:00 +00:00
} ) ;
2014-02-25 13:43:22 +00:00
2014-07-23 23:00:37 +00:00
QUnit . test ( 'About links' , 3 , function ( assert ) {
2014-04-23 20:31:43 +00:00
var panel ,
2014-06-11 02:14:59 +00:00
$qf = $ ( '#qunit-fixture' ) ,
oldWgMediaViewerIsInBeta = mw . config . get ( 'wgMediaViewerIsInBeta' ) ;
2014-04-23 20:31:43 +00:00
this . sandbox . stub ( mw . user , 'isAnon' ) ;
2014-06-11 02:14:59 +00:00
mw . config . set ( 'wgMediaViewerIsInBeta' , false ) ;
2014-09-12 18:05:13 +00:00
panel = new mw . mmv . ui . MetadataPanel ( $qf . empty ( ) , $ ( '<div>' ) . appendTo ( $qf ) , window . localStorage , new mw . mmv . Config ( { } , mw . config , mw . user , new mw . Api ( ) , window . localStorage ) ) ;
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
mw . config . set ( 'wgMediaViewerIsInBeta' , oldWgMediaViewerIsInBeta ) ;
} ) ;
2014-02-07 14:47:00 +00:00
} ( mediaWiki , jQuery ) ) ;