f48382a431011bede0a6d9372b8f730871cc4758
[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 /**
16 * We also use this test to test padding bytes don't
17 * screw stuff up
18 *
19 * @param $file filename
20 *
21 * @dataProvider dataUtf8Comment
22 */
23 public function testUtf8Comment( $file ) {
24 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . $file );
25 $this->assertEquals( array( 'UTF-8 JPEG Comment — ¼' ), $res['COM'] );
26 }
27 public function dataUtf8Comment() {
28 return array(
29 array( 'jpeg-comment-utf.jpg' ),
30 array( 'jpeg-padding-even.jpg' ),
31 array( 'jpeg-padding-odd.jpg' ),
32 );
33 }
34 /** The file is iso-8859-1, but it should get auto converted */
35 public function testIso88591Comment() {
36 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-iso8859-1.jpg' );
37 $this->assertEquals( array( 'ISO-8859-1 JPEG Comment - ¼' ), $res['COM'] );
38 }
39 /** Comment values that are non-textual (random binary junk) should not be shown.
40 * The example test file has a comment with a 0x5 byte in it which is a control character
41 * and considered binary junk for our purposes.
42 */
43 public function testBinaryCommentStripped() {
44 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' );
45 $this->assertEmpty( $res['COM'] );
46 }
47 /* Very rarely a file can have multiple comments.
48 * Order of comments is based on order inside the file.
49 */
50 public function testMultipleComment() {
51 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' );
52 $this->assertEquals( array( 'foo', 'bar' ), $res['COM'] );
53 }
54 public function testXMPExtraction() {
55 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
56 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
57 $this->assertEquals( $expected, $res['XMP'] );
58 }
59 public function testPSIRExtraction() {
60 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
61 $expected = '50686f746f73686f7020332e30003842494d04040000000000181c02190004746573741c02190003666f6f1c020000020004';
62 $this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) );
63 }
64 public function testXMPExtractionAltAppId() {
65 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' );
66 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
67 $this->assertEquals( $expected, $res['XMP'] );
68 }
69
70
71 public function testIPTCHashComparisionNoHash() {
72 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
73 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
74
75 $this->assertEquals( 'iptc-no-hash', $res );
76 }
77 public function testIPTCHashComparisionBadHash() {
78 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' );
79 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
80
81 $this->assertEquals( 'iptc-bad-hash', $res );
82 }
83 public function testIPTCHashComparisionGoodHash() {
84 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' );
85 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
86
87 $this->assertEquals( 'iptc-good-hash', $res );
88 }
89 public function testExifByteOrder() {
90 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' );
91 $expected = 'BE';
92 $this->assertEquals( $expected, $res['byteOrder'] );
93 }
94 }