3 use MediaWiki\MainConfigNames
;
4 use MediaWiki\Request\FauxRequest
;
5 use MediaWiki\Title\Title
;
6 use Wikimedia\TestingAccessWrapper
;
8 class ImagePageTest
extends MediaWikiMediaTestCase
{
10 protected function setUp(): void
{
11 $this->overrideConfigValue(
12 MainConfigNames
::ImageLimits
,
24 public function getImagePage( $filename ) {
25 $title = Title
::makeTitleSafe( NS_FILE
, $filename );
26 $title->setContentModel( CONTENT_MODEL_WIKITEXT
);
27 $file = $this->dataFile( $filename );
28 $iPage = new ImagePage( $title );
29 $iPage->setFile( $file );
34 * @covers \ImagePage::getThumbSizes
35 * @dataProvider providerGetThumbSizes
36 * @param string $filename
37 * @param int $expectedNumberThumbs How many thumbnails to show
39 public function testGetThumbSizes( $filename, $expectedNumberThumbs ) {
40 $iPage = $this->getImagePage( $filename );
41 $reflection = new ReflectionClass( $iPage );
42 $reflMethod = $reflection->getMethod( 'getThumbSizes' );
43 $reflMethod->setAccessible( true );
45 $actual = $reflMethod->invoke( $iPage, 545, 700 );
46 $this->assertCount( $expectedNumberThumbs, $actual );
49 public static function providerGetThumbSizes() {
51 [ 'animated.gif', 2 ],
52 [ 'Toll_Texas_1.svg', 1 ],
53 [ '80x60-Greyscale.xcf', 1 ],
54 [ 'jpeg-comment-binary.jpg', 2 ],
59 * @covers \ImagePage::getLanguageForRendering()
60 * @dataProvider provideGetLanguageForRendering
62 * @param string|null $expected Expected IETF language code
63 * @param string $wikiLangCode Wiki language code (zh)
64 * @param string|null $wikiLangVariant Wiki language code variant (zh-cn)
65 * @param string|null $lang lang=... URL parameter
67 public function testGetLanguageForRendering( $expected, $wikiLangCode, $wikiLangVariant = null, $lang = null ) {
69 if ( $lang !== null ) {
70 $params['lang'] = $lang;
72 $request = new FauxRequest( $params );
73 $this->overrideConfigValues( [
74 MainConfigNames
::LanguageCode
=> $wikiLangCode,
75 MainConfigNames
::DefaultLanguageVariant
=> $wikiLangVariant
78 $page = $this->getImagePage( 'translated.svg' );
79 $page = TestingAccessWrapper
::newFromObject( $page );
81 /** @var ImagePage $page */
82 $result = $page->getLanguageForRendering( $request, $page->getDisplayedFile() );
83 $this->assertEquals( $expected, $result );
86 public static function provideGetLanguageForRendering() {
89 [ 'ru', 'ru', null, 'ru' ],
92 [ null, 'en', null, 'en' ],
93 [ null, 'fr', null, 'fr' ],
94 [ null, 'ru', null, 'en' ],
95 [ 'de', 'ru', null, 'de' ],
96 [ 'gsw', 'als' ], /* als MW lang code (which is not a valid IETF lang code) */
97 [ 'als', 'en', null, 'als' ], /* als IETF lang code */
98 [ 'zh-hans-cn', 'zh', 'zh-cn' ],
99 [ 'zh-hant-tw', 'zh', 'zh-tw' ],
100 [ 'zh-hans-cn', 'zh-Hans-cn' ], /* Should not happen, not a MW lang code */
101 [ 'zh-hans-cn', 'de', null, 'zh-Hans' ],
102 [ null, 'de', null, 'zh-cn' ], /* MW language code via param */
103 [ 'zh-hans-cn', 'zh', 'zh-cn', 'zh' ],
104 [ 'zh-hans-cn', 'zh', 'zh-cn', 'zh-Hans' ],
105 [ 'zh-hans-cn', 'zh', 'zh-cn', 'zh-Hans-CN' ],
106 [ 'zh-hant-tw', 'zh', 'zh-tw', 'zh-Hant-TW' ],
107 [ 'zh-hant-tw', 'zh', 'zh-cn', 'zh-Hant-TW' ],