]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/media/BitmapMetadataHandlerTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / tests / phpunit / includes / media / BitmapMetadataHandlerTest.php
1 <?php
2
3 /**
4  * @group Media
5  */
6 class BitmapMetadataHandlerTest extends MediaWikiTestCase {
7
8         protected function setUp() {
9                 parent::setUp();
10
11                 $this->setMwGlobals( 'wgShowEXIF', false );
12
13                 $this->filePath = __DIR__ . '/../../data/media/';
14         }
15
16         /**
17          * Test if having conflicting metadata values from different
18          * types of metadata, that the right one takes precedence.
19          *
20          * Basically the file has IPTC and XMP metadata, the
21          * IPTC should override the XMP, except for the multilingual
22          * translation (to en) where XMP should win.
23          * @covers BitmapMetadataHandler::Jpeg
24          */
25         public function testMultilingualCascade() {
26                 $this->checkPHPExtension( 'exif' );
27                 $this->checkPHPExtension( 'xml' );
28
29                 $this->setMwGlobals( 'wgShowEXIF', true );
30
31                 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
32                         '/Xmp-exif-multilingual_test.jpg' );
33
34                 $expected = [
35                         'x-default' => 'right(iptc)',
36                         'en' => 'right translation',
37                         '_type' => 'lang'
38                 ];
39
40                 $this->assertArrayHasKey( 'ImageDescription', $meta,
41                         'Did not extract any ImageDescription info?!' );
42
43                 $this->assertEquals( $expected, $meta['ImageDescription'] );
44         }
45
46         /**
47          * Test for jpeg comments are being handled by
48          * BitmapMetadataHandler correctly.
49          *
50          * There's more extensive tests of comment extraction in
51          * JpegMetadataExtractorTests.php
52          * @covers BitmapMetadataHandler::Jpeg
53          */
54         public function testJpegComment() {
55                 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
56                         'jpeg-comment-utf.jpg' );
57
58                 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
59                         $meta['JPEGFileComment'][0] );
60         }
61
62         /**
63          * Make sure a bad iptc block doesn't stop the other metadata
64          * from being extracted.
65          * @covers BitmapMetadataHandler::Jpeg
66          */
67         public function testBadIPTC() {
68                 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
69                         'iptc-invalid-psir.jpg' );
70                 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
71         }
72
73         /**
74          * @covers BitmapMetadataHandler::Jpeg
75          */
76         public function testIPTCDates() {
77                 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
78                         'iptc-timetest.jpg' );
79
80                 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
81                 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
82         }
83
84         /**
85          * File has an invalid time (+ one valid but really weird time)
86          * that shouldn't be included
87          * @covers BitmapMetadataHandler::Jpeg
88          */
89         public function testIPTCDatesInvalid() {
90                 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
91                         'iptc-timetest-invalid.jpg' );
92
93                 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
94                 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
95         }
96
97         /**
98          * XMP data should take priority over iptc data
99          * when hash has been updated, but not when
100          * the hash is wrong.
101          * @covers BitmapMetadataHandler::addMetadata
102          * @covers BitmapMetadataHandler::getMetadataArray
103          */
104         public function testMerging() {
105                 $merger = new BitmapMetadataHandler();
106                 $merger->addMetadata( [ 'foo' => 'xmp' ], 'xmp-general' );
107                 $merger->addMetadata( [ 'bar' => 'xmp' ], 'xmp-general' );
108                 $merger->addMetadata( [ 'baz' => 'xmp' ], 'xmp-general' );
109                 $merger->addMetadata( [ 'fred' => 'xmp' ], 'xmp-general' );
110                 $merger->addMetadata( [ 'foo' => 'iptc (hash)' ], 'iptc-good-hash' );
111                 $merger->addMetadata( [ 'bar' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
112                 $merger->addMetadata( [ 'baz' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
113                 $merger->addMetadata( [ 'fred' => 'iptc (no hash)' ], 'iptc-no-hash' );
114                 $merger->addMetadata( [ 'baz' => 'exif' ], 'exif' );
115
116                 $actual = $merger->getMetadataArray();
117                 $expected = [
118                         'foo' => 'xmp',
119                         'bar' => 'iptc (bad hash)',
120                         'baz' => 'exif',
121                         'fred' => 'xmp',
122                 ];
123                 $this->assertEquals( $expected, $actual );
124         }
125
126         /**
127          * @covers BitmapMetadataHandler::png
128          */
129         public function testPNGXMP() {
130                 if ( !extension_loaded( 'xml' ) ) {
131                         $this->markTestSkipped( "This test needs the xml extension." );
132                 }
133                 $handler = new BitmapMetadataHandler();
134                 $result = $handler->PNG( $this->filePath . 'xmp.png' );
135                 $expected = [
136                         'frameCount' => 0,
137                         'loopCount' => 1,
138                         'duration' => 0,
139                         'bitDepth' => 1,
140                         'colorType' => 'index-coloured',
141                         'metadata' => [
142                                 'SerialNumber' => '123456789',
143                                 '_MW_PNG_VERSION' => 1,
144                         ],
145                 ];
146                 $this->assertEquals( $expected, $result );
147         }
148
149         /**
150          * @covers BitmapMetadataHandler::png
151          */
152         public function testPNGNative() {
153                 $handler = new BitmapMetadataHandler();
154                 $result = $handler->PNG( $this->filePath . 'Png-native-test.png' );
155                 $expected = 'http://example.com/url';
156                 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
157         }
158
159         /**
160          * @covers BitmapMetadataHandler::getTiffByteOrder
161          */
162         public function testTiffByteOrder() {
163                 $handler = new BitmapMetadataHandler();
164                 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
165                 $this->assertEquals( 'LE', $res );
166         }
167 }