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',
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',
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(
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',
170 $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
172 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
173 'modules' => 'unknown',
174 'image' => 'example',
176 $this->assertFalse( $context->getImageObj(), 'Not an image module' );
178 $rl = new EmptyResourceLoader();
179 $rl->register( 'test', [
180 'class' => ResourceLoaderImageModule
::class,
182 'images' => [ 'example' => 'example.png' ],
184 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
186 'image' => 'unknown',
188 $this->assertFalse( $context->getImageObj(), 'Unknown image' );
190 $rl = new EmptyResourceLoader();
191 $rl->register( 'test', [
192 'class' => ResourceLoaderImageModule
::class,
194 'images' => [ 'example' => 'example.png' ],
196 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
198 'image' => 'example',
200 $this->assertInstanceOf( ResourceLoaderImage
::class, $context->getImageObj() );
203 public static function providerGetStyleDeclarations() {
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);
214 'data:image/svg+xml',
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);
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(
240 $this->assertEquals( $expected, $styles );
243 private function getImageMock( ResourceLoaderContext
$context, $dataUriReturnValue ) {
244 $image = $this->getMockBuilder( 'ResourceLoaderImage' )
245 ->disableOriginalConstructor()
247 $image->method( 'getDataUri' )
248 ->will( $this->returnValue( $dataUriReturnValue ) );
249 $image->expects( $this->any() )
251 ->will( $this->returnValueMap( [
252 [ $context, 'load.php', null, 'original', 'original.svg' ],
253 [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
260 class ResourceLoaderImageModuleTestable
extends ResourceLoaderImageModule
{
262 * Replace with a stub to make test cases easier to write.
264 protected function getCssDeclarations( $primary, $fallback ) {