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() {
23 if ( !wfDl( 'exif' ) ) {
24 $this->markTestSkipped( "This test needs the exif extension." );
26 if ( !wfDl( 'xml' ) ) {
27 $this->markTestSkipped( "This test needs the xml extension." );
32 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
33 '/Xmp-exif-multilingual_test.jpg' );
36 'x-default' => 'right(iptc)',
37 'en' => 'right translation',
41 $this->assertArrayHasKey( 'ImageDescription', $meta,
42 'Did not extract any ImageDescription info?!' );
44 $this->assertEquals( $expected, $meta['ImageDescription'] );
48 * Test for jpeg comments are being handled by
49 * BitmapMetadataHandler correctly.
51 * There's more extensive tests of comment extraction in
52 * JpegMetadataExtractorTests.php
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.
66 public function testBadIPTC() {
67 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
68 'iptc-invalid-psir.jpg' );
69 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
72 public function testIPTCDates() {
73 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
74 'iptc-timetest.jpg' );
76 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
77 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
81 * File has an invalid time (+ one valid but really weird time)
82 * that shouldn't be included
84 public function testIPTCDatesInvalid() {
85 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
86 'iptc-timetest-invalid.jpg' );
88 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
89 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
93 * XMP data should take priority over iptc data
94 * when hash has been updated, but not when
97 public function testMerging() {
98 $merger = new BitmapMetadataHandler();
99 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
100 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
101 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
102 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
103 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
104 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
105 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
106 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
107 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
109 $actual = $merger->getMetadataArray();
112 'bar' => 'iptc (bad hash)',
116 $this->assertEquals( $expected, $actual );
119 public function testPNGXMP() {
120 if ( !wfDl( 'xml' ) ) {
121 $this->markTestSkipped( "This test needs the xml extension." );
123 $handler = new BitmapMetadataHandler();
124 $result = $handler->png( $this->filePath
. 'xmp.png' );
130 'colorType' => 'index-coloured',
131 'metadata' => array (
132 'SerialNumber' => '123456789',
133 '_MW_PNG_VERSION' => 1,
136 $this->assertEquals( $expected, $result );
139 public function testPNGNative() {
140 $handler = new BitmapMetadataHandler();
141 $result = $handler->png( $this->filePath
. 'Png-native-test.png' );
142 $expected = 'http://example.com/url';
143 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
146 public function testTiffByteOrder() {
147 $handler = new BitmapMetadataHandler();
148 $res = $handler->getTiffByteOrder( $this->filePath
. 'test.tiff' );
149 $this->assertEquals( 'LE', $res );