Merge "filebackend: avoid use of wfWikiId() in FileBackendGroup"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / resourceloader / ResourceLoaderImageTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderImageTest extends MediaWikiUnitTestCase {
7
8 private $imagesPath;
9
10 protected function setUp() {
11 parent::setUp();
12 $this->imagesPath = __DIR__ . '/../../../data/resourceloader';
13 }
14
15 protected function tearDown() {
16 Language::$dataCache = null;
17 }
18
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(
26 $name,
27 'test',
28 $fileDescriptor,
29 $this->imagesPath,
30 $variants
31 );
32 }
33
34 public static function provideGetPath() {
35 return [
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' ],
52 ];
53 }
54
55 /**
56 * @covers ResourceLoaderImage::getPath
57 * @dataProvider provideGetPath
58 */
59 public function testGetPath( $imageName, $languageCode, $path ) {
60 static $dirMap = [
61 'en' => 'ltr',
62 'en-gb' => 'ltr',
63 'de' => 'ltr',
64 'de-formal' => 'ltr',
65 'fr' => 'ltr',
66 'he' => 'rtl',
67 'ar' => 'rtl',
68 ];
69
70 $image = $this->getTestImage( $imageName );
71 $context = new DerivativeResourceLoaderContext(
72 $this->createMock( ResourceLoaderContext::class )
73 );
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 ) {
78 return ( [
79 'en-gb' => [ 'en' ],
80 'de-formal' => [ 'de' ],
81 ] )[ $code ] ?? [];
82 } ) );
83
84 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
85 }
86
87 /**
88 * @covers ResourceLoaderImage::getExtension
89 * @covers ResourceLoaderImage::getMimeType
90 */
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' );
100 }
101
102 /**
103 * @covers ResourceLoaderImage::getImageData
104 * @covers ResourceLoaderImage::variantize
105 * @covers ResourceLoaderImage::massageSvgPathdata
106 */
107 public function testGetImageData() {
108 $context = $this->createMock( ResourceLoaderContext::class );
109
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 );
114 $this->assertEquals(
115 $image->getImageData( $context, 'destructive', 'original' ),
116 $dataConstructive
117 );
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' );
120
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 );
125 }
126
127 /**
128 * @covers ResourceLoaderImage::massageSvgPathdata
129 */
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 );
135 }
136 }
137
138 class ResourceLoaderImageTestable extends ResourceLoaderImage {
139 // Make some protected methods public
140 public function massageSvgPathdata( $svg ) {
141 return parent::massageSvgPathdata( $svg );
142 }
143
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';
147 }
148 }