MediaWiki REL1_31
ResourceLoaderImageModuleTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
9
10 public static $commonImageData = [
11 'abc' => 'abc.gif',
12 'def' => [
13 'file' => 'def.svg',
14 'variants' => [ 'destructive' ],
15 ],
16 'ghi' => [
17 'file' => [
18 'ltr' => 'ghi.svg',
19 'rtl' => 'jkl.svg'
20 ],
21 ],
22 'mno' => [
23 'file' => [
24 'ltr' => 'mno-ltr.svg',
25 'rtl' => 'mno-rtl.svg',
26 'lang' => [
27 'he' => 'mno-ltr.svg',
28 ]
29 ],
30 ],
31 'pqr' => [
32 'file' => [
33 'default' => 'pqr-a.svg',
34 'lang' => [
35 'en' => 'pqr-b.svg',
36 'ar,de' => 'pqr-f.svg',
37 ]
38 ],
39 ]
40 ];
41
42 public static $commonImageVariants = [
43 'invert' => [
44 'color' => '#FFFFFF',
45 'global' => true,
46 ],
47 'primary' => [
48 'color' => '#598AD1',
49 ],
50 'constructive' => [
51 'color' => '#00C697',
52 ],
53 'destructive' => [
54 'color' => '#E81915',
55 ],
56 ];
57
58 public static function providerGetModules() {
59 return [
60 [
61 [
62 'class' => ResourceLoaderImageModule::class,
63 'prefix' => 'oo-ui-icon',
64 'variants' => self::$commonImageVariants,
65 'images' => self::$commonImageData,
66 ],
67 '.oo-ui-icon-abc {
68 ...
69}
70.oo-ui-icon-abc-invert {
71 ...
72}
73.oo-ui-icon-def {
74 ...
75}
76.oo-ui-icon-def-invert {
77 ...
78}
79.oo-ui-icon-def-destructive {
80 ...
81}
82.oo-ui-icon-ghi {
83 ...
84}
85.oo-ui-icon-ghi-invert {
86 ...
87}
88.oo-ui-icon-mno {
89 ...
90}
91.oo-ui-icon-mno-invert {
92 ...
93}
94.oo-ui-icon-pqr {
95 ...
96}
97.oo-ui-icon-pqr-invert {
98 ...
99}',
100 ],
101 [
102 [
103 'class' => ResourceLoaderImageModule::class,
104 'selectorWithoutVariant' => '.mw-ui-icon-{name}:after, .mw-ui-icon-{name}:before',
105 'selectorWithVariant' =>
106 '.mw-ui-icon-{name}-{variant}:after, .mw-ui-icon-{name}-{variant}:before',
107 'variants' => self::$commonImageVariants,
108 'images' => self::$commonImageData,
109 ],
110 '.mw-ui-icon-abc:after, .mw-ui-icon-abc:before {
111 ...
112}
113.mw-ui-icon-abc-invert:after, .mw-ui-icon-abc-invert:before {
114 ...
115}
116.mw-ui-icon-def:after, .mw-ui-icon-def:before {
117 ...
118}
119.mw-ui-icon-def-invert:after, .mw-ui-icon-def-invert:before {
120 ...
121}
122.mw-ui-icon-def-destructive:after, .mw-ui-icon-def-destructive:before {
123 ...
124}
125.mw-ui-icon-ghi:after, .mw-ui-icon-ghi:before {
126 ...
127}
128.mw-ui-icon-ghi-invert:after, .mw-ui-icon-ghi-invert:before {
129 ...
130}
131.mw-ui-icon-mno:after, .mw-ui-icon-mno:before {
132 ...
133}
134.mw-ui-icon-mno-invert:after, .mw-ui-icon-mno-invert:before {
135 ...
136}
137.mw-ui-icon-pqr:after, .mw-ui-icon-pqr:before {
138 ...
139}
140.mw-ui-icon-pqr-invert:after, .mw-ui-icon-pqr-invert:before {
141 ...
142}',
143 ],
144 ];
145 }
146
151 public function testGetStyles( $module, $expected ) {
153 $module,
154 __DIR__ . '/../../data/resourceloader'
155 );
156 $styles = $module->getStyles( $this->getResourceLoaderContext() );
157 $this->assertEquals( $expected, $styles['all'] );
158 }
159
163 public function testContext() {
165 $this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
166
168 'image' => 'example',
169 ] ) );
170 $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
171
173 'modules' => 'unknown',
174 'image' => 'example',
175 ] ) );
176 $this->assertFalse( $context->getImageObj(), 'Not an image module' );
177
178 $rl = new EmptyResourceLoader();
179 $rl->register( 'test', [
180 'class' => ResourceLoaderImageModule::class,
181 'prefix' => 'test',
182 'images' => [ 'example' => 'example.png' ],
183 ] );
185 'modules' => 'test',
186 'image' => 'unknown',
187 ] ) );
188 $this->assertFalse( $context->getImageObj(), 'Unknown image' );
189
190 $rl = new EmptyResourceLoader();
191 $rl->register( 'test', [
192 'class' => ResourceLoaderImageModule::class,
193 'prefix' => 'test',
194 'images' => [ 'example' => 'example.png' ],
195 ] );
197 'modules' => 'test',
198 'image' => 'example',
199 ] ) );
200 $this->assertInstanceOf( ResourceLoaderImage::class, $context->getImageObj() );
201 }
202
203 public static function providerGetStyleDeclarations() {
204 return [
205 [
206 false,
207<<<TEXT
208background-image: url(rasterized.png);
209 background-image: linear-gradient(transparent, transparent), url(original.svg);
210TEXT
211 ],
212 [
213 'data:image/svg+xml',
214<<<TEXT
215background-image: url(rasterized.png);
216 background-image: linear-gradient(transparent, transparent), url(data:image/svg+xml);
217TEXT
218 ],
219
220 ];
221 }
222
227 public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
228 $module = TestingAccessWrapper::newFromObject( new ResourceLoaderImageModule() );
230 $image = $this->getImageMock( $context, $dataUriReturnValue );
231
232 $styles = $module->getStyleDeclarations(
233 $context,
234 $image,
235 'load.php'
236 );
237
238 $this->assertEquals( $expected, $styles );
239 }
240
241 private function getImageMock( ResourceLoaderContext $context, $dataUriReturnValue ) {
242 $image = $this->getMockBuilder( ResourceLoaderImage::class )
243 ->disableOriginalConstructor()
244 ->getMock();
245 $image->method( 'getDataUri' )
246 ->will( $this->returnValue( $dataUriReturnValue ) );
247 $image->expects( $this->any() )
248 ->method( 'getUrl' )
249 ->will( $this->returnValueMap( [
250 [ $context, 'load.php', null, 'original', 'original.svg' ],
251 [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
252 ] ) );
253
254 return $image;
255 }
256}
257
262 protected function getCssDeclarations( $primary, $fallback ) {
263 return [ '...' ];
264 }
265}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
$fallback
WebRequest clone which takes values from a provided array.
Object passed around to modules which contains information about the state of a specific loader reque...
testGetStyleDeclarations( $dataUriReturnValue, $expected)
providerGetStyleDeclarations ResourceLoaderImageModule::getStyleDeclarations
testGetStyles( $module, $expected)
providerGetModules ResourceLoaderImageModule::getStyles
testContext()
ResourceLoaderContext::getImageObj.
getImageMock(ResourceLoaderContext $context, $dataUriReturnValue)
getCssDeclarations( $primary, $fallback)
Replace with a stub to make test cases easier to write.
ResourceLoader module for generated and embedded images.
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition hooks.txt:895
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811