test: coverage recording now needs to be explicit
[mediawiki.git] / tests / phpunit / includes / media / JpegMetadataExtractorTest.php
blobcae7137b35d71a09b997b4bfb774df9d61e4461f
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 {
11 protected function setUp() {
12 parent::setUp();
14 $this->filePath = __DIR__ . '/../../data/media/';
17 /**
18 * We also use this test to test padding bytes don't
19 * screw stuff up
21 * @param $file filename
23 * @dataProvider provideUtf8Comment
25 public function testUtf8Comment( $file ) {
26 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . $file );
27 $this->assertEquals( array( 'UTF-8 JPEG Comment — ¼' ), $res['COM'] );
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' ),
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'] );
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.
48 public function testBinaryCommentStripped() {
49 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' );
50 $this->assertEmpty( $res['COM'] );
53 /* Very rarely a file can have multiple comments.
54 * Order of comments is based on order inside the file.
56 public function testMultipleComment() {
57 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' );
58 $this->assertEquals( array( 'foo', 'bar' ), $res['COM'] );
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'] );
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] ) );
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'] );
80 public function testIPTCHashComparisionNoHash() {
81 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
82 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
84 $this->assertEquals( 'iptc-no-hash', $res );
87 public function testIPTCHashComparisionBadHash() {
88 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' );
89 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
91 $this->assertEquals( 'iptc-bad-hash', $res );
94 public function testIPTCHashComparisionGoodHash() {
95 $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' );
96 $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
98 $this->assertEquals( 'iptc-good-hash', $res );
101 public function testExifByteOrder() {
102 $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' );
103 $expected = 'BE';
104 $this->assertEquals( $expected, $res['byteOrder'] );