| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use Wikimedia\TestingAccessWrapper; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class SvgHandlerTest extends MediaWikiMediaTestCase { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public function testGetIndependentMetaArray( $filename, $expected ) { |
| 19 |
$this->filePath = __DIR__ . '/../../data/media/'; |
| 20 |
$this->overrideConfigValue( MainConfigNames::ShowEXIF, true ); |
| 21 |
|
| 22 |
$file = $this->dataFile( $filename, 'image/svg+xml' ); |
| 23 |
$handler = new SvgHandler(); |
| 24 |
$res = $handler->getCommonMetaArray( $file ); |
| 25 |
|
| 26 |
self::assertEquals( $expected, $res ); |
| 27 |
} |
| 28 |
|
| 29 |
public static function provideGetIndependentMetaArray() { |
| 30 |
return [ |
| 31 |
[ 'Tux.svg', [ |
| 32 |
'ObjectName' => 'Tux', |
| 33 |
'ImageDescription' => |
| 34 |
'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg', |
| 35 |
] ], |
| 36 |
[ 'Wikimedia-logo.svg', [] ] |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testGetMatchedLanguage( $userPreferredLanguage, $svgLanguages, $expectedMatch ) { |
| 49 |
$handler = new SvgHandler(); |
| 50 |
$match = $handler->getMatchedLanguage( $userPreferredLanguage, $svgLanguages ); |
| 51 |
self::assertEquals( $expectedMatch, $match ); |
| 52 |
} |
| 53 |
|
| 54 |
public static function provideGetMatchedLanguage() { |
| 55 |
return [ |
| 56 |
'no match' => [ |
| 57 |
'userPreferredLanguage' => 'en', |
| 58 |
'svgLanguages' => [ 'de-DE', 'zh', 'ga', 'fr', 'sr-Latn-ME' ], |
| 59 |
'expectedMatch' => null, |
| 60 |
], |
| 61 |
'no subtags' => [ |
| 62 |
'userPreferredLanguage' => 'en', |
| 63 |
'svgLanguages' => [ 'de', 'zh', 'en', 'fr' ], |
| 64 |
'expectedMatch' => 'en', |
| 65 |
], |
| 66 |
'user no subtags, svg 1 subtag' => [ |
| 67 |
'userPreferredLanguage' => 'en', |
| 68 |
'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ], |
| 69 |
'expectedMatch' => 'en-GB', |
| 70 |
], |
| 71 |
'user no subtags, svg >1 subtag' => [ |
| 72 |
'userPreferredLanguage' => 'sr', |
| 73 |
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ], |
| 74 |
'expectedMatch' => 'sr-Cyrl-BA', |
| 75 |
], |
| 76 |
'user 1 subtag, svg no subtags' => [ |
| 77 |
'userPreferredLanguage' => 'en-US', |
| 78 |
'svgLanguages' => [ 'de', 'en', 'en', 'fr' ], |
| 79 |
'expectedMatch' => null, |
| 80 |
], |
| 81 |
'user 1 subtag, svg 1 subtag' => [ |
| 82 |
'userPreferredLanguage' => 'en-US', |
| 83 |
'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ], |
| 84 |
'expectedMatch' => 'en-US', |
| 85 |
], |
| 86 |
'user 1 subtag, svg >1 subtag' => [ |
| 87 |
'userPreferredLanguage' => 'sr-Latn', |
| 88 |
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'fr' ], |
| 89 |
'expectedMatch' => 'sr-Latn-ME', |
| 90 |
], |
| 91 |
'user >1 subtag, svg >1 subtag' => [ |
| 92 |
'userPreferredLanguage' => 'sr-Latn-ME', |
| 93 |
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ], |
| 94 |
'expectedMatch' => 'sr-Latn-ME', |
| 95 |
], |
| 96 |
'user >1 subtag, svg <=1 subtag' => [ |
| 97 |
'userPreferredLanguage' => 'sr-Latn-ME', |
| 98 |
'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn', 'en-US', 'fr' ], |
| 99 |
'expectedMatch' => null, |
| 100 |
], |
| 101 |
'ensure case-insensitive' => [ |
| 102 |
'userPreferredLanguage' => 'sr-latn', |
| 103 |
'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn-ME', 'en-US', 'fr' ], |
| 104 |
'expectedMatch' => 'sr-Latn-ME', |
| 105 |
], |
| 106 |
'deprecated MW code als' => [ |
| 107 |
'userPreferredLanguage' => 'als', |
| 108 |
'svgLanguages' => [ 'en', 'als', 'gsw' ], |
| 109 |
'expectedMatch' => 'als', |
| 110 |
], |
| 111 |
'deprecated language code i-klingon' => [ |
| 112 |
'userPreferredLanguage' => 'i-klingon', |
| 113 |
'svgLanguages' => [ 'i-klingon' ], |
| 114 |
'expectedMatch' => 'i-klingon', |
| 115 |
], |
| 116 |
'complex IETF language code' => [ |
| 117 |
'userPreferredLanguage' => 'he', |
| 118 |
'svgLanguages' => [ 'he-IL-u-ca-hebrew-tz-jeruslm' ], |
| 119 |
'expectedMatch' => 'he-IL-u-ca-hebrew-tz-jeruslm', |
| 120 |
], |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function testMakeParamString( array $params, $expected, $message = '' ) { |
| 133 |
$handler = new SvgHandler(); |
| 134 |
self::assertEquals( $expected, $handler->makeParamString( $params ), $message ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function provideMakeParamString() { |
| 138 |
return [ |
| 139 |
[ |
| 140 |
[], |
| 141 |
false, |
| 142 |
"Don't thumbnail without knowing width" |
| 143 |
], |
| 144 |
[ |
| 145 |
[ 'lang' => 'ru' ], |
| 146 |
false, |
| 147 |
"Don't thumbnail without knowing width, even with lang" |
| 148 |
], |
| 149 |
[ |
| 150 |
[ 'width' => 123, ], |
| 151 |
'123px', |
| 152 |
'Width in thumb' |
| 153 |
], |
| 154 |
[ |
| 155 |
[ 'width' => 113, 'physicalWidth' => 120 ], |
| 156 |
'120px', |
| 157 |
'physicalWidth should override width in thumb' |
| 158 |
], |
| 159 |
[ |
| 160 |
[ 'width' => 123, 'lang' => 'en' ], |
| 161 |
'123px', |
| 162 |
'Ignore lang=en' |
| 163 |
], |
| 164 |
[ |
| 165 |
[ 'width' => 123, 'targetlang' => 'en' ], |
| 166 |
'123px', |
| 167 |
'Ignore targetlang=en' |
| 168 |
], |
| 169 |
[ |
| 170 |
[ 'width' => 123, 'lang' => 'en', 'targetlang' => 'ru' ], |
| 171 |
'123px', |
| 172 |
"lang should override targetlang even if it's in English" |
| 173 |
], |
| 174 |
[ |
| 175 |
[ 'width' => 123, 'targetlang' => 'en' ], |
| 176 |
'123px', |
| 177 |
'Ignore targetlang=en' |
| 178 |
], |
| 179 |
[ |
| 180 |
[ 'width' => 123, 'lang' => 'en', 'targetlang' => 'en' ], |
| 181 |
'123px', |
| 182 |
'Ignore lang=targetlang=en' |
| 183 |
], |
| 184 |
[ |
| 185 |
[ 'width' => 123, 'lang' => 'ru' ], |
| 186 |
'langru-123px', |
| 187 |
'Include lang in thumb' |
| 188 |
], |
| 189 |
[ |
| 190 |
[ 'width' => 123, 'lang' => 'zh-Hans' ], |
| 191 |
'langzh-hans-123px', |
| 192 |
'Lowercase language codes', |
| 193 |
], |
| 194 |
[ |
| 195 |
[ 'width' => 123, 'targetlang' => 'ru' ], |
| 196 |
'langru-123px', |
| 197 |
'Include targetlang in thumb' |
| 198 |
], |
| 199 |
[ |
| 200 |
[ 'width' => 123, 'lang' => 'fr', 'targetlang' => 'sq' ], |
| 201 |
'langfr-123px', |
| 202 |
'lang should override targetlang' |
| 203 |
], |
| 204 |
]; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function testNormaliseParamsInternal( $message, |
| 212 |
$width, |
| 213 |
$height, |
| 214 |
array $params, |
| 215 |
?array $paramsExpected = null |
| 216 |
) { |
| 217 |
$this->overrideConfigValues( [ |
| 218 |
MainConfigNames::SVGMaxSize => 1000, |
| 219 |
MainConfigNames::ThumbnailSteps => null, |
| 220 |
] ); |
| 221 |
|
| 222 |
|
| 223 |
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() ); |
| 224 |
|
| 225 |
$file = $this->getMockBuilder( File::class ) |
| 226 |
->disableOriginalConstructor() |
| 227 |
->onlyMethods( [ 'getWidth', 'getHeight', 'getMetadataArray', 'getHandler' ] ) |
| 228 |
->getMock(); |
| 229 |
|
| 230 |
$file->method( 'getWidth' ) |
| 231 |
->willReturn( $width ); |
| 232 |
$file->method( 'getHeight' ) |
| 233 |
->willReturn( $height ); |
| 234 |
$file->method( 'getMetadataArray' ) |
| 235 |
->willReturn( [ |
| 236 |
'version' => SvgHandler::SVG_METADATA_VERSION, |
| 237 |
'translations' => [ |
| 238 |
'en' => SVGReader::LANG_FULL_MATCH, |
| 239 |
'ru' => SVGReader::LANG_FULL_MATCH, |
| 240 |
], |
| 241 |
] ); |
| 242 |
$file->method( 'getHandler' ) |
| 243 |
->willReturn( $handler ); |
| 244 |
|
| 245 |
|
| 246 |
$params += [ 'page' => 1 ]; |
| 247 |
$paramsExpected += [ 'page' => 1 ]; |
| 248 |
|
| 249 |
|
| 250 |
$params = $handler->normaliseParamsInternal( $file, $params ); |
| 251 |
self::assertEquals( $paramsExpected, $params, $message ); |
| 252 |
} |
| 253 |
|
| 254 |
public static function provideNormaliseParamsInternal() { |
| 255 |
return [ |
| 256 |
[ |
| 257 |
'No need to change anything', |
| 258 |
400, 500, |
| 259 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ], |
| 260 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ], |
| 261 |
], |
| 262 |
[ |
| 263 |
'Resize horizontal image', |
| 264 |
2000, 1600, |
| 265 |
[ 'physicalWidth' => 2000, 'physicalHeight' => 1600 ], |
| 266 |
[ 'physicalWidth' => 1250, 'physicalHeight' => 1000 ], |
| 267 |
], |
| 268 |
[ |
| 269 |
'Resize vertical image', |
| 270 |
1600, 2000, |
| 271 |
[ 'physicalWidth' => 1600, 'physicalHeight' => 2000 ], |
| 272 |
[ 'physicalWidth' => 1000, 'physicalHeight' => 1250 ], |
| 273 |
], |
| 274 |
[ |
| 275 |
'Preserve targetlang present in the image', |
| 276 |
400, 500, |
| 277 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ], |
| 278 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ], |
| 279 |
], |
| 280 |
[ |
| 281 |
'Preserve targetlang present in the image 2', |
| 282 |
400, 500, |
| 283 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ], |
| 284 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ], |
| 285 |
], |
| 286 |
[ |
| 287 |
'Remove targetlang not present in the image', |
| 288 |
400, 500, |
| 289 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'de' ], |
| 290 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ], |
| 291 |
], |
| 292 |
[ |
| 293 |
'Remove targetlang not present in the image 2', |
| 294 |
400, 500, |
| 295 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'ru-UA' ], |
| 296 |
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ], |
| 297 |
], |
| 298 |
]; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function testIsEnabled( $converter, $expected ) { |
| 309 |
$this->overrideConfigValues( [ |
| 310 |
MainConfigNames::SVGConverter => $converter, |
| 311 |
MainConfigNames::SVGNativeRendering => false, |
| 312 |
] ); |
| 313 |
|
| 314 |
$handler = new SvgHandler(); |
| 315 |
self::assertEquals( $expected, $handler->isEnabled() ); |
| 316 |
} |
| 317 |
|
| 318 |
public static function provideIsEnabled() { |
| 319 |
return [ |
| 320 |
[ 'ImageMagick', true ], |
| 321 |
[ 'sodipodi', true ], |
| 322 |
[ 'invalid', false ], |
| 323 |
]; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function testGetAvailableLanguages( array $metadata, array $expected ) { |
| 331 |
$metadata['version'] = SvgHandler::SVG_METADATA_VERSION; |
| 332 |
$file = $this->getMockBuilder( File::class ) |
| 333 |
->disableOriginalConstructor() |
| 334 |
->onlyMethods( [ 'getMetadataArray' ] ) |
| 335 |
->getMock(); |
| 336 |
$file->method( 'getMetadataArray' ) |
| 337 |
->willReturn( $metadata ); |
| 338 |
|
| 339 |
$handler = new SvgHandler(); |
| 340 |
|
| 341 |
self::assertEquals( $expected, $handler->getAvailableLanguages( $file ) ); |
| 342 |
} |
| 343 |
|
| 344 |
public static function provideAvailableLanguages() { |
| 345 |
return [ |
| 346 |
[ [], [] ], |
| 347 |
[ [ 'translations' => [] ], [] ], |
| 348 |
[ |
| 349 |
[ |
| 350 |
'translations' => [ |
| 351 |
'ru-RU' => SVGReader::LANG_PREFIX_MATCH |
| 352 |
] |
| 353 |
], |
| 354 |
[], |
| 355 |
], |
| 356 |
[ |
| 357 |
[ |
| 358 |
'translations' => [ |
| 359 |
'en' => SVGReader::LANG_FULL_MATCH, |
| 360 |
'ru-RU' => SVGReader::LANG_PREFIX_MATCH, |
| 361 |
'ru' => SVGReader::LANG_FULL_MATCH, |
| 362 |
'fr-CA' => SVGReader::LANG_PREFIX_MATCH, |
| 363 |
'zh-Hans' => SVGReader::LANG_FULL_MATCH, |
| 364 |
'zh-Hans-TW' => SVGReader::LANG_FULL_MATCH, |
| 365 |
'he-IL-u-ca-hebrew-tz-jeruslm' => SVGReader::LANG_FULL_MATCH, |
| 366 |
], |
| 367 |
], |
| 368 |
[ 'en', 'ru', 'zh-hans', 'zh-hans-tw', 'he-il-u-ca-hebrew-tz-jeruslm' ], |
| 369 |
], |
| 370 |
]; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function testGetLanguageFromParams( array $params, $expected, $message ) { |
| 382 |
|
| 383 |
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() ); |
| 384 |
self::assertEquals( $expected, $handler->getLanguageFromParams( $params ), $message ); |
| 385 |
} |
| 386 |
|
| 387 |
public static function provideGetLanguageFromParams() { |
| 388 |
return [ |
| 389 |
[ [], 'en', 'Default no language to en' ], |
| 390 |
[ [ 'preserve' => 'this' ], 'en', 'Default no language to en 2' ], |
| 391 |
[ [ 'preserve' => 'this', 'lang' => 'ru' ], 'ru', 'Language from lang' ], |
| 392 |
[ [ 'lang' => 'ru' ], 'ru', 'Language from lang 2' ], |
| 393 |
[ [ 'targetlang' => 'fr' ], 'fr', 'Language from targetlang' ], |
| 394 |
[ [ 'lang' => 'fr', 'targetlang' => 'de' ], 'fr', 'lang overrides targetlang' ], |
| 395 |
]; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function testParseParamString( string $paramString, $expected, $message ) { |
| 408 |
|
| 409 |
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() ); |
| 410 |
$params = $handler->parseParamString( $paramString ); |
| 411 |
self::assertSame( $expected, $params, $message ); |
| 412 |
if ( $params === false ) { |
| 413 |
return; |
| 414 |
} |
| 415 |
foreach ( $expected as $key => $value ) { |
| 416 |
self::assertArrayHasKey( $key, $params, $message ); |
| 417 |
self::assertEquals( $value, $params[$key], $message ); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
public static function provideParseParamString() { |
| 422 |
return [ |
| 423 |
[ '100px', [ 'width' => '100', 'lang' => 'en' ], 'Only width' ], |
| 424 |
[ 'langde-100px', [ 'width' => '100', 'lang' => 'de' ], 'German language and width' ], |
| 425 |
[ 'langzh-hans-100px', [ 'width' => '100', 'lang' => 'zh-hans' ], 'Chinese language and width' ], |
| 426 |
[ 'langzh-Hans-100px', false, 'Capitalized language code' ], |
| 427 |
[ 'langzh-TW-100px', false, 'Deprecated MW language code' ], |
| 428 |
[ 'langund-100px', [ 'width' => '100', 'lang' => 'und' ], 'Undetermined language code' ], |
| 429 |
[ 'langzh-%25-100px', false, 'Invalid IETF language code' ], |
| 430 |
[ 'langhe-il-u-ca-hebrew-tz-jeruslm-100px', |
| 431 |
[ 'width' => '100', 'lang' => 'he-il-u-ca-hebrew-tz-jeruslm' ], |
| 432 |
'Very complex IETF language code' |
| 433 |
], |
| 434 |
]; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public function testNativeSVGDataRendering( $filename, $svgEnabled, $filesizeLimit, $expected ) { |
| 448 |
$this->filePath = __DIR__ . '/../../data/media/'; |
| 449 |
$this->overrideConfigValues( [ |
| 450 |
MainConfigNames::SVGNativeRendering => $svgEnabled, |
| 451 |
MainConfigNames::SVGNativeRenderingSizeLimit => $filesizeLimit, |
| 452 |
] ); |
| 453 |
|
| 454 |
$file = $this->dataFile( $filename, 'image/svg+xml' ); |
| 455 |
$handler = new SvgHandler(); |
| 456 |
self::assertEquals( $expected, $handler->allowRenderingByUserAgent( $file ) ); |
| 457 |
} |
| 458 |
|
| 459 |
public static function provideNativeSVGDataRendering() { |
| 460 |
return [ |
| 461 |
[ 'Tux.svg', false, 50 * 1024, false, 'SVG without Native rendering enabled' ], |
| 462 |
[ 'Tux.svg', true, 50 * 1024, true, 'SVG with Native rendering enforced' ], |
| 463 |
[ 'Tux.svg', true, 1, true, 'SVG with Native rendering enforced ignoring filesize limit' ], |
| 464 |
[ 'Tux.svg', 'partial', 223250, true, 'SVG with partial Native rendering' ], |
| 465 |
[ 'Tux.svg', 'partial', 1, false, 'SVG with partial Native rendering, where filesize is bigger than the limit' ], |
| 466 |
[ 'translated.svg', 'partial', null, false, 'SVG with translations should not be left to native rendering' ], |
| 467 |
]; |
| 468 |
} |
| 469 |
} |