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