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