MediaWiki  1.29.1
ResourceLoaderImageModuleTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
9 
10  public static $commonImageData = [
11  'add' => 'add.gif',
12  'remove' => [
13  'file' => 'remove.svg',
14  'variants' => [ 'destructive' ],
15  ],
16  'next' => [
17  'file' => [
18  'ltr' => 'next.svg',
19  'rtl' => 'prev.svg'
20  ],
21  ],
22  'help' => [
23  'file' => [
24  'ltr' => 'help-ltr.svg',
25  'rtl' => 'help-rtl.svg',
26  'lang' => [
27  'he' => 'help-ltr.svg',
28  ]
29  ],
30  ],
31  'bold' => [
32  'file' => [
33  'default' => 'bold-a.svg',
34  'lang' => [
35  'en' => 'bold-b.svg',
36  'ar,de' => 'bold-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',
63  'prefix' => 'oo-ui-icon',
64  'variants' => self::$commonImageVariants,
65  'images' => self::$commonImageData,
66  ],
67  '.oo-ui-icon-add {
68  ...
69 }
70 .oo-ui-icon-add-invert {
71  ...
72 }
73 .oo-ui-icon-remove {
74  ...
75 }
76 .oo-ui-icon-remove-invert {
77  ...
78 }
79 .oo-ui-icon-remove-destructive {
80  ...
81 }
82 .oo-ui-icon-next {
83  ...
84 }
85 .oo-ui-icon-next-invert {
86  ...
87 }
88 .oo-ui-icon-help {
89  ...
90 }
91 .oo-ui-icon-help-invert {
92  ...
93 }
94 .oo-ui-icon-bold {
95  ...
96 }
97 .oo-ui-icon-bold-invert {
98  ...
99 }',
100  ],
101  [
102  [
103  'class' => 'ResourceLoaderImageModule',
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-add:after, .mw-ui-icon-add:before {
111  ...
112 }
113 .mw-ui-icon-add-invert:after, .mw-ui-icon-add-invert:before {
114  ...
115 }
116 .mw-ui-icon-remove:after, .mw-ui-icon-remove:before {
117  ...
118 }
119 .mw-ui-icon-remove-invert:after, .mw-ui-icon-remove-invert:before {
120  ...
121 }
122 .mw-ui-icon-remove-destructive:after, .mw-ui-icon-remove-destructive:before {
123  ...
124 }
125 .mw-ui-icon-next:after, .mw-ui-icon-next:before {
126  ...
127 }
128 .mw-ui-icon-next-invert:after, .mw-ui-icon-next-invert:before {
129  ...
130 }
131 .mw-ui-icon-help:after, .mw-ui-icon-help:before {
132  ...
133 }
134 .mw-ui-icon-help-invert:after, .mw-ui-icon-help-invert:before {
135  ...
136 }
137 .mw-ui-icon-bold:after, .mw-ui-icon-bold:before {
138  ...
139 }
140 .mw-ui-icon-bold-invert:after, .mw-ui-icon-bold-invert:before {
141  ...
142 }',
143  ],
144  ];
145  }
146 
151  public function testGetStyles( $module, $expected ) {
152  $module = new ResourceLoaderImageModuleTestable(
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', [
181  'prefix' => 'test',
182  'images' => [ 'example' => 'example.png' ],
183  ] );
184  $context = new ResourceLoaderContext( $rl, new FauxRequest( [
185  'modules' => 'test',
186  'image' => 'unknown',
187  ] ) );
188  $this->assertFalse( $context->getImageObj(), 'Unknown image' );
189 
190  $rl = new EmptyResourceLoader();
191  $rl->register( 'test', [
193  'prefix' => 'test',
194  'images' => [ 'example' => 'example.png' ],
195  ] );
196  $context = new ResourceLoaderContext( $rl, new FauxRequest( [
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
208 background-image: url(rasterized.png);
209  background-image: linear-gradient(transparent, transparent), url(original.svg);
210  background-image: -o-linear-gradient(transparent, transparent), url(rasterized.png);
211 TEXT
212  ],
213  [
214  'data:image/svg+xml',
215 <<<TEXT
216 background-image: url(rasterized.png);
217  background-image: linear-gradient(transparent, transparent), url(data:image/svg+xml);
218  background-image: -o-linear-gradient(transparent, transparent), url(rasterized.png);
219 TEXT
220  ],
221 
222  ];
223  }
224 
229  public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
230  $module = TestingAccessWrapper::newFromObject( new ResourceLoaderImageModule() );
232  $image = $this->getImageMock( $context, $dataUriReturnValue );
233 
234  $styles = $module->getStyleDeclarations(
235  $context,
236  $image,
237  'load.php'
238  );
239 
240  $this->assertEquals( $expected, $styles );
241  }
242 
243  private function getImageMock( ResourceLoaderContext $context, $dataUriReturnValue ) {
244  $image = $this->getMockBuilder( 'ResourceLoaderImage' )
245  ->disableOriginalConstructor()
246  ->getMock();
247  $image->method( 'getDataUri' )
248  ->will( $this->returnValue( $dataUriReturnValue ) );
249  $image->expects( $this->any() )
250  ->method( 'getUrl' )
251  ->will( $this->returnValueMap( [
252  [ $context, 'load.php', null, 'original', 'original.svg' ],
253  [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
254  ] ) );
255 
256  return $image;
257  }
258 }
259 
264  protected function getCssDeclarations( $primary, $fallback ) {
265  return [ '...' ];
266  }
267 }
ResourceLoaderImageModuleTest\getImageMock
getImageMock(ResourceLoaderContext $context, $dataUriReturnValue)
Definition: ResourceLoaderImageModuleTest.php:243
ResourceLoaderContext
Object passed around to modules which contains information about the state of a specific loader reque...
Definition: ResourceLoaderContext.php:32
ResourceLoaderImageModuleTest\testGetStyles
testGetStyles( $module, $expected)
providerGetModules ResourceLoaderImageModule::getStyles
Definition: ResourceLoaderImageModuleTest.php:151
$context
error also a ContextSource you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2612
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ResourceLoaderImageModuleTest
ResourceLoader.
Definition: ResourceLoaderImageModuleTest.php:8
$fallback
$fallback
Definition: MessagesAb.php:11
ResourceLoaderImageModuleTestable\getCssDeclarations
getCssDeclarations( $primary, $fallback)
Replace with a stub to make test cases easier to write.
Definition: ResourceLoaderImageModuleTest.php:256
original
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the original
Definition: COPYING.txt:43
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
php
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
ResourceLoaderImageModuleTest\$commonImageVariants
static $commonImageVariants
Definition: ResourceLoaderImageModuleTest.php:42
ResourceLoaderImageModuleTest\testGetStyleDeclarations
testGetStyleDeclarations( $dataUriReturnValue, $expected)
providerGetStyleDeclarations ResourceLoaderImageModule::getStyleDeclarations
Definition: ResourceLoaderImageModuleTest.php:229
ResourceLoaderImageModuleTestable
Definition: ResourceLoaderImageModuleTest.php:252
$image
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:783
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
ResourceLoaderImageModuleTest\$commonImageData
static $commonImageData
Definition: ResourceLoaderImageModuleTest.php:10
ResourceLoaderImageModuleTest\testContext
testContext()
ResourceLoaderContext::getImageObj.
Definition: ResourceLoaderImageModuleTest.php:163
EmptyResourceLoader
Definition: ResourceLoaderTestCase.php:154
ResourceLoaderTestCase\getResourceLoaderContext
getResourceLoaderContext( $options=[], ResourceLoader $rl=null)
Definition: ResourceLoaderTestCase.php:22
ResourceLoaderImageModuleTest\providerGetStyleDeclarations
static providerGetStyleDeclarations()
Definition: ResourceLoaderImageModuleTest.php:203
class
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
ResourceLoaderImageModule
ResourceLoader module for generated and embedded images.
Definition: ResourceLoaderImageModule.php:29
data
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
ResourceLoaderImageModuleTest\providerGetModules
static providerGetModules()
Definition: ResourceLoaderImageModuleTest.php:58
ResourceLoaderTestCase
Definition: ResourceLoaderTestCase.php:7