Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / media / BitmapMetadataHandlerTest.php
bloba70c00549793bb397774f6d4dc4d2aad9220890a
1 <?php
3 /**
4 * @group Media
5 */
6 class BitmapMetadataHandlerTest extends MediaWikiTestCase {
8 protected function setUp() {
9 parent::setUp();
11 $this->setMwGlobals( 'wgShowEXIF', false );
13 $this->filePath = __DIR__ . '/../../data/media/';
16 /**
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' );
34 $expected = [
35 'x-default' => 'right(iptc)',
36 'en' => 'right translation',
37 '_type' => 'lang'
40 $this->assertArrayHasKey( 'ImageDescription', $meta,
41 'Did not extract any ImageDescription info?!' );
43 $this->assertEquals( $expected, $meta['ImageDescription'] );
46 /**
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] );
62 /**
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] );
73 /**
74 * @covers BitmapMetadataHandler::Jpeg
76 public function testIPTCDates() {
77 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
78 'iptc-timetest.jpg' );
80 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
81 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
84 /**
85 * File has an invalid time (+ one valid but really weird time)
86 * that shouldn't be included
87 * @covers BitmapMetadataHandler::Jpeg
89 public function testIPTCDatesInvalid() {
90 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
91 'iptc-timetest-invalid.jpg' );
93 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
94 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
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
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' );
116 $actual = $merger->getMetadataArray();
117 $expected = [
118 'foo' => 'xmp',
119 'bar' => 'iptc (bad hash)',
120 'baz' => 'exif',
121 'fred' => 'xmp',
123 $this->assertEquals( $expected, $actual );
127 * @covers BitmapMetadataHandler::png
129 public function testPNGXMP() {
130 if ( !extension_loaded( 'xml' ) ) {
131 $this->markTestSkipped( "This test needs the xml extension." );
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,
146 $this->assertEquals( $expected, $result );
150 * @covers BitmapMetadataHandler::png
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'] );
160 * @covers BitmapMetadataHandler::getTiffByteOrder
162 public function testTiffByteOrder() {
163 $handler = new BitmapMetadataHandler();
164 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
165 $this->assertEquals( 'LE', $res );