ResourceLoaderImageModule: Add basic tests
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderImageModuleTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase {
7
8 public static function providerGetModules() {
9 $commonVariants = array(
10 'invert' => array(
11 'color' => '#FFFFFF',
12 'global' => true,
13 ),
14 'primary' => array(
15 'color' => '#598AD1',
16 ),
17 'constructive' => array(
18 'color' => '#00C697',
19 ),
20 'destructive' => array(
21 'color' => '#E81915',
22 ),
23 );
24
25 $commonImageData = array(
26 'advanced' => 'advanced.svg',
27 'remove' => array(
28 'image' => 'remove.svg',
29 'variants' => array( 'destructive' ),
30 ),
31 'next' => array(
32 'image' => array(
33 'ltr' => 'next.svg',
34 'rtl' => 'prev.svg'
35 ),
36 ),
37 'help' => array(
38 'image' => array(
39 'ltr' => 'help-ltr.svg',
40 'rtl' => 'help-rtl.svg',
41 'lang' => array(
42 'he' => 'help-ltr.svg',
43 )
44 ),
45 ),
46 'bold' => array(
47 'image' => array(
48 'default' => 'bold-a.svg',
49 'lang' => array(
50 'en' => 'bold-b.svg',
51 'de' => 'bold-f.svg',
52 )
53 ),
54 )
55 );
56
57 return array(
58 array(
59 array(
60 'class' => 'ResourceLoaderImageModule',
61 'prefix' => 'oo-ui',
62 'variants' => array(
63 'icon' => $commonVariants,
64 ),
65 'images' => array(
66 'icon' => $commonImageData,
67 ),
68 ),
69 '.oo-ui-icon-advanced {
70 ...
71 }
72 .oo-ui-icon-advanced-invert {
73 ...
74 }
75 .oo-ui-icon-remove {
76 ...
77 }
78 .oo-ui-icon-remove-invert {
79 ...
80 }
81 .oo-ui-icon-remove-destructive {
82 ...
83 }
84 .oo-ui-icon-next {
85 ...
86 }
87 .oo-ui-icon-next-invert {
88 ...
89 }
90 .oo-ui-icon-help {
91 ...
92 }
93 .oo-ui-icon-help-invert {
94 ...
95 }
96 .oo-ui-icon-bold {
97 ...
98 }
99 .oo-ui-icon-bold-invert {
100 ...
101 }',
102 ),
103 );
104 }
105
106 /**
107 * @dataProvider providerGetModules
108 * @covers ResourceLoaderImageModule::getStyles
109 */
110 public function testGetStyles( $module, $expected ) {
111 $module = new ResourceLoaderImageModuleTestable( $module );
112 $styles = $module->getStyles( $this->getResourceLoaderContext() );
113 $this->assertEquals( $expected, $styles['all'] );
114 }
115 }
116
117 class ResourceLoaderImageModuleTestable extends ResourceLoaderImageModule {
118 /**
119 * Replace with a stub to make test cases easier to write.
120 */
121 protected function getCssDeclarations( $primary, $fallback ) {
122 return array( '...' );
123 }
124
125 /**
126 * Return mock ResourceLoaderImages that don't call file_get_contents and such.
127 */
128 public function getImages() {
129 $images = parent::getImages();
130 foreach ( $images as $name => &$image ) {
131 $image = new ResourceLoaderImageWrapper( $image );
132 }
133 return $images;
134 }
135 }
136
137 /**
138 * Wraps a ResourceLoaderImage not to call file_get_contents and such.
139 */
140 class ResourceLoaderImageWrapper extends ResourceLoaderImage {
141 public function __construct( ResourceLoaderImage $image ) {
142 $this->image = $image;
143 }
144
145 public function getUrl( ResourceLoaderContext $context, $script, $variant, $format ) {
146 return null;
147 }
148
149 public function getDataUri( ResourceLoaderContext $context, $variant, $format ) {
150 return null;
151 }
152
153 public function __call( $method, $arguments ) {
154 return call_user_func_array( array( $this->image, $method ), $arguments );
155 }
156
157 public function __get( $name ) {
158 return $this->image->$name;
159 }
160 }