3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderImageModuleTest
extends ResourceLoaderTestCase
{
10 public static $commonImageData = [
14 'variants' => [ 'destructive' ],
24 'ltr' => 'mno-ltr.svg',
25 'rtl' => 'mno-rtl.svg',
27 'he' => 'mno-ltr.svg',
33 'default' => 'pqr-a.svg',
36 'ar,de' => 'pqr-f.svg',
42 public static $commonImageVariants = [
58 public static function providerGetModules() {
62 'class' => ResourceLoaderImageModule
::class,
63 'prefix' => 'oo-ui-icon',
64 'variants' => self
::$commonImageVariants,
65 'images' => self
::$commonImageData,
70 .oo-ui-icon-abc-invert {
76 .oo-ui-icon-def-invert {
79 .oo-ui-icon-def-destructive {
85 .oo-ui-icon-ghi-invert {
91 .oo-ui-icon-mno-invert {
97 .oo-ui-icon-pqr-invert {
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,
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 * Test reading files from elsewhere than localBasePath using ResourceLoaderFilePath.
150 * This mimics modules modified by skins using 'ResourceModuleSkinStyles' and 'OOUIThemePaths'
153 * @covers ResourceLoaderFilePath::getLocalBasePath
154 * @covers ResourceLoaderFilePath::getRemoteBasePath
156 public function testResourceLoaderFilePath() {
157 $basePath = __DIR__
. '/../../data/blahblah';
158 $filePath = __DIR__
. '/../../data/rlfilepath';
159 $testModule = new ResourceLoaderImageModule( [
160 'localBasePath' => $basePath,
161 'remoteBasePath' => 'blahblah',
164 'eye' => new ResourceLoaderFilePath( 'eye.svg', $filePath, 'rlfilepath' ),
167 'ltr' => new ResourceLoaderFilePath( 'flag-ltr.svg', $filePath, 'rlfilepath' ),
168 'rtl' => new ResourceLoaderFilePath( 'flag-rtl.svg', $filePath, 'rlfilepath' ),
173 $expectedModule = new ResourceLoaderImageModule( [
174 'localBasePath' => $filePath,
175 'remoteBasePath' => 'rlfilepath',
181 'ltr' => 'flag-ltr.svg',
182 'rtl' => 'flag-rtl.svg',
188 $context = $this->getResourceLoaderContext();
190 $expectedModule->getModuleContent( $context ),
191 $testModule->getModuleContent( $context ),
192 "Using ResourceLoaderFilePath works correctly"
197 * @dataProvider providerGetModules
198 * @covers ResourceLoaderImageModule::getStyles
200 public function testGetStyles( $module, $expected ) {
201 $module = new ResourceLoaderImageModuleTestable(
203 __DIR__
. '/../../data/resourceloader'
205 $styles = $module->getStyles( $this->getResourceLoaderContext() );
206 $this->assertEquals( $expected, $styles['all'] );
210 * @covers ResourceLoaderContext::getImageObj
212 public function testContext() {
213 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
214 $this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
216 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
217 'image' => 'example',
219 $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
221 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
222 'modules' => 'unknown',
223 'image' => 'example',
225 $this->assertFalse( $context->getImageObj(), 'Not an image module' );
227 $rl = new EmptyResourceLoader();
228 $rl->register( 'test', [
229 'class' => ResourceLoaderImageModule
::class,
231 'images' => [ 'example' => 'example.png' ],
233 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
235 'image' => 'unknown',
237 $this->assertFalse( $context->getImageObj(), 'Unknown image' );
239 $rl = new EmptyResourceLoader();
240 $rl->register( 'test', [
241 'class' => ResourceLoaderImageModule
::class,
243 'images' => [ 'example' => 'example.png' ],
245 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
247 'image' => 'example',
249 $this->assertInstanceOf( ResourceLoaderImage
::class, $context->getImageObj() );
252 public static function providerGetStyleDeclarations() {
257 background-image: url(rasterized.png);
258 background-image: linear-gradient(transparent, transparent), url(original.svg);
262 'data:image/svg+xml',
264 background-image: url(rasterized.png);
265 background-image: linear-gradient(transparent, transparent), url(data:image/svg+xml);
273 * @dataProvider providerGetStyleDeclarations
274 * @covers ResourceLoaderImageModule::getStyleDeclarations
276 public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
277 $module = TestingAccessWrapper
::newFromObject( new ResourceLoaderImageModule() );
278 $context = $this->getResourceLoaderContext();
279 $image = $this->getImageMock( $context, $dataUriReturnValue );
281 $styles = $module->getStyleDeclarations(
287 $this->assertEquals( $expected, $styles );
290 private function getImageMock( ResourceLoaderContext
$context, $dataUriReturnValue ) {
291 $image = $this->getMockBuilder( ResourceLoaderImage
::class )
292 ->disableOriginalConstructor()
294 $image->method( 'getDataUri' )
295 ->will( $this->returnValue( $dataUriReturnValue ) );
296 $image->expects( $this->any() )
298 ->will( $this->returnValueMap( [
299 [ $context, 'load.php', null, 'original', 'original.svg' ],
300 [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
307 class ResourceLoaderImageModuleTestable
extends ResourceLoaderImageModule
{
309 * Replace with a stub to make test cases easier to write.
311 protected function getCssDeclarations( $primary, $fallback ) {