Merge "Make ExternalStoreDB "wiki" context override the server "dbname" field"
[lhc/web/wiklou.git] / tests / phpunit / includes / resourceloader / ResourceLoaderSkinModuleTest.php
1 <?php
2
3 /**
4 * @group ResourceLoader
5 */
6 class ResourceLoaderSkinModuleTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9
10 public static function provideGetStyles() {
11 // phpcs:disable Generic.Files.LineLength
12 return [
13 [
14 'parent' => [],
15 'logo' => '/logo.png',
16 'expected' => [
17 'all' => [ '.mw-wiki-logo { background-image: url(/logo.png); }' ],
18 ],
19 ],
20 [
21 'parent' => [
22 'screen' => '.example {}',
23 ],
24 'logo' => '/logo.png',
25 'expected' => [
26 'screen' => [ '.example {}' ],
27 'all' => [ '.mw-wiki-logo { background-image: url(/logo.png); }' ],
28 ],
29 ],
30 [
31 'parent' => [],
32 'logo' => [
33 '1x' => '/logo.png',
34 '1.5x' => '/logo@1.5x.png',
35 '2x' => '/logo@2x.png',
36 ],
37 'expected' => [
38 'all' => [ <<<CSS
39 .mw-wiki-logo { background-image: url(/logo.png); }
40 CSS
41 ],
42 '(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (min-resolution: 1.5dppx), (min-resolution: 144dpi)' => [ <<<CSS
43 .mw-wiki-logo { background-image: url(/logo@1.5x.png);background-size: 135px auto; }
44 CSS
45 ],
46 '(-webkit-min-device-pixel-ratio: 2), (min--moz-device-pixel-ratio: 2), (min-resolution: 2dppx), (min-resolution: 192dpi)' => [ <<<CSS
47 .mw-wiki-logo { background-image: url(/logo@2x.png);background-size: 135px auto; }
48 CSS
49 ],
50 ],
51 ],
52 [
53 'parent' => [],
54 'logo' => [
55 '1x' => '/logo.png',
56 'svg' => '/logo.svg',
57 ],
58 'expected' => [
59 'all' => [ <<<CSS
60 .mw-wiki-logo { background-image: url(/logo.png); }
61 CSS
62 , <<<CSS
63 .mw-wiki-logo { background-image: -webkit-linear-gradient(transparent, transparent), url(/logo.svg); background-image: linear-gradient(transparent, transparent), url(/logo.svg);background-size: 135px auto; }
64 CSS
65 ],
66 ],
67 ],
68 ];
69 // phpcs:enable
70 }
71
72 /**
73 * @dataProvider provideGetStyles
74 * @covers ResourceLoaderSkinModule
75 */
76 public function testGetStyles( $parent, $logo, $expected ) {
77 $module = $this->getMockBuilder( ResourceLoaderSkinModule::class )
78 ->disableOriginalConstructor()
79 ->setMethods( [ 'readStyleFiles', 'getConfig', 'getLogoData' ] )
80 ->getMock();
81 $module->expects( $this->once() )->method( 'readStyleFiles' )
82 ->willReturn( $parent );
83 $module->expects( $this->once() )->method( 'getConfig' )
84 ->willReturn( new HashConfig() );
85 $module->expects( $this->once() )->method( 'getLogoData' )
86 ->willReturn( $logo );
87
88 $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
89 ->disableOriginalConstructor()->getMock();
90
91 $this->assertEquals(
92 $expected,
93 $module->getStyles( $ctx )
94 );
95 }
96
97 /**
98 * @covers ResourceLoaderSkinModule::isKnownEmpty
99 */
100 public function testIsKnownEmpty() {
101 $module = $this->getMockBuilder( ResourceLoaderSkinModule::class )
102 ->disableOriginalConstructor()->setMethods( null )->getMock();
103 $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
104 ->disableOriginalConstructor()->getMock();
105
106 $this->assertFalse( $module->isKnownEmpty( $ctx ) );
107 }
108
109 /**
110 * @dataProvider provideGetLogo
111 * @covers ResourceLoaderSkinModule::getLogo
112 */
113 public function testGetLogo( $config, $expected, $baseDir = null ) {
114 if ( $baseDir ) {
115 $oldIP = $GLOBALS['IP'];
116 $GLOBALS['IP'] = $baseDir;
117 $teardown = new Wikimedia\ScopedCallback( function () use ( $oldIP ) {
118 $GLOBALS['IP'] = $oldIP;
119 } );
120 }
121
122 $this->assertEquals(
123 $expected,
124 ResourceLoaderSkinModule::getLogo( new HashConfig( $config ) )
125 );
126 }
127
128 public function provideGetLogo() {
129 return [
130 'simple' => [
131 'config' => [
132 'ResourceBasePath' => '/w',
133 'Logo' => '/img/default.png',
134 'LogoHD' => false,
135 ],
136 'expected' => '/img/default.png',
137 ],
138 'default and 2x' => [
139 'config' => [
140 'ResourceBasePath' => '/w',
141 'Logo' => '/img/default.png',
142 'LogoHD' => [
143 '2x' => '/img/two-x.png',
144 ],
145 ],
146 'expected' => [
147 '1x' => '/img/default.png',
148 '2x' => '/img/two-x.png',
149 ],
150 ],
151 'default and all HiDPIs' => [
152 'config' => [
153 'ResourceBasePath' => '/w',
154 'Logo' => '/img/default.png',
155 'LogoHD' => [
156 '1.5x' => '/img/one-point-five.png',
157 '2x' => '/img/two-x.png',
158 ],
159 ],
160 'expected' => [
161 '1x' => '/img/default.png',
162 '1.5x' => '/img/one-point-five.png',
163 '2x' => '/img/two-x.png',
164 ],
165 ],
166 'default and SVG' => [
167 'config' => [
168 'ResourceBasePath' => '/w',
169 'Logo' => '/img/default.png',
170 'LogoHD' => [
171 'svg' => '/img/vector.svg',
172 ],
173 ],
174 'expected' => [
175 '1x' => '/img/default.png',
176 'svg' => '/img/vector.svg',
177 ],
178 ],
179 'everything' => [
180 'config' => [
181 'ResourceBasePath' => '/w',
182 'Logo' => '/img/default.png',
183 'LogoHD' => [
184 '1.5x' => '/img/one-point-five.png',
185 '2x' => '/img/two-x.png',
186 'svg' => '/img/vector.svg',
187 ],
188 ],
189 'expected' => [
190 '1x' => '/img/default.png',
191 'svg' => '/img/vector.svg',
192 ],
193 ],
194 'versioned url' => [
195 'config' => [
196 'ResourceBasePath' => '/w',
197 'Logo' => '/w/test.jpg',
198 'LogoHD' => false,
199 'UploadPath' => '/w/images',
200 ],
201 'expected' => '/w/test.jpg?edcf2',
202 'baseDir' => dirname( dirname( __DIR__ ) ) . '/data/media',
203 ],
204 ];
205 }
206 }