2011-07-11 16:42:15 +00:00
< ? php
2014-07-15 17:31:07 +00:00
2013-10-23 22:12:39 +00:00
/**
2014-07-15 17:31:07 +00:00
* @ group Media
2013-10-24 18:04:38 +00:00
* @ covers JpegHandler
2013-10-23 22:12:39 +00:00
*/
2014-05-26 20:16:07 +00:00
class JpegTest extends MediaWikiMediaTestCase {
2013-10-24 18:04:38 +00:00
2012-10-08 10:56:20 +00:00
protected function setUp () {
parent :: setUp ();
2013-11-23 19:20:23 +00:00
$this -> checkPHPExtension ( 'exif' );
2012-10-08 10:56:20 +00:00
$this -> setMwGlobals ( 'wgShowEXIF' , true );
2013-08-28 23:09:07 +00:00
$this -> handler = new JpegHandler ;
2011-08-17 23:28:31 +00:00
}
2011-07-11 16:42:15 +00:00
public function testInvalidFile () {
2013-08-28 23:09:07 +00:00
$file = $this -> dataFile ( 'README' , 'image/jpeg' );
$res = $this -> handler -> getMetadata ( $file , $this -> filePath . 'README' );
2011-07-11 16:42:15 +00:00
$this -> assertEquals ( ExifBitmapHandler :: BROKEN_FILE , $res );
}
2012-10-08 10:56:20 +00:00
2011-08-18 03:53:53 +00:00
public function testJpegMetadataExtraction () {
2013-08-28 23:09:07 +00:00
$file = $this -> dataFile ( 'test.jpg' , 'image/jpeg' );
$res = $this -> handler -> getMetadata ( $file , $this -> filePath . 'test.jpg' );
2018-01-01 13:10:16 +00:00
// phpcs:ignore Generic.Files.LineLength
2017-05-20 15:53:19 +00:00
$expected = 'a:7:{s:16:"ImageDescription";s:9:"Test file";s:11:"XResolution";s:4:"72/1";s:11:"YResolution";s:4:"72/1";s:14:"ResolutionUnit";i:2;s:16:"YCbCrPositioning";i:1;s:15:"JPEGFileComment";a:1:{i:0;s:17:"Created with GIMP";}s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}' ;
2011-08-17 21:53:45 +00:00
// Unserialize in case serialization format ever changes.
$this -> assertEquals ( unserialize ( $expected ), unserialize ( $res ) );
2011-07-11 16:42:15 +00:00
}
2013-08-07 23:03:34 +00:00
/**
* @ covers JpegHandler :: getCommonMetaArray
*/
2013-08-28 23:09:07 +00:00
public function testGetIndependentMetaArray () {
$file = $this -> dataFile ( 'test.jpg' , 'image/jpeg' );
$res = $this -> handler -> getCommonMetaArray ( $file );
2016-02-17 09:09:32 +00:00
$expected = [
2013-08-28 23:09:07 +00:00
'ImageDescription' => 'Test file' ,
'XResolution' => '72/1' ,
'YResolution' => '72/1' ,
'ResolutionUnit' => 2 ,
'YCbCrPositioning' => 1 ,
2016-02-17 09:09:32 +00:00
'JPEGFileComment' => [
2013-08-28 23:09:07 +00:00
'Created with GIMP' ,
2016-02-17 09:09:32 +00:00
],
];
2013-08-28 23:09:07 +00:00
$this -> assertEquals ( $res , $expected );
}
2016-08-30 14:36:09 +00:00
/**
* @ dataProvider provideSwappingICCProfile
2017-05-05 20:20:43 +00:00
* @ covers JpegHandler :: swapICCProfile
2016-08-30 14:36:09 +00:00
*/
public function testSwappingICCProfile (
$sourceFilename , $controlFilename , $newProfileFilename , $oldProfileName
) {
global $wgExiftool ;
if ( ! $wgExiftool || ! is_file ( $wgExiftool ) ) {
$this -> markTestSkipped ( " Exiftool not installed, cannot test ICC profile swapping " );
}
$this -> setMwGlobals ( 'wgUseTinyRGBForJPGThumbnails' , true );
$sourceFilepath = $this -> filePath . $sourceFilename ;
$controlFilepath = $this -> filePath . $controlFilename ;
$profileFilepath = $this -> filePath . $newProfileFilename ;
$filepath = $this -> getNewTempFile ();
copy ( $sourceFilepath , $filepath );
$file = $this -> dataFile ( $sourceFilename , 'image/jpeg' );
$this -> handler -> swapICCProfile (
$filepath ,
[ 'sRGB' , '-' ],
[ $oldProfileName ],
$profileFilepath
);
$this -> assertEquals (
sha1 ( file_get_contents ( $filepath ) ),
sha1 ( file_get_contents ( $controlFilepath ) )
);
}
public function provideSwappingICCProfile () {
return [
// File with sRGB should end up with TinyRGB
[
'srgb.jpg' ,
'tinyrgb.jpg' ,
'tinyrgb.icc' ,
'sRGB IEC61966-2.1'
],
// File with TinyRGB should be left unchanged
[
'tinyrgb.jpg' ,
'tinyrgb.jpg' ,
'tinyrgb.icc' ,
'sRGB IEC61966-2.1'
],
// File without profile should end up with TinyRGB
[
'missingprofile.jpg' ,
'tinyrgb.jpg' ,
'tinyrgb.icc' ,
'sRGB IEC61966-2.1'
],
// Non-sRGB file should be left untouched
[
'adobergb.jpg' ,
'adobergb.jpg' ,
'tinyrgb.icc' ,
'sRGB IEC61966-2.1'
]
];
}
2011-07-11 16:42:15 +00:00
}