| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use MediaWiki\HookContainer\HookContainer; |
| 6 |
use MediaWiki\HookContainer\StaticHookRegistry; |
| 7 |
use MediaWiki\Request\FauxRequest; |
| 8 |
use MediaWiki\ResourceLoader\Context; |
| 9 |
use MediaWiki\ResourceLoader\FilePath; |
| 10 |
use MediaWiki\ResourceLoader\Image; |
| 11 |
use MediaWiki\ResourceLoader\ImageModule; |
| 12 |
use Wikimedia\TestingAccessWrapper; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class ImageModuleTest extends ResourceLoaderTestCase { |
| 19 |
|
| 20 |
public const COMMON_IMAGE_DATA = [ |
| 21 |
'abc' => 'abc.gif', |
| 22 |
'def' => [ |
| 23 |
'file' => 'def.svg', |
| 24 |
'variants' => [ 'destructive' ], |
| 25 |
], |
| 26 |
'ghi' => [ |
| 27 |
'file' => [ |
| 28 |
'ltr' => 'ghi.svg', |
| 29 |
'rtl' => 'jkl.svg' |
| 30 |
], |
| 31 |
], |
| 32 |
'mno' => [ |
| 33 |
'file' => [ |
| 34 |
'ltr' => 'mno-ltr.svg', |
| 35 |
'rtl' => 'mno-rtl.svg', |
| 36 |
'lang' => [ |
| 37 |
'he' => 'mno-ltr.svg', |
| 38 |
] |
| 39 |
], |
| 40 |
], |
| 41 |
'pqr' => [ |
| 42 |
'file' => [ |
| 43 |
'default' => 'pqr-a.svg', |
| 44 |
'lang' => [ |
| 45 |
'en' => 'pqr-b.svg', |
| 46 |
'ar,de' => 'pqr-f.svg', |
| 47 |
] |
| 48 |
], |
| 49 |
] |
| 50 |
]; |
| 51 |
|
| 52 |
private const COMMON_IMAGE_VARIANTS = [ |
| 53 |
'invert' => [ |
| 54 |
'color' => '#FFFFFF', |
| 55 |
'global' => true, |
| 56 |
], |
| 57 |
'primary' => [ |
| 58 |
'color' => '#598AD1', |
| 59 |
], |
| 60 |
'constructive' => [ |
| 61 |
'color' => '#00C697', |
| 62 |
], |
| 63 |
'destructive' => [ |
| 64 |
'color' => '#E81915', |
| 65 |
], |
| 66 |
]; |
| 67 |
|
| 68 |
public static function providerGetModules() { |
| 69 |
return [ |
| 70 |
[ |
| 71 |
[ |
| 72 |
'class' => ImageModule::class, |
| 73 |
'prefix' => 'oo-ui-icon', |
| 74 |
'variants' => self::COMMON_IMAGE_VARIANTS, |
| 75 |
'images' => self::COMMON_IMAGE_DATA, |
| 76 |
], |
| 77 |
'.oo-ui-icon-abc { |
| 78 |
... |
| 79 |
} |
| 80 |
.oo-ui-icon-abc-invert { |
| 81 |
... |
| 82 |
} |
| 83 |
.oo-ui-icon-def { |
| 84 |
... |
| 85 |
} |
| 86 |
.oo-ui-icon-def-invert { |
| 87 |
... |
| 88 |
} |
| 89 |
.oo-ui-icon-def-destructive { |
| 90 |
... |
| 91 |
} |
| 92 |
.oo-ui-icon-ghi { |
| 93 |
... |
| 94 |
} |
| 95 |
.oo-ui-icon-ghi-invert { |
| 96 |
... |
| 97 |
} |
| 98 |
.oo-ui-icon-mno { |
| 99 |
... |
| 100 |
} |
| 101 |
.oo-ui-icon-mno-invert { |
| 102 |
... |
| 103 |
} |
| 104 |
.oo-ui-icon-pqr { |
| 105 |
... |
| 106 |
} |
| 107 |
.oo-ui-icon-pqr-invert { |
| 108 |
... |
| 109 |
}', |
| 110 |
], |
| 111 |
[ |
| 112 |
[ |
| 113 |
'class' => ImageModule::class, |
| 114 |
'selectorWithoutVariant' => '.mw-ui-icon-{name}:after, .mw-ui-icon-{name}:before', |
| 115 |
'selectorWithVariant' => |
| 116 |
'.mw-ui-icon-{name}-{variant}:after, .mw-ui-icon-{name}-{variant}:before', |
| 117 |
'variants' => self::COMMON_IMAGE_VARIANTS, |
| 118 |
'images' => self::COMMON_IMAGE_DATA, |
| 119 |
], |
| 120 |
'.mw-ui-icon-abc:after, .mw-ui-icon-abc:before { |
| 121 |
... |
| 122 |
} |
| 123 |
.mw-ui-icon-abc-invert:after, .mw-ui-icon-abc-invert:before { |
| 124 |
... |
| 125 |
} |
| 126 |
.mw-ui-icon-def:after, .mw-ui-icon-def:before { |
| 127 |
... |
| 128 |
} |
| 129 |
.mw-ui-icon-def-invert:after, .mw-ui-icon-def-invert:before { |
| 130 |
... |
| 131 |
} |
| 132 |
.mw-ui-icon-def-destructive:after, .mw-ui-icon-def-destructive:before { |
| 133 |
... |
| 134 |
} |
| 135 |
.mw-ui-icon-ghi:after, .mw-ui-icon-ghi:before { |
| 136 |
... |
| 137 |
} |
| 138 |
.mw-ui-icon-ghi-invert:after, .mw-ui-icon-ghi-invert:before { |
| 139 |
... |
| 140 |
} |
| 141 |
.mw-ui-icon-mno:after, .mw-ui-icon-mno:before { |
| 142 |
... |
| 143 |
} |
| 144 |
.mw-ui-icon-mno-invert:after, .mw-ui-icon-mno-invert:before { |
| 145 |
... |
| 146 |
} |
| 147 |
.mw-ui-icon-pqr:after, .mw-ui-icon-pqr:before { |
| 148 |
... |
| 149 |
} |
| 150 |
.mw-ui-icon-pqr-invert:after, .mw-ui-icon-pqr-invert:before { |
| 151 |
... |
| 152 |
}', |
| 153 |
], |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function testResourceLoaderFilePath() { |
| 164 |
$hookContainer = new HookContainer( |
| 165 |
new StaticHookRegistry(), |
| 166 |
$this->getServiceContainer()->getObjectFactory() |
| 167 |
); |
| 168 |
$basePath = __DIR__ . '/../../data/blahblah'; |
| 169 |
$filePath = __DIR__ . '/../../data/rlfilepath'; |
| 170 |
$testModule = new ImageModule( [ |
| 171 |
'localBasePath' => $basePath, |
| 172 |
'remoteBasePath' => 'blahblah', |
| 173 |
'prefix' => 'foo', |
| 174 |
'images' => [ |
| 175 |
'eye' => new FilePath( 'eye.svg', $filePath, 'rlfilepath' ), |
| 176 |
'flag' => [ |
| 177 |
'file' => [ |
| 178 |
'ltr' => new FilePath( 'flag-ltr.svg', $filePath, 'rlfilepath' ), |
| 179 |
'rtl' => new FilePath( 'flag-rtl.svg', $filePath, 'rlfilepath' ), |
| 180 |
], |
| 181 |
], |
| 182 |
], |
| 183 |
] ); |
| 184 |
$testModule->setName( 'testModule' ); |
| 185 |
$testModule->setHookContainer( $hookContainer ); |
| 186 |
$expectedModule = new ImageModule( [ |
| 187 |
'localBasePath' => $filePath, |
| 188 |
'remoteBasePath' => 'rlfilepath', |
| 189 |
'prefix' => 'foo', |
| 190 |
'images' => [ |
| 191 |
'eye' => 'eye.svg', |
| 192 |
'flag' => [ |
| 193 |
'file' => [ |
| 194 |
'ltr' => 'flag-ltr.svg', |
| 195 |
'rtl' => 'flag-rtl.svg', |
| 196 |
], |
| 197 |
], |
| 198 |
], |
| 199 |
] ); |
| 200 |
$expectedModule->setName( 'testModule' ); |
| 201 |
$expectedModule->setHookContainer( $hookContainer ); |
| 202 |
|
| 203 |
$context = $this->getResourceLoaderContext(); |
| 204 |
$this->assertEquals( |
| 205 |
$expectedModule->getModuleContent( $context ), |
| 206 |
$testModule->getModuleContent( $context ), |
| 207 |
"Using ResourceLoaderFilePath works correctly" |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function testGetStyles( $module, $expected ) { |
| 215 |
$module = new ImageModuleTestable( |
| 216 |
$module, |
| 217 |
__DIR__ . '/../../data/resourceloader' |
| 218 |
); |
| 219 |
$module->setHookContainer( new HookContainer( |
| 220 |
new StaticHookRegistry(), |
| 221 |
$this->getServiceContainer()->getObjectFactory() |
| 222 |
) ); |
| 223 |
$styles = $module->getStyles( $this->getResourceLoaderContext() ); |
| 224 |
$this->assertEquals( $expected, $styles['all'] ); |
| 225 |
} |
| 226 |
|
| 227 |
public function testContext() { |
| 228 |
$context = new Context( new EmptyResourceLoader(), new FauxRequest() ); |
| 229 |
$this->assertFalse( $context->getImageObj(), 'Missing image parameter' ); |
| 230 |
|
| 231 |
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [ |
| 232 |
'image' => 'example', |
| 233 |
] ) ); |
| 234 |
$this->assertFalse( $context->getImageObj(), 'Missing module parameter' ); |
| 235 |
|
| 236 |
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [ |
| 237 |
'modules' => 'unknown', |
| 238 |
'image' => 'example', |
| 239 |
] ) ); |
| 240 |
$this->assertFalse( $context->getImageObj(), 'Not an image module' ); |
| 241 |
|
| 242 |
$rl = new EmptyResourceLoader(); |
| 243 |
$rl->register( 'test', [ |
| 244 |
'class' => ImageModule::class, |
| 245 |
'prefix' => 'test', |
| 246 |
'images' => [ 'example' => 'example.png' ], |
| 247 |
] ); |
| 248 |
$context = new Context( $rl, new FauxRequest( [ |
| 249 |
'modules' => 'test', |
| 250 |
'image' => 'unknown', |
| 251 |
] ) ); |
| 252 |
$this->assertFalse( $context->getImageObj(), 'Unknown image' ); |
| 253 |
|
| 254 |
$rl = new EmptyResourceLoader(); |
| 255 |
$rl->register( 'test', [ |
| 256 |
'class' => ImageModule::class, |
| 257 |
'prefix' => 'test', |
| 258 |
'images' => [ 'example' => 'example.png' ], |
| 259 |
] ); |
| 260 |
$context = new Context( $rl, new FauxRequest( [ |
| 261 |
'modules' => 'test', |
| 262 |
'image' => 'example', |
| 263 |
] ) ); |
| 264 |
$this->assertInstanceOf( Image::class, $context->getImageObj() ); |
| 265 |
} |
| 266 |
|
| 267 |
public static function providerGetStyleDeclarations() { |
| 268 |
return [ |
| 269 |
[ |
| 270 |
false, |
| 271 |
'background-image: url(original.svg);', |
| 272 |
], |
| 273 |
[ |
| 274 |
'data:image/svg+xml', |
| 275 |
'background-image: url(data:image/svg+xml);', |
| 276 |
], |
| 277 |
[ |
| 278 |
'data:image/svg+xml', |
| 279 |
"-webkit-mask-image: url(data:image/svg+xml);\n mask-image: url(data:image/svg+xml);", |
| 280 |
true |
| 281 |
] |
| 282 |
]; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public function testGetStyleDeclarations( $dataUriReturnValue, $expected, $useMaskImage = false ) { |
| 289 |
$module = TestingAccessWrapper::newFromObject( new ImageModule( [ 'useMaskImage' => $useMaskImage ] ) ); |
| 290 |
$context = $this->getResourceLoaderContext(); |
| 291 |
$image = $this->getImageMock( $context, $dataUriReturnValue ); |
| 292 |
|
| 293 |
$styles = $module->getStyleDeclarations( |
| 294 |
$context, |
| 295 |
$image, |
| 296 |
'load.php' |
| 297 |
); |
| 298 |
|
| 299 |
$this->assertEquals( $expected, $styles ); |
| 300 |
} |
| 301 |
|
| 302 |
private function getImageMock( Context $context, $dataUriReturnValue ) { |
| 303 |
$image = $this->createMock( Image::class ); |
| 304 |
$image->method( 'getDataUri' ) |
| 305 |
->willReturn( $dataUriReturnValue ); |
| 306 |
$image->method( 'getUrl' ) |
| 307 |
->willReturnMap( [ |
| 308 |
[ $context, 'load.php', null, 'original', 'original.svg' ], |
| 309 |
[ $context, 'load.php', null, 'rasterized', 'rasterized.png' ], |
| 310 |
] ); |
| 311 |
|
| 312 |
return $image; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
class ImageModuleTestable extends ImageModule { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
protected function getCssDeclarations( $primary ): array { |
| 322 |
return [ '...' ]; |
| 323 |
} |
| 324 |
} |