Debian Codegraph @

tests/phpunit/includes/ResourceLoader/SkinModuleTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\ResourceLoader;
4
5 use Generator;
6 use InvalidArgumentException;
7 use MediaWiki\Config\HashConfig;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\ResourceLoader\Context;
10 use MediaWiki\ResourceLoader\FilePath;
11 use MediaWiki\ResourceLoader\SkinModule;
12 use ReflectionClass;
13 use Wikimedia\TestingAccessWrapper;
14
15 /**
16 * @group ResourceLoader
17 * @covers \MediaWiki\ResourceLoader\SkinModule
18 */
19 class SkinModuleTest extends ResourceLoaderTestCase {
20 public static function provideApplyFeaturesCompatibility() {
21 return [
22 'Alias for unset target (content-thumbnails)' => [
23 [
24 'content-thumbnails' => true,
25 ],
26 [
27 'content-media' => true,
28 ],
29 true
30 ],
31 'Alias with conflict (content-thumbnails)' => [
32 [
33 'content-thumbnails' => true,
34 'content-media' => false,
35 ],
36 [
37 'content-media' => false,
38 ],
39 true
40 ],
41 'Alias that no-ops (legacy)' => [
42 [
43 'toc' => true,
44 'legacy' => true,
45 ],
46 [
47 'toc' => true,
48 ],
49 true
50 ],
51 'content-links enables content-links-external if unset' => [
52 [
53 'content-links' => true,
54 ],
55 [
56 'content-links-external' => true,
57 'content-links' => true,
58 ],
59 true
60 ],
61 'elements enables content-links if unset' => [
62 [
63 'elements' => true,
64 ],
65 [
66 'elements' => true,
67 'content-links' => true,
68 ],
69 true
70 ],
71 'content-links does not change content-links-external if set' => [
72 [
73 'content-links-external' => false,
74 'content-links' => true,
75 ],
76 [
77 'content-links-external' => false,
78 'content-links' => true,
79 ],
80 true
81 ],
82 'list-form does not add unwanted defaults (aliases)' => [
83 [
84 'content-links' => true,
85 'content-thumbnails' => true,
86 ],
87 [
88 'content-links' => true,
89 'content-media' => true,
90 ],
91 false
92 ],
93 'list-form does not add unwanted defaults (no aliases)' => [
94 [
95 'elements' => true,
96 ],
97 [
98 'elements' => true,
99 ],
100 false
101 ],
102 ];
103 }
104
105 /**
106 * @dataProvider provideApplyFeaturesCompatibility
107 */
108 public function testApplyFeaturesCompatibility( array $features, array $expected, bool $optInPolicy ) {
109 // Test protected method
110 $class = TestingAccessWrapper::newFromClass( SkinModule::class );
111 $actual = $class->applyFeaturesCompatibility( $features, $optInPolicy );
112 $this->assertEquals( $expected, $actual );
113 }
114
115 public static function provideGetAvailableLogos() {
116 return [
117 [
118 [
119 MainConfigNames::Logos => [],
120 MainConfigNames::Logo => '/logo.png',
121 ],
122 [
123 '1x' => '/logo.png',
124 ]
125 ],
126 [
127 [
128 MainConfigNames::Logos => [
129 'svg' => '/logo.svg',
130 '2x' => 'logo-2x.png'
131 ],
132 MainConfigNames::Logo => '/logo.png',
133 ],
134 [
135 'svg' => '/logo.svg',
136 '2x' => 'logo-2x.png',
137 '1x' => '/logo.png',
138 ]
139 ],
140 [
141 [
142 MainConfigNames::Logos => [
143 'wordmark' => [
144 'src' => '/logo-wordmark.png',
145 'width' => 100,
146 'height' => 15,
147 ],
148 '1x' => '/logo.png',
149 'svg' => '/logo.svg',
150 '2x' => 'logo-2x.png'
151 ],
152 ],
153 [
154 'wordmark' => [
155 'src' => '/logo-wordmark.png',
156 'width' => 100,
157 'height' => 15,
158 'style' => 'width: 6.25em; height: 0.9375em;',
159 ],
160 '1x' => '/logo.png',
161 'svg' => '/logo.svg',
162 '2x' => 'logo-2x.png',
163 ]
164 ]
165 ];
166 }
167
168 public static function provideGetLogoStyles() {
169 return [
170 [
171 'features' => [],
172 'logo' => '/logo.png',
173 'expected' => [
174 'all' => [ '.mw-wiki-logo { background-image: url(/logo.png); }' ],
175 ],
176 ],
177 [
178 'features' => [
179 'screen' => '.example {}',
180 ],
181 'logo' => '/logo.png',
182 'expected' => [
183 'screen' => '.example {}',
184 'all' => [ '.mw-wiki-logo { background-image: url(/logo.png); }' ],
185 ],
186 ],
187 [
188 'features' => [],
189 'logo' => [
190 '1x' => '/logo.png',
191 '1.5x' => '/[email protected]',
192 '2x' => '/[email protected]',
193 ],
194 'expected' => [
195 'all' => [
196 '.mw-wiki-logo { background-image: url(/logo.png); }',
197 ],
198 '(-webkit-min-device-pixel-ratio: 1.5), (min-resolution: 1.5dppx), (min-resolution: 144dpi)' => [
199 '.mw-wiki-logo { background-image: url(/[email protected]);background-size: 135px auto; }',
200 ],
201 '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 2dppx), (min-resolution: 192dpi)' => [
202 '.mw-wiki-logo { background-image: url(/[email protected]);background-size: 135px auto; }',
203 ],
204 ],
205 ],
206 [
207 'features' => [],
208 'logo' => [
209 '1x' => '/logo.png',
210 'svg' => '/logo.svg',
211 ],
212 'expected' => [
213 'all' => [
214 '.mw-wiki-logo { background-image: url(/logo.svg); }',
215 '.mw-wiki-logo { background-size: 135px auto; }',
216 ],
217 ],
218 ],
219 ];
220 // phpcs:enable
221 }
222
223 /**
224 * @dataProvider provideGetLogoStyles
225 */
226 public function testGenerateAndAppendLogoStyles( $features, $logo, $expected ) {
227 $module = $this->getMockBuilder( SkinModule::class )
228 ->onlyMethods( [ 'getLogoData' ] )
229 ->getMock();
230 $module->expects( $this->atLeast( 1 ) )->method( 'getLogoData' )
231 ->willReturn( $logo );
232 $module->setConfig( new HashConfig( [
233 MainConfigNames::ParserEnableLegacyMediaDOM => false,
234 ] + self::getSettings() ) );
235
236 $ctx = $this->createMock( Context::class );
237
238 $this->assertEquals(
239 $expected,
240 $module->generateAndAppendLogoStyles( $features, $ctx )
241 );
242 }
243
244 /**
245 * @dataProvider provideGetAvailableLogos
246 */
247 public function testGetAvailableLogos( $config, $expected ) {
248 $logos = SkinModule::getAvailableLogos( new HashConfig( $config ) );
249 $this->assertSame( $expected, $logos );
250 }
251
252 public function testGetAvailableLogosRuntimeException() {
253 $logos = SkinModule::getAvailableLogos( new HashConfig( [
254 MainConfigNames::Logo => false,
255 MainConfigNames::Logos => false,
256 ] ) );
257 $this->assertSame( [], $logos );
258 }
259
260 public function testIsKnownEmpty() {
261 $module = new SkinModule();
262 $ctx = $this->createMock( Context::class );
263
264 $this->assertFalse( $module->isKnownEmpty( $ctx ) );
265 }
266
267 /**
268 * @dataProvider provideGetLogoData
269 */
270 public function testGetLogoData( $config, $expected ) {
271 // Allow testing of protected method
272 $module = TestingAccessWrapper::newFromObject( new SkinModule() );
273
274 $this->assertEquals(
275 $expected,
276 $module->getLogoData( new HashConfig( $config ) )
277 );
278 }
279
280 public static function provideGetLogoData() {
281 return [
282 'wordmark' => [
283 'config' => [
284 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
285 MainConfigNames::ResourceBasePath => '/w',
286 MainConfigNames::Logos => [
287 '1x' => '/img/default.png',
288 'wordmark' => [
289 'src' => '/img/wordmark.png',
290 'width' => 120,
291 'height' => 20,
292 ],
293 ],
294 ],
295 'expected' => '/img/default.png',
296 ],
297 'simple' => [
298 'config' => [
299 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
300 MainConfigNames::ResourceBasePath => '/w',
301 MainConfigNames::Logos => [
302 '1x' => '/img/default.png',
303 ],
304 ],
305 'expected' => '/img/default.png',
306 ],
307 'default and 2x' => [
308 'config' => [
309 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
310 MainConfigNames::ResourceBasePath => '/w',
311 MainConfigNames::Logos => [
312 '1x' => '/img/default.png',
313 '2x' => '/img/two-x.png',
314 ],
315 ],
316 'expected' => [
317 '1x' => '/img/default.png',
318 '2x' => '/img/two-x.png',
319 ],
320 ],
321 'default and all HiDPIs' => [
322 'config' => [
323 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
324 MainConfigNames::ResourceBasePath => '/w',
325 MainConfigNames::Logos => [
326 '1x' => '/img/default.png',
327 '1.5x' => '/img/one-point-five.png',
328 '2x' => '/img/two-x.png',
329 ],
330 ],
331 'expected' => [
332 '1x' => '/img/default.png',
333 '1.5x' => '/img/one-point-five.png',
334 '2x' => '/img/two-x.png',
335 ],
336 ],
337 'default and SVG' => [
338 'config' => [
339 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
340 MainConfigNames::ResourceBasePath => '/w',
341 MainConfigNames::Logos => [
342 '1x' => '/img/default.png',
343 'svg' => '/img/vector.svg',
344 ],
345 ],
346 'expected' => [
347 '1x' => '/img/default.png',
348 'svg' => '/img/vector.svg',
349 ],
350 ],
351 'everything' => [
352 'config' => [
353 MainConfigNames::BaseDirectory => MW_INSTALL_PATH,
354 MainConfigNames::ResourceBasePath => '/w',
355 MainConfigNames::Logos => [
356 '1x' => '/img/default.png',
357 '1.5x' => '/img/one-point-five.png',
358 '2x' => '/img/two-x.png',
359 'svg' => '/img/vector.svg',
360 ],
361 ],
362 'expected' => [
363 '1x' => '/img/default.png',
364 'svg' => '/img/vector.svg',
365 ],
366 ],
367 'versioned url' => [
368 'config' => [
369 MainConfigNames::BaseDirectory => dirname( dirname( __DIR__ ) ) . '/data/media',
370 MainConfigNames::ResourceBasePath => '/w',
371 MainConfigNames::UploadPath => '/w/images',
372 MainConfigNames::Logos => [
373 '1x' => '/w/test.jpg',
374 ],
375 ],
376 'expected' => '/w/test.jpg?edcf2',
377 ],
378 ];
379 }
380
381 /**
382 * @dataProvider providePreloadLinks
383 */
384 public function testPreloadLinkHeaders( $config, $lang, $result ) {
385 $ctx = $this->createMock( Context::class );
386 $ctx->method( 'getLanguage' )->willReturn( $lang );
387 $module = new SkinModule();
388 $module->setConfig( new HashConfig( $config + [
389 MainConfigNames::BaseDirectory => '/dummy',
390 MainConfigNames::ResourceBasePath => '/w',
391 MainConfigNames::Logo => false,
392 ] + self::getSettings() ) );
393
394 $this->assertEquals( [ $result ], $module->getHeaders( $ctx ) );
395 }
396
397 public static function providePreloadLinks() {
398 return [
399 [
400 [
401 MainConfigNames::Logos => [
402 '1x' => '/img/default.png',
403 '1.5x' => '/img/one-point-five.png',
404 '2x' => '/img/two-x.png',
405 ],
406 ],
407 'en',
408 'Link: </img/default.png>;rel=preload;as=image;media=' .
409 'not all and (min-resolution: 1.5dppx),' .
410 '</img/one-point-five.png>;rel=preload;as=image;media=' .
411 '(min-resolution: 1.5dppx) and (max-resolution: 1.999999dppx),' .
412 '</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
413 ],
414 [
415 [
416 MainConfigNames::Logos => [ '1x' => '/img/default.png' ],
417 ],
418 'en',
419 'Link: </img/default.png>;rel=preload;as=image'
420 ],
421 [
422 [
423 MainConfigNames::Logos => [
424 '1x' => '/img/default.png',
425 '2x' => '/img/two-x.png',
426 ],
427 ],
428 'en',
429 'Link: </img/default.png>;rel=preload;as=image;media=' .
430 'not all and (min-resolution: 2dppx),' .
431 '</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
432 ],
433 [
434 [
435 MainConfigNames::Logos => [
436 '1x' => '/img/default.png',
437 'svg' => '/img/vector.svg',
438 ],
439 ],
440 'en',
441 'Link: </img/vector.svg>;rel=preload;as=image'
442
443 ],
444 [
445 [
446 MainConfigNames::BaseDirectory => dirname( dirname( __DIR__ ) ) . '/data/media',
447 MainConfigNames::Logos => [ '1x' => '/w/test.jpg' ],
448 MainConfigNames::UploadPath => '/w/images',
449 ],
450 'en',
451 'Link: </w/test.jpg?edcf2>;rel=preload;as=image',
452 ],
453 [
454 [
455 MainConfigNames::Logos => [
456 '1x' => '/img/default.png',
457 '1.5x' => '/img/one-point-five.png',
458 '2x' => '/img/two-x.png',
459 'variants' => [
460 'zh-hans' => [
461 '1x' => '/img/default-zh-hans.png',
462 '1.5x' => '/img/one-point-five-zh-hans.png',
463 ]
464 ]
465 ],
466 ],
467 'zh-hans',
468 'Link: </img/default-zh-hans.png>;rel=preload;as=image;media=' .
469 'not all and (min-resolution: 1.5dppx),' .
470 '</img/one-point-five-zh-hans.png>;rel=preload;as=image;media=' .
471 '(min-resolution: 1.5dppx) and (max-resolution: 1.999999dppx),' .
472 '</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
473 ],
474 ];
475 }
476
477 public function testNoPreloadLogos() {
478 $module = new SkinModule( [ 'features' => [ 'logo' => false ] ] );
479 $context =
480 $this->createMock( Context::class );
481 $preloadLinks = $module->getPreloadLinks( $context );
482 $this->assertArrayEquals( [], $preloadLinks );
483 }
484
485 public function testPreloadLogos() {
486 $module = new SkinModule();
487 $module->setConfig( self::getMinimalConfig() );
488 $context = $this->createMock( Context::class );
489
490 $preloadLinks = $module->getPreloadLinks( $context );
491 $this->assertNotSameSize( [], $preloadLinks );
492 }
493
494 /**
495 * Covers SkinModule::FEATURE_FILES, but not annotatable.
496 *
497 * @dataProvider provideFeatureFiles
498 * @param string $file
499 */
500 public function testFeatureFilesExist( string $file ): void {
501 $this->assertFileExists( $file );
502 }
503
504 public static function provideFeatureFiles(): Generator {
505 global $IP;
506
507 $featureFiles = ( new ReflectionClass( SkinModule::class ) )
508 ->getConstant( 'FEATURE_FILES' );
509
510 foreach ( $featureFiles as $feature => $files ) {
511 foreach ( $files as $media => $stylesheets ) {
512 foreach ( $stylesheets as $stylesheet ) {
513 yield "$feature: $media: $stylesheet" => [ "$IP/$stylesheet" ];
514 }
515 }
516 }
517 }
518
519 public static function getSkinFeaturePath( $feature, $mediaType ) {
520 global $IP;
521 $featureFiles = ( new ReflectionClass( SkinModule::class ) )->getConstant( 'FEATURE_FILES' );
522 return new FilePath( $featureFiles[ $feature ][ $mediaType ][ 0 ], $IP, '/w' );
523 }
524
525 public static function provideGetFeatureFilePathsOrder() {
526 return [
527 [
528 'The "logo" skin-feature is loaded when the "features" key is absent',
529 [],
530 [
531 'all' => [ self::getSkinFeaturePath( 'logo', 'all' ) ],
532 'print' => [ self::getSkinFeaturePath( 'logo', 'print' ) ],
533 ],
534 ],
535 [
536 'The "normalize" skin-feature is always output first',
537 [
538 'features' => [ 'elements', 'normalize' ],
539 ],
540 [
541 'all' => [ self::getSkinFeaturePath( 'normalize', 'all' ) ],
542 'screen' => [ self::getSkinFeaturePath( 'elements', 'screen' ) ],
543 'print' => [ self::getSkinFeaturePath( 'elements', 'print' ) ],
544 ],
545 ],
546 [
547 'Empty media query blocks are not included in output',
548 [
549 'features' => [
550 'accessibility' => false,
551 'content-body' => false,
552 'interface-core' => false,
553 'toc' => false
554 ],
555 ],
556 [],
557 ],
558 [
559 'Empty "features" key outputs default skin-features',
560 [
561 'features' => [],
562 ],
563 [
564 'all' => [
565 self::getSkinFeaturePath( 'accessibility', 'all' ),
566 self::getSkinFeaturePath( 'toc', 'all' )
567 ],
568 'screen' => [
569 self::getSkinFeaturePath( 'content-body', 'screen' ),
570 self::getSkinFeaturePath( 'interface-core', 'screen' ),
571 self::getSkinFeaturePath( 'toc', 'screen' ),
572 ],
573 'print' => [
574 self::getSkinFeaturePath( 'content-body', 'print' ),
575 self::getSkinFeaturePath( 'interface-core', 'print' ),
576 self::getSkinFeaturePath( 'toc', 'print' )
577 ]
578 ],
579 ],
580 [
581 'skin-features are output in the order defined in SkinModule.php',
582 [
583 'features' => [ 'interface-message-box', 'normalize', 'accessibility' ],
584 ],
585 [
586 'all' => [
587 self::getSkinFeaturePath( 'accessibility', 'all' ),
588 self::getSkinFeaturePath( 'normalize', 'all' ),
589 self::getSkinFeaturePath( 'interface-message-box', 'all' )
590 ],
591 ]
592 ]
593 ];
594 }
595
596 /**
597 * @dataProvider provideGetFeatureFilePathsOrder
598 * @param string $msg to show for debugging
599 * @param array $skinModuleConfig
600 * @param array $expectedStyleOrder
601 */
602 public function testGetFeatureFilePathsOrder(
603 $msg, $skinModuleConfig, $expectedStyleOrder
604 ): void {
605 $module = new SkinModule( $skinModuleConfig );
606 $module->setConfig( self::getMinimalConfig() );
607
608 $actual = $module->getFeatureFilePaths();
609
610 $this->assertEquals(
611 array_values( $expectedStyleOrder ),
612 array_values( $actual )
613 );
614 }
615
616 public static function provideInvalidFeatures() {
617 yield 'listed unknown' => [
618 [ 'logo', 'unknown' ],
619 ];
620
621 yield 'enabled unknown' => [
622 [
623 'logo' => true,
624 'toc' => false,
625 'unknown' => true,
626 ],
627 ];
628
629 yield 'disabled unknown' => [
630 [
631 'logo' => true,
632 'toc' => false,
633 'unknown' => false,
634 ],
635 ];
636 }
637
638 /**
639 * @dataProvider provideInvalidFeatures
640 */
641 public function testConstructInvalidFeatures( array $features ) {
642 $this->expectException( InvalidArgumentException::class );
643 $this->expectExceptionMessage( "Feature 'unknown' is not recognised" );
644 $module = new SkinModule( [
645 'features' => $features,
646 ] );
647 }
648 }