02042b8667f6d9fed0213a0c2d272efb1cae5a48
[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 tearDown() {
17 Language::$dataCache = null;
18 }
19
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(
27 $name,
28 'test',
29 $fileDescriptor,
30 $this->imagesPath,
31 $variants
32 );
33 }
34
35 public static function provideGetPath() {
36 return [
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' ],
53 ];
54 }
55
56 /**
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 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' );
96 }
97
98 public function testGetImageData() {
99 $context = $this->createMock( ResourceLoaderContext::class );
100
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 );
105 $this->assertEquals(
106 $image->getImageData( $context, 'destructive', 'original' ),
107 $dataConstructive
108 );
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' );
111
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 );
116 }
117
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 );
123 }
124 }
125
126 class ResourceLoaderImageTestable extends ResourceLoaderImage {
127 // Make some protected methods public
128 public function massageSvgPathdata( $svg ) {
129 return parent::massageSvgPathdata( $svg );
130 }
131
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';
135 }
136 }