Merge "(bug 40857) fix non-array sidebar links handling in CologneBlue"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / BitmapMetadataHandlerTest.php
1 <?php
2 class BitmapMetadataHandlerTest extends MediaWikiTestCase {
3
4 protected function setUp() {
5 parent::setUp();
6
7 $this->setMwGlobals( 'wgShowEXIF', false );
8
9 $this->filePath = __DIR__ . '/../../data/media/';
10 }
11
12 /**
13 * Test if having conflicting metadata values from different
14 * types of metadata, that the right one takes precedence.
15 *
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 */
20 public function testMultilingualCascade() {
21 global $wgShowEXIF;
22
23 if ( !wfDl( 'exif' ) ) {
24 $this->markTestSkipped( "This test needs the exif extension." );
25 }
26 if ( !wfDl( 'xml' ) ) {
27 $this->markTestSkipped( "This test needs the xml extension." );
28 }
29
30 $wgShowEXIF = true;
31
32 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
33 '/Xmp-exif-multilingual_test.jpg' );
34
35 $expected = array(
36 'x-default' => 'right(iptc)',
37 'en' => 'right translation',
38 '_type' => 'lang'
39 );
40
41 $this->assertArrayHasKey( 'ImageDescription', $meta,
42 'Did not extract any ImageDescription info?!' );
43
44 $this->assertEquals( $expected, $meta['ImageDescription'] );
45 }
46
47 /**
48 * Test for jpeg comments are being handled by
49 * BitmapMetadataHandler correctly.
50 *
51 * There's more extensive tests of comment extraction in
52 * JpegMetadataExtractorTests.php
53 */
54 public function testJpegComment() {
55 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
56 'jpeg-comment-utf.jpg' );
57
58 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
59 $meta['JPEGFileComment'][0] );
60 }
61
62 /**
63 * Make sure a bad iptc block doesn't stop the other metadata
64 * from being extracted.
65 */
66 public function testBadIPTC() {
67 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
68 'iptc-invalid-psir.jpg' );
69 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
70 }
71
72 public function testIPTCDates() {
73 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
74 'iptc-timetest.jpg' );
75
76 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
77 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
78 }
79
80 /**
81 * File has an invalid time (+ one valid but really weird time)
82 * that shouldn't be included
83 */
84 public function testIPTCDatesInvalid() {
85 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
86 'iptc-timetest-invalid.jpg' );
87
88 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
89 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
90 }
91
92 /**
93 * XMP data should take priority over iptc data
94 * when hash has been updated, but not when
95 * the hash is wrong.
96 */
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' );
108
109 $actual = $merger->getMetadataArray();
110 $expected = array(
111 'foo' => 'xmp',
112 'bar' => 'iptc (bad hash)',
113 'baz' => 'exif',
114 'fred' => 'xmp',
115 );
116 $this->assertEquals( $expected, $actual );
117 }
118
119 public function testPNGXMP() {
120 if ( !wfDl( 'xml' ) ) {
121 $this->markTestSkipped( "This test needs the xml extension." );
122 }
123 $handler = new BitmapMetadataHandler();
124 $result = $handler->png( $this->filePath . 'xmp.png' );
125 $expected = array (
126 'frameCount' => 0,
127 'loopCount' => 1,
128 'duration' => 0,
129 'bitDepth' => 1,
130 'colorType' => 'index-coloured',
131 'metadata' => array (
132 'SerialNumber' => '123456789',
133 '_MW_PNG_VERSION' => 1,
134 ),
135 );
136 $this->assertEquals( $expected, $result );
137 }
138
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'] );
144 }
145
146 public function testTiffByteOrder() {
147 $handler = new BitmapMetadataHandler();
148 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
149 $this->assertEquals( 'LE', $res );
150 }
151
152 }