| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\ResourceLoader\Context; |
| 4 |
use MediaWiki\ResourceLoader\DerivativeContext; |
| 5 |
use MediaWiki\ResourceLoader\Image; |
| 6 |
use MediaWiki\Tests\ResourceLoader\ImageModuleTest; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ImageTest extends MediaWikiUnitTestCase { |
| 13 |
private const IMAGES_PATH = __DIR__ . '/../../../data/resourceloader'; |
| 14 |
|
| 15 |
protected function getTestImage( $name ) { |
| 16 |
$options = ImageModuleTest::COMMON_IMAGE_DATA[$name]; |
| 17 |
$fileDescriptor = is_string( $options ) ? $options : $options['file']; |
| 18 |
$allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ? |
| 19 |
$options['variants'] : []; |
| 20 |
$variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] ); |
| 21 |
return new ResourceLoaderImageTestable( |
| 22 |
$name, |
| 23 |
'test', |
| 24 |
$fileDescriptor, |
| 25 |
self::IMAGES_PATH, |
| 26 |
$variants |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public static function provideGetPath() { |
| 31 |
return [ |
| 32 |
[ 'abc', 'en', 'abc.gif' ], |
| 33 |
[ 'abc', 'he', 'abc.gif' ], |
| 34 |
[ 'def', 'en', 'def.svg' ], |
| 35 |
[ 'def', 'he', 'def.svg' ], |
| 36 |
[ 'ghi', 'en', 'ghi.svg' ], |
| 37 |
[ 'ghi', 'he', 'jkl.svg' ], |
| 38 |
[ 'mno', 'en', 'mno-ltr.svg' ], |
| 39 |
[ 'mno', 'ar', 'mno-rtl.svg' ], |
| 40 |
[ 'mno', 'he', 'mno-ltr.svg' ], |
| 41 |
[ 'pqr', 'en', 'pqr-b.svg' ], |
| 42 |
[ 'pqr', 'en-gb', 'pqr-b.svg' ], |
| 43 |
[ 'pqr', 'de', 'pqr-f.svg' ], |
| 44 |
[ 'pqr', 'de-formal', 'pqr-f.svg' ], |
| 45 |
[ 'pqr', 'ar', 'pqr-f.svg' ], |
| 46 |
[ 'pqr', 'fr', 'pqr-a.svg' ], |
| 47 |
[ 'pqr', 'he', 'pqr-a.svg' ], |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testGetPath( $imageName, $languageCode, $path ) { |
| 55 |
static $dirMap = [ |
| 56 |
'en' => 'ltr', |
| 57 |
'en-gb' => 'ltr', |
| 58 |
'de' => 'ltr', |
| 59 |
'de-formal' => 'ltr', |
| 60 |
'fr' => 'ltr', |
| 61 |
'he' => 'rtl', |
| 62 |
'ar' => 'rtl', |
| 63 |
]; |
| 64 |
|
| 65 |
$image = $this->getTestImage( $imageName ); |
| 66 |
$context = new DerivativeContext( |
| 67 |
$this->createMock( Context::class ) |
| 68 |
); |
| 69 |
$context->setLanguage( $languageCode ); |
| 70 |
$context->setDirection( $dirMap[$languageCode] ); |
| 71 |
|
| 72 |
$this->assertEquals( $image->getPath( $context ), self::IMAGES_PATH . '/' . $path ); |
| 73 |
} |
| 74 |
|
| 75 |
public function testGetExtension() { |
| 76 |
$image = $this->getTestImage( 'def' ); |
| 77 |
$this->assertSame( 'svg', $image->getExtension() ); |
| 78 |
$this->assertSame( 'svg', $image->getExtension( 'original' ) ); |
| 79 |
$this->assertSame( 'png', $image->getExtension( 'rasterized' ) ); |
| 80 |
$image = $this->getTestImage( 'abc' ); |
| 81 |
$this->assertSame( 'gif', $image->getExtension() ); |
| 82 |
$this->assertSame( 'gif', $image->getExtension( 'original' ) ); |
| 83 |
$this->assertSame( 'gif', $image->getExtension( 'rasterized' ) ); |
| 84 |
} |
| 85 |
|
| 86 |
public function testGetImageData() { |
| 87 |
$context = $this->createMock( Context::class ); |
| 88 |
|
| 89 |
$image = $this->getTestImage( 'def' ); |
| 90 |
$data = file_get_contents( self::IMAGES_PATH . '/def.svg' ); |
| 91 |
$dataConstructive = file_get_contents( self::IMAGES_PATH . '/def_variantize.svg' ); |
| 92 |
$this->assertEquals( $image->getImageData( $context, null, 'original' ), $data ); |
| 93 |
$this->assertEquals( |
| 94 |
$image->getImageData( $context, 'destructive', 'original' ), |
| 95 |
$dataConstructive |
| 96 |
); |
| 97 |
|
| 98 |
$this->assertSame( 'RASTERIZESTUB', $image->getImageData( $context, null, 'rasterized' ) ); |
| 99 |
|
| 100 |
$image = $this->getTestImage( 'abc' ); |
| 101 |
$data = file_get_contents( self::IMAGES_PATH . '/abc.gif' ); |
| 102 |
$this->assertEquals( $image->getImageData( $context, null, 'original' ), $data ); |
| 103 |
$this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data ); |
| 104 |
} |
| 105 |
|
| 106 |
public function testMassageSvgPathdata() { |
| 107 |
$image = $this->getTestImage( 'ghi' ); |
| 108 |
$data = file_get_contents( self::IMAGES_PATH . '/ghi.svg' ); |
| 109 |
$dataMassaged = file_get_contents( self::IMAGES_PATH . '/ghi_massage.svg' ); |
| 110 |
$this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
class ResourceLoaderImageTestable extends Image { |
| 115 |
private const MOCK_FALLBACKS = [ |
| 116 |
'en-gb' => [ 'en' ], |
| 117 |
'de-formal' => [ 'de' ], |
| 118 |
]; |
| 119 |
|
| 120 |
|
| 121 |
public function massageSvgPathdata( $svg ) { |
| 122 |
return parent::massageSvgPathdata( $svg ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public function rasterize( $svg ) { |
| 127 |
return 'RASTERIZESTUB'; |
| 128 |
} |
| 129 |
|
| 130 |
protected function getLangFallbacks( string $code ): array { |
| 131 |
return self::MOCK_FALLBACKS[$code] ?? []; |
| 132 |
} |
| 133 |
} |