4 * @group ResourceLoader
6 class ResourceLoaderImageModuleTest
extends ResourceLoaderTestCase
{
8 public static $commonImageData = [
11 'file' => 'remove.svg',
12 'variants' => [ 'destructive' ],
22 'ltr' => 'help-ltr.svg',
23 'rtl' => 'help-rtl.svg',
25 'he' => 'help-ltr.svg',
31 'default' => 'bold-a.svg',
34 'ar,de' => 'bold-f.svg',
40 public static $commonImageVariants = [
56 public static function providerGetModules() {
60 'class' => 'ResourceLoaderImageModule',
61 'prefix' => 'oo-ui-icon',
62 'variants' => self
::$commonImageVariants,
63 'images' => self
::$commonImageData,
68 .oo-ui-icon-add-invert {
74 .oo-ui-icon-remove-invert {
77 .oo-ui-icon-remove-destructive {
83 .oo-ui-icon-next-invert {
89 .oo-ui-icon-help-invert {
95 .oo-ui-icon-bold-invert {
101 'class' => 'ResourceLoaderImageModule',
102 'selectorWithoutVariant' => '.mw-ui-icon-{name}:after, .mw-ui-icon-{name}:before',
103 'selectorWithVariant' =>
104 '.mw-ui-icon-{name}-{variant}:after, .mw-ui-icon-{name}-{variant}:before',
105 'variants' => self
::$commonImageVariants,
106 'images' => self
::$commonImageData,
108 '.mw-ui-icon-add:after, .mw-ui-icon-add:before {
111 .mw-ui-icon-add-invert:after, .mw-ui-icon-add-invert:before {
114 .mw-ui-icon-remove:after, .mw-ui-icon-remove:before {
117 .mw-ui-icon-remove-invert:after, .mw-ui-icon-remove-invert:before {
120 .mw-ui-icon-remove-destructive:after, .mw-ui-icon-remove-destructive:before {
123 .mw-ui-icon-next:after, .mw-ui-icon-next:before {
126 .mw-ui-icon-next-invert:after, .mw-ui-icon-next-invert:before {
129 .mw-ui-icon-help:after, .mw-ui-icon-help:before {
132 .mw-ui-icon-help-invert:after, .mw-ui-icon-help-invert:before {
135 .mw-ui-icon-bold:after, .mw-ui-icon-bold:before {
138 .mw-ui-icon-bold-invert:after, .mw-ui-icon-bold-invert:before {
146 * @dataProvider providerGetModules
147 * @covers ResourceLoaderImageModule::getStyles
149 public function testGetStyles( $module, $expected ) {
150 $module = new ResourceLoaderImageModuleTestable(
152 __DIR__
. '/../../data/resourceloader'
154 $styles = $module->getStyles( $this->getResourceLoaderContext() );
155 $this->assertEquals( $expected, $styles['all'] );
159 * @covers ResourceLoaderContext::getImageObj
161 public function testContext() {
162 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() );
163 $this->assertFalse( $context->getImageObj(), 'Missing image parameter' );
165 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
166 'image' => 'example',
168 $this->assertFalse( $context->getImageObj(), 'Missing module parameter' );
170 $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest( [
171 'modules' => 'unknown',
172 'image' => 'example',
174 $this->assertFalse( $context->getImageObj(), 'Not an image module' );
176 $rl = new EmptyResourceLoader();
177 $rl->register( 'test', [
178 'class' => ResourceLoaderImageModule
::class,
180 'images' => [ 'example' => 'example.png' ],
182 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
184 'image' => 'unknown',
186 $this->assertFalse( $context->getImageObj(), 'Unknown image' );
188 $rl = new EmptyResourceLoader();
189 $rl->register( 'test', [
190 'class' => ResourceLoaderImageModule
::class,
192 'images' => [ 'example' => 'example.png' ],
194 $context = new ResourceLoaderContext( $rl, new FauxRequest( [
196 'image' => 'example',
198 $this->assertInstanceOf( ResourceLoaderImage
::class, $context->getImageObj() );
201 public static function providerGetStyleDeclarations() {
206 background-image: url(rasterized.png);
207 background-image: linear-gradient(transparent, transparent), url(original.svg);
208 background-image: -o-linear-gradient(transparent, transparent), url(rasterized.png);
212 'data:image/svg+xml',
214 background-image: url(rasterized.png);
215 background-image: linear-gradient(transparent, transparent), url(data:image/svg+xml);
216 background-image: -o-linear-gradient(transparent, transparent), url(rasterized.png);
224 * @dataProvider providerGetStyleDeclarations
225 * @covers ResourceLoaderImageModule::getStyleDeclarations
227 public function testGetStyleDeclarations( $dataUriReturnValue, $expected ) {
228 $module = TestingAccessWrapper
::newFromObject( new ResourceLoaderImageModule() );
229 $context = $this->getResourceLoaderContext();
230 $image = $this->getImageMock( $context, $dataUriReturnValue );
232 $styles = $module->getStyleDeclarations(
238 $this->assertEquals( $expected, $styles );
241 private function getImageMock( ResourceLoaderContext
$context, $dataUriReturnValue ) {
242 $image = $this->getMockBuilder( 'ResourceLoaderImage' )
243 ->disableOriginalConstructor()
245 $image->method( 'getDataUri' )
246 ->will( $this->returnValue( $dataUriReturnValue ) );
247 $image->expects( $this->any() )
249 ->will( $this->returnValueMap( [
250 [ $context, 'load.php', null, 'original', 'original.svg' ],
251 [ $context, 'load.php', null, 'rasterized', 'rasterized.png' ],
258 class ResourceLoaderImageModuleTestable
extends ResourceLoaderImageModule
{
260 * Replace with a stub to make test cases easier to write.
262 protected function getCssDeclarations( $primary, $fallback ) {