Merge "(bug 40257) action=info no longer shows subpages where disabled"
[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 = __DIR__ . '/../../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 /**
60 * Make sure a bad iptc block doesn't stop the other metadata
61 * from being extracted.
62 */
63 public function testBadIPTC() {
64 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
65 'iptc-invalid-psir.jpg' );
66 $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
67 }
68
69 public function testIPTCDates() {
70 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
71 'iptc-timetest.jpg' );
72
73 $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
74 $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
75 }
76 /**
77 * File has an invalid time (+ one valid but really weird time)
78 * that shouldn't be included
79 */
80 public function testIPTCDatesInvalid() {
81 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
82 'iptc-timetest-invalid.jpg' );
83
84 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
85 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
86 }
87
88 /**
89 * XMP data should take priority over iptc data
90 * when hash has been updated, but not when
91 * the hash is wrong.
92 */
93 public function testMerging() {
94 $merger = new BitmapMetadataHandler();
95 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
96 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
97 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
98 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
99 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
100 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
101 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
102 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
103 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
104
105 $actual = $merger->getMetadataArray();
106 $expected = array(
107 'foo' => 'xmp',
108 'bar' => 'iptc (bad hash)',
109 'baz' => 'exif',
110 'fred' => 'xmp',
111 );
112 $this->assertEquals( $expected, $actual );
113 }
114
115 public function testPNGXMP() {
116 if ( !wfDl( 'xml' ) ) {
117 $this->markTestSkipped( "This test needs the xml extension." );
118 }
119 $handler = new BitmapMetadataHandler();
120 $result = $handler->png( $this->filePath . 'xmp.png' );
121 $expected = array (
122 'frameCount' => 0,
123 'loopCount' => 1,
124 'duration' => 0,
125 'bitDepth' => 1,
126 'colorType' => 'index-coloured',
127 'metadata' => array (
128 'SerialNumber' => '123456789',
129 '_MW_PNG_VERSION' => 1,
130 ),
131 );
132 $this->assertEquals( $expected, $result );
133 }
134 public function testPNGNative() {
135 $handler = new BitmapMetadataHandler();
136 $result = $handler->png( $this->filePath . 'Png-native-test.png' );
137 $expected = 'http://example.com/url';
138 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
139 }
140 public function testTiffByteOrder() {
141 $handler = new BitmapMetadataHandler();
142 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
143 $this->assertEquals( 'LE', $res );
144 }
145
146 }