Debian Codegraph @

skins/Vector/tests/phpunit/integration/VectorHooksTest.php

raw symbols SCIP
1 <?php
2 /*
3 * @file
4 * @ingroup skins
5 */
6
7 namespace MediaWiki\Skins\Vector\Tests\Integration;
8
9 use MediaWiki\Config\HashConfig;
10 use MediaWiki\Context\RequestContext;
11 use MediaWiki\Request\FauxRequest;
12 use MediaWiki\ResourceLoader\Context;
13 use MediaWiki\Skins\Vector\Constants;
14 use MediaWiki\Skins\Vector\FeatureManagement\Requirements\LimitedWidthContentRequirement;
15 use MediaWiki\Skins\Vector\Hooks;
16 use MediaWiki\Skins\Vector\SkinVector22;
17 use MediaWiki\Skins\Vector\SkinVectorLegacy;
18 use MediaWiki\Title\Title;
19 use MediaWiki\User\Options\UserOptionsManager;
20 use MediaWiki\User\User;
21 use MediaWikiIntegrationTestCase;
22 use ReflectionMethod;
23 use RuntimeException;
24
25 /**
26 * Integration tests for Vector Hooks.
27 *
28 * @group Vector
29 * @coversDefaultClass \MediaWiki\Skins\Vector\Hooks
30 */
31 class VectorHooksTest extends MediaWikiIntegrationTestCase {
32
33 /**
34 * @param bool $excludeMainPage
35 * @param array $excludeNamespaces
36 * @param array $include
37 * @param array $querystring
38 * @return array
39 */
40 private static function makeMaxWidthConfig(
41 $excludeMainPage,
42 $excludeNamespaces = [],
43 $include = [],
44 $querystring = []
45 ) {
46 return [
47 'exclude' => [
48 'mainpage' => $excludeMainPage,
49 'namespaces' => $excludeNamespaces,
50 'querystring' => $querystring,
51 ],
52 'include' => $include
53 ];
54 }
55
56 public static function provideGetActiveABTest() {
57 return [
58 [
59 [
60 'VectorWebABTestEnrollment' => [],
61 ],
62 []
63 ],
64 [
65 [
66 'VectorWebABTestEnrollment' => [
67 'name' => 'vector.sticky_header',
68 'enabled' => true,
69 'buckets' => [
70 'unsampled' => [
71 'samplingRate' => 1,
72 ],
73 'control' => [
74 'samplingRate' => 0
75 ],
76 'stickyHeaderEnabled' => [
77 'samplingRate' => 0
78 ],
79 'stickyHeaderDisabled' => [
80 'samplingRate' => 0
81 ],
82 ],
83 ],
84 ],
85 [
86 'name' => 'vector.sticky_header',
87 'enabled' => true,
88 'buckets' => [
89 'unsampled' => [
90 'samplingRate' => 1,
91 ],
92 'control' => [
93 'samplingRate' => 0
94 ],
95 'stickyHeaderEnabled' => [
96 'samplingRate' => 0
97 ],
98 'stickyHeaderDisabled' => [
99 'samplingRate' => 0
100 ],
101 ],
102 ]
103 ],
104 ];
105 }
106
107 public static function provideGetActiveABTestWithExceptions() {
108 return [
109 # Bad experiment (no buckets)
110 [
111 [
112 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
113 'VectorWebABTestEnrollment' => [
114 'name' => 'vector.sticky_header',
115 'enabled' => true,
116 ],
117 ]
118 ],
119 # Bad experiment (no unsampled bucket)
120 [
121 [
122 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
123 'VectorWebABTestEnrollment' => [
124 'name' => 'vector.sticky_header',
125 'enabled' => true,
126 'buckets' => [
127 'a' => [
128 'samplingRate' => 0
129 ],
130 ]
131 ],
132 ]
133 ],
134 # Bad experiment (wrong format)
135 [
136 [
137 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
138 'VectorWebABTestEnrollment' => [
139 'name' => 'vector.sticky_header',
140 'enabled' => true,
141 'buckets' => [
142 'unsampled' => 1,
143 ]
144 ],
145 ]
146 ],
147 # Bad experiment (samplingRate defined as string)
148 [
149 [
150 'VectorSearchApiUrl' => 'https://en.wikipedia.org/w/rest.php',
151 'VectorWebABTestEnrollment' => [
152 'name' => 'vector.sticky_header',
153 'enabled' => true,
154 'buckets' => [
155 'unsampled' => [
156 'samplingRate' => '1'
157 ],
158 ]
159 ],
160 ]
161 ],
162 ];
163 }
164
165 /**
166 * @covers ::shouldDisableMaxWidth
167 */
168 public static function providerShouldDisableMaxWidth() {
169 $excludeTalkFooConfig = self::makeMaxWidthConfig(
170 false,
171 [ NS_TALK ],
172 [ 'Talk:Foo' ],
173 []
174 );
175
176 return [
177 [
178 'No options, nothing disables max width',
179 [],
180 Title::makeTitle( NS_MAIN, 'Foo' ),
181 [],
182 false
183 ],
184 [
185 'Main page disables max width if exclude.mainpage set',
186 self::makeMaxWidthConfig( true ),
187 Title::newMainPage(),
188 [],
189 true
190 ],
191 [
192 'Namespaces can be excluded',
193 self::makeMaxWidthConfig( false, [ NS_CATEGORY ] ),
194 Title::makeTitle( NS_CATEGORY, 'Category' ),
195 [],
196 true
197 ],
198 [
199 'Namespaces are included if not excluded',
200 self::makeMaxWidthConfig( false, [ NS_CATEGORY ] ),
201 Title::makeTitle( NS_SPECIAL, 'SpecialPages' ),
202 [],
203 false
204 ],
205 [
206 'More than one namespace can be included',
207 self::makeMaxWidthConfig( false, [ NS_CATEGORY, NS_SPECIAL ] ),
208 Title::makeTitle( NS_SPECIAL, 'Specialpages' ),
209 [],
210 true
211 ],
212 [
213 'Can be disabled on history page',
214 self::makeMaxWidthConfig(
215 false,
216 [
217 /* no namespaces excluded */
218 ],
219 [
220 /* no includes */
221 ],
222 [ 'action' => 'history' ]
223 ),
224 Title::makeTitle( NS_MAIN, 'History page' ),
225 [ 'action' => 'history' ],
226 true
227 ],
228 [
229 'Can be disabled with a regex match.',
230 self::makeMaxWidthConfig(
231 false,
232 [
233 /* no namespaces excluded */
234 ],
235 [
236 /* no includes */
237 ],
238 [ 'foo' => '[0-9]+' ]
239 ),
240 Title::makeTitle( NS_MAIN, 'Test' ),
241 [ 'foo' => '1234' ],
242 true
243 ],
244 [
245 'Can be disabled with a regex match, also for falsy 0.',
246 self::makeMaxWidthConfig(
247 false,
248 [
249 /* no namespaces excluded */
250 ],
251 [
252 /* no includes */
253 ],
254 [ 'foo' => '[0-9]+' ]
255 ),
256 Title::makeTitle( NS_MAIN, 'Test' ),
257 [ 'foo' => '0' ],
258 true
259 ],
260 [
261 'Can be disabled with a non-regex wildcard (for backwards compatibility).',
262 self::makeMaxWidthConfig(
263 false,
264 [
265 /* no namespaces excluded */
266 ],
267 [
268 /* no includes */
269 ],
270 [ 'foo' => '*' ]
271 ),
272 Title::makeTitle( NS_MAIN, 'Test' ),
273 [ 'foo' => 'bar' ],
274 true
275 ],
276 [
277 'Include can override exclusions',
278 self::makeMaxWidthConfig(
279 false,
280 [ NS_CATEGORY, NS_SPECIAL ],
281 [ 'Special:Specialpages' ],
282 [ 'action' => 'history' ]
283 ),
284 Title::makeTitle( NS_SPECIAL, 'Specialpages' ),
285 [ 'action' => 'history' ],
286 true
287 ],
288 [
289 'Max width can be disabled on talk pages',
290 $excludeTalkFooConfig,
291 Title::makeTitle( NS_TALK, 'A talk page' ),
292 [],
293 true
294 ],
295 [
296 'includes can be used to override any page in a disabled namespace',
297 $excludeTalkFooConfig,
298 Title::makeTitle( NS_TALK, 'Foo' ),
299 [],
300 false
301 ],
302 [
303 'Excludes/includes are based on root title so should apply to subpages',
304 $excludeTalkFooConfig,
305 Title::makeTitle( NS_TALK, 'Foo/subpage' ),
306 [],
307 false
308 ]
309 ];
310 }
311
312 /**
313 * @todo move into MediaWiki\Skins\Vector\FeatureManagement\Requirements\LimitedWidthContentRequirement
314 * test in future.
315 * @covers MediaWiki\Skins\Vector\FeatureManagement\Requirements\LimitedWidthContentRequirement::isMet
316 * @dataProvider providerShouldDisableMaxWidth
317 */
318 public function testShouldDisableMaxWidth(
319 $msg,
320 $options,
321 $title,
322 $requestValues,
323 $shouldDisableMaxWidth
324 ) {
325 $requirement = new LimitedWidthContentRequirement(
326 new HashConfig( [ 'VectorMaxWidthOptions' => $options ] ),
327 new FauxRequest( $requestValues ),
328 $title
329 );
330 $this->assertSame(
331 !$shouldDisableMaxWidth,
332 $requirement->isMet(),
333 $msg
334 );
335 }
336
337 /**
338 * @covers ::getActiveABTest
339 * @dataProvider provideGetActiveABTest
340 */
341 public function testGetActiveABTest( $configData, $expected ) {
342 $config = new HashConfig( $configData );
343 $vectorConfig = Hooks::getActiveABTest(
344 $this->createMock( Context::class ),
345 $config
346 );
347
348 $this->assertSame(
349 $expected,
350 $vectorConfig
351 );
352 }
353
354 /**
355 * @covers ::getActiveABTest
356 * @dataProvider provideGetActiveABTestWithExceptions
357 */
358 public function testGetActiveABTestWithExceptions( $configData ) {
359 $config = new HashConfig( $configData );
360 $this->expectException( RuntimeException::class );
361 Hooks::getActiveABTest(
362 $this->createMock( Context::class ),
363 $config
364 );
365 }
366
367 /**
368 * @covers ::onLocalUserCreated
369 */
370 public function testOnLocalUserCreatedLegacy() {
371 $config = new HashConfig( [
372 'VectorDefaultSkinVersionForNewAccounts' => Constants::SKIN_VERSION_LEGACY,
373 ] );
374
375 $user = $this->createMock( User::class );
376 $userOptionsManager = $this->createMock( UserOptionsManager::class );
377 $userOptionsManager->expects( $this->once() )
378 ->method( 'setOption' )
379 ->with( $user, 'skin', Constants::SKIN_NAME_LEGACY );
380
381 // NOTE: Using $this->getServiceContainer()->getHookContainer()->run( ... )
382 // will instead call Echo's legacy hook as that is already registered which
383 // will break this test. Use Vector's hook handler instead.
384 ( new Hooks(
385 $config,
386 $userOptionsManager
387 ) )->onLocalUserCreated( $user, false );
388 }
389
390 /**
391 * @covers ::onLocalUserCreated
392 */
393 public function testOnLocalUserCreatedLatest() {
394 $config = new HashConfig( [
395 'VectorDefaultSkinVersionForNewAccounts' => Constants::SKIN_VERSION_LATEST,
396 ] );
397
398 $user = $this->createMock( User::class );
399 $userOptionsManager = $this->createMock( UserOptionsManager::class );
400 $userOptionsManager->expects( $this->once() )
401 ->method( 'setOption' )
402 ->with( $user, 'skin', Constants::SKIN_NAME_MODERN );
403
404 // NOTE: Using $this->getServiceContainer()->getHookContainer()->run( ... )
405 // will instead call Echo's legacy hook as that is already registered which
406 // will break this test. Use Vector's hook handler instead.
407 ( new Hooks(
408 $config,
409 $userOptionsManager
410 ) )->onLocalUserCreated( $user, false );
411 }
412
413 /**
414 * @covers ::onSkinTemplateNavigation
415 */
416 public function testOnSkinTemplateNavigation() {
417 $this->overrideConfigValue( 'VectorUseIconWatch', true );
418 $skin = new SkinVector22(
419 $this->getServiceContainer()->getLanguageConverterFactory(),
420 $this->getServiceContainer()->get( 'Vector.FeatureManagerFactory' ),
421 [ 'name' => 'vector' ]
422 );
423 $skin->getContext()->setTitle( Title::newFromText( 'Foo' ) );
424 $contentNavWatch = [
425 'associated-pages' => [],
426 'views' => [],
427 'actions' => [
428 'watch' => [ 'class' => [ 'watch' ], 'icon' => 'star' ],
429 ]
430 ];
431 $contentNavUnWatch = [
432 'associated-pages' => [],
433 'views' => [],
434 'actions' => [
435 'move' => [ 'class' => [ 'move' ] ],
436 'unwatch' => [ 'icon' => 'unStar' ],
437 ],
438 ];
439
440 Hooks::onSkinTemplateNavigation( $skin, $contentNavUnWatch );
441 Hooks::onSkinTemplateNavigation( $skin, $contentNavWatch );
442
443 $this->assertContains(
444 'icon', $contentNavWatch['views']['watch']['class'],
445 'Watch list items require an "icon" class'
446 );
447 $this->assertContains(
448 'icon', $contentNavUnWatch['views']['unwatch']['class'],
449 'Unwatch list items require an "icon" class'
450 );
451 $this->assertNotContains(
452 'icon', $contentNavUnWatch['actions']['move']['class'],
453 'List item other than watch or unwatch should not have an "icon" class'
454 );
455 }
456
457 /**
458 * @covers ::updateUserLinksItems
459 */
460 public function testUpdateUserLinksItems() {
461 $vector2022Skin = new SkinVector22(
462 $this->getServiceContainer()->getLanguageConverterFactory(),
463 $this->getServiceContainer()->get( 'Vector.FeatureManagerFactory' ),
464 [ 'name' => 'vector-2022' ]
465 );
466 $contentNav = [
467 'associated-pages' => [],
468 'views' => [],
469 'user-page' => [
470 'userpage' => [ 'class' => [], 'icon' => 'userpage' ],
471 ],
472 'user-menu' => [
473 'login' => [ 'class' => [], 'icon' => 'login' ],
474 ]
475 ];
476 $vectorLegacySkin = new SkinVectorLegacy(
477 $this->getServiceContainer()->getLanguageConverterFactory(),
478 [ 'name' => 'vector' ]
479 );
480 $contentNavLegacy = [
481 'associated-pages' => [],
482 'views' => [],
483 'user-page' => [
484 'userpage' => [ 'class' => [], 'icon' => 'userpage' ],
485 ]
486 ];
487
488 Hooks::onSkinTemplateNavigation( $vector2022Skin, $contentNav );
489 $this->assertFalse( isset( $contentNav['user-page']['login'] ),
490 'updateUserLinksDropdownItems is called when not legacy'
491 );
492 Hooks::onSkinTemplateNavigation( $vectorLegacySkin, $contentNavLegacy );
493 $this->assertFalse( isset( $contentNavLegacy['user-page'] ),
494 'user-page is unset for legacy vector'
495 );
496 }
497
498 /**
499 * @covers ::updateUserLinksDropdownItems
500 */
501 public function testUpdateUserLinksDropdownItems() {
502 $updateUserLinksDropdownItems = new ReflectionMethod(
503 Hooks::class,
504 'updateUserLinksDropdownItems'
505 );
506
507 // Anon users
508 $skin = new SkinVector22(
509 $this->getServiceContainer()->getLanguageConverterFactory(),
510 $this->getServiceContainer()->get( 'Vector.FeatureManagerFactory' ),
511 [ 'name' => 'vector-2022' ]
512 );
513 $contentAnon = [
514 'user-menu' => [
515 'anonuserpage' => [ 'class' => [], 'icon' => 'anonuserpage' ],
516 'createaccount' => [ 'class' => [], 'icon' => 'createaccount' ],
517 'login' => [ 'class' => [], 'icon' => 'login' ],
518 'anontalk' => [ 'class' => [], 'icon' => 'anontalk' ],
519 'anoncontribs' => [ 'class' => [], 'icon' => 'anoncontribs' ],
520 ],
521 ];
522 $updateUserLinksDropdownItems->invokeArgs( null, [ $skin, &$contentAnon ] );
523 $this->assertTrue(
524 count( $contentAnon['user-menu'] ) === 2 &&
525 isset( $contentAnon['user-menu']['createaccount'] ) &&
526 isset( $contentAnon['user-menu']['login'] ),
527 'Anon user page, anon talk, anon contribs links are removed from user-menu'
528 );
529
530 $this->assertTrue(
531 count( $contentAnon['user-menu'] ) === 2 &&
532 isset( $contentAnon['user-menu-anon-editor']['anontalk'] ) &&
533 isset( $contentAnon['user-menu-anon-editor']['anoncontribs'] ),
534 'Anon talk, anon contribs links are moved to user-menu-anon-editor'
535 );
536
537 // Registered user
538 $registeredUser = $this->createMock( User::class );
539 $registeredUser->method( 'isRegistered' )->willReturn( true );
540 $context = new RequestContext();
541 $context->setUser( $registeredUser );
542 $skin->setContext( $context );
543 $contentRegistered = [
544 'user-menu' => [
545 'userpage' => [ 'class' => [], 'icon' => 'userpage' ],
546 'watchlist' => [ 'class' => [], 'icon' => 'watchlist' ],
547 'logout' => [ 'class' => [], 'icon' => 'logout' ],
548 ],
549 ];
550 $updateUserLinksDropdownItems->invokeArgs( null, [ $skin, &$contentRegistered ] );
551 $this->assertContains( 'user-links-collapsible-item', $contentRegistered['user-menu']['userpage']['class'],
552 'User page link in user links dropdown requires collapsible class'
553 );
554 $this->assertEquals(
555 '<span class="vector-icon mw-ui-icon-userpage mw-ui-icon-wikimedia-userpage"></span>',
556 $contentRegistered['user-menu']['userpage']['link-html'],
557 'User page link in user links dropdown has link-html'
558 );
559 $this->assertContains( 'user-links-collapsible-item', $contentRegistered['user-menu']['watchlist']['class'],
560 'Watchlist link in user links dropdown requires collapsible class'
561 );
562 $this->assertEquals(
563 '<span class="vector-icon mw-ui-icon-watchlist mw-ui-icon-wikimedia-watchlist"></span>',
564 $contentRegistered['user-menu']['watchlist']['link-html'],
565 'Watchlist link in user links dropdown has link-html'
566 );
567 $this->assertFalse( isset( $contentRegistered['user-menu']['logout'] ),
568 'Logout link in user links dropdown is not set'
569 );
570 $this->assertTrue( isset( $contentRegistered['user-menu-logout']['logout'] ),
571 'Logout link in user links dropdown is not set'
572 );
573 }
574
575 public static function provideAppendClassToItem() {
576 return [
577 // Add array class to array
578 [
579 [],
580 [ 'array1', 'array2' ],
581 [ 'array1', 'array2' ],
582 ],
583 // Add string class to array
584 [
585 [],
586 'array1 array2',
587 [ 'array1 array2' ],
588 ],
589 // Add array class to string
590 [
591 '',
592 [ 'array1', 'array2' ],
593 'array1 array2',
594 ],
595 // Add string class to string
596 [
597 '',
598 'array1 array2',
599 'array1 array2',
600 ],
601 // Add string class to undefined
602 [
603 null,
604 'array1 array2',
605 'array1 array2',
606 ],
607 // Add array class to undefined
608 [
609 null,
610 [ 'array1', 'array2' ],
611 [ 'array1', 'array2' ],
612 ],
613 ];
614 }
615
616 /**
617 * @covers ::appendClassToItem
618 * @dataProvider provideAppendClassToItem
619 */
620 public function testAppendClassToItem(
621 $item,
622 $classes,
623 $expected
624 ) {
625 $appendClassToItem = new ReflectionMethod(
626 Hooks::class,
627 'appendClassToItem'
628 );
629 $appendClassToItem->invokeArgs( null, [ &$item, $classes ] );
630 $this->assertEquals( $expected, $item );
631 }
632
633 public static function provideUpdateItemData() {
634 return [
635 // Removes extra attributes
636 [
637 [ 'class' => [], 'icon' => '', 'button' => false, 'text-hidden' => false, 'collapsible' => false ],
638 'link-class',
639 'link-html',
640 [ 'class' => [] ],
641 ],
642 // Adds icon html
643 [
644 [ 'class' => [], 'icon' => 'userpage' ],
645 'link-class',
646 'link-html',
647 [
648 'class' => [],
649 'link-html' =>
650 '<span class="vector-icon mw-ui-icon-userpage mw-ui-icon-wikimedia-userpage"></span>'
651 ],
652 ],
653 // Adds collapsible class
654 [
655 [ 'class' => [], 'collapsible' => true ],
656 'link-class',
657 'link-html',
658 [ 'class' => [ 'user-links-collapsible-item' ] ],
659 ],
660 // Adds button classes
661 [
662 [ 'class' => [], 'button' => true ],
663 'link-class',
664 'link-html',
665 [ 'class' => [], 'link-class' => [
666 'cdx-button',
667 'cdx-button--fake-button',
668 'cdx-button--fake-button--enabled',
669 'cdx-button--weight-quiet'
670 ] ],
671 ],
672 // Adds text hidden classes
673 [
674 [ 'class' => [], 'button' => true, 'text-hidden' => true, 'icon' => 'userpage' ],
675 'link-class',
676 'link-html',
677 [
678 'class' => [],
679 'link-class' => [
680 'cdx-button',
681 'cdx-button--fake-button',
682 'cdx-button--fake-button--enabled',
683 'cdx-button--weight-quiet',
684 'cdx-button--icon-only'
685 ],
686 'link-html' =>
687 '<span class="vector-icon mw-ui-icon-userpage mw-ui-icon-wikimedia-userpage"></span>'
688 ],
689 ],
690 // Adds button and icon classes
691 [
692 [ 'class' => [], 'button' => true, 'icon' => 'userpage' ],
693 'class',
694 'link-html',
695 [ 'class' => [
696 'cdx-button',
697 'cdx-button--fake-button',
698 'cdx-button--fake-button--enabled',
699 'cdx-button--weight-quiet'
700 ], 'link-html' =>
701 '<span class="vector-icon mw-ui-icon-userpage mw-ui-icon-wikimedia-userpage"></span>'
702 ],
703 ]
704 ];
705 }
706
707 /**
708 * @covers ::updateItemData
709 * @dataProvider provideUpdateItemData
710 */
711 public function testUpdateItemData(
712 array $item,
713 string $buttonClassProp,
714 string $iconHtmlProp,
715 array $expected
716 ) {
717 $updateItemData = new ReflectionMethod(
718 Hooks::class,
719 'updateItemData'
720 );
721 $data = $updateItemData->invokeArgs( null, [ $item, $buttonClassProp, $iconHtmlProp ] );
722 $this->assertArraySubmapSame( $expected, $data );
723 }
724 }