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