rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / resourceloader / ResourceLoaderImageModuleTest.php
blobf53cd069c21591e3427b1930628c783b972b21b8
1 <?php
3 use Wikimedia\TestingAccessWrapper;
5 /**
6 * @group ResourceLoader
7 */
8 class ResourceLoaderImageModuleTest extends ResourceLoaderTestCase {
10 public static $commonImageData = [
11 'abc' => 'abc.gif',
12 'def' => [
13 'file' => 'def.svg',
14 'variants' => [ 'destructive' ],
16 'ghi' => [
17 'file' => [
18 'ltr' => 'ghi.svg',
19 'rtl' => 'jkl.svg'
22 'mno' => [
23 'file' => [
24 'ltr' => 'mno-ltr.svg',
25 'rtl' => 'mno-rtl.svg',
26 'lang' => [
27 'he' => 'mno-ltr.svg',
31 'pqr' => [
32 'file' => [
33 'default' => 'pqr-a.svg',
34 'lang' => [
35 'en' => 'pqr-b.svg',
36 'ar,de' => 'pqr-f.svg',
42 public static $commonImageVariants = [
43 'invert' => [
44 'color' => '#FFFFFF',
45 'global' => true,
47 'primary' => [
48 'color' => '#598AD1',
50 'constructive' => [
51 'color' => '#00C697',
53 'destructive' => [
54 'color' => '#E81915',
58 public static function providerGetModules() {
59 return [
62 'class' => 'ResourceLoaderImageModule',
63 'prefix' => 'oo-ui-icon',
64 'variants' => self::$commonImageVariants,
65 'images' => self::$commonImageData,
67 '.oo-ui-icon-abc {
68 ...
70 .oo-ui-icon-abc-invert {
71 ...
73 .oo-ui-icon-def {
74 ...
76 .oo-ui-icon-def-invert {
77 ...
79 .oo-ui-icon-def-destructive {
80 ...
82 .oo-ui-icon-ghi {
83 ...
85 .oo-ui-icon-ghi-invert {
86 ...
88 .oo-ui-icon-mno {
89 ...
91 .oo-ui-icon-mno-invert {
92 ...
94 .oo-ui-icon-pqr {
95 ...
97 .oo-ui-icon-pqr-invert {
98 ...
99 }',
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,
110 '.mw-ui-icon-abc:after, .mw-ui-icon-abc:before {
113 .mw-ui-icon-abc-invert:after, .mw-ui-icon-abc-invert:before {
116 .mw-ui-icon-def:after, .mw-ui-icon-def:before {
119 .mw-ui-icon-def-invert:after, .mw-ui-icon-def-invert:before {
122 .mw-ui-icon-def-destructive:after, .mw-ui-icon-def-destructive:before {
125 .mw-ui-icon-ghi:after, .mw-ui-icon-ghi:before {
128 .mw-ui-icon-ghi-invert:after, .mw-ui-icon-ghi-invert:before {
131 .mw-ui-icon-mno:after, .mw-ui-icon-mno:before {
134 .mw-ui-icon-mno-invert:after, .mw-ui-icon-mno-invert:before {
137 .mw-ui-icon-pqr:after, .mw-ui-icon-pqr:before {
140 .mw-ui-icon-pqr-invert:after, .mw-ui-icon-pqr-invert:before {
148 * @dataProvider providerGetModules
149 * @covers ResourceLoaderImageModule::getStyles
151 public function testGetStyles( $module, $expected ) {
152 $module = new ResourceLoaderImageModuleTestable(
153 $module,
154 __DIR__ . '/../../data/resourceloader'
156 $styles = $module->getStyles( $this->getResourceLoaderContext() );
157 $this->assertEquals( $expected, $styles['all'] );
161 * @covers ResourceLoaderContext::getImageObj
163 public function testContext() {
164 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
165 $this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
167 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
168 'image' => 'example',
169 ] ) );
170 $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
172 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
173 'modules' => 'unknown',
174 'image' => 'example',
175 ] ) );
176 $this->assertFalse( $context->getImageObj(), 'Not an image module' );
178 $rl = new EmptyResourceLoader();
179 $rl->register( 'test', [
180 'class' => ResourceLoaderImageModule::class,
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' );
190 $rl = new EmptyResourceLoader();
191 $rl->register( 'test', [
192 'class' => ResourceLoaderImageModule::class,
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() );
203 public static function providerGetStyleDeclarations() {
204 return [
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
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
226 * @dataProvider providerGetStyleDeclarations
227 * @covers ResourceLoaderImageModule::getStyleDeclarations
229 public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
230 $module = TestingAccessWrapper::newFromObject( new ResourceLoaderImageModule() );
231 $context = $this->getResourceLoaderContext();
232 $image = $this->getImageMock( $context, $dataUriReturnValue );
234 $styles = $module->getStyleDeclarations(
235 $context,
236 $image,
237 'load.php'
240 $this->assertEquals( $expected, $styles );
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 ] ) );
256 return $image;
260 class ResourceLoaderImageModuleTestable extends ResourceLoaderImageModule {
262 * Replace with a stub to make test cases easier to write.
264 protected function getCssDeclarations( $primary, $fallback ) {
265 return [ '...' ];