Merge "AutoLoader: Use require_once rather than require"
[mediawiki.git] / tests / phpunit / includes / media / JpegTest.php
blob422e81007bd53ef809aadfc80f05c02f08c8652d
1 <?php
3 use MediaWiki\MainConfigNames;
5 /**
6 * @group Media
7 * @covers \JpegHandler
8 * @requires extension exif
9 */
10 class JpegTest extends MediaWikiMediaTestCase {
11 /** @var JpegHandler */
12 private $handler;
14 protected function setUp(): void {
15 parent::setUp();
17 $this->overrideConfigValue( MainConfigNames::ShowEXIF, true );
19 $this->handler = new JpegHandler;
22 public function testInvalidFile() {
23 $file = $this->dataFile( 'README', 'image/jpeg' );
24 $res = $this->handler->getSizeAndMetadataWithFallback( $file, $this->filePath . 'README' );
25 $this->assertEquals( [ '_error' => ExifBitmapHandler::BROKEN_FILE ], $res['metadata'] );
28 public function testJpegMetadataExtraction() {
29 $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
30 $res = $this->handler->getSizeAndMetadataWithFallback( $file, $this->filePath . 'test.jpg' );
31 $expected = [
32 'ImageDescription' => 'Test file',
33 'XResolution' => '72/1',
34 'YResolution' => '72/1',
35 'ResolutionUnit' => 2,
36 'YCbCrPositioning' => 1,
37 'JPEGFileComment' => [
38 0 => 'Created with GIMP',
40 'MEDIAWIKI_EXIF_VERSION' => 2,
43 // Unserialize in case serialization format ever changes.
44 $this->assertEquals( $expected, $res['metadata'] );
47 /**
48 * @covers \JpegHandler::getCommonMetaArray
50 public function testGetIndependentMetaArray() {
51 $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
52 $res = $this->handler->getCommonMetaArray( $file );
53 $expected = [
54 'ImageDescription' => 'Test file',
55 'XResolution' => '72/1',
56 'YResolution' => '72/1',
57 'ResolutionUnit' => 2,
58 'YCbCrPositioning' => 1,
59 'JPEGFileComment' => [
60 'Created with GIMP',
64 $this->assertEquals( $expected, $res );
67 /**
68 * @dataProvider provideSwappingICCProfile
69 * @covers \JpegHandler::swapICCProfile
71 public function testSwappingICCProfile(
72 $sourceFilename, $controlFilename, $newProfileFilename, $oldProfileName
73 ) {
74 global $wgExiftool;
76 if ( !$wgExiftool || !is_file( $wgExiftool ) ) {
77 $this->markTestSkipped( "Exiftool not installed, cannot test ICC profile swapping" );
80 $this->overrideConfigValue( MainConfigNames::UseTinyRGBForJPGThumbnails, true );
82 $sourceFilepath = $this->filePath . $sourceFilename;
83 $controlFilepath = $this->filePath . $controlFilename;
84 $profileFilepath = $this->filePath . $newProfileFilename;
85 $filepath = $this->getNewTempFile();
87 copy( $sourceFilepath, $filepath );
89 $this->handler->swapICCProfile(
90 $filepath,
91 [ 'sRGB', '-' ],
92 [ $oldProfileName ],
93 $profileFilepath
96 $this->assertEquals(
97 sha1( file_get_contents( $filepath ) ),
98 sha1( file_get_contents( $controlFilepath ) )
102 public static function provideSwappingICCProfile() {
103 return [
104 // File with sRGB should end up with TinyRGB
106 'srgb.jpg',
107 'tinyrgb.jpg',
108 'tinyrgb.icc',
109 'sRGB IEC61966-2.1'
111 // File with TinyRGB should be left unchanged
113 'tinyrgb.jpg',
114 'tinyrgb.jpg',
115 'tinyrgb.icc',
116 'sRGB IEC61966-2.1'
118 // File without profile should end up with TinyRGB
120 'missingprofile.jpg',
121 'tinyrgb.jpg',
122 'tinyrgb.icc',
123 'sRGB IEC61966-2.1'
125 // Non-sRGB file should be left untouched
127 'adobergb.jpg',
128 'adobergb.jpg',
129 'tinyrgb.icc',
130 'sRGB IEC61966-2.1'