MediaWiki  master
ResourceLoaderImageModuleTest.php
Go to the documentation of this file.
1 <?php
2 
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  [
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  [
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 
156  public function testResourceLoaderFilePath() {
157  $basePath = __DIR__ . '/../../data/blahblah';
158  $filePath = __DIR__ . '/../../data/rlfilepath';
159  $testModule = new ResourceLoaderImageModule( [
160  'localBasePath' => $basePath,
161  'remoteBasePath' => 'blahblah',
162  'prefix' => 'foo',
163  'images' => [
164  'eye' => new ResourceLoaderFilePath( 'eye.svg', $filePath, 'rlfilepath' ),
165  'flag' => [
166  'file' => [
167  'ltr' => new ResourceLoaderFilePath( 'flag-ltr.svg', $filePath, 'rlfilepath' ),
168  'rtl' => new ResourceLoaderFilePath( 'flag-rtl.svg', $filePath, 'rlfilepath' ),
169  ],
170  ],
171  ],
172  ] );
173  $expectedModule = new ResourceLoaderImageModule( [
174  'localBasePath' => $filePath,
175  'remoteBasePath' => 'rlfilepath',
176  'prefix' => 'foo',
177  'images' => [
178  'eye' => 'eye.svg',
179  'flag' => [
180  'file' => [
181  'ltr' => 'flag-ltr.svg',
182  'rtl' => 'flag-rtl.svg',
183  ],
184  ],
185  ],
186  ] );
187 
189  $this->assertEquals(
190  $expectedModule->getModuleContent( $context ),
191  $testModule->getModuleContent( $context ),
192  "Using ResourceLoaderFilePath works correctly"
193  );
194  }
195 
200  public function testGetStyles( $module, $expected ) {
201  $module = new ResourceLoaderImageModuleTestable(
202  $module,
203  __DIR__ . '/../../data/resourceloader'
204  );
205  $styles = $module->getStyles( $this->getResourceLoaderContext() );
206  $this->assertEquals( $expected, $styles['all'] );
207  }
208 
212  public function testContext() {
214  $this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
215 
217  'image' => 'example',
218  ] ) );
219  $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
220 
221  $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
222  'modules' => 'unknown',
223  'image' => 'example',
224  ] ) );
225  $this->assertFalse( $context->getImageObj(), 'Not an image module' );
226 
227  $rl = new EmptyResourceLoader();
228  $rl->register( 'test', [
230  'prefix' => 'test',
231  'images' => [ 'example' => 'example.png' ],
232  ] );
233  $context = new ResourceLoaderContext( $rl, new FauxRequest( [
234  'modules' => 'test',
235  'image' => 'unknown',
236  ] ) );
237  $this->assertFalse( $context->getImageObj(), 'Unknown image' );
238 
239  $rl = new EmptyResourceLoader();
240  $rl->register( 'test', [
242  'prefix' => 'test',
243  'images' => [ 'example' => 'example.png' ],
244  ] );
245  $context = new ResourceLoaderContext( $rl, new FauxRequest( [
246  'modules' => 'test',
247  'image' => 'example',
248  ] ) );
249  $this->assertInstanceOf( ResourceLoaderImage::class, $context->getImageObj() );
250  }
251 
252  public static function providerGetStyleDeclarations() {
253  return [
254  [
255  false,
256 <<<TEXT
257 background-image: url(rasterized.png);
258  background-image: linear-gradient(transparent, transparent), url(original.svg);
259 TEXT
260  ],
261  [
262  'data:image/svg+xml',
263 <<<TEXT
264 background-image: url(rasterized.png);
265  background-image: linear-gradient(transparent, transparent), url(data:image/svg+xml);
266 TEXT
267  ],
268 
269  ];
270  }
271 
276  public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
277  $module = TestingAccessWrapper::newFromObject( new ResourceLoaderImageModule() );
279  $image = $this->getImageMock( $context, $dataUriReturnValue );
280 
281  $styles = $module->getStyleDeclarations(
282  $context,
283  $image,
284  'load.php'
285  );
286 
287  $this->assertEquals( $expected, $styles );
288  }
289 
290  private function getImageMock( ResourceLoaderContext $context, $dataUriReturnValue ) {
291  $image = $this->getMockBuilder( ResourceLoaderImage::class )
292  ->disableOriginalConstructor()
293  ->getMock();
294  $image->method( 'getDataUri' )
295  ->will( $this->returnValue( $dataUriReturnValue ) );
296  $image->expects( $this->any() )
297  ->method( 'getUrl' )
298  ->will( $this->returnValueMap( [
299  [ $context, 'load.php', null, 'original', 'original.svg' ],
300  [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
301  ] ) );
302 
303  return $image;
304  }
305 }
306 
311  protected function getCssDeclarations( $primary, $fallback ) {
312  return [ '...' ];
313  }
314 }
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
ResourceLoader module for generated and embedded images.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getImageMock(ResourceLoaderContext $context, $dataUriReturnValue)
$basePath
Definition: addSite.php:5
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
testResourceLoaderFilePath()
Test reading files from elsewhere than localBasePath using ResourceLoaderFilePath.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
testGetStyleDeclarations( $dataUriReturnValue, $expected)
providerGetStyleDeclarations ResourceLoaderImageModule::getStyleDeclarations
An object to represent a path to a JavaScript/CSS file, along with a remote and local base path...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testContext()
ResourceLoaderContext::getImageObj.
$fallback
Definition: MessagesAb.php:11
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:767
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:2620
testGetStyles( $module, $expected)
providerGetModules ResourceLoaderImageModule::getStyles
Object passed around to modules which contains information about the state of a specific loader reque...