Merge "jquery.makeCollapsible: minor fixes to togglingHandler()"
[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 if ( !wfDl( 'exif' ) ) {
22 $this->markTestSkipped( "This test needs the exif extension." );
23 }
24 if ( !wfDl( 'xml' ) ) {
25 $this->markTestSkipped( "This test needs the xml extension." );
26 }
27
28 $this->setMwGlobals( 'wgShowEXIF', true );
29
30 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
31 '/Xmp-exif-multilingual_test.jpg' );
32
33 $expected = array(
34 'x-default' => 'right(iptc)',
35 'en' => 'right translation',
36 '_type' => 'lang'
37 );
38
39 $this->assertArrayHasKey( 'ImageDescription', $meta,
40 'Did not extract any ImageDescription info?!' );
41
42 $this->assertEquals( $expected, $meta['ImageDescription'] );
43 }
44
45 /**
46 * Test for jpeg comments are being handled by
47 * BitmapMetadataHandler correctly.
48 *
49 * There's more extensive tests of comment extraction in
50 * JpegMetadataExtractorTests.php
51 */
52 public function testJpegComment() {
53 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
54 'jpeg-comment-utf.jpg' );
55
56 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
57 $meta['JPEGFileComment'][0] );
58 }
59
60 /**
61 * Make sure a bad iptc block doesn't stop the other metadata
62 * from being extracted.
63 */
64 public function testBadIPTC() {
65 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
66 'iptc-invalid-psir.jpg' );
67 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
68 }
69
70 public function testIPTCDates() {
71 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
72 'iptc-timetest.jpg' );
73
74 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
75 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
76 }
77
78 /**
79 * File has an invalid time (+ one valid but really weird time)
80 * that shouldn't be included
81 */
82 public function testIPTCDatesInvalid() {
83 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
84 'iptc-timetest-invalid.jpg' );
85
86 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
87 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
88 }
89
90 /**
91 * XMP data should take priority over iptc data
92 * when hash has been updated, but not when
93 * the hash is wrong.
94 */
95 public function testMerging() {
96 $merger = new BitmapMetadataHandler();
97 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
98 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
99 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
100 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
101 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
102 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
103 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
104 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
105 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
106
107 $actual = $merger->getMetadataArray();
108 $expected = array(
109 'foo' => 'xmp',
110 'bar' => 'iptc (bad hash)',
111 'baz' => 'exif',
112 'fred' => 'xmp',
113 );
114 $this->assertEquals( $expected, $actual );
115 }
116
117 public function testPNGXMP() {
118 if ( !wfDl( 'xml' ) ) {
119 $this->markTestSkipped( "This test needs the xml extension." );
120 }
121 $handler = new BitmapMetadataHandler();
122 $result = $handler->png( $this->filePath . 'xmp.png' );
123 $expected = array(
124 'frameCount' => 0,
125 'loopCount' => 1,
126 'duration' => 0,
127 'bitDepth' => 1,
128 'colorType' => 'index-coloured',
129 'metadata' => array(
130 'SerialNumber' => '123456789',
131 '_MW_PNG_VERSION' => 1,
132 ),
133 );
134 $this->assertEquals( $expected, $result );
135 }
136
137 public function testPNGNative() {
138 $handler = new BitmapMetadataHandler();
139 $result = $handler->png( $this->filePath . 'Png-native-test.png' );
140 $expected = 'http://example.com/url';
141 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
142 }
143
144 public function testTiffByteOrder() {
145 $handler = new BitmapMetadataHandler();
146 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
147 $this->assertEquals( 'LE', $res );
148 }
149 }