Merge "Use structured logging/MWLoggerFactory for TransactionProfiler"
[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'] ) ? $options['variants'] : array();
19 $variants = array_fill_keys( $allowedVariants, array( 'color' => 'red' ) );
20 return new ResourceLoaderImageTestable( $name, 'test', $fileDescriptor, $this->imagesPath, $variants );
21 }
22
23 public static function provideGetPath() {
24 return array(
25 array( 'add', 'en', 'add.gif' ),
26 array( 'add', 'he', 'add.gif' ),
27 array( 'remove', 'en', 'remove.svg' ),
28 array( 'remove', 'he', 'remove.svg' ),
29 array( 'next', 'en', 'next.svg' ),
30 array( 'next', 'he', 'prev.svg' ),
31 array( 'help', 'en', 'help-ltr.svg' ),
32 array( 'help', 'ar', 'help-rtl.svg' ),
33 array( 'help', 'he', 'help-ltr.svg' ),
34 array( 'bold', 'en', 'bold-b.svg' ),
35 array( 'bold', 'de', 'bold-f.svg' ),
36 array( 'bold', 'fr', 'bold-a.svg' ),
37 array( 'bold', 'he', 'bold-a.svg' ),
38 );
39 }
40
41 /**
42 * @covers ResourceLoaderImage::getPath
43 * @dataProvider provideGetPath
44 */
45 public function testGetPath( $imageName, $languageCode, $path ) {
46 static $dirMap = array(
47 'en' => 'ltr',
48 'de' => 'ltr',
49 'fr' => 'ltr',
50 'he' => 'rtl',
51 'ar' => 'rtl',
52 );
53 static $contexts = array();
54
55 $image = $this->getTestImage( $imageName );
56 $context = $this->getResourceLoaderContext( $languageCode, $dirMap[$languageCode] );
57
58 $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path );
59 }
60
61 /**
62 * @covers ResourceLoaderImage::getExtension
63 * @covers ResourceLoaderImage::getMimeType
64 */
65 public function testGetExtension() {
66 $image = $this->getTestImage( 'remove' );
67 $this->assertEquals( $image->getExtension(), 'svg' );
68 $this->assertEquals( $image->getExtension( 'original' ), 'svg' );
69 $this->assertEquals( $image->getExtension( 'rasterized' ), 'png' );
70 $image = $this->getTestImage( 'add' );
71 $this->assertEquals( $image->getExtension(), 'gif' );
72 $this->assertEquals( $image->getExtension( 'original' ), 'gif' );
73 $this->assertEquals( $image->getExtension( 'rasterized' ), 'gif' );
74 }
75
76 /**
77 * @covers ResourceLoaderImage::getImageData
78 * @covers ResourceLoaderImage::variantize
79 * @covers ResourceLoaderImage::massageSvgPathdata
80 */
81 public function testGetImageData() {
82 $context = $this->getResourceLoaderContext( 'en', 'ltr' );
83
84 $image = $this->getTestImage( 'remove' );
85 $data = file_get_contents( $this->imagesPath . '/remove.svg' );
86 $dataConstructive = file_get_contents( $this->imagesPath . '/remove_variantize.svg' );
87 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
88 $this->assertEquals( $image->getImageData( $context, 'destructive', 'original' ), $dataConstructive );
89 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
90 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), 'RASTERIZESTUB' );
91
92 $image = $this->getTestImage( 'add' );
93 $data = file_get_contents( $this->imagesPath . '/add.gif' );
94 $this->assertEquals( $image->getImageData( $context, null, 'original' ), $data );
95 $this->assertEquals( $image->getImageData( $context, null, 'rasterized' ), $data );
96 }
97
98 /**
99 * @covers ResourceLoaderImage::massageSvgPathdata
100 */
101 public function testMassageSvgPathdata() {
102 $image = $this->getTestImage( 'next' );
103 $data = file_get_contents( $this->imagesPath . '/next.svg' );
104 $dataMassaged = file_get_contents( $this->imagesPath . '/next_massage.svg' );
105 $this->assertEquals( $image->massageSvgPathdata( $data ), $dataMassaged );
106 }
107 }
108
109 class ResourceLoaderImageTestable extends ResourceLoaderImage {
110 // Make some protected methods public
111 public function getPath( ResourceLoaderContext $context ) {
112 return parent::getPath( $context );
113 }
114 public function massageSvgPathdata( $svg ) {
115 return parent::massageSvgPathdata( $svg );
116 }
117 // Stub, since we don't know if we even have a SVG handler, much less what exactly it'll output
118 public function rasterize( $svg ) {
119 return 'RASTERIZESTUB';
120 }
121 }