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