Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / media / PNGMetadataExtractorTest.php
bloba9eaa9e7c59b17e1834b059f34abd745e638e64b
1 <?php
3 /**
4 * @group Media
5 * @covers PNGMetadataExtractor
6 */
7 class PNGMetadataExtractorTest extends MediaWikiTestCase {
9 protected function setUp() {
10 parent::setUp();
11 $this->filePath = __DIR__ . '/../../data/media/';
14 /**
15 * Tests zTXt tag (compressed textual metadata)
17 public function testPngNativetZtxt() {
18 $this->checkPHPExtension( 'zlib' );
20 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
21 'Png-native-test.png' );
22 $expected = "foo bar baz foo foo foo foof foo foo foo foo";
23 $this->assertArrayHasKey( 'text', $meta );
24 $meta = $meta['text'];
25 $this->assertArrayHasKey( 'Make', $meta );
26 $this->assertArrayHasKey( 'x-default', $meta['Make'] );
28 $this->assertEquals( $expected, $meta['Make']['x-default'] );
31 /**
32 * Test tEXt tag (Uncompressed textual metadata)
34 public function testPngNativeText() {
35 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
36 'Png-native-test.png' );
37 $expected = "Some long image desc";
38 $this->assertArrayHasKey( 'text', $meta );
39 $meta = $meta['text'];
40 $this->assertArrayHasKey( 'ImageDescription', $meta );
41 $this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
42 $this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
44 $this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
47 /**
48 * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
49 * Make sure non-ascii characters get converted properly
51 public function testPngNativeTextNonAscii() {
52 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
53 'Png-native-test.png' );
55 // Note the Copyright symbol here is a utf-8 one
56 // (aka \xC2\xA9) where in the file its iso-8859-1
57 // encoded as just \xA9.
58 $expected = "© 2010 Bawolff";
60 $this->assertArrayHasKey( 'text', $meta );
61 $meta = $meta['text'];
62 $this->assertArrayHasKey( 'Copyright', $meta );
63 $this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
65 $this->assertEquals( $expected, $meta['Copyright']['x-default'] );
68 /**
69 * Test extraction of pHYs tags, which can tell what the
70 * actual resolution of the image is (aka in dots per meter).
73 public function testPngPhysTag() {
74 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
75 'Png-native-test.png' );
77 $this->assertArrayHasKey( 'text', $meta );
78 $meta = $meta['text'];
80 $this->assertEquals( '2835/100', $meta['XResolution'] );
81 $this->assertEquals( '2835/100', $meta['YResolution'] );
82 $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm
86 /**
87 * Given a normal static PNG, check the animation metadata returned.
89 public function testStaticPngAnimationMetadata() {
90 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
91 'Png-native-test.png' );
93 $this->assertEquals( 0, $meta['frameCount'] );
94 $this->assertEquals( 1, $meta['loopCount'] );
95 $this->assertEquals( 0, $meta['duration'] );
98 /**
99 * Given an animated APNG image file
100 * check it gets animated metadata right.
102 public function testApngAnimationMetadata() {
103 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
104 'Animated_PNG_example_bouncing_beach_ball.png' );
106 $this->assertEquals( 20, $meta['frameCount'] );
107 // Note loop count of 0 = infinity
108 $this->assertEquals( 0, $meta['loopCount'] );
109 $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 );
112 public function testPngBitDepth8() {
113 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
114 'Png-native-test.png' );
116 $this->assertEquals( 8, $meta['bitDepth'] );
119 public function testPngBitDepth1() {
120 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
121 '1bit-png.png' );
122 $this->assertEquals( 1, $meta['bitDepth'] );
125 public function testPngIndexColour() {
126 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
127 'Png-native-test.png' );
129 $this->assertEquals( 'index-coloured', $meta['colorType'] );
132 public function testPngRgbColour() {
133 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
134 'rgb-png.png' );
135 $this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
138 public function testPngRgbNoAlphaColour() {
139 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
140 'rgb-na-png.png' );
141 $this->assertEquals( 'truecolour', $meta['colorType'] );
144 public function testPngGreyscaleColour() {
145 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
146 'greyscale-png.png' );
147 $this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
150 public function testPngGreyscaleNoAlphaColour() {
151 $meta = PNGMetadataExtractor::getMetadata( $this->filePath .
152 'greyscale-na-png.png' );
153 $this->assertEquals( 'greyscale', $meta['colorType'] );