2015-03-27 17:08:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
namespace MediaWiki\Tests\ResourceLoader;
|
|
|
|
|
|
|
|
|
|
use EmptyResourceLoader;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2022-05-06 09:09:56 +00:00
|
|
|
use MediaWiki\ResourceLoader\Context;
|
|
|
|
|
use MediaWiki\ResourceLoader\FilePath;
|
|
|
|
|
use MediaWiki\ResourceLoader\Image;
|
|
|
|
|
use MediaWiki\ResourceLoader\ImageModule;
|
|
|
|
|
use ResourceLoaderTestCase;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
2015-03-27 17:08:11 +00:00
|
|
|
/**
|
|
|
|
|
* @group ResourceLoader
|
2022-08-08 15:22:31 +00:00
|
|
|
* @covers \MediaWiki\ResourceLoader\ImageModule
|
2015-03-27 17:08:11 +00:00
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
class ImageModuleTest extends ResourceLoaderTestCase {
|
2015-03-27 17:08:11 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
public static $commonImageData = [
|
2017-06-06 13:04:09 +00:00
|
|
|
'abc' => 'abc.gif',
|
|
|
|
|
'def' => [
|
|
|
|
|
'file' => 'def.svg',
|
2016-02-17 09:09:32 +00:00
|
|
|
'variants' => [ 'destructive' ],
|
|
|
|
|
],
|
2017-06-06 13:04:09 +00:00
|
|
|
'ghi' => [
|
2016-02-17 09:09:32 +00:00
|
|
|
'file' => [
|
2017-06-06 13:04:09 +00:00
|
|
|
'ltr' => 'ghi.svg',
|
|
|
|
|
'rtl' => 'jkl.svg'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
2017-06-06 13:04:09 +00:00
|
|
|
'mno' => [
|
2016-02-17 09:09:32 +00:00
|
|
|
'file' => [
|
2017-06-06 13:04:09 +00:00
|
|
|
'ltr' => 'mno-ltr.svg',
|
|
|
|
|
'rtl' => 'mno-rtl.svg',
|
2016-02-17 09:09:32 +00:00
|
|
|
'lang' => [
|
2017-06-06 13:04:09 +00:00
|
|
|
'he' => 'mno-ltr.svg',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
2017-06-06 13:04:09 +00:00
|
|
|
'pqr' => [
|
2016-02-17 09:09:32 +00:00
|
|
|
'file' => [
|
2017-06-06 13:04:09 +00:00
|
|
|
'default' => 'pqr-a.svg',
|
2016-02-17 09:09:32 +00:00
|
|
|
'lang' => [
|
2017-06-06 13:04:09 +00:00
|
|
|
'en' => 'pqr-b.svg',
|
|
|
|
|
'ar,de' => 'pqr-f.svg',
|
2016-02-17 09:09:32 +00:00
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
];
|
2015-03-27 17:08:11 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
public static $commonImageVariants = [
|
|
|
|
|
'invert' => [
|
2015-03-29 17:53:47 +00:00
|
|
|
'color' => '#FFFFFF',
|
|
|
|
|
'global' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'primary' => [
|
2015-03-29 17:53:47 +00:00
|
|
|
'color' => '#598AD1',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'constructive' => [
|
2015-03-29 17:53:47 +00:00
|
|
|
'color' => '#00C697',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'destructive' => [
|
2015-03-29 17:53:47 +00:00
|
|
|
'color' => '#E81915',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-03-27 17:08:11 +00:00
|
|
|
|
2015-03-29 17:53:47 +00:00
|
|
|
public static function providerGetModules() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
[
|
2022-05-06 09:09:56 +00:00
|
|
|
'class' => ImageModule::class,
|
2015-03-27 17:27:47 +00:00
|
|
|
'prefix' => 'oo-ui-icon',
|
2015-03-29 17:53:47 +00:00
|
|
|
'variants' => self::$commonImageVariants,
|
|
|
|
|
'images' => self::$commonImageData,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2017-06-06 13:04:09 +00:00
|
|
|
'.oo-ui-icon-abc {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-abc-invert {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-def {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-def-invert {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-def-destructive {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-ghi {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-ghi-invert {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-mno {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-mno-invert {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-pqr {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.oo-ui-icon-pqr-invert {
|
2015-03-27 17:08:11 +00:00
|
|
|
...
|
2015-03-23 21:04:54 +00:00
|
|
|
}',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
[
|
2022-05-06 09:09:56 +00:00
|
|
|
'class' => ImageModule::class,
|
2015-03-23 21:04:54 +00:00
|
|
|
'selectorWithoutVariant' => '.mw-ui-icon-{name}:after, .mw-ui-icon-{name}:before',
|
2015-09-26 19:48:17 +00:00
|
|
|
'selectorWithVariant' =>
|
|
|
|
|
'.mw-ui-icon-{name}-{variant}:after, .mw-ui-icon-{name}-{variant}:before',
|
2015-03-29 17:53:47 +00:00
|
|
|
'variants' => self::$commonImageVariants,
|
|
|
|
|
'images' => self::$commonImageData,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2017-06-06 13:04:09 +00:00
|
|
|
'.mw-ui-icon-abc:after, .mw-ui-icon-abc:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-abc-invert:after, .mw-ui-icon-abc-invert:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-def:after, .mw-ui-icon-def:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-def-invert:after, .mw-ui-icon-def-invert:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-def-destructive:after, .mw-ui-icon-def-destructive:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-ghi:after, .mw-ui-icon-ghi:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-ghi-invert:after, .mw-ui-icon-ghi-invert:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-mno:after, .mw-ui-icon-mno:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-mno-invert:after, .mw-ui-icon-mno-invert:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-pqr:after, .mw-ui-icon-pqr:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
|
|
|
|
}
|
2017-06-06 13:04:09 +00:00
|
|
|
.mw-ui-icon-pqr-invert:after, .mw-ui-icon-pqr-invert:before {
|
2015-03-23 21:04:54 +00:00
|
|
|
...
|
2015-03-27 17:08:11 +00:00
|
|
|
}',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-03-27 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
2017-03-17 02:14:05 +00:00
|
|
|
/**
|
2023-03-27 23:21:06 +00:00
|
|
|
* Test reading files from elsewhere than localBasePath using FilePath.
|
2017-03-17 02:14:05 +00:00
|
|
|
*
|
|
|
|
|
* This mimics modules modified by skins using 'ResourceModuleSkinStyles' and 'OOUIThemePaths'
|
|
|
|
|
* skin attributes.
|
|
|
|
|
*/
|
|
|
|
|
public function testResourceLoaderFilePath() {
|
|
|
|
|
$basePath = __DIR__ . '/../../data/blahblah';
|
|
|
|
|
$filePath = __DIR__ . '/../../data/rlfilepath';
|
2022-05-06 09:09:56 +00:00
|
|
|
$testModule = new ImageModule( [
|
2017-03-17 02:14:05 +00:00
|
|
|
'localBasePath' => $basePath,
|
|
|
|
|
'remoteBasePath' => 'blahblah',
|
|
|
|
|
'prefix' => 'foo',
|
|
|
|
|
'images' => [
|
2022-05-06 09:09:56 +00:00
|
|
|
'eye' => new FilePath( 'eye.svg', $filePath, 'rlfilepath' ),
|
2017-03-17 02:14:05 +00:00
|
|
|
'flag' => [
|
|
|
|
|
'file' => [
|
2022-05-06 09:09:56 +00:00
|
|
|
'ltr' => new FilePath( 'flag-ltr.svg', $filePath, 'rlfilepath' ),
|
|
|
|
|
'rtl' => new FilePath( 'flag-rtl.svg', $filePath, 'rlfilepath' ),
|
2017-03-17 02:14:05 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
] );
|
2022-07-13 18:08:43 +00:00
|
|
|
$testModule->setName( 'testModule' );
|
2022-05-06 09:09:56 +00:00
|
|
|
$expectedModule = new ImageModule( [
|
2017-03-17 02:14:05 +00:00
|
|
|
'localBasePath' => $filePath,
|
|
|
|
|
'remoteBasePath' => 'rlfilepath',
|
|
|
|
|
'prefix' => 'foo',
|
|
|
|
|
'images' => [
|
|
|
|
|
'eye' => 'eye.svg',
|
|
|
|
|
'flag' => [
|
|
|
|
|
'file' => [
|
|
|
|
|
'ltr' => 'flag-ltr.svg',
|
|
|
|
|
'rtl' => 'flag-rtl.svg',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
] );
|
2022-07-13 18:08:43 +00:00
|
|
|
$expectedModule->setName( 'testModule' );
|
2017-03-17 02:14:05 +00:00
|
|
|
|
|
|
|
|
$context = $this->getResourceLoaderContext();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedModule->getModuleContent( $context ),
|
|
|
|
|
$testModule->getModuleContent( $context ),
|
|
|
|
|
"Using ResourceLoaderFilePath works correctly"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 17:08:11 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider providerGetModules
|
|
|
|
|
*/
|
|
|
|
|
public function testGetStyles( $module, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = new ImageModuleTestable(
|
2015-09-26 19:48:17 +00:00
|
|
|
$module,
|
|
|
|
|
__DIR__ . '/../../data/resourceloader'
|
|
|
|
|
);
|
2015-03-27 17:08:11 +00:00
|
|
|
$styles = $module->getStyles( $this->getResourceLoaderContext() );
|
|
|
|
|
$this->assertEquals( $expected, $styles['all'] );
|
|
|
|
|
}
|
2016-08-29 23:35:55 +00:00
|
|
|
|
|
|
|
|
public function testContext() {
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( new EmptyResourceLoader(), new FauxRequest() );
|
2016-08-29 23:35:55 +00:00
|
|
|
$this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [
|
2016-08-29 23:35:55 +00:00
|
|
|
'image' => 'example',
|
|
|
|
|
] ) );
|
|
|
|
|
$this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( new EmptyResourceLoader(), new FauxRequest( [
|
2016-08-29 23:35:55 +00:00
|
|
|
'modules' => 'unknown',
|
|
|
|
|
'image' => 'example',
|
|
|
|
|
] ) );
|
|
|
|
|
$this->assertFalse( $context->getImageObj(), 'Not an image module' );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( 'test', [
|
2022-05-06 09:09:56 +00:00
|
|
|
'class' => ImageModule::class,
|
2016-08-29 23:35:55 +00:00
|
|
|
'prefix' => 'test',
|
|
|
|
|
'images' => [ 'example' => 'example.png' ],
|
|
|
|
|
] );
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( $rl, new FauxRequest( [
|
2016-08-29 23:35:55 +00:00
|
|
|
'modules' => 'test',
|
|
|
|
|
'image' => 'unknown',
|
|
|
|
|
] ) );
|
|
|
|
|
$this->assertFalse( $context->getImageObj(), 'Unknown image' );
|
|
|
|
|
|
|
|
|
|
$rl = new EmptyResourceLoader();
|
|
|
|
|
$rl->register( 'test', [
|
2022-05-06 09:09:56 +00:00
|
|
|
'class' => ImageModule::class,
|
2016-08-29 23:35:55 +00:00
|
|
|
'prefix' => 'test',
|
|
|
|
|
'images' => [ 'example' => 'example.png' ],
|
|
|
|
|
] );
|
2022-05-06 09:09:56 +00:00
|
|
|
$context = new Context( $rl, new FauxRequest( [
|
2016-08-29 23:35:55 +00:00
|
|
|
'modules' => 'test',
|
|
|
|
|
'image' => 'example',
|
|
|
|
|
] ) );
|
2022-05-06 09:09:56 +00:00
|
|
|
$this->assertInstanceOf( Image::class, $context->getImageObj() );
|
2016-08-29 23:35:55 +00:00
|
|
|
}
|
2017-03-15 19:09:17 +00:00
|
|
|
|
|
|
|
|
public static function providerGetStyleDeclarations() {
|
|
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
false,
|
2023-02-07 23:31:01 +00:00
|
|
|
'background-image: url(original.svg);',
|
2017-03-15 19:09:17 +00:00
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'data:image/svg+xml',
|
2023-02-07 23:31:01 +00:00
|
|
|
'background-image: url(data:image/svg+xml);',
|
2017-03-15 19:09:17 +00:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providerGetStyleDeclarations
|
|
|
|
|
*/
|
|
|
|
|
public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
|
2022-05-06 09:09:56 +00:00
|
|
|
$module = TestingAccessWrapper::newFromObject( new ImageModule() );
|
2017-03-15 19:09:17 +00:00
|
|
|
$context = $this->getResourceLoaderContext();
|
|
|
|
|
$image = $this->getImageMock( $context, $dataUriReturnValue );
|
|
|
|
|
|
|
|
|
|
$styles = $module->getStyleDeclarations(
|
|
|
|
|
$context,
|
|
|
|
|
$image,
|
|
|
|
|
'load.php'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $styles );
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
private function getImageMock( Context $context, $dataUriReturnValue ) {
|
2022-07-14 12:42:07 +00:00
|
|
|
$image = $this->createMock( Image::class );
|
2017-03-15 19:09:17 +00:00
|
|
|
$image->method( 'getDataUri' )
|
2019-09-24 22:48:48 +00:00
|
|
|
->willReturn( $dataUriReturnValue );
|
|
|
|
|
$image->method( 'getUrl' )
|
2022-06-05 23:39:02 +00:00
|
|
|
->willReturnMap( [
|
2017-03-15 19:09:17 +00:00
|
|
|
[ $context, 'load.php', null, 'original', 'original.svg' ],
|
|
|
|
|
[ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
|
2022-06-05 23:39:02 +00:00
|
|
|
] );
|
2017-03-15 19:09:17 +00:00
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
|
}
|
2015-03-27 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
class ImageModuleTestable extends ImageModule {
|
2015-03-27 17:08:11 +00:00
|
|
|
/**
|
|
|
|
|
* Replace with a stub to make test cases easier to write.
|
2021-01-16 19:44:17 +00:00
|
|
|
* @inheritDoc
|
2015-03-27 17:08:11 +00:00
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function getCssDeclarations( $primary, $fallback ): array {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ '...' ];
|
2015-03-27 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
}
|