MediaWiki  1.33.0
SvgHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
9 
17  public function testGetIndependentMetaArray( $filename, $expected ) {
18  $this->filePath = __DIR__ . '/../../data/media/';
19  $this->setMwGlobals( 'wgShowEXIF', true );
20 
21  $file = $this->dataFile( $filename, 'image/svg+xml' );
22  $handler = new SvgHandler();
23  $res = $handler->getCommonMetaArray( $file );
24 
25  self::assertEquals( $res, $expected );
26  }
27 
28  public static function provideGetIndependentMetaArray() {
29  return [
30  [ 'Tux.svg', [
31  'ObjectName' => 'Tux',
32  'ImageDescription' =>
33  'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
34  ] ],
35  [ 'Wikimedia-logo.svg', [] ]
36  ];
37  }
38 
47  public function testGetMatchedLanguage( $userPreferredLanguage, $svgLanguages, $expectedMatch ) {
48  $handler = new SvgHandler();
49  $match = $handler->getMatchedLanguage( $userPreferredLanguage, $svgLanguages );
50  self::assertEquals( $expectedMatch, $match );
51  }
52 
53  public function provideGetMatchedLanguage() {
54  return [
55  'no match' => [
56  'userPreferredLanguage' => 'en',
57  'svgLanguages' => [ 'de-DE', 'zh', 'ga', 'fr', 'sr-Latn-ME' ],
58  'expectedMatch' => null,
59  ],
60  'no subtags' => [
61  'userPreferredLanguage' => 'en',
62  'svgLanguages' => [ 'de', 'zh', 'en', 'fr' ],
63  'expectedMatch' => 'en',
64  ],
65  'user no subtags, svg 1 subtag' => [
66  'userPreferredLanguage' => 'en',
67  'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ],
68  'expectedMatch' => 'en-GB',
69  ],
70  'user no subtags, svg >1 subtag' => [
71  'userPreferredLanguage' => 'sr',
72  'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ],
73  'expectedMatch' => 'sr-Cyrl-BA',
74  ],
75  'user 1 subtag, svg no subtags' => [
76  'userPreferredLanguage' => 'en-US',
77  'svgLanguages' => [ 'de', 'en', 'en', 'fr' ],
78  'expectedMatch' => null,
79  ],
80  'user 1 subtag, svg 1 subtag' => [
81  'userPreferredLanguage' => 'en-US',
82  'svgLanguages' => [ 'de-DE', 'en-GB', 'en-US', 'fr' ],
83  'expectedMatch' => 'en-US',
84  ],
85  'user 1 subtag, svg >1 subtag' => [
86  'userPreferredLanguage' => 'sr-Latn',
87  'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'fr' ],
88  'expectedMatch' => 'sr-Latn-ME',
89  ],
90  'user >1 subtag, svg >1 subtag' => [
91  'userPreferredLanguage' => 'sr-Latn-ME',
92  'svgLanguages' => [ 'de-DE', 'sr-Cyrl-BA', 'sr-Latn-ME', 'en-US', 'fr' ],
93  'expectedMatch' => 'sr-Latn-ME',
94  ],
95  'user >1 subtag, svg <=1 subtag' => [
96  'userPreferredLanguage' => 'sr-Latn-ME',
97  'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn', 'en-US', 'fr' ],
98  'expectedMatch' => null,
99  ],
100  'ensure case-insensitive' => [
101  'userPreferredLanguage' => 'sr-latn',
102  'svgLanguages' => [ 'de-DE', 'sr-Cyrl', 'sr-Latn-ME', 'en-US', 'fr' ],
103  'expectedMatch' => 'sr-Latn-ME',
104  ],
105  ];
106  }
107 
116  public function testMakeParamString( array $params, $expected, $message = '' ) {
117  $handler = new SvgHandler();
118  self::assertEquals( $expected, $handler->makeParamString( $params ), $message );
119  }
120 
121  public function provideMakeParamString() {
122  return [
123  [
124  [],
125  false,
126  "Don't thumbnail without knowing width"
127  ],
128  [
129  [ 'lang' => 'ru' ],
130  false,
131  "Don't thumbnail without knowing width, even with lang"
132  ],
133  [
134  [ 'width' => 123, ],
135  '123px',
136  'Width in thumb'
137  ],
138  [
139  [ 'width' => 123, 'lang' => 'en' ],
140  '123px',
141  'Ignore lang=en'
142  ],
143  [
144  [ 'width' => 123, 'targetlang' => 'en' ],
145  '123px',
146  'Ignore targetlang=en'
147  ],
148  [
149  [ 'width' => 123, 'lang' => 'en', 'targetlang' => 'ru' ],
150  '123px',
151  "lang should override targetlang even of it's in English"
152  ],
153  [
154  [ 'width' => 123, 'targetlang' => 'en' ],
155  '123px',
156  'Ignore targetlang=en'
157  ],
158  [
159  [ 'width' => 123, 'lang' => 'en', 'targetlang' => 'en' ],
160  '123px',
161  'Ignore lang=targetlang=en'
162  ],
163  [
164  [ 'width' => 123, 'lang' => 'ru' ],
165  'langru-123px',
166  'Include lang in thumb'
167  ],
168  [
169  [ 'width' => 123, 'targetlang' => 'ru' ],
170  'langru-123px',
171  'Include targetlang in thumb'
172  ],
173  [
174  [ 'width' => 123, 'lang' => 'fr', 'targetlang' => 'sq' ],
175  'langfr-123px',
176  'lang should override targetlang'
177  ],
178  ];
179  }
180 
191  public function testNormaliseParamsInternal( $message,
192  $width,
193  $height,
194  array $params,
195  array $paramsExpected = null
196  ) {
197  $this->setMwGlobals( 'wgSVGMaxSize', 1000 );
198 
200  $handler = TestingAccessWrapper::newFromObject( new SvgHandler() );
201 
202  $file = $this->getMockBuilder( File::class )
203  ->disableOriginalConstructor()
204  ->setMethods( [ 'getWidth', 'getHeight', 'getMetadata', 'getHandler' ] )
205  ->getMock();
206 
207  $file->method( 'getWidth' )
208  ->willReturn( $width );
209  $file->method( 'getHeight' )
210  ->willReturn( $height );
211  $file->method( 'getMetadata' )
212  ->willReturn( serialize( [
214  'translations' => [
217  ],
218  ] ) );
219  $file->method( 'getHandler' )
220  ->willReturn( $handler );
221 
223  $params = $handler->normaliseParamsInternal( $file, $params );
224  self::assertEquals( $paramsExpected, $params, $message );
225  }
226 
227  public function provideNormaliseParamsInternal() {
228  return [
229  [
230  'No need to change anything',
231  400, 500,
232  [ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
233  [ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
234  ],
235  [
236  'Resize horizontal image',
237  2000, 1600,
238  [ 'physicalWidth' => 2000, 'physicalHeight' => 1600, 'page' => 0 ],
239  [ 'physicalWidth' => 1250, 'physicalHeight' => 1000, 'page' => 0 ],
240  ],
241  [
242  'Resize vertical image',
243  1600, 2000,
244  [ 'physicalWidth' => 1600, 'physicalHeight' => 2000, 'page' => 0 ],
245  [ 'physicalWidth' => 1000, 'physicalHeight' => 1250, 'page' => 0 ],
246  ],
247  [
248  'Preserve targetlang present in the image',
249  400, 500,
250  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
251  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
252  ],
253  [
254  'Preserve targetlang present in the image 2',
255  400, 500,
256  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
257  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'en' ],
258  ],
259  [
260  'Remove targetlang not present in the image',
261  400, 500,
262  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'de' ],
263  [ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
264  ],
265  [
266  'Remove targetlang not present in the image 2',
267  400, 500,
268  [ 'physicalWidth' => 400, 'physicalHeight' => 500, 'targetlang' => 'ru-UA' ],
269  [ 'physicalWidth' => 400, 'physicalHeight' => 500 ],
270  ],
271  ];
272  }
273 
281  public function testIsEnabled( $converter, $expected ) {
282  $this->setMwGlobals( 'wgSVGConverter', $converter );
283 
284  $handler = new SvgHandler();
285  self::assertEquals( $handler->isEnabled(), $expected );
286  }
287 
288  public function provideIsEnabled() {
289  return [
290  [ 'ImageMagick', true ],
291  [ 'sodipodi', true ],
292  [ 'invalid', false ],
293  ];
294  }
295 
303  public function testGetAvailableLanguages( array $metadata, array $expected ) {
304  $metadata['version'] = SvgHandler::SVG_METADATA_VERSION;
305  $file = $this->getMockBuilder( File::class )
306  ->disableOriginalConstructor()
307  ->setMethods( [ 'getMetadata' ] )
308  ->getMock();
309  $file->method( 'getMetadata' )
310  ->willReturn( serialize( $metadata ) );
311 
312  $handler = new SvgHandler();
314  self::assertEquals( $expected, $handler->getAvailableLanguages( $file ) );
315  }
316 
317  public function provideAvailableLanguages() {
318  return [
319  [ [], [] ],
320  [ [ 'translations' => [] ], [] ],
321  [
322  [
323  'translations' => [
325  ]
326  ],
327  [],
328  ],
329  [
330  [
331  'translations' => [
333  'ru-RU' => SVGReader::LANG_PREFIX_MATCH,
335  'fr-CA' => SVGReader::LANG_PREFIX_MATCH,
336  ],
337  ],
338  [ 'en', 'ru' ],
339  ],
340  ];
341  }
342 
351  public function testGetLanguageFromParams( array $params, $expected, $message ) {
353  $handler = TestingAccessWrapper::newFromObject( new SvgHandler() );
354  self::assertEquals( $expected, $handler->getLanguageFromParams( $params ), $message );
355  }
356 
357  public function provideGetLanguageFromParams() {
358  return [
359  [ [], 'en', 'Default no language to en' ],
360  [ [ 'preserve' => 'this' ], 'en', 'Default no language to en 2' ],
361  [ [ 'preserve' => 'this', 'lang' => 'ru' ], 'ru', 'Language from lang' ],
362  [ [ 'lang' => 'ru' ], 'ru', 'Language from lang 2' ],
363  [ [ 'targetlang' => 'fr' ], 'fr', 'Language from targetlang' ],
364  [ [ 'lang' => 'fr', 'targetlang' => 'de' ], 'fr', 'lang overrides targetlang' ],
365  ];
366  }
367 }
SVGReader\LANG_PREFIX_MATCH
const LANG_PREFIX_MATCH
Definition: SVGMetadataExtractor.php:46
SvgHandlerTest\provideAvailableLanguages
provideAvailableLanguages()
Definition: SvgHandlerTest.php:317
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
SvgHandlerTest\testGetAvailableLanguages
testGetAvailableLanguages(array $metadata, array $expected)
\SvgHandler::getAvailableLanguages() provideAvailableLanguages
Definition: SvgHandlerTest.php:303
SvgHandlerTest\testGetMatchedLanguage
testGetMatchedLanguage( $userPreferredLanguage, $svgLanguages, $expectedMatch)
SvgHandler::getMatchedLanguage() provideGetMatchedLanguage.
Definition: SvgHandlerTest.php:47
SvgHandlerTest\provideGetMatchedLanguage
provideGetMatchedLanguage()
Definition: SvgHandlerTest.php:53
SvgHandlerTest
Media.
Definition: SvgHandlerTest.php:8
SvgHandlerTest\provideGetLanguageFromParams
provideGetLanguageFromParams()
Definition: SvgHandlerTest.php:357
$params
$params
Definition: styleTest.css.php:44
$res
$res
Definition: database.txt:21
serialize
serialize()
Definition: ApiMessageTrait.php:134
MediaWikiMediaTestCase\dataFile
dataFile( $name, $type=false)
Utility function: Get a new file object for a file on disk but not actually in db.
Definition: MediaWikiMediaTestCase.php:76
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
SvgHandlerTest\provideIsEnabled
provideIsEnabled()
Definition: SvgHandlerTest.php:288
SvgHandlerTest\testGetIndependentMetaArray
testGetIndependentMetaArray( $filename, $expected)
\SvgHandler::getCommonMetaArray() provideGetIndependentMetaArray
Definition: SvgHandlerTest.php:17
MediaWikiMediaTestCase
Specificly for testing Media handlers.
Definition: MediaWikiMediaTestCase.php:5
SvgHandlerTest\testMakeParamString
testMakeParamString(array $params, $expected, $message='')
\SvgHandler::makeParamString() provideMakeParamString
Definition: SvgHandlerTest.php:116
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
SvgHandler
Handler for SVG images.
Definition: SvgHandler.php:32
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SVGReader\LANG_FULL_MATCH
const LANG_FULL_MATCH
Definition: SVGMetadataExtractor.php:47
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
SvgHandlerTest\testGetLanguageFromParams
testGetLanguageFromParams(array $params, $expected, $message)
SvgHandler::getLanguageFromParams() provideGetLanguageFromParams.
Definition: SvgHandlerTest.php:351
SvgHandlerTest\provideMakeParamString
provideMakeParamString()
Definition: SvgHandlerTest.php:121
SvgHandlerTest\provideNormaliseParamsInternal
provideNormaliseParamsInternal()
Definition: SvgHandlerTest.php:227
SvgHandlerTest\provideGetIndependentMetaArray
static provideGetIndependentMetaArray()
Definition: SvgHandlerTest.php:28
SvgHandlerTest\testNormaliseParamsInternal
testNormaliseParamsInternal( $message, $width, $height, array $params, array $paramsExpected=null)
SvgHandler::normaliseParamsInternal() provideNormaliseParamsInternal.
Definition: SvgHandlerTest.php:191
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1985
SvgHandlerTest\testIsEnabled
testIsEnabled( $converter, $expected)
\SvgHandler::isEnabled() provideIsEnabled
Definition: SvgHandlerTest.php:281
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
SvgHandler\SVG_METADATA_VERSION
const SVG_METADATA_VERSION
Definition: SvgHandler.php:33