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.
19 * @covers BitmapMetadataHandler::Jpeg
21 public function testMultilingualCascade() {
22 $this->checkPHPExtension( 'exif' );
23 $this->checkPHPExtension( 'xml' );
25 $this->setMwGlobals( 'wgShowEXIF', true );
27 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
28 '/Xmp-exif-multilingual_test.jpg' );
31 'x-default' => 'right(iptc)',
32 'en' => 'right translation',
36 $this->assertArrayHasKey( 'ImageDescription', $meta,
37 'Did not extract any ImageDescription info?!' );
39 $this->assertEquals( $expected, $meta['ImageDescription'] );
43 * Test for jpeg comments are being handled by
44 * BitmapMetadataHandler correctly.
46 * There's more extensive tests of comment extraction in
47 * JpegMetadataExtractorTests.php
48 * @covers BitmapMetadataHandler::Jpeg
50 public function testJpegComment() {
51 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
52 'jpeg-comment-utf.jpg' );
54 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
55 $meta['JPEGFileComment'][0] );
59 * Make sure a bad iptc block doesn't stop the other metadata
60 * from being extracted.
61 * @covers BitmapMetadataHandler::Jpeg
63 public function testBadIPTC() {
64 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
65 'iptc-invalid-psir.jpg' );
66 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
70 * @covers BitmapMetadataHandler::Jpeg
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
83 * @covers BitmapMetadataHandler::Jpeg
85 public function testIPTCDatesInvalid() {
86 $meta = BitmapMetadataHandler
::Jpeg( $this->filePath
.
87 'iptc-timetest-invalid.jpg' );
89 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
90 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
94 * XMP data should take priority over iptc data
95 * when hash has been updated, but not when
97 * @covers BitmapMetadataHandler::addMetadata
98 * @covers BitmapMetadataHandler::getMetadataArray
100 public function testMerging() {
101 $merger = new BitmapMetadataHandler();
102 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
103 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
104 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
105 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
106 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
107 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
108 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
109 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
110 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
112 $actual = $merger->getMetadataArray();
115 'bar' => 'iptc (bad hash)',
119 $this->assertEquals( $expected, $actual );
123 * @covers BitmapMetadataHandler::png
125 public function testPNGXMP() {
126 if ( !extension_loaded( 'xml' ) ) {
127 $this->markTestSkipped( "This test needs the xml extension." );
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 );