| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class PNGMetadataExtractorTest extends MediaWikiIntegrationTestCase { |
| 8 |
private const FILE_PATH = __DIR__ . '/../../data/media/'; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public function testPngNativetZtxt() { |
| 15 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 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 |
|
| 28 |
|
| 29 |
public function testPngNativeText() { |
| 30 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testPngNativeTextNonAscii() { |
| 47 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 48 |
'Png-native-test.png' ); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
$expected = "© 2010 Bawolff"; |
| 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 |
|
| 65 |
|
| 66 |
public function testStaticPngAnimationMetadata() { |
| 67 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 68 |
'Png-native-test.png' ); |
| 69 |
|
| 70 |
$this->assertSame( 0, $meta['frameCount'] ); |
| 71 |
$this->assertSame( 1, $meta['loopCount'] ); |
| 72 |
$this->assertSame( 0.0, $meta['duration'] ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function testApngAnimationMetadata() { |
| 80 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 81 |
'Animated_PNG_example_bouncing_beach_ball.png' ); |
| 82 |
|
| 83 |
$this->assertEquals( 20, $meta['frameCount'] ); |
| 84 |
|
| 85 |
$this->assertSame( 0, $meta['loopCount'] ); |
| 86 |
$this->assertEqualsWithDelta( 1.5, $meta['duration'], 0.00001, '' ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testPngBitDepth8() { |
| 90 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 91 |
'Png-native-test.png' ); |
| 92 |
|
| 93 |
$this->assertEquals( 8, $meta['bitDepth'] ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testPngBitDepth1() { |
| 97 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 98 |
'1bit-png.png' ); |
| 99 |
$this->assertSame( 1, $meta['bitDepth'] ); |
| 100 |
} |
| 101 |
|
| 102 |
public function testPngIndexColour() { |
| 103 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 104 |
'Png-native-test.png' ); |
| 105 |
|
| 106 |
$this->assertEquals( 'index-coloured', $meta['colorType'] ); |
| 107 |
} |
| 108 |
|
| 109 |
public function testPngRgbColour() { |
| 110 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 111 |
'rgb-png.png' ); |
| 112 |
$this->assertEquals( 'truecolour-alpha', $meta['colorType'] ); |
| 113 |
} |
| 114 |
|
| 115 |
public function testPngRgbNoAlphaColour() { |
| 116 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 117 |
'rgb-na-png.png' ); |
| 118 |
$this->assertEquals( 'truecolour', $meta['colorType'] ); |
| 119 |
} |
| 120 |
|
| 121 |
public function testPngGreyscaleColour() { |
| 122 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 123 |
'greyscale-png.png' ); |
| 124 |
$this->assertEquals( 'greyscale-alpha', $meta['colorType'] ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testPngGreyscaleNoAlphaColour() { |
| 128 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 129 |
'greyscale-na-png.png' ); |
| 130 |
$this->assertEquals( 'greyscale', $meta['colorType'] ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function testPngInvalidChunk() { |
| 137 |
$meta = PNGMetadataExtractor::getMetadata( self::FILE_PATH . |
| 138 |
'tEXt-invalid-masked.png' ); |
| 139 |
$this->assertEquals( 10, $meta['width'] ); |
| 140 |
$this->assertEquals( 10, $meta['height'] ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function testPngOversizeChunk() { |
| 147 |
|
| 148 |
|
| 149 |
$path = $this->getNewTempFile(); |
| 150 |
copy( self::FILE_PATH . '1bit-png.png', $path ); |
| 151 |
$chunkTypeAndData = "tEXtkey\0value" . str_repeat( '.', 10000000 ); |
| 152 |
$crc = crc32( $chunkTypeAndData ); |
| 153 |
$chunkLength = strlen( $chunkTypeAndData ) - 4; |
| 154 |
$file = fopen( $path, 'r+' ); |
| 155 |
fseek( $file, -12, SEEK_END ); |
| 156 |
$iend = fread( $file, 12 ); |
| 157 |
fseek( $file, -12, SEEK_END ); |
| 158 |
fwrite( $file, pack( 'N', $chunkLength ) ); |
| 159 |
fwrite( $file, $chunkTypeAndData ); |
| 160 |
fwrite( $file, pack( 'N', $crc ) ); |
| 161 |
fwrite( $file, $iend ); |
| 162 |
fclose( $file ); |
| 163 |
|
| 164 |
|
| 165 |
$meta = PNGMetadataExtractor::getMetadata( $path ); |
| 166 |
$this->assertEquals( 50, $meta['width'] ); |
| 167 |
$this->assertEquals( 50, $meta['height'] ); |
| 168 |
|
| 169 |
|
| 170 |
$this->assertLessThan( 100000, strlen( serialize( $meta ) ) ); |
| 171 |
} |
| 172 |
|
| 173 |
} |