bb0f0bb8c6e709b756c34d46ee4217b8acbf711e
[lhc/web/wiklou.git] / tests / phpunit / includes / media / BitmapMetadataHandlerTest.php
1 <?php
2 class BitmapMetadataHandlerTest extends MediaWikiTestCase {
3
4 public function setUp() {
5 $this->filePath = dirname( __FILE__ ) . '/../../data/media/';
6 }
7
8 /**
9 * Test if having conflicting metadata values from different
10 * types of metadata, that the right one takes precedence.
11 *
12 * Basically the file has IPTC and XMP metadata, the
13 * IPTC should override the XMP, except for the multilingual
14 * translation (to en) where XMP should win.
15 */
16 public function testMultilingualCascade() {
17 if ( !wfDl( 'exif' ) ) {
18 $this->markTestSkipped( "This test needs the exif extension." );
19 }
20 if ( !wfDl( 'xml' ) ) {
21 $this->markTestSkipped( "This test needs the xml extension." );
22 }
23 global $wgShowEXIF;
24 $oldExif = $wgShowEXIF;
25 $wgShowEXIF = true;
26
27 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
28 '/Xmp-exif-multilingual_test.jpg' );
29
30 $expected = array(
31 'x-default' => 'right(iptc)',
32 'en' => 'right translation',
33 '_type' => 'lang'
34 );
35
36 $this->assertArrayHasKey( 'ImageDescription', $meta,
37 'Did not extract any ImageDescription info?!' );
38
39 $this->assertEquals( $expected, $meta['ImageDescription'] );
40
41 $wgShowEXIF = $oldExif;
42 }
43
44 /**
45 * Test for jpeg comments are being handled by
46 * BitmapMetadataHandler correctly.
47 *
48 * There's more extensive tests of comment extraction in
49 * JpegMetadataExtractorTests.php
50 */
51 public function testJpegComment() {
52 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
53 'jpeg-comment-utf.jpg' );
54
55 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
56 $meta['JPEGFileComment'][0] );
57 }
58
59 public function testIPTCDates() {
60 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
61 'iptc-timetest.jpg' );
62
63 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
64 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
65 }
66 /* File has an invalid time (+ one valid but really weird time)
67 * that shouldn't be included
68 */
69 public function testIPTCDatesInvalid() {
70 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
71 'iptc-timetest-invalid.jpg' );
72
73 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
74 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
75 }
76
77 /**
78 * XMP data should take priority over iptc data
79 * when hash has been updated, but not when
80 * the hash is wrong.
81 */
82 public function testMerging() {
83 $merger = new BitmapMetadataHandler();
84 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
85 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
86 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
87 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
88 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
89 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
90 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
91 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
92 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
93
94 $actual = $merger->getMetadataArray();
95 $expected = array(
96 'foo' => 'xmp',
97 'bar' => 'iptc (bad hash)',
98 'baz' => 'exif',
99 'fred' => 'xmp',
100 );
101 $this->assertEquals( $expected, $actual );
102 }
103
104 public function testPNGXMP() {
105 if ( !wfDl( 'xml' ) ) {
106 $this->markTestSkipped( "This test needs the xml extension." );
107 }
108 $handler = new BitmapMetadataHandler();
109 $result = $handler->png( $this->filePath . 'xmp.png' );
110 $expected = array (
111 'frameCount' => 0,
112 'loopCount' => 1,
113 'duration' => 0,
114 'bitDepth' => 1,
115 'colorType' => 'index-coloured',
116 'metadata' => array (
117 'SerialNumber' => '123456789',
118 '_MW_PNG_VERSION' => 1,
119 ),
120 );
121 $this->assertEquals( $expected, $result );
122 }
123 public function testPNGNative() {
124 $handler = new BitmapMetadataHandler();
125 $result = $handler->png( $this->filePath . 'Png-native-test.png' );
126 $expected = 'http://example.com/url';
127 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
128 }
129 public function testTiffByteOrder() {
130 $handler = new BitmapMetadataHandler();
131 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
132 $this->assertEquals( 'LE', $res );
133 }
134
135 }