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