Merge "CSSJanus: Account for attribute selectors in brace lookahead"
[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 * @covers BitmapMetadataHandler::Jpeg
20 */
21 public function testMultilingualCascade() {
22 $this->checkPHPExtension( 'exif' );
23 $this->checkPHPExtension( 'xml' );
24
25 $this->setMwGlobals( '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
42 /**
43 * Test for jpeg comments are being handled by
44 * BitmapMetadataHandler correctly.
45 *
46 * There's more extensive tests of comment extraction in
47 * JpegMetadataExtractorTests.php
48 * @covers BitmapMetadataHandler::Jpeg
49 */
50 public function testJpegComment() {
51 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
52 'jpeg-comment-utf.jpg' );
53
54 $this->assertEquals( 'UTF-8 JPEG Comment — ¼',
55 $meta['JPEGFileComment'][0] );
56 }
57
58 /**
59 * Make sure a bad iptc block doesn't stop the other metadata
60 * from being extracted.
61 * @covers BitmapMetadataHandler::Jpeg
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 /**
70 * @covers BitmapMetadataHandler::Jpeg
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 * @covers BitmapMetadataHandler::Jpeg
84 */
85 public function testIPTCDatesInvalid() {
86 $meta = BitmapMetadataHandler::Jpeg( $this->filePath .
87 'iptc-timetest-invalid.jpg' );
88
89 $this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
90 $this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
91 }
92
93 /**
94 * XMP data should take priority over iptc data
95 * when hash has been updated, but not when
96 * the hash is wrong.
97 * @covers BitmapMetadataHandler::addMetadata
98 * @covers BitmapMetadataHandler::getMetadataArray
99 */
100 public function testMerging() {
101 $merger = new BitmapMetadataHandler();
102 $merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
103 $merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
104 $merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
105 $merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
106 $merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
107 $merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
108 $merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
109 $merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
110 $merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
111
112 $actual = $merger->getMetadataArray();
113 $expected = array(
114 'foo' => 'xmp',
115 'bar' => 'iptc (bad hash)',
116 'baz' => 'exif',
117 'fred' => 'xmp',
118 );
119 $this->assertEquals( $expected, $actual );
120 }
121
122 /**
123 * @covers BitmapMetadataHandler::png
124 */
125 public function testPNGXMP() {
126 if ( !extension_loaded( 'xml' ) ) {
127 $this->markTestSkipped( "This test needs the xml extension." );
128 }
129 $handler = new BitmapMetadataHandler();
130 $result = $handler->png( $this->filePath . 'xmp.png' );
131 $expected = array(
132 'frameCount' => 0,
133 'loopCount' => 1,
134 'duration' => 0,
135 'bitDepth' => 1,
136 'colorType' => 'index-coloured',
137 'metadata' => array(
138 'SerialNumber' => '123456789',
139 '_MW_PNG_VERSION' => 1,
140 ),
141 );
142 $this->assertEquals( $expected, $result );
143 }
144
145 /**
146 * @covers BitmapMetadataHandler::png
147 */
148 public function testPNGNative() {
149 $handler = new BitmapMetadataHandler();
150 $result = $handler->png( $this->filePath . 'Png-native-test.png' );
151 $expected = 'http://example.com/url';
152 $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
153 }
154
155 /**
156 * @covers BitmapMetadataHandler::getTiffByteOrder
157 */
158 public function testTiffByteOrder() {
159 $handler = new BitmapMetadataHandler();
160 $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
161 $this->assertEquals( 'LE', $res );
162 }
163 }