Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderImageTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderImageTest extends ResourceLoaderTestCase {
7
8 protected $imagesPath;
9
10 protected function setUp() {
11 parent::setUp();
12 $this->imagesPath = __DIR__ . '/../../data/resourceloader';
13 }
14
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(
22 $name,
23 'test',
24 $fileDescriptor,
25 $this->imagesPath,
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', 'de', 'pqr-f.svg' ],
43 [ 'pqr', 'ar', 'pqr-f.svg' ],
44 [ 'pqr', 'fr', 'pqr-a.svg' ],
45 [ 'pqr', 'he', 'pqr-a.svg' ],
46 ];
47 }
48
49 /**
50 * @covers ResourceLoaderImage::getPath
51 * @dataProvider provideGetPath
52 */
53 public function testGetPath( $imageName, $languageCode, $path ) {
54 static $dirMap = [
55 'en' => 'ltr',
56 'de' => 'ltr',
57 'fr' => 'ltr',
58 'he' => 'rtl',
59 'ar' => 'rtl',
60 ];
61 static $contexts = [];
62
63 $image = $this->getTestImage( $imageName );
64 $context = $this->getResourceLoaderContext( [
65 'lang' => $languageCode,
66 'dir' => $dirMap[$languageCode],
67 ] );
68
69 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
70 }
71
72 /**
73 * @covers ResourceLoaderImage::getExtension
74 * @covers ResourceLoaderImage::getMimeType
75 */
76 public function testGetExtension() {
77 $image = $this->getTestImage( 'def' );
78 $this->assertEquals( $image->getExtension(), 'svg' );
79 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
80 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
81 $image = $this->getTestImage( 'abc' );
82 $this->assertEquals( $image->getExtension(), 'gif' );
83 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
84 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
85 }
86
87 /**
88 * @covers ResourceLoaderImage::getImageData
89 * @covers ResourceLoaderImage::variantize
90 * @covers ResourceLoaderImage::massageSvgPathdata
91 */
92 public function testGetImageData() {
93 $context = $this->getResourceLoaderContext();
94
95 $image = $this->getTestImage( 'def' );
96 $data = file_get_contents( $this->imagesPath . '/def.svg' );
97 $dataConstructive = file_get_contents( $this->imagesPath . '/def_variantize.svg' );
98 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
99 $this->assertEquals(
100 $image->getImageData( $context, 'destructive', 'original' ),
101 $dataConstructive
102 );
103 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
104 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
105
106 $image = $this->getTestImage( 'abc' );
107 $data = file_get_contents( $this->imagesPath . '/abc.gif' );
108 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
109 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
110 }
111
112 /**
113 * @covers ResourceLoaderImage::massageSvgPathdata
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 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
129 public function rasterize( $svg ) {
130 return 'RASTERIZESTUB';
131 }
132 }