4 * @group ResourceLoader
6 class ResourceLoaderImageTest
extends MediaWikiUnitTestCase
{
10 protected function setUp() {
12 $this->imagesPath
= __DIR__
. '/../../../data/resourceloader';
15 protected function tearDown() {
16 Language
::$dataCache = null;
19 protected function getTestImage( $name ) {
20 $options = ResourceLoaderImageModuleTest
::$commonImageData[$name];
21 $fileDescriptor = is_string( $options ) ?
$options : $options['file'];
22 $allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ?
23 $options['variants'] : [];
24 $variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] );
25 return new ResourceLoaderImageTestable(
34 public static function provideGetPath() {
36 [ 'abc', 'en', 'abc.gif' ],
37 [ 'abc', 'he', 'abc.gif' ],
38 [ 'def', 'en', 'def.svg' ],
39 [ 'def', 'he', 'def.svg' ],
40 [ 'ghi', 'en', 'ghi.svg' ],
41 [ 'ghi', 'he', 'jkl.svg' ],
42 [ 'mno', 'en', 'mno-ltr.svg' ],
43 [ 'mno', 'ar', 'mno-rtl.svg' ],
44 [ 'mno', 'he', 'mno-ltr.svg' ],
45 [ 'pqr', 'en', 'pqr-b.svg' ],
46 [ 'pqr', 'en-gb', 'pqr-b.svg' ],
47 [ 'pqr', 'de', 'pqr-f.svg' ],
48 [ 'pqr', 'de-formal', 'pqr-f.svg' ],
49 [ 'pqr', 'ar', 'pqr-f.svg' ],
50 [ 'pqr', 'fr', 'pqr-a.svg' ],
51 [ 'pqr', 'he', 'pqr-a.svg' ],
56 * @covers ResourceLoaderImage::getPath
57 * @dataProvider provideGetPath
59 public function testGetPath( $imageName, $languageCode, $path ) {
70 $image = $this->getTestImage( $imageName );
71 $context = new DerivativeResourceLoaderContext(
72 $this->createMock( ResourceLoaderContext
::class )
74 $context->setLanguage( $languageCode );
75 $context->setDirection( $dirMap[$languageCode] );
76 Language
::$dataCache = $this->createMock( LocalisationCache
::class );
77 Language
::$dataCache->method( 'getItem' )->will( $this->returnCallback( function ( $code ) {
80 'de-formal' => [ 'de' ],
84 $this->assertEquals( $image->getPath( $context ), $this->imagesPath
. '/' . $path );
88 * @covers ResourceLoaderImage::getExtension
89 * @covers ResourceLoaderImage::getMimeType
91 public function testGetExtension() {
92 $image = $this->getTestImage( 'def' );
93 $this->assertEquals( $image->getExtension(), 'svg' );
94 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
95 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
96 $image = $this->getTestImage( 'abc' );
97 $this->assertEquals( $image->getExtension(), 'gif' );
98 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
99 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
103 * @covers ResourceLoaderImage::getImageData
104 * @covers ResourceLoaderImage::variantize
105 * @covers ResourceLoaderImage::massageSvgPathdata
107 public function testGetImageData() {
108 $context = $this->createMock( ResourceLoaderContext
::class );
110 $image = $this->getTestImage( 'def' );
111 $data = file_get_contents( $this->imagesPath
. '/def.svg' );
112 $dataConstructive = file_get_contents( $this->imagesPath
. '/def_variantize.svg' );
113 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
115 $image->getImageData( $context, 'destructive', 'original' ),
118 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
119 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
121 $image = $this->getTestImage( 'abc' );
122 $data = file_get_contents( $this->imagesPath
. '/abc.gif' );
123 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
124 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
128 * @covers ResourceLoaderImage::massageSvgPathdata
130 public function testMassageSvgPathdata() {
131 $image = $this->getTestImage( 'ghi' );
132 $data = file_get_contents( $this->imagesPath
. '/ghi.svg' );
133 $dataMassaged = file_get_contents( $this->imagesPath
. '/ghi_massage.svg' );
134 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
138 class ResourceLoaderImageTestable
extends ResourceLoaderImage
{
139 // Make some protected methods public
140 public function massageSvgPathdata( $svg ) {
141 return parent
::massageSvgPathdata( $svg );
144 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
145 public function rasterize( $svg ) {
146 return 'RASTERIZESTUB';