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