Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / media / BitmapMetadataHandlerTest.php
blob8240e71c4ba56a4de00da85e1e7976dbbf65faf6
1 <?php
3 use MediaWiki\MainConfigNames;
5 /**
6 * @group Media
7 */
8 class BitmapMetadataHandlerTest extends MediaWikiIntegrationTestCase {
9 private const FILE_PATH = __DIR__ . '/../../data/media/';
11 protected function setUp(): void {
12 parent::setUp();
14 $this->overrideConfigValue( MainConfigNames::ShowEXIF, false );
17 /**
18 * Test if having conflicting metadata values from different
19 * types of metadata, that the right one takes precedence.
21 * Basically the file has IPTC and XMP metadata, the
22 * IPTC should override the XMP, except for the multilingual
23 * translation (to en) where XMP should win.
24 * @covers \BitmapMetadataHandler::Jpeg
25 * @requires extension exif
27 public function testMultilingualCascade() {
28 $this->overrideConfigValue( MainConfigNames::ShowEXIF, true );
30 $meta = BitmapMetadataHandler::Jpeg( self::FILE_PATH .
31 '/Xmp-exif-multilingual_test.jpg' );
33 $expected = [
34 'x-default' => 'right(iptc)',
35 'en' => 'right translation',
36 '_type' => 'lang'
39 $this->assertArrayHasKey( 'ImageDescription', $meta,
40 'Did not extract any ImageDescription info?!' );
42 $this->assertEquals( $expected, $meta['ImageDescription'] );
45 /**
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
51 * @covers \BitmapMetadataHandler::Jpeg
53 public function testJpegComment() {
54 $meta = BitmapMetadataHandler::Jpeg( self::FILE_PATH .
55 'jpeg-comment-utf.jpg' );
57 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
58 $meta['JPEGFileComment'][0] );
61 /**
62 * Make sure a bad iptc block doesn't stop the other metadata
63 * from being extracted.
64 * @covers \BitmapMetadataHandler::Jpeg
66 public function testBadIPTC() {
67 $meta = BitmapMetadataHandler::Jpeg( self::FILE_PATH .
68 'iptc-invalid-psir.jpg' );
69 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
72 /**
73 * @covers \BitmapMetadataHandler::Jpeg
75 public function testIPTCDates() {
76 $meta = BitmapMetadataHandler::Jpeg( self::FILE_PATH .
77 'iptc-timetest.jpg' );
79 // raw date is 2020:07:13 14:04:05+11:32
80 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
81 // raw date is 1997:03:02 03:01:02-03:00
82 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
84 $meta = BitmapMetadataHandler::Jpeg( self::FILE_PATH .
85 'iptc-timetest-invalid.jpg' );
87 // raw date is 1845:03:02 03:01:02-03:00
88 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
89 // raw date is 1942:07:13 25:05:02+00:00
90 $this->assertSame( '1942:07:14 01:05:02', $meta['DateTimeDigitized'] );
93 /**
94 * XMP data should take priority over iptc data
95 * when hash has been updated, but not when
96 * the hash is wrong.
97 * @covers \BitmapMetadataHandler::addMetadata
98 * @covers \BitmapMetadataHandler::getMetadataArray
100 public function testMerging() {
101 $merger = new BitmapMetadataHandler();
102 $merger->addMetadata( [ 'foo' => 'xmp' ], 'xmp-general' );
103 $merger->addMetadata( [ 'bar' => 'xmp' ], 'xmp-general' );
104 $merger->addMetadata( [ 'baz' => 'xmp' ], 'xmp-general' );
105 $merger->addMetadata( [ 'fred' => 'xmp' ], 'xmp-general' );
106 $merger->addMetadata( [ 'foo' => 'iptc (hash)' ], 'iptc-good-hash' );
107 $merger->addMetadata( [ 'bar' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
108 $merger->addMetadata( [ 'baz' => 'iptc (bad hash)' ], 'iptc-bad-hash' );
109 $merger->addMetadata( [ 'fred' => 'iptc (no hash)' ], 'iptc-no-hash' );
110 $merger->addMetadata( [ 'baz' => 'exif' ], 'exif' );
112 $actual = $merger->getMetadataArray();
113 $expected = [
114 'foo' => 'xmp',
115 'bar' => 'iptc (bad hash)',
116 'baz' => 'exif',
117 'fred' => 'xmp',
119 $this->assertEquals( $expected, $actual );
123 * @covers \BitmapMetadataHandler::png
125 public function testPNGXMP() {
126 $result = BitmapMetadataHandler::PNG( self::FILE_PATH . 'xmp.png' );
127 $expected = [
128 'width' => 50,
129 'height' => 50,
130 'frameCount' => 0,
131 'loopCount' => 1,
132 'duration' => 0,
133 'bitDepth' => 1,
134 'colorType' => 'index-coloured',
135 'metadata' => [
136 'SerialNumber' => '123456789',
137 '_MW_PNG_VERSION' => 1,
140 $this->assertEquals( $expected, $result );
144 * @covers \BitmapMetadataHandler::png
146 public function testPNGNative() {
147 $result = BitmapMetadataHandler::PNG( self::FILE_PATH . 'Png-native-test.png' );
148 $expected = 'http://example.com/url';
149 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
153 * @covers \BitmapMetadataHandler::getTiffByteOrder
155 public function testTiffByteOrder() {
156 $res = BitmapMetadataHandler::getTiffByteOrder( self::FILE_PATH . 'test.tiff' );
157 $this->assertEquals( 'LE', $res );