clear magicword cache before testing cleanSig
[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 /* File has an invalid time (+ one valid but really weird time)
77 * that shouldn't be included
78 */
79 public function testIPTCDatesInvalid() {
80 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
81 'iptc-timetest-invalid.jpg' );
82
83 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
84 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
85 }
86
87 /**
88 * XMP data should take priority over iptc data
89 * when hash has been updated, but not when
90 * the hash is wrong.
91 */
92 public function testMerging() {
93 $merger = new BitmapMetadataHandler();
94 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
95 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
96 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
97 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
98 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
99 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
100 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
101 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
102 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
103
104 $actual = $merger->getMetadataArray();
105 $expected = array(
106 'foo' => 'xmp',
107 'bar' => 'iptc (bad hash)',
108 'baz' => 'exif',
109 'fred' => 'xmp',
110 );
111 $this->assertEquals( $expected, $actual );
112 }
113
114 public function testPNGXMP() {
115 if ( !wfDl( 'xml' ) ) {
116 $this->markTestSkipped( "This test needs the xml extension." );
117 }
118 $handler = new BitmapMetadataHandler();
119 $result = $handler->png( $this->filePath . 'xmp.png' );
120 $expected = array (
121 'frameCount' => 0,
122 'loopCount' => 1,
123 'duration' => 0,
124 'bitDepth' => 1,
125 'colorType' => 'index-coloured',
126 'metadata' => array (
127 'SerialNumber' => '123456789',
128 '_MW_PNG_VERSION' => 1,
129 ),
130 );
131 $this->assertEquals( $expected, $result );
132 }
133 public function testPNGNative() {
134 $handler = new BitmapMetadataHandler();
135 $result = $handler->png( $this->filePath . 'Png-native-test.png' );
136 $expected = 'http://example.com/url';
137 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
138 }
139 public function testTiffByteOrder() {
140 $handler = new BitmapMetadataHandler();
141 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
142 $this->assertEquals( 'LE', $res );
143 }
144
145 }