| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
class PNGHandlerTest extends MediaWikiMediaTestCase { |
| 7 |
|
| 8 |
|
| 9 |
protected $handler; |
| 10 |
|
| 11 |
protected function setUp(): void { |
| 12 |
parent::setUp(); |
| 13 |
$this->handler = new PNGHandler(); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
private function brokenFile() { |
| 21 |
return [ '_error' => '0' ]; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function testInvalidFile() { |
| 28 |
$res = $this->handler->getSizeAndMetadata( null, $this->filePath . '/README' ); |
| 29 |
$this->assertEquals( |
| 30 |
[ |
| 31 |
'metadata' => $this->brokenFile() |
| 32 |
], |
| 33 |
$res ); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function testIsAnimanted( $filename, $expected ) { |
| 43 |
$file = $this->dataFile( $filename, 'image/png' ); |
| 44 |
$actual = $this->handler->isAnimatedImage( $file ); |
| 45 |
$this->assertEquals( $expected, $actual ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function provideIsAnimated() { |
| 49 |
return [ |
| 50 |
[ 'Animated_PNG_example_bouncing_beach_ball.png', true ], |
| 51 |
[ '1bit-png.png', false ], |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testGetImageArea( $filename, $expected ) { |
| 62 |
$file = $this->dataFile( $filename, 'image/png' ); |
| 63 |
$actual = $this->handler->getImageArea( $file ); |
| 64 |
$this->assertEquals( $expected, $actual ); |
| 65 |
} |
| 66 |
|
| 67 |
public static function provideGetImageArea() { |
| 68 |
return [ |
| 69 |
[ '1bit-png.png', 2500 ], |
| 70 |
[ 'greyscale-png.png', 2500 ], |
| 71 |
[ 'Png-native-test.png', 126000 ], |
| 72 |
[ 'Animated_PNG_example_bouncing_beach_ball.png', 10000 ], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function testIsFileMetadataValid( $metadata, $expected ) { |
| 83 |
$actual = $this->handler->isFileMetadataValid( $this->getMockFileWithMetadata( $metadata ) ); |
| 84 |
$this->assertEquals( $expected, $actual ); |
| 85 |
} |
| 86 |
|
| 87 |
public static function provideIsMetadataValid() { |
| 88 |
return [ |
| 89 |
[ '0', PNGHandler::METADATA_GOOD ], |
| 90 |
[ '', PNGHandler::METADATA_BAD ], |
| 91 |
[ 'a:0:{}', PNGHandler::METADATA_BAD ], |
| 92 |
[ 'Something invalid!', PNGHandler::METADATA_BAD ], |
| 93 |
[ |
| 94 |
'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}', |
| 95 |
PNGHandler::METADATA_GOOD |
| 96 |
], |
| 97 |
]; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testGetSizeAndMetadata( $filename, $expected ) { |
| 108 |
$file = $this->dataFile( $filename, 'image/png' ); |
| 109 |
$actual = $this->handler->getSizeAndMetadata( $file, "$this->filePath/$filename" ); |
| 110 |
$this->assertEquals( $expected, $actual ); |
| 111 |
} |
| 112 |
|
| 113 |
public static function provideGetSizeAndMetadata() { |
| 114 |
return [ |
| 115 |
[ |
| 116 |
'rgb-na-png.png', |
| 117 |
[ |
| 118 |
'width' => 50, |
| 119 |
'height' => 50, |
| 120 |
'bits' => 8, |
| 121 |
'metadata' => [ |
| 122 |
'frameCount' => 0, |
| 123 |
'loopCount' => 1, |
| 124 |
'duration' => 0.0, |
| 125 |
'bitDepth' => 8, |
| 126 |
'colorType' => 'truecolour', |
| 127 |
'metadata' => [ |
| 128 |
'_MW_PNG_VERSION' => 1, |
| 129 |
], |
| 130 |
], |
| 131 |
], |
| 132 |
], |
| 133 |
[ |
| 134 |
'xmp.png', |
| 135 |
[ |
| 136 |
'width' => 50, |
| 137 |
'height' => 50, |
| 138 |
'bits' => 1, |
| 139 |
'metadata' => [ |
| 140 |
'frameCount' => 0, |
| 141 |
'loopCount' => 1, |
| 142 |
'duration' => 0.0, |
| 143 |
'bitDepth' => 1, |
| 144 |
'colorType' => 'index-coloured', |
| 145 |
'metadata' => [ |
| 146 |
'SerialNumber' => '123456789', |
| 147 |
'_MW_PNG_VERSION' => 1, |
| 148 |
], |
| 149 |
] |
| 150 |
] |
| 151 |
], |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function testGetIndependentMetaArray( $filename, $expected ) { |
| 162 |
$file = $this->dataFile( $filename, 'image/png' ); |
| 163 |
$actual = $this->handler->getCommonMetaArray( $file ); |
| 164 |
$this->assertEquals( $expected, $actual ); |
| 165 |
} |
| 166 |
|
| 167 |
public static function provideGetIndependentMetaArray() { |
| 168 |
return [ |
| 169 |
[ 'rgb-na-png.png', [] ], |
| 170 |
[ 'xmp.png', |
| 171 |
[ |
| 172 |
'SerialNumber' => '123456789', |
| 173 |
] |
| 174 |
], |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function testGetLength( $filename, $expectedLength ) { |
| 185 |
$file = $this->dataFile( $filename, 'image/png' ); |
| 186 |
$actualLength = $file->getLength(); |
| 187 |
$this->assertEqualsWithDelta( $expectedLength, $actualLength, 0.00001 ); |
| 188 |
} |
| 189 |
|
| 190 |
public static function provideGetLength() { |
| 191 |
return [ |
| 192 |
[ 'Animated_PNG_example_bouncing_beach_ball.png', 1.5 ], |
| 193 |
[ 'Png-native-test.png', 0.0 ], |
| 194 |
[ 'greyscale-png.png', 0.0 ], |
| 195 |
[ '1bit-png.png', 0.0 ], |
| 196 |
]; |
| 197 |
} |
| 198 |
} |