2 class BitmapMetadataHandlerTest
extends MediaWikiTestCase
{
4 protected function setUp() {
7 $this->setMwGlobals( 'wgShowEXIF', false );
9 $this->filePath
= __DIR__
. '/../../data/media/';
13 * Test if having conflicting metadata values from different
14 * types of metadata, that the right one takes precedence.
16 * Basically the file has IPTC and XMP metadata, the
17 * IPTC should override the XMP, except for the multilingual
18 * translation (to en) where XMP should win.
20 public function testMultilingualCascade() {
21 if ( !wfDl( 'exif' ) ) {
22 $this->markTestSkipped( "This test needs the exif extension." );
24 if ( !wfDl( 'xml' ) ) {
25 $this->markTestSkipped( "This test needs the xml extension." );
28 $this->setMwGlobals( 'wgShowEXIF', true );
30 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
31 '/Xmp-exif-multilingual_test.jpg' );
34 'x-default' => 'right(iptc)',
35 'en' => 'right translation',
39 $this->assertArrayHasKey( 'ImageDescription', $meta,
40 'Did not extract any ImageDescription info?!' );
42 $this->assertEquals( $expected, $meta['ImageDescription'] );
46 * Test for jpeg comments are being handled by
47 * BitmapMetadataHandler correctly.
49 * There's more extensive tests of comment extraction in
50 * JpegMetadataExtractorTests.php
52 public function testJpegComment() {
53 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
54 'jpeg-comment-utf.jpg' );
56 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
57 $meta['JPEGFileComment'][0] );
61 * Make sure a bad iptc block doesn't stop the other metadata
62 * from being extracted.
64 public function testBadIPTC() {
65 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
66 'iptc-invalid-psir.jpg' );
67 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
70 public function testIPTCDates() {
71 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
72 'iptc-timetest.jpg' );
74 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
75 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
79 * File has an invalid time (+ one valid but really weird time)
80 * that shouldn't be included
82 public function testIPTCDatesInvalid() {
83 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
84 'iptc-timetest-invalid.jpg' );
86 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
87 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
91 * XMP data should take priority over iptc data
92 * when hash has been updated, but not when
95 public function testMerging() {
96 $merger = new BitmapMetadataHandler();
97 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
98 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
99 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
100 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
101 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
102 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
103 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
104 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
105 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
107 $actual = $merger->getMetadataArray();
110 'bar' => 'iptc (bad hash)',
114 $this->assertEquals( $expected, $actual );
117 public function testPNGXMP() {
118 if ( !wfDl( 'xml' ) ) {
119 $this->markTestSkipped( "This test needs the xml extension." );
121 $handler = new BitmapMetadataHandler();
122 $result = $handler->png( $this->filePath
. 'xmp.png' );
128 'colorType' => 'index-coloured',
130 'SerialNumber' => '123456789',
131 '_MW_PNG_VERSION' => 1,
134 $this->assertEquals( $expected, $result );
137 public function testPNGNative() {
138 $handler = new BitmapMetadataHandler();
139 $result = $handler->png( $this->filePath
. 'Png-native-test.png' );
140 $expected = 'http://example.com/url';
141 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
144 public function testTiffByteOrder() {
145 $handler = new BitmapMetadataHandler();
146 $res = $handler->getTiffByteOrder( $this->filePath
. 'test.tiff' );
147 $this->assertEquals( 'LE', $res );