wiki.techinc.nl/tests/phpunit/includes/media/SvgHandlerTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

458 lines
14 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MainConfigNames;
use Wikimedia\TestingAccessWrapper;
/**
* @group Media
*/
class SvgHandlerTest extends MediaWikiMediaTestCase {
/**
* @covers \SvgHandler::getCommonMetaArray()
* @dataProvider provideGetIndependentMetaArray
*
* @param string $filename
* @param array $expected The expected independent metadata
*/
public function testGetIndependentMetaArray( $filename, $expected ) {
$this->filePath = __DIR__ . '/../../data/media/';
$this->overrideConfigValue( MainConfigNames::ShowEXIF, true );
$file = $this->dataFile( $filename, 'image/svg+xml' );
$handler = new SvgHandler();
$res = $handler->getCommonMetaArray( $file );
self::assertEquals( $expected, $res );
}
public static function provideGetIndependentMetaArray() {
return [
[ 'Tux.svg', [
'ObjectName' => 'Tux',
'ImageDescription' =>
'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
] ],
[ 'Wikimedia-logo.svg', [] ]
];
}
/**
* @covers \SvgHandler::getMatchedLanguage()
* @dataProvider provideGetMatchedLanguage
*
* @param string $userPreferredLanguage
* @param array $svgLanguages
* @param string $expectedMatch
*/
public function testGetMatchedLanguage( $userPreferredLanguage, $svgLanguages, $expectedMatch ) {
$handler = new SvgHandler();
$match = $handler->getMatchedLanguage( $userPreferredLanguage, $svgLanguages );
self::assertEquals( $expectedMatch, $match );
}
public static function provideGetMatchedLanguage() {
return [
'no match' => [
'userPreferredLanguage' => 'en',
'svgLanguages' => [ 'de-DE', 'zh', 'ga', 'fr', 'sr-Latn-ME' ],
'expectedMatch' => null,
],
'no subtags' => [
'userPreferredLanguage' => 'en',
'svgLanguages' => [ 'de', 'zh', 'en', 'fr' ],
'expectedMatch' => 'en',
],
'user no subtags, svg 1 subtag' => [
'userPreferredLanguage' => 'en',
'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ],
'expectedMatch' => 'en-GB',
],
'user no subtags, svg >1 subtag' => [
'userPreferredLanguage' => 'sr',
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ],
'expectedMatch' => 'sr-Cyrl-BA',
],
'user 1 subtag, svg no subtags' => [
'userPreferredLanguage' => 'en-US',
'svgLanguages' => [ 'de', 'en', 'en', 'fr' ],
'expectedMatch' => null,
],
'user 1 subtag, svg 1 subtag' => [
'userPreferredLanguage' => 'en-US',
'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ],
'expectedMatch' => 'en-US',
],
'user 1 subtag, svg >1 subtag' => [
'userPreferredLanguage' => 'sr-Latn',
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'fr' ],
'expectedMatch' => 'sr-Latn-ME',
],
'user >1 subtag, svg >1 subtag' => [
'userPreferredLanguage' => 'sr-Latn-ME',
'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ],
'expectedMatch' => 'sr-Latn-ME',
],
'user >1 subtag, svg <=1 subtag' => [
'userPreferredLanguage' => 'sr-Latn-ME',
'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn', 'en-US', 'fr' ],
'expectedMatch' => null,
],
'ensure case-insensitive' => [
'userPreferredLanguage' => 'sr-latn',
'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn-ME', 'en-US', 'fr' ],
'expectedMatch' => 'sr-Latn-ME',
],
'deprecated MW code als' => [
'userPreferredLanguage' => 'als',
'svgLanguages' => [ 'en', 'als', 'gsw' ],
'expectedMatch' => 'als',
],
'deprecated language code i-klingon' => [
'userPreferredLanguage' => 'i-klingon',
'svgLanguages' => [ 'i-klingon' ],
'expectedMatch' => 'i-klingon',
],
'complex IETF language code' => [
'userPreferredLanguage' => 'he',
'svgLanguages' => [ 'he-IL-u-ca-hebrew-tz-jeruslm' ],
'expectedMatch' => 'he-IL-u-ca-hebrew-tz-jeruslm',
],
];
}
/**
* @covers \SvgHandler::makeParamString()
* @dataProvider provideMakeParamString
*
* @param array $params
* @param string $expected
* @param string $message
*/
public function testMakeParamString( array $params, $expected, $message = '' ) {
$handler = new SvgHandler();
self::assertEquals( $expected, $handler->makeParamString( $params ), $message );
}
public static function provideMakeParamString() {
return [
[
[],
false,
"Don't thumbnail without knowing width"
],
[
[ 'lang' => 'ru' ],
false,
"Don't thumbnail without knowing width, even with lang"
],
[
[ 'width' => 123, ],
'123px',
'Width in thumb'
],
[
[ 'width' => 123, 'lang' => 'en' ],
'123px',
'Ignore lang=en'
],
[
[ 'width' => 123, 'targetlang' => 'en' ],
'123px',
'Ignore targetlang=en'
],
[
[ 'width' => 123, 'lang' => 'en', 'targetlang' => 'ru' ],
'123px',
"lang should override targetlang even if it's in English"
],
[
[ 'width' => 123, 'targetlang' => 'en' ],
'123px',
'Ignore targetlang=en'
],
[
[ 'width' => 123, 'lang' => 'en', 'targetlang' => 'en' ],
'123px',
'Ignore lang=targetlang=en'
],
[
[ 'width' => 123, 'lang' => 'ru' ],
'langru-123px',
'Include lang in thumb'
],
[
[ 'width' => 123, 'lang' => 'zh-Hans' ],
'langzh-hans-123px',
'Lowercase language codes',
],
[
[ 'width' => 123, 'targetlang' => 'ru' ],
'langru-123px',
'Include targetlang in thumb'
],
[
[ 'width' => 123, 'lang' => 'fr', 'targetlang' => 'sq' ],
'langfr-123px',
'lang should override targetlang'
],
];
}
/**
* @covers \SvgHandler::normaliseParamsInternal()
* @dataProvider provideNormaliseParamsInternal
*/
public function testNormaliseParamsInternal( $message,
$width,
$height,
array $params,
array $paramsExpected = null
) {
$this->overrideConfigValue( MainConfigNames::SVGMaxSize, 1000 );
/** @var SvgHandler $handler */
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() );
$file = $this->getMockBuilder( File::class )
->disableOriginalConstructor()
Use the unserialized form of image metadata internally Image metadata is usually a serialized string representing an array. Passing the string around internally and having everything unserialize it is an awkward convention. Also, many image handlers were reading the file twice: once for getMetadata() and again for getImageSize(). Often getMetadata() would actually read the width and height and then throw it away. So, in filerepo: * Add File::getMetadataItem(), which promises to allow partial loading of metadata per my proposal on T275268 in a future commit. * Add File::getMetadataArray(), which returns the unserialized array. Some file handlers were returning non-serializable strings from getMetadata(), so I gave them a legacy array form ['_error' => ...] * Changed MWFileProps to return the array form of metadata. * Deprecate the weird File::getImageSize(). It was apparently not called by anything, but was overridden by UnregisteredLocalFile. * Wrap serialize/unserialize with File::getMetadataForDb() and File::loadMetadataFromDb() in preparation for T275268. In MediaHandler: * Merged MediaHandler::getImageSize() and MediaHandler::getMetadata() into getSizeAndMetadata(). Deprecated the old methods. * Instead of isMetadataValid() we now have isFileMetadataValid(), which only gets a File object, so it can decide what data it needs to load. * Simplified getPageDimensions() by having it return false for non-paged media. It was not called in that case, but was implemented anyway. In specific handlers: * Rename DjVuHandler::getUnserializedMetadata() and extractTreesFromMetadata() for clarity. "Metadata" in these function names meant an XML string. * Updated DjVuImage::getImageSize() to provide image sizes in the new style. * In ExifBitmapHandler, getRotationForExif() now takes just the Orientation tag, rather than a serialized string. Also renamed for clarity. * In GIFMetadataExtractor, return the width, height and bits per channel instead of throwing them away. There was some conflation in decodeBPP() which I picked apart. Refer to GIF89a section 18. * In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract bits per channel, width, height and components (channel count). This is essentially a port of PHP's getimagesize(), so should be bugwards compatible. * In PNGMetadataExtractor, return the width and height, which were previously assigned to unused local variables. I verified the implementation by referring to the specification. * In SvgHandler, retain the version validation from unpackMetadata(), but rename the function since it now takes an array as input. In tests: * In ExifBitmapTest, refactored some tests by using a provider. * In GIFHandlerTest and PNGHandlerTest, I removed the tests in which getMetadata() returns null, since it doesn't make sense when ported to getMetadataArray(). I added tests for empty arrays instead. * In tests, I retained serialization of input data since I figure it's useful to confirm that existing database rows will continue to be read correctly. I removed serialization of expected values, replacing them with plain data. * In tests, I replaced access to private class constants like BROKEN_FILE with string literals, since stability is essential. If the class constant changes, the test should fail. Elsewhere: * In maintenance/refreshImageMetadata.php, I removed the check for shrinking image metadata, since it's not easy to implement and is not future compatible. Image metadata is expected to shrink in future. Bug: T275268 Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
->onlyMethods( [ 'getWidth', 'getHeight', 'getMetadataArray', 'getHandler' ] )
->getMock();
$file->method( 'getWidth' )
->willReturn( $width );
$file->method( 'getHeight' )
->willReturn( $height );
Use the unserialized form of image metadata internally Image metadata is usually a serialized string representing an array. Passing the string around internally and having everything unserialize it is an awkward convention. Also, many image handlers were reading the file twice: once for getMetadata() and again for getImageSize(). Often getMetadata() would actually read the width and height and then throw it away. So, in filerepo: * Add File::getMetadataItem(), which promises to allow partial loading of metadata per my proposal on T275268 in a future commit. * Add File::getMetadataArray(), which returns the unserialized array. Some file handlers were returning non-serializable strings from getMetadata(), so I gave them a legacy array form ['_error' => ...] * Changed MWFileProps to return the array form of metadata. * Deprecate the weird File::getImageSize(). It was apparently not called by anything, but was overridden by UnregisteredLocalFile. * Wrap serialize/unserialize with File::getMetadataForDb() and File::loadMetadataFromDb() in preparation for T275268. In MediaHandler: * Merged MediaHandler::getImageSize() and MediaHandler::getMetadata() into getSizeAndMetadata(). Deprecated the old methods. * Instead of isMetadataValid() we now have isFileMetadataValid(), which only gets a File object, so it can decide what data it needs to load. * Simplified getPageDimensions() by having it return false for non-paged media. It was not called in that case, but was implemented anyway. In specific handlers: * Rename DjVuHandler::getUnserializedMetadata() and extractTreesFromMetadata() for clarity. "Metadata" in these function names meant an XML string. * Updated DjVuImage::getImageSize() to provide image sizes in the new style. * In ExifBitmapHandler, getRotationForExif() now takes just the Orientation tag, rather than a serialized string. Also renamed for clarity. * In GIFMetadataExtractor, return the width, height and bits per channel instead of throwing them away. There was some conflation in decodeBPP() which I picked apart. Refer to GIF89a section 18. * In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract bits per channel, width, height and components (channel count). This is essentially a port of PHP's getimagesize(), so should be bugwards compatible. * In PNGMetadataExtractor, return the width and height, which were previously assigned to unused local variables. I verified the implementation by referring to the specification. * In SvgHandler, retain the version validation from unpackMetadata(), but rename the function since it now takes an array as input. In tests: * In ExifBitmapTest, refactored some tests by using a provider. * In GIFHandlerTest and PNGHandlerTest, I removed the tests in which getMetadata() returns null, since it doesn't make sense when ported to getMetadataArray(). I added tests for empty arrays instead. * In tests, I retained serialization of input data since I figure it's useful to confirm that existing database rows will continue to be read correctly. I removed serialization of expected values, replacing them with plain data. * In tests, I replaced access to private class constants like BROKEN_FILE with string literals, since stability is essential. If the class constant changes, the test should fail. Elsewhere: * In maintenance/refreshImageMetadata.php, I removed the check for shrinking image metadata, since it's not easy to implement and is not future compatible. Image metadata is expected to shrink in future. Bug: T275268 Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
$file->method( 'getMetadataArray' )
->willReturn( [
'version' => SvgHandler::SVG_METADATA_VERSION,
'translations' => [
'en' => SVGReader::LANG_FULL_MATCH,
'ru' => SVGReader::LANG_FULL_MATCH,
],
Use the unserialized form of image metadata internally Image metadata is usually a serialized string representing an array. Passing the string around internally and having everything unserialize it is an awkward convention. Also, many image handlers were reading the file twice: once for getMetadata() and again for getImageSize(). Often getMetadata() would actually read the width and height and then throw it away. So, in filerepo: * Add File::getMetadataItem(), which promises to allow partial loading of metadata per my proposal on T275268 in a future commit. * Add File::getMetadataArray(), which returns the unserialized array. Some file handlers were returning non-serializable strings from getMetadata(), so I gave them a legacy array form ['_error' => ...] * Changed MWFileProps to return the array form of metadata. * Deprecate the weird File::getImageSize(). It was apparently not called by anything, but was overridden by UnregisteredLocalFile. * Wrap serialize/unserialize with File::getMetadataForDb() and File::loadMetadataFromDb() in preparation for T275268. In MediaHandler: * Merged MediaHandler::getImageSize() and MediaHandler::getMetadata() into getSizeAndMetadata(). Deprecated the old methods. * Instead of isMetadataValid() we now have isFileMetadataValid(), which only gets a File object, so it can decide what data it needs to load. * Simplified getPageDimensions() by having it return false for non-paged media. It was not called in that case, but was implemented anyway. In specific handlers: * Rename DjVuHandler::getUnserializedMetadata() and extractTreesFromMetadata() for clarity. "Metadata" in these function names meant an XML string. * Updated DjVuImage::getImageSize() to provide image sizes in the new style. * In ExifBitmapHandler, getRotationForExif() now takes just the Orientation tag, rather than a serialized string. Also renamed for clarity. * In GIFMetadataExtractor, return the width, height and bits per channel instead of throwing them away. There was some conflation in decodeBPP() which I picked apart. Refer to GIF89a section 18. * In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract bits per channel, width, height and components (channel count). This is essentially a port of PHP's getimagesize(), so should be bugwards compatible. * In PNGMetadataExtractor, return the width and height, which were previously assigned to unused local variables. I verified the implementation by referring to the specification. * In SvgHandler, retain the version validation from unpackMetadata(), but rename the function since it now takes an array as input. In tests: * In ExifBitmapTest, refactored some tests by using a provider. * In GIFHandlerTest and PNGHandlerTest, I removed the tests in which getMetadata() returns null, since it doesn't make sense when ported to getMetadataArray(). I added tests for empty arrays instead. * In tests, I retained serialization of input data since I figure it's useful to confirm that existing database rows will continue to be read correctly. I removed serialization of expected values, replacing them with plain data. * In tests, I replaced access to private class constants like BROKEN_FILE with string literals, since stability is essential. If the class constant changes, the test should fail. Elsewhere: * In maintenance/refreshImageMetadata.php, I removed the check for shrinking image metadata, since it's not easy to implement and is not future compatible. Image metadata is expected to shrink in future. Bug: T275268 Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
] );
$file->method( 'getHandler' )
->willReturn( $handler );
/** @var File $file */
$params = $handler->normaliseParamsInternal( $file, $params );
self::assertEquals( $paramsExpected, $params, $message );
}
public static function provideNormaliseParamsInternal() {
return [
[
'No need to change anything',
400, 500,
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
],
[
'Resize horizontal image',
2000, 1600,
[ 'physicalWidth' => 2000, 'physicalHeight' => 1600, 'page' => 0 ],
[ 'physicalWidth' => 1250, 'physicalHeight' => 1000, 'page' => 0 ],
],
[
'Resize vertical image',
1600, 2000,
[ 'physicalWidth' => 1600, 'physicalHeight' => 2000, 'page' => 0 ],
[ 'physicalWidth' => 1000, 'physicalHeight' => 1250, 'page' => 0 ],
],
[
'Preserve targetlang present in the image',
400, 500,
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
],
[
'Preserve targetlang present in the image 2',
400, 500,
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
],
[
'Remove targetlang not present in the image',
400, 500,
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'de' ],
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
],
[
'Remove targetlang not present in the image 2',
400, 500,
[ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'ru-UA' ],
[ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
],
];
}
/**
* @covers \SvgHandler::isEnabled()
* @dataProvider provideIsEnabled
*
* @param string $converter
* @param bool $expected
*/
public function testIsEnabled( $converter, $expected ) {
$this->overrideConfigValues( [
MainConfigNames::SVGConverter => $converter,
MainConfigNames::SVGNativeRendering => false,
] );
$handler = new SvgHandler();
self::assertEquals( $expected, $handler->isEnabled() );
}
public static function provideIsEnabled() {
return [
[ 'ImageMagick', true ],
[ 'sodipodi', true ],
[ 'invalid', false ],
];
}
/**
* @covers \SvgHandler::getAvailableLanguages()
* @dataProvider provideAvailableLanguages
*/
public function testGetAvailableLanguages( array $metadata, array $expected ) {
$metadata['version'] = SvgHandler::SVG_METADATA_VERSION;
$file = $this->getMockBuilder( File::class )
->disableOriginalConstructor()
Use the unserialized form of image metadata internally Image metadata is usually a serialized string representing an array. Passing the string around internally and having everything unserialize it is an awkward convention. Also, many image handlers were reading the file twice: once for getMetadata() and again for getImageSize(). Often getMetadata() would actually read the width and height and then throw it away. So, in filerepo: * Add File::getMetadataItem(), which promises to allow partial loading of metadata per my proposal on T275268 in a future commit. * Add File::getMetadataArray(), which returns the unserialized array. Some file handlers were returning non-serializable strings from getMetadata(), so I gave them a legacy array form ['_error' => ...] * Changed MWFileProps to return the array form of metadata. * Deprecate the weird File::getImageSize(). It was apparently not called by anything, but was overridden by UnregisteredLocalFile. * Wrap serialize/unserialize with File::getMetadataForDb() and File::loadMetadataFromDb() in preparation for T275268. In MediaHandler: * Merged MediaHandler::getImageSize() and MediaHandler::getMetadata() into getSizeAndMetadata(). Deprecated the old methods. * Instead of isMetadataValid() we now have isFileMetadataValid(), which only gets a File object, so it can decide what data it needs to load. * Simplified getPageDimensions() by having it return false for non-paged media. It was not called in that case, but was implemented anyway. In specific handlers: * Rename DjVuHandler::getUnserializedMetadata() and extractTreesFromMetadata() for clarity. "Metadata" in these function names meant an XML string. * Updated DjVuImage::getImageSize() to provide image sizes in the new style. * In ExifBitmapHandler, getRotationForExif() now takes just the Orientation tag, rather than a serialized string. Also renamed for clarity. * In GIFMetadataExtractor, return the width, height and bits per channel instead of throwing them away. There was some conflation in decodeBPP() which I picked apart. Refer to GIF89a section 18. * In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract bits per channel, width, height and components (channel count). This is essentially a port of PHP's getimagesize(), so should be bugwards compatible. * In PNGMetadataExtractor, return the width and height, which were previously assigned to unused local variables. I verified the implementation by referring to the specification. * In SvgHandler, retain the version validation from unpackMetadata(), but rename the function since it now takes an array as input. In tests: * In ExifBitmapTest, refactored some tests by using a provider. * In GIFHandlerTest and PNGHandlerTest, I removed the tests in which getMetadata() returns null, since it doesn't make sense when ported to getMetadataArray(). I added tests for empty arrays instead. * In tests, I retained serialization of input data since I figure it's useful to confirm that existing database rows will continue to be read correctly. I removed serialization of expected values, replacing them with plain data. * In tests, I replaced access to private class constants like BROKEN_FILE with string literals, since stability is essential. If the class constant changes, the test should fail. Elsewhere: * In maintenance/refreshImageMetadata.php, I removed the check for shrinking image metadata, since it's not easy to implement and is not future compatible. Image metadata is expected to shrink in future. Bug: T275268 Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
->onlyMethods( [ 'getMetadataArray' ] )
->getMock();
Use the unserialized form of image metadata internally Image metadata is usually a serialized string representing an array. Passing the string around internally and having everything unserialize it is an awkward convention. Also, many image handlers were reading the file twice: once for getMetadata() and again for getImageSize(). Often getMetadata() would actually read the width and height and then throw it away. So, in filerepo: * Add File::getMetadataItem(), which promises to allow partial loading of metadata per my proposal on T275268 in a future commit. * Add File::getMetadataArray(), which returns the unserialized array. Some file handlers were returning non-serializable strings from getMetadata(), so I gave them a legacy array form ['_error' => ...] * Changed MWFileProps to return the array form of metadata. * Deprecate the weird File::getImageSize(). It was apparently not called by anything, but was overridden by UnregisteredLocalFile. * Wrap serialize/unserialize with File::getMetadataForDb() and File::loadMetadataFromDb() in preparation for T275268. In MediaHandler: * Merged MediaHandler::getImageSize() and MediaHandler::getMetadata() into getSizeAndMetadata(). Deprecated the old methods. * Instead of isMetadataValid() we now have isFileMetadataValid(), which only gets a File object, so it can decide what data it needs to load. * Simplified getPageDimensions() by having it return false for non-paged media. It was not called in that case, but was implemented anyway. In specific handlers: * Rename DjVuHandler::getUnserializedMetadata() and extractTreesFromMetadata() for clarity. "Metadata" in these function names meant an XML string. * Updated DjVuImage::getImageSize() to provide image sizes in the new style. * In ExifBitmapHandler, getRotationForExif() now takes just the Orientation tag, rather than a serialized string. Also renamed for clarity. * In GIFMetadataExtractor, return the width, height and bits per channel instead of throwing them away. There was some conflation in decodeBPP() which I picked apart. Refer to GIF89a section 18. * In JpegMetadataExtractor, process the SOF0/SOF2 segment to extract bits per channel, width, height and components (channel count). This is essentially a port of PHP's getimagesize(), so should be bugwards compatible. * In PNGMetadataExtractor, return the width and height, which were previously assigned to unused local variables. I verified the implementation by referring to the specification. * In SvgHandler, retain the version validation from unpackMetadata(), but rename the function since it now takes an array as input. In tests: * In ExifBitmapTest, refactored some tests by using a provider. * In GIFHandlerTest and PNGHandlerTest, I removed the tests in which getMetadata() returns null, since it doesn't make sense when ported to getMetadataArray(). I added tests for empty arrays instead. * In tests, I retained serialization of input data since I figure it's useful to confirm that existing database rows will continue to be read correctly. I removed serialization of expected values, replacing them with plain data. * In tests, I replaced access to private class constants like BROKEN_FILE with string literals, since stability is essential. If the class constant changes, the test should fail. Elsewhere: * In maintenance/refreshImageMetadata.php, I removed the check for shrinking image metadata, since it's not easy to implement and is not future compatible. Image metadata is expected to shrink in future. Bug: T275268 Change-Id: I039785d5b6439d71dcc21dcb972177dba5c3a67d
2021-05-19 00:24:32 +00:00
$file->method( 'getMetadataArray' )
->willReturn( $metadata );
$handler = new SvgHandler();
/** @var File $file */
self::assertEquals( $expected, $handler->getAvailableLanguages( $file ) );
}
public static function provideAvailableLanguages() {
return [
[ [], [] ],
[ [ 'translations' => [] ], [] ],
[
[
'translations' => [
'ru-RU' => SVGReader::LANG_PREFIX_MATCH
]
],
[],
],
[
[
'translations' => [
'en' => SVGReader::LANG_FULL_MATCH,
'ru-RU' => SVGReader::LANG_PREFIX_MATCH,
'ru' => SVGReader::LANG_FULL_MATCH,
'fr-CA' => SVGReader::LANG_PREFIX_MATCH,
'zh-Hans' => SVGReader::LANG_FULL_MATCH,
'zh-Hans-TW' => SVGReader::LANG_FULL_MATCH,
'he-IL-u-ca-hebrew-tz-jeruslm' => SVGReader::LANG_FULL_MATCH,
],
],
[ 'en', 'ru', 'zh-hans', 'zh-hans-tw', 'he-il-u-ca-hebrew-tz-jeruslm' ],
],
];
}
/**
* @covers \SvgHandler::getLanguageFromParams()
* @dataProvider provideGetLanguageFromParams
*
* @param array $params
* @param string $expected
* @param string $message
*/
public function testGetLanguageFromParams( array $params, $expected, $message ) {
/** @var SvgHandler $handler */
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() );
self::assertEquals( $expected, $handler->getLanguageFromParams( $params ), $message );
}
public static function provideGetLanguageFromParams() {
return [
[ [], 'en', 'Default no language to en' ],
[ [ 'preserve' => 'this' ], 'en', 'Default no language to en 2' ],
[ [ 'preserve' => 'this', 'lang' => 'ru' ], 'ru', 'Language from lang' ],
[ [ 'lang' => 'ru' ], 'ru', 'Language from lang 2' ],
[ [ 'targetlang' => 'fr' ], 'fr', 'Language from targetlang' ],
[ [ 'lang' => 'fr', 'targetlang' => 'de' ], 'fr', 'lang overrides targetlang' ],
];
}
/**
* @covers \SvgHandler::parseParamString()
* @dataProvider provideParseParamString
*
* @param string $paramString
* @param array $expected
* @param string $message
* @return void
*/
public function testParseParamString( string $paramString, $expected, $message ) {
/** @var SvgHandler $handler */
$handler = TestingAccessWrapper::newFromObject( new SvgHandler() );
$params = $handler->parseParamString( $paramString );
self::assertSame( $expected, $params, $message );
if ( $params === false ) {
return;
}
foreach ( $expected as $key => $value ) {
self::assertArrayHasKey( $key, $params, $message );
self::assertEquals( $value, $params[$key], $message );
}
}
public static function provideParseParamString() {
return [
[ '100px', [ 'width' => '100', 'lang' => 'en' ], 'Only width' ],
[ 'langde-100px', [ 'width' => '100', 'lang' => 'de' ], 'German language and width' ],
[ 'langzh-hans-100px', [ 'width' => '100', 'lang' => 'zh-hans' ], 'Chinese language and width' ],
[ 'langzh-Hans-100px', false, 'Capitalized language code' ],
[ 'langzh-TW-100px', false, 'Deprecated MW language code' ],
[ 'langund-100px', [ 'width' => '100', 'lang' => 'und' ], 'Undetermined language code' ],
[ 'langzh-%25-100px', false, 'Invalid IETF language code' ],
[ 'langhe-il-u-ca-hebrew-tz-jeruslm-100px',
[ 'width' => '100', 'lang' => 'he-il-u-ca-hebrew-tz-jeruslm' ],
'Very complex IETF language code'
],
];
}
/**
* @covers \SvgHandler::allowRenderingByUserAgent()
* @dataProvider provideNativeSVGDataRendering
*
* @param string $filename of the file to test
* @param bool $svgEnabled value of MainConfigNames::SVGNativeRendering
* @param int $filesizeLimit value of MainConfigNames::SVGNativeRenderingSizeLimit
* @param bool $expected if the SVG is expected to be rendered natively by browser agent
* @return void
*/
public function testNativeSVGDataRendering( $filename, $svgEnabled, $filesizeLimit, $expected ) {
$this->filePath = __DIR__ . '/../../data/media/';
$this->overrideConfigValues( [
MainConfigNames::SVGNativeRendering => $svgEnabled,
MainConfigNames::SVGNativeRenderingSizeLimit => $filesizeLimit,
] );
$file = $this->dataFile( $filename, 'image/svg+xml' );
$handler = new SvgHandler();
self::assertEquals( $expected, $handler->allowRenderingByUserAgent( $file ) );
}
public static function provideNativeSVGDataRendering() {
return [
[ 'Tux.svg', false, 50 * 1024, false, 'SVG without Native rendering enabled' ],
[ 'Tux.svg', true, 50 * 1024, true, 'SVG with Native rendering enforced' ],
[ 'Tux.svg', true, 1, true, 'SVG with Native rendering enforced ignoring filesize limit' ],
[ 'Tux.svg', 'partial', 223250, true, 'SVG with partial Native rendering' ],
[ 'Tux.svg', 'partial', 1, false, 'SVG with partial Native rendering, where filesize is bigger than the limit' ],
[ 'translated.svg', 'partial', null, false, 'SVG with translations should not be left to native rendering' ],
];
}
}