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.
10 * @covers JpegMetadataExtractor
12 class JpegMetadataExtractorTest
extends MediaWikiTestCase
{
16 protected function setUp() {
19 $this->filePath
= __DIR__
. '/../../data/media/';
23 * We also use this test to test padding bytes don't
26 * @param string $file Filename
28 * @dataProvider provideUtf8Comment
30 public function testUtf8Comment( $file ) {
31 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. $file );
32 $this->assertEquals( [ 'UTF-8 JPEG Comment — ¼' ], $res['COM'] );
35 public static function provideUtf8Comment() {
37 [ 'jpeg-comment-utf.jpg' ],
38 [ 'jpeg-padding-even.jpg' ],
39 [ 'jpeg-padding-odd.jpg' ],
43 /** The file is iso-8859-1, but it should get auto converted */
44 public function testIso88591Comment() {
45 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-comment-iso8859-1.jpg' );
46 $this->assertEquals( [ 'ISO-8859-1 JPEG Comment - ¼' ], $res['COM'] );
49 /** Comment values that are non-textual (random binary junk) should not be shown.
50 * The example test file has a comment with a 0x5 byte in it which is a control character
51 * and considered binary junk for our purposes.
53 public function testBinaryCommentStripped() {
54 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-comment-binary.jpg' );
55 $this->assertEmpty( $res['COM'] );
58 /* Very rarely a file can have multiple comments.
59 * Order of comments is based on order inside the file.
61 public function testMultipleComment() {
62 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-comment-multiple.jpg' );
63 $this->assertEquals( [ 'foo', 'bar' ], $res['COM'] );
66 public function testXMPExtraction() {
67 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-xmp-psir.jpg' );
68 $expected = file_get_contents( $this->filePath
. 'jpeg-xmp-psir.xmp' );
69 $this->assertEquals( $expected, $res['XMP'] );
72 public function testPSIRExtraction() {
73 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-xmp-psir.jpg' );
74 $expected = '50686f746f73686f7020332e30003842494d04040000000'
75 . '000181c02190004746573741c02190003666f6f1c020000020004';
76 $this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) );
79 public function testXMPExtractionAltAppId() {
80 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-xmp-alt.jpg' );
81 $expected = file_get_contents( $this->filePath
. 'jpeg-xmp-psir.xmp' );
82 $this->assertEquals( $expected, $res['XMP'] );
85 public function testIPTCHashComparisionNoHash() {
86 $segments = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-xmp-psir.jpg' );
87 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
89 $this->assertEquals( 'iptc-no-hash', $res );
92 public function testIPTCHashComparisionBadHash() {
93 $segments = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-iptc-bad-hash.jpg' );
94 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
96 $this->assertEquals( 'iptc-bad-hash', $res );
99 public function testIPTCHashComparisionGoodHash() {
100 $segments = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'jpeg-iptc-good-hash.jpg' );
101 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
103 $this->assertEquals( 'iptc-good-hash', $res );
106 public function testExifByteOrder() {
107 $res = JpegMetadataExtractor
::segmentSplitter( $this->filePath
. 'exif-user-comment.jpg' );
109 $this->assertEquals( $expected, $res['byteOrder'] );