Merge "Type hint against LinkTarget in WatchedItemStore"
[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', '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' ],
48 ];
49 }
50
51 /**
52 * @covers ResourceLoaderImage::getPath
53 * @dataProvider provideGetPath
54 */
55 public function testGetPath( $imageName, $languageCode, $path ) {
56 static $dirMap = [
57 'en' => 'ltr',
58 'en-gb' => 'ltr',
59 'de' => 'ltr',
60 'de-formal' => 'ltr',
61 'fr' => 'ltr',
62 'he' => 'rtl',
63 'ar' => 'rtl',
64 ];
65
66 $image = $this->getTestImage( $imageName );
67 $context = $this->getResourceLoaderContext( [
68 'lang' => $languageCode,
69 'dir' => $dirMap[$languageCode],
70 ] );
71
72 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
73 }
74
75 /**
76 * @covers ResourceLoaderImage::getExtension
77 * @covers ResourceLoaderImage::getMimeType
78 */
79 public function testGetExtension() {
80 $image = $this->getTestImage( 'def' );
81 $this->assertEquals( $image->getExtension(), 'svg' );
82 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
83 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
84 $image = $this->getTestImage( 'abc' );
85 $this->assertEquals( $image->getExtension(), 'gif' );
86 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
87 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
88 }
89
90 /**
91 * @covers ResourceLoaderImage::getImageData
92 * @covers ResourceLoaderImage::variantize
93 * @covers ResourceLoaderImage::massageSvgPathdata
94 */
95 public function testGetImageData() {
96 $context = $this->getResourceLoaderContext();
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 /**
116 * @covers ResourceLoaderImage::massageSvgPathdata
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 }