f70b42de4207c2dba3756e46c2e41cad20122214
[lhc/web/wiklou.git] / tests / phpunit / includes / media / ExifBitmapTest.php
1 <?php
2
3 /**
4 * @group Media
5 */
6 class ExifBitmapTest extends MediaWikiMediaTestCase {
7
8 /**
9 * @var ExifBitmapHandler
10 */
11 protected $handler;
12
13 protected function setUp() {
14 parent::setUp();
15 $this->checkPHPExtension( 'exif' );
16
17 $this->setMwGlobals( 'wgShowEXIF', true );
18
19 $this->handler = new ExifBitmapHandler;
20
21 }
22
23 /**
24 * @covers ExifBitmapHandler::isMetadataValid
25 */
26 public function testIsOldBroken() {
27 $res = $this->handler->isMetadataValid( null, ExifBitmapHandler::OLD_BROKEN_FILE );
28 $this->assertEquals( ExifBitmapHandler::METADATA_COMPATIBLE, $res );
29 }
30
31 /**
32 * @covers ExifBitmapHandler::isMetadataValid
33 */
34 public function testIsBrokenFile() {
35 $res = $this->handler->isMetadataValid( null, ExifBitmapHandler::BROKEN_FILE );
36 $this->assertEquals( ExifBitmapHandler::METADATA_GOOD, $res );
37 }
38
39 /**
40 * @covers ExifBitmapHandler::isMetadataValid
41 */
42 public function testIsInvalid() {
43 $res = $this->handler->isMetadataValid( null, 'Something Invalid Here.' );
44 $this->assertEquals( ExifBitmapHandler::METADATA_BAD, $res );
45 }
46
47 /**
48 * @covers ExifBitmapHandler::isMetadataValid
49 */
50 public function testGoodMetadata() {
51 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
52 $meta = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}';
53 // @codingStandardsIgnoreEnd
54 $res = $this->handler->isMetadataValid( null, $meta );
55 $this->assertEquals( ExifBitmapHandler::METADATA_GOOD, $res );
56 }
57
58 /**
59 * @covers ExifBitmapHandler::isMetadataValid
60 */
61 public function testIsOldGood() {
62 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
63 $meta = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:1;}';
64 // @codingStandardsIgnoreEnd
65 $res = $this->handler->isMetadataValid( null, $meta );
66 $this->assertEquals( ExifBitmapHandler::METADATA_COMPATIBLE, $res );
67 }
68
69 /**
70 * Handle metadata from paged tiff handler (gotten via instant commons) gracefully.
71 * @covers ExifBitmapHandler::isMetadataValid
72 */
73 public function testPagedTiffHandledGracefully() {
74 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
75 $meta = 'a:6:{s:9:"page_data";a:1:{i:1;a:5:{s:5:"width";i:643;s:6:"height";i:448;s:5:"alpha";s:4:"true";s:4:"page";i:1;s:6:"pixels";i:288064;}}s:10:"page_count";i:1;s:10:"first_page";i:1;s:9:"last_page";i:1;s:4:"exif";a:9:{s:10:"ImageWidth";i:643;s:11:"ImageLength";i:448;s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:4;s:12:"RowsPerStrip";i:50;s:19:"PlanarConfiguration";i:1;s:22:"MEDIAWIKI_EXIF_VERSION";i:1;}s:21:"TIFF_METADATA_VERSION";s:3:"1.4";}';
76 // @codingStandardsIgnoreEnd
77 $res = $this->handler->isMetadataValid( null, $meta );
78 $this->assertEquals( ExifBitmapHandler::METADATA_BAD, $res );
79 }
80
81 /**
82 * @covers ExifBitmapHandler::convertMetadataVersion
83 */
84 public function testConvertMetadataLatest() {
85 $metadata = [
86 'foo' => [ 'First', 'Second', '_type' => 'ol' ],
87 'MEDIAWIKI_EXIF_VERSION' => 2
88 ];
89 $res = $this->handler->convertMetadataVersion( $metadata, 2 );
90 $this->assertEquals( $metadata, $res );
91 }
92
93 /**
94 * @covers ExifBitmapHandler::convertMetadataVersion
95 */
96 public function testConvertMetadataToOld() {
97 $metadata = [
98 'foo' => [ 'First', 'Second', '_type' => 'ol' ],
99 'bar' => [ 'First', 'Second', '_type' => 'ul' ],
100 'baz' => [ 'First', 'Second' ],
101 'fred' => 'Single',
102 'MEDIAWIKI_EXIF_VERSION' => 2,
103 ];
104 $expected = [
105 'foo' => "\n#First\n#Second",
106 'bar' => "\n*First\n*Second",
107 'baz' => "\n*First\n*Second",
108 'fred' => 'Single',
109 'MEDIAWIKI_EXIF_VERSION' => 1,
110 ];
111 $res = $this->handler->convertMetadataVersion( $metadata, 1 );
112 $this->assertEquals( $expected, $res );
113 }
114
115 /**
116 * @covers ExifBitmapHandler::convertMetadataVersion
117 */
118 public function testConvertMetadataSoftware() {
119 $metadata = [
120 'Software' => [ [ 'GIMP', '1.1' ] ],
121 'MEDIAWIKI_EXIF_VERSION' => 2,
122 ];
123 $expected = [
124 'Software' => 'GIMP (Version 1.1)',
125 'MEDIAWIKI_EXIF_VERSION' => 1,
126 ];
127 $res = $this->handler->convertMetadataVersion( $metadata, 1 );
128 $this->assertEquals( $expected, $res );
129 }
130
131 /**
132 * @covers ExifBitmapHandler::convertMetadataVersion
133 */
134 public function testConvertMetadataSoftwareNormal() {
135 $metadata = [
136 'Software' => [ "GIMP 1.2", "vim" ],
137 'MEDIAWIKI_EXIF_VERSION' => 2,
138 ];
139 $expected = [
140 'Software' => "\n*GIMP 1.2\n*vim",
141 'MEDIAWIKI_EXIF_VERSION' => 1,
142 ];
143 $res = $this->handler->convertMetadataVersion( $metadata, 1 );
144 $this->assertEquals( $expected, $res );
145 }
146
147 /**
148 * @dataProvider provideSwappingICCProfile
149 * @covers ExifBitmapHandler::swapICCProfile
150 */
151 public function testSwappingICCProfile(
152 $sourceFilename, $controlFilename, $newProfileFilename, $oldProfileName
153 ) {
154 global $wgExiftool;
155
156 if ( !$wgExiftool || !is_file( $wgExiftool ) ) {
157 $this->markTestSkipped( "Exiftool not installed, cannot test ICC profile swapping" );
158 }
159
160 $this->setMwGlobals( 'wgUseTinyRGBForJPGThumbnails', true );
161
162 $sourceFilepath = $this->filePath . $sourceFilename;
163 $controlFilepath = $this->filePath . $controlFilename;
164 $profileFilepath = $this->filePath . $newProfileFilename;
165 $filepath = $this->getNewTempFile();
166
167 copy( $sourceFilepath, $filepath );
168
169 $file = $this->dataFile( $sourceFilename, 'image/jpeg' );
170 $this->handler->swapICCProfile( $filepath, $oldProfileName, $profileFilepath );
171
172 $this->assertEquals(
173 sha1( file_get_contents( $filepath ) ),
174 sha1( file_get_contents( $controlFilepath ) )
175 );
176 }
177
178 public function provideSwappingICCProfile() {
179 return [
180 // File with sRGB should end up with TinyRGB
181 [
182 'srgb.jpg',
183 'tinyrgb.jpg',
184 'tinyrgb.icc',
185 'IEC 61966-2.1 Default RGB colour space - sRGB'
186 ],
187 // File with TinyRGB should be left unchanged
188 [
189 'tinyrgb.jpg',
190 'tinyrgb.jpg',
191 'tinyrgb.icc',
192 'IEC 61966-2.1 Default RGB colour space - sRGB'
193 ],
194 // File with no profile should be left unchanged
195 [
196 'test.jpg',
197 'test.jpg',
198 'tinyrgb.icc',
199 'IEC 61966-2.1 Default RGB colour space - sRGB'
200 ]
201 ];
202 }
203 }