6 class BitmapMetadataHandlerTest
extends MediaWikiTestCase
{
8 protected function setUp() {
11 $this->setMwGlobals( 'wgShowEXIF', false );
13 $this->filePath
= __DIR__
. '/../../data/media/';
17 * Test if having conflicting metadata values from different
18 * types of metadata, that the right one takes precedence.
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
25 public function testMultilingualCascade() {
26 $this->checkPHPExtension( 'exif' );
27 $this->checkPHPExtension( 'xml' );
29 $this->setMwGlobals( 'wgShowEXIF', true );
31 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
32 '/Xmp-exif-multilingual_test.jpg' );
35 'x-default' => 'right(iptc)',
36 'en' => 'right translation',
40 $this->assertArrayHasKey( 'ImageDescription', $meta,
41 'Did not extract any ImageDescription info?!' );
43 $this->assertEquals( $expected, $meta['ImageDescription'] );
47 * Test for jpeg comments are being handled by
48 * BitmapMetadataHandler correctly.
50 * There's more extensive tests of comment extraction in
51 * JpegMetadataExtractorTests.php
52 * @covers BitmapMetadataHandler::Jpeg
54 public function testJpegComment() {
55 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
56 'jpeg-comment-utf.jpg' );
58 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
59 $meta['JPEGFileComment'][0] );
63 * Make sure a bad iptc block doesn't stop the other metadata
64 * from being extracted.
65 * @covers BitmapMetadataHandler::Jpeg
67 public function testBadIPTC() {
68 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
69 'iptc-invalid-psir.jpg' );
70 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
74 * @covers BitmapMetadataHandler::Jpeg
76 public function testIPTCDates() {
77 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
78 'iptc-timetest.jpg' );
80 // raw date is 2020:07:13 14:04:05+11:32
81 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
82 // raw date is 1997:03:02 03:01:02-03:00
83 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
85 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
86 'iptc-timetest-invalid.jpg' );
88 // raw date is 1845:03:02 03:01:02-03:00
89 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
90 // raw date is 1942:07:13 25:05:02+00:00
91 $this->assertSame( '1942:07:14 01:05:02', $meta['DateTimeDigitized'] );
95 * XMP data should take priority over iptc data
96 * when hash has been updated, but not when
98 * @covers BitmapMetadataHandler::addMetadata
99 * @covers BitmapMetadataHandler::getMetadataArray
101 public function testMerging() {
102 $merger = new BitmapMetadataHandler();
103 $merger->addMetadata( [ 'foo' => 'xmp' ], 'xmp-general' );
104 $merger->addMetadata( [ 'bar' => 'xmp' ], 'xmp-general' );
105 $merger->addMetadata( [ 'baz' => 'xmp' ], 'xmp-general' );
106 $merger->addMetadata( [ 'fred' => 'xmp' ], 'xmp-general' );
107 $merger->addMetadata( [ 'foo' => 'iptc (hash)' ], 'iptc-good-hash' );
108 $merger->addMetadata( [ 'bar' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
109 $merger->addMetadata( [ 'baz' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
110 $merger->addMetadata( [ 'fred' => 'iptc (no hash)' ], 'iptc-no-hash' );
111 $merger->addMetadata( [ 'baz' => 'exif' ], 'exif' );
113 $actual = $merger->getMetadataArray();
116 'bar' => 'iptc (bad hash)',
120 $this->assertEquals( $expected, $actual );
124 * @covers BitmapMetadataHandler::png
126 public function testPNGXMP() {
127 $this->checkPHPExtension( 'xml' );
129 $handler = new BitmapMetadataHandler();
130 $result = $handler->PNG( $this->filePath
. 'xmp.png' );
136 'colorType' => 'index-coloured',
138 'SerialNumber' => '123456789',
139 '_MW_PNG_VERSION' => 1,
142 $this->assertEquals( $expected, $result );
146 * @covers BitmapMetadataHandler::png
148 public function testPNGNative() {
149 $handler = new BitmapMetadataHandler();
150 $result = $handler->PNG( $this->filePath
. 'Png-native-test.png' );
151 $expected = 'http://example.com/url';
152 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
156 * @covers BitmapMetadataHandler::getTiffByteOrder
158 public function testTiffByteOrder() {
159 $handler = new BitmapMetadataHandler();
160 $res = $handler->getTiffByteOrder( $this->filePath
. 'test.tiff' );
161 $this->assertEquals( 'LE', $res );