4 * @group ResourceLoader
6 class ResourceLoaderImageTest
extends ResourceLoaderTestCase
{
10 protected function setUp() {
12 $this->imagesPath
= __DIR__
. '/../../data/resourceloader';
15 protected function getTestImage( $name ) {
16 $options = ResourceLoaderImageModuleTest
::$commonImageData[$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(
30 public static function provideGetPath() {
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' ],
52 * @covers ResourceLoaderImage::getPath
53 * @dataProvider provideGetPath
55 public function testGetPath( $imageName, $languageCode, $path ) {
65 static $contexts = [];
67 $image = $this->getTestImage( $imageName );
68 $context = $this->getResourceLoaderContext( [
69 'lang' => $languageCode,
70 'dir' => $dirMap[$languageCode],
73 $this->assertEquals( $image->getPath( $context ), $this->imagesPath
. '/' . $path );
77 * @covers ResourceLoaderImage::getExtension
78 * @covers ResourceLoaderImage::getMimeType
80 public function testGetExtension() {
81 $image = $this->getTestImage( 'def' );
82 $this->assertEquals( $image->getExtension(), 'svg' );
83 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
84 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
85 $image = $this->getTestImage( 'abc' );
86 $this->assertEquals( $image->getExtension(), 'gif' );
87 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
88 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
92 * @covers ResourceLoaderImage::getImageData
93 * @covers ResourceLoaderImage::variantize
94 * @covers ResourceLoaderImage::massageSvgPathdata
96 public function testGetImageData() {
97 $context = $this->getResourceLoaderContext();
99 $image = $this->getTestImage( 'def' );
100 $data = file_get_contents( $this->imagesPath
. '/def.svg' );
101 $dataConstructive = file_get_contents( $this->imagesPath
. '/def_variantize.svg' );
102 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
104 $image->getImageData( $context, 'destructive', 'original' ),
107 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
108 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
110 $image = $this->getTestImage( 'abc' );
111 $data = file_get_contents( $this->imagesPath
. '/abc.gif' );
112 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
113 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
117 * @covers ResourceLoaderImage::massageSvgPathdata
119 public function testMassageSvgPathdata() {
120 $image = $this->getTestImage( 'ghi' );
121 $data = file_get_contents( $this->imagesPath
. '/ghi.svg' );
122 $dataMassaged = file_get_contents( $this->imagesPath
. '/ghi_massage.svg' );
123 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
127 class ResourceLoaderImageTestable
extends ResourceLoaderImage
{
128 // Make some protected methods public
129 public function massageSvgPathdata( $svg ) {
130 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';