RCFilters: Style the Saved Links placeholder and add a title
[lhc/web/wiklou.git] / tests / phpunit / includes / media / JpegTest.php
1 <?php
2
3 /**
4 * @group Media
5 * @covers JpegHandler
6 */
7 class JpegTest extends MediaWikiMediaTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11 $this->checkPHPExtension( 'exif' );
12
13 $this->setMwGlobals( 'wgShowEXIF', true );
14
15 $this->handler = new JpegHandler;
16 }
17
18 public function testInvalidFile() {
19 $file = $this->dataFile( 'README', 'image/jpeg' );
20 $res = $this->handler->getMetadata( $file, $this->filePath . 'README' );
21 $this->assertEquals( ExifBitmapHandler::BROKEN_FILE, $res );
22 }
23
24 public function testJpegMetadataExtraction() {
25 $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
26 $res = $this->handler->getMetadata( $file, $this->filePath . 'test.jpg' );
27 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
28 $expected = 'a:9:{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;s:5:"Width";i:20;s:6:"Height";i:20;}';
29 // @codingStandardsIgnoreEnd
30
31 // Unserialize in case serialization format ever changes.
32 $this->assertEquals( unserialize( $expected ), unserialize( $res ) );
33 }
34
35 /**
36 * @covers JpegHandler::getCommonMetaArray
37 */
38 public function testGetIndependentMetaArray() {
39 $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
40 $res = $this->handler->getCommonMetaArray( $file );
41 $expected = [
42 'Height' => 20,
43 'Width' => 20,
44 'ImageDescription' => 'Test file',
45 'XResolution' => '72/1',
46 'YResolution' => '72/1',
47 'ResolutionUnit' => 2,
48 'YCbCrPositioning' => 1,
49 'JPEGFileComment' => [
50 'Created with GIMP',
51 ],
52 ];
53
54 $this->assertEquals( $res, $expected );
55 }
56
57 /**
58 * @dataProvider provideSwappingICCProfile
59 * @covers JpegHandler::swapICCProfile
60 */
61 public function testSwappingICCProfile(
62 $sourceFilename, $controlFilename, $newProfileFilename, $oldProfileName
63 ) {
64 global $wgExiftool;
65
66 if ( !$wgExiftool || !is_file( $wgExiftool ) ) {
67 $this->markTestSkipped( "Exiftool not installed, cannot test ICC profile swapping" );
68 }
69
70 $this->setMwGlobals( 'wgUseTinyRGBForJPGThumbnails', true );
71
72 $sourceFilepath = $this->filePath . $sourceFilename;
73 $controlFilepath = $this->filePath . $controlFilename;
74 $profileFilepath = $this->filePath . $newProfileFilename;
75 $filepath = $this->getNewTempFile();
76
77 copy( $sourceFilepath, $filepath );
78
79 $file = $this->dataFile( $sourceFilename, 'image/jpeg' );
80 $this->handler->swapICCProfile(
81 $filepath,
82 [ 'sRGB', '-' ],
83 [ $oldProfileName ],
84 $profileFilepath
85 );
86
87 $this->assertEquals(
88 sha1( file_get_contents( $filepath ) ),
89 sha1( file_get_contents( $controlFilepath ) )
90 );
91 }
92
93 public function provideSwappingICCProfile() {
94 return [
95 // File with sRGB should end up with TinyRGB
96 [
97 'srgb.jpg',
98 'tinyrgb.jpg',
99 'tinyrgb.icc',
100 'sRGB IEC61966-2.1'
101 ],
102 // File with TinyRGB should be left unchanged
103 [
104 'tinyrgb.jpg',
105 'tinyrgb.jpg',
106 'tinyrgb.icc',
107 'sRGB IEC61966-2.1'
108 ],
109 // File without profile should end up with TinyRGB
110 [
111 'missingprofile.jpg',
112 'tinyrgb.jpg',
113 'tinyrgb.icc',
114 'sRGB IEC61966-2.1'
115 ],
116 // Non-sRGB file should be left untouched
117 [
118 'adobergb.jpg',
119 'adobergb.jpg',
120 'tinyrgb.icc',
121 'sRGB IEC61966-2.1'
122 ]
123 ];
124 }
125 }