4 * @group ResourceLoader
5 * @covers ResourceLoaderImage
7 class ResourceLoaderImageTest
extends MediaWikiUnitTestCase
{
11 protected function setUp() {
13 $this->imagesPath
= __DIR__
. '/../../../data/resourceloader';
16 protected function getTestImage( $name ) {
17 $options = ResourceLoaderImageModuleTest
::$commonImageData[$name];
18 $fileDescriptor = is_string( $options ) ?
$options : $options['file'];
19 $allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ?
20 $options['variants'] : [];
21 $variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] );
22 return new ResourceLoaderImageTestable(
31 public static function provideGetPath() {
33 [ 'abc', 'en', 'abc.gif' ],
34 [ 'abc', 'he', 'abc.gif' ],
35 [ 'def', 'en', 'def.svg' ],
36 [ 'def', 'he', 'def.svg' ],
37 [ 'ghi', 'en', 'ghi.svg' ],
38 [ 'ghi', 'he', 'jkl.svg' ],
39 [ 'mno', 'en', 'mno-ltr.svg' ],
40 [ 'mno', 'ar', 'mno-rtl.svg' ],
41 [ 'mno', 'he', 'mno-ltr.svg' ],
42 [ 'pqr', 'en', 'pqr-b.svg' ],
43 [ 'pqr', 'en-gb', 'pqr-b.svg' ],
44 [ 'pqr', 'de', 'pqr-f.svg' ],
45 [ 'pqr', 'de-formal', 'pqr-f.svg' ],
46 [ 'pqr', 'ar', 'pqr-f.svg' ],
47 [ 'pqr', 'fr', 'pqr-a.svg' ],
48 [ 'pqr', 'he', 'pqr-a.svg' ],
53 * @dataProvider provideGetPath
55 public function testGetPath( $imageName, $languageCode, $path ) {
56 $this->markTestSkipped( 'Depends on overriding LanguageFallback/LocalisationCache' );
67 $image = $this->getTestImage( $imageName );
68 $context = new DerivativeResourceLoaderContext(
69 $this->createMock( ResourceLoaderContext
::class )
71 $context->setLanguage( $languageCode );
72 $context->setDirection( $dirMap[$languageCode] );
73 Language
::$dataCache = $this->createMock( LocalisationCache
::class );
74 Language
::$dataCache->method( 'getItem' )->will( $this->returnCallback( function ( $code ) {
77 'de-formal' => [ 'de' ],
81 $this->assertEquals( $image->getPath( $context ), $this->imagesPath
. '/' . $path );
84 public function testGetExtension() {
85 $image = $this->getTestImage( 'def' );
86 $this->assertEquals( $image->getExtension(), 'svg' );
87 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
88 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
89 $image = $this->getTestImage( 'abc' );
90 $this->assertEquals( $image->getExtension(), 'gif' );
91 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
92 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
95 public function testGetImageData() {
96 $context = $this->createMock( ResourceLoaderContext
::class );
98 $image = $this->getTestImage( 'def' );
99 $data = file_get_contents( $this->imagesPath
. '/def.svg' );
100 $dataConstructive = file_get_contents( $this->imagesPath
. '/def_variantize.svg' );
101 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
103 $image->getImageData( $context, 'destructive', 'original' ),
106 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
107 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
109 $image = $this->getTestImage( 'abc' );
110 $data = file_get_contents( $this->imagesPath
. '/abc.gif' );
111 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
112 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
115 public function testMassageSvgPathdata() {
116 $image = $this->getTestImage( 'ghi' );
117 $data = file_get_contents( $this->imagesPath
. '/ghi.svg' );
118 $dataMassaged = file_get_contents( $this->imagesPath
. '/ghi_massage.svg' );
119 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
123 class ResourceLoaderImageTestable
extends ResourceLoaderImage
{
124 // Make some protected methods public
125 public function massageSvgPathdata( $svg ) {
126 return parent
::massageSvgPathdata( $svg );
129 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
130 public function rasterize( $svg ) {
131 return 'RASTERIZESTUB';