4 * @group ResourceLoader
5 * @covers ResourceLoaderImage
7 class ResourceLoaderImageTest
extends MediaWikiUnitTestCase
{
11 protected function setUp() {
13 $this->imagesPath
= __DIR__
. '/../../../data/resourceloader';
16 protected function tearDown() {
17 Language
::$dataCache = null;
20 protected function getTestImage( $name ) {
21 $options = ResourceLoaderImageModuleTest
::$commonImageData[$name];
22 $fileDescriptor = is_string( $options ) ?
$options : $options['file'];
23 $allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ?
24 $options['variants'] : [];
25 $variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] );
26 return new ResourceLoaderImageTestable(
35 public static function provideGetPath() {
37 [ 'abc', 'en', 'abc.gif' ],
38 [ 'abc', 'he', 'abc.gif' ],
39 [ 'def', 'en', 'def.svg' ],
40 [ 'def', 'he', 'def.svg' ],
41 [ 'ghi', 'en', 'ghi.svg' ],
42 [ 'ghi', 'he', 'jkl.svg' ],
43 [ 'mno', 'en', 'mno-ltr.svg' ],
44 [ 'mno', 'ar', 'mno-rtl.svg' ],
45 [ 'mno', 'he', 'mno-ltr.svg' ],
46 [ 'pqr', 'en', 'pqr-b.svg' ],
47 [ 'pqr', 'en-gb', 'pqr-b.svg' ],
48 [ 'pqr', 'de', 'pqr-f.svg' ],
49 [ 'pqr', 'de-formal', 'pqr-f.svg' ],
50 [ 'pqr', 'ar', 'pqr-f.svg' ],
51 [ 'pqr', 'fr', 'pqr-a.svg' ],
52 [ 'pqr', 'he', 'pqr-a.svg' ],
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 );
87 public function testGetExtension() {
88 $image = $this->getTestImage( 'def' );
89 $this->assertEquals( $image->getExtension(), 'svg' );
90 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
91 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
92 $image = $this->getTestImage( 'abc' );
93 $this->assertEquals( $image->getExtension(), 'gif' );
94 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
95 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
98 public function testGetImageData() {
99 $context = $this->createMock( ResourceLoaderContext
::class );
101 $image = $this->getTestImage( 'def' );
102 $data = file_get_contents( $this->imagesPath
. '/def.svg' );
103 $dataConstructive = file_get_contents( $this->imagesPath
. '/def_variantize.svg' );
104 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
106 $image->getImageData( $context, 'destructive', 'original' ),
109 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
110 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
112 $image = $this->getTestImage( 'abc' );
113 $data = file_get_contents( $this->imagesPath
. '/abc.gif' );
114 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
115 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
118 public function testMassageSvgPathdata() {
119 $image = $this->getTestImage( 'ghi' );
120 $data = file_get_contents( $this->imagesPath
. '/ghi.svg' );
121 $dataMassaged = file_get_contents( $this->imagesPath
. '/ghi_massage.svg' );
122 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
126 class ResourceLoaderImageTestable
extends ResourceLoaderImage
{
127 // Make some protected methods public
128 public function massageSvgPathdata( $svg ) {
129 return parent
::massageSvgPathdata( $svg );
132 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
133 public function rasterize( $svg ) {
134 return 'RASTERIZESTUB';