Make partial dates in XMP not have the ommitted fields fulled out to 1's (reported...
[lhc/web/wiklou.git] / tests / phpunit / includes / media / JpegMetadataExtractorTest.php
1 <?php
2 /*
3 * @todo Could use a test of extended XMP segments. Hard to find programs that
4 * create example files, and creating my own in vim propbably wouldn't
5 * serve as a very good "test". (Adobe photoshop probably creates such files
6 * but it costs money). The implementation of it currently in MediaWiki is based
7 * solely on reading the standard, without any real world test files.
8 */
9 class JpegMetadataExtractorTest extends MediaWikiTestCase {
10
11 public function setUp() {
12 $this->filePath = dirname( __FILE__ ) . '/../../data/media/';
13 }
14
15 public function testUtf8Comment() {
16 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-utf.jpg' );
17 $this->assertEquals( array( 'UTF-8 JPEG Comment — ¼' ), $res['COM'] );
18 }
19 /** The file is iso-8859-1, but it should get auto converted */
20 public function testIso88591Comment() {
21 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-iso8859-1.jpg' );
22 $this->assertEquals( array( 'ISO-8859-1 JPEG Comment - ¼' ), $res['COM'] );
23 }
24 /** Comment values that are non-textual (random binary junk) should not be shown.
25 * The example test file has a comment with a 0x5 byte in it which is a control character
26 * and considered binary junk for our purposes.
27 */
28 public function testBinaryCommentStripped() {
29 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' );
30 $this->assertEmpty( $res['COM'] );
31 }
32 /* Very rarely a file can have multiple comments.
33 * Order of comments is based on order inside the file.
34 */
35 public function testMultipleComment() {
36 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' );
37 $this->assertEquals( array( 'foo', 'bar' ), $res['COM'] );
38 }
39 public function testXMPExtraction() {
40 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
41 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
42 $this->assertEquals( $expected, $res['XMP'] );
43 }
44 public function testPSIRExtraction() {
45 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
46 $expected = '50686f746f73686f7020332e30003842494d04040000000000181c02190004746573741c02190003666f6f1c020000020004';
47 $this->assertEquals( $expected, bin2hex( $res['PSIR'] ) );
48 }
49 public function testXMPExtractionAltAppId() {
50 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' );
51 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
52 $this->assertEquals( $expected, $res['XMP'] );
53 }
54
55
56 public function testIPTCHashComparisionNoHash() {
57 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
58 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'] );
59
60 $this->assertEquals( 'iptc-no-hash', $res );
61 }
62 public function testIPTCHashComparisionBadHash() {
63 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' );
64 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'] );
65
66 $this->assertEquals( 'iptc-bad-hash', $res );
67 }
68 public function testIPTCHashComparisionGoodHash() {
69 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' );
70 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'] );
71
72 $this->assertEquals( 'iptc-good-hash', $res );
73 }
74 public function testExifByteOrder() {
75 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' );
76 $expected = 'BE';
77 $this->assertEquals( $expected, $res['byteOrder'] );
78 }
79 }