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