Merge "CSSMin: Skip #default#behaviorName when detecting local files"
[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: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;}';
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 'ImageDescription' => 'Test file',
43 'XResolution' => '72/1',
44 'YResolution' => '72/1',
45 'ResolutionUnit' => 2,
46 'YCbCrPositioning' => 1,
47 'JPEGFileComment' => [
48 'Created with GIMP',
49 ],
50 ];
51
52 $this->assertEquals( $res, $expected );
53 }
54
55 /**
56 * @dataProvider provideSwappingICCProfile
57 * @covers JpegHandler::swapICCProfile
58 */
59 public function testSwappingICCProfile(
60 $sourceFilename, $controlFilename, $newProfileFilename, $oldProfileName
61 ) {
62 global $wgExiftool;
63
64 if ( !$wgExiftool || !is_file( $wgExiftool ) ) {
65 $this->markTestSkipped( "Exiftool not installed, cannot test ICC profile swapping" );
66 }
67
68 $this->setMwGlobals( 'wgUseTinyRGBForJPGThumbnails', true );
69
70 $sourceFilepath = $this->filePath . $sourceFilename;
71 $controlFilepath = $this->filePath . $controlFilename;
72 $profileFilepath = $this->filePath . $newProfileFilename;
73 $filepath = $this->getNewTempFile();
74
75 copy( $sourceFilepath, $filepath );
76
77 $file = $this->dataFile( $sourceFilename, 'image/jpeg' );
78 $this->handler->swapICCProfile(
79 $filepath,
80 [ 'sRGB', '-' ],
81 [ $oldProfileName ],
82 $profileFilepath
83 );
84
85 $this->assertEquals(
86 sha1( file_get_contents( $filepath ) ),
87 sha1( file_get_contents( $controlFilepath ) )
88 );
89 }
90
91 public function provideSwappingICCProfile() {
92 return [
93 // File with sRGB should end up with TinyRGB
94 [
95 'srgb.jpg',
96 'tinyrgb.jpg',
97 'tinyrgb.icc',
98 'sRGB IEC61966-2.1'
99 ],
100 // File with TinyRGB should be left unchanged
101 [
102 'tinyrgb.jpg',
103 'tinyrgb.jpg',
104 'tinyrgb.icc',
105 'sRGB IEC61966-2.1'
106 ],
107 // File without profile should end up with TinyRGB
108 [
109 'missingprofile.jpg',
110 'tinyrgb.jpg',
111 'tinyrgb.icc',
112 'sRGB IEC61966-2.1'
113 ],
114 // Non-sRGB file should be left untouched
115 [
116 'adobergb.jpg',
117 'adobergb.jpg',
118 'tinyrgb.icc',
119 'sRGB IEC61966-2.1'
120 ]
121 ];
122 }
123 }