Make LocalisationCache a service
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / resourceloader / ResourceLoaderImageTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 * @covers ResourceLoaderImage
6 */
7 class ResourceLoaderImageTest extends MediaWikiUnitTestCase {
8
9 private $imagesPath;
10
11 protected function setUp() {
12 parent::setUp();
13 $this->imagesPath = __DIR__ . '/../../../data/resourceloader';
14 }
15
16 protected function getTestImage( $name ) {
17 $options = ResourceLoaderImageModuleTest::$commonImageData[$name];
18 $fileDescriptor = is_string( $options ) ? $options : $options['file'];
19 $allowedVariants = ( is_array( $options ) && isset( $options['variants'] ) ) ?
20 $options['variants'] : [];
21 $variants = array_fill_keys( $allowedVariants, [ 'color' => 'red' ] );
22 return new ResourceLoaderImageTestable(
23 $name,
24 'test',
25 $fileDescriptor,
26 $this->imagesPath,
27 $variants
28 );
29 }
30
31 public static function provideGetPath() {
32 return [
33 [ 'abc', 'en', 'abc.gif' ],
34 [ 'abc', 'he', 'abc.gif' ],
35 [ 'def', 'en', 'def.svg' ],
36 [ 'def', 'he', 'def.svg' ],
37 [ 'ghi', 'en', 'ghi.svg' ],
38 [ 'ghi', 'he', 'jkl.svg' ],
39 [ 'mno', 'en', 'mno-ltr.svg' ],
40 [ 'mno', 'ar', 'mno-rtl.svg' ],
41 [ 'mno', 'he', 'mno-ltr.svg' ],
42 [ 'pqr', 'en', 'pqr-b.svg' ],
43 [ 'pqr', 'en-gb', 'pqr-b.svg' ],
44 [ 'pqr', 'de', 'pqr-f.svg' ],
45 [ 'pqr', 'de-formal', 'pqr-f.svg' ],
46 [ 'pqr', 'ar', 'pqr-f.svg' ],
47 [ 'pqr', 'fr', 'pqr-a.svg' ],
48 [ 'pqr', 'he', 'pqr-a.svg' ],
49 ];
50 }
51
52 /**
53 * @dataProvider provideGetPath
54 */
55 public function testGetPath( $imageName, $languageCode, $path ) {
56 $this->markTestSkipped( 'Depends on overriding LanguageFallback/LocalisationCache' );
57 static $dirMap = [
58 'en' => 'ltr',
59 'en-gb' => 'ltr',
60 'de' => 'ltr',
61 'de-formal' => 'ltr',
62 'fr' => 'ltr',
63 'he' => 'rtl',
64 'ar' => 'rtl',
65 ];
66
67 $image = $this->getTestImage( $imageName );
68 $context = new DerivativeResourceLoaderContext(
69 $this->createMock( ResourceLoaderContext::class )
70 );
71 $context->setLanguage( $languageCode );
72 $context->setDirection( $dirMap[$languageCode] );
73 Language::$dataCache = $this->createMock( LocalisationCache::class );
74 Language::$dataCache->method( 'getItem' )->will( $this->returnCallback( function ( $code ) {
75 return ( [
76 'en-gb' => [ 'en' ],
77 'de-formal' => [ 'de' ],
78 ] )[ $code ] ?? [];
79 } ) );
80
81 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
82 }
83
84 public function testGetExtension() {
85 $image = $this->getTestImage( 'def' );
86 $this->assertEquals( $image->getExtension(), 'svg' );
87 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
88 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
89 $image = $this->getTestImage( 'abc' );
90 $this->assertEquals( $image->getExtension(), 'gif' );
91 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
92 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
93 }
94
95 public function testGetImageData() {
96 $context = $this->createMock( ResourceLoaderContext::class );
97
98 $image = $this->getTestImage( 'def' );
99 $data = file_get_contents( $this->imagesPath . '/def.svg' );
100 $dataConstructive = file_get_contents( $this->imagesPath . '/def_variantize.svg' );
101 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
102 $this->assertEquals(
103 $image->getImageData( $context, 'destructive', 'original' ),
104 $dataConstructive
105 );
106 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
107 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
108
109 $image = $this->getTestImage( 'abc' );
110 $data = file_get_contents( $this->imagesPath . '/abc.gif' );
111 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
112 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
113 }
114
115 public function testMassageSvgPathdata() {
116 $image = $this->getTestImage( 'ghi' );
117 $data = file_get_contents( $this->imagesPath . '/ghi.svg' );
118 $dataMassaged = file_get_contents( $this->imagesPath . '/ghi_massage.svg' );
119 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
120 }
121 }
122
123 class ResourceLoaderImageTestable extends ResourceLoaderImage {
124 // Make some protected methods public
125 public function massageSvgPathdata( $svg ) {
126 return parent::massageSvgPathdata( $svg );
127 }
128
129 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
130 public function rasterize( $svg ) {
131 return 'RASTERIZESTUB';
132 }
133 }