Merge "Added a function to LoginForm to show the "return to" page."
[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
30 public static function provideUtf8Comment() {
31 return array(
32 array( 'jpeg-comment-utf.jpg' ),
33 array( 'jpeg-padding-even.jpg' ),
34 array( 'jpeg-padding-odd.jpg' ),
35 );
36 }
37
38 /** The file is iso-8859-1, but it should get auto converted */
39 public function testIso88591Comment() {
40 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-iso8859-1.jpg' );
41 $this->assertEquals( array( 'ISO-8859-1 JPEG Comment - ¼' ), $res['COM'] );
42 }
43
44 /** Comment values that are non-textual (random binary junk) should not be shown.
45 * The example test file has a comment with a 0x5 byte in it which is a control character
46 * and considered binary junk for our purposes.
47 */
48 public function testBinaryCommentStripped() {
49 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' );
50 $this->assertEmpty( $res['COM'] );
51 }
52
53 /* Very rarely a file can have multiple comments.
54 * Order of comments is based on order inside the file.
55 */
56 public function testMultipleComment() {
57 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' );
58 $this->assertEquals( array( 'foo', 'bar' ), $res['COM'] );
59 }
60
61 public function testXMPExtraction() {
62 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
63 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
64 $this->assertEquals( $expected, $res['XMP'] );
65 }
66
67 public function testPSIRExtraction() {
68 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
69 $expected = '50686f746f73686f7020332e30003842494d04040000000000181c02190004746573741c02190003666f6f1c020000020004';
70 $this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) );
71 }
72
73 public function testXMPExtractionAltAppId() {
74 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' );
75 $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
76 $this->assertEquals( $expected, $res['XMP'] );
77 }
78
79
80 public function testIPTCHashComparisionNoHash() {
81 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
82 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
83
84 $this->assertEquals( 'iptc-no-hash', $res );
85 }
86
87 public function testIPTCHashComparisionBadHash() {
88 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' );
89 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
90
91 $this->assertEquals( 'iptc-bad-hash', $res );
92 }
93
94 public function testIPTCHashComparisionGoodHash() {
95 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' );
96 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
97
98 $this->assertEquals( 'iptc-good-hash', $res );
99 }
100
101 public function testExifByteOrder() {
102 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' );
103 $expected = 'BE';
104 $this->assertEquals( $expected, $res['byteOrder'] );
105 }
106 }