From a1afa7ccb3eb56fdf85a0450333db72ea8a41999 Mon Sep 17 00:00:00 2001 From: bwang Date: Fri, 4 Mar 2022 09:45:45 -0600 Subject: [PATCH] Ensure watchlist in user menu dropdown has the collapsible class Bug: T302084 Change-Id: I27d7c0e46ee809185133fd787fc0c6fa5fcdac2e --- includes/Hooks.php | 8 +-- tests/phpunit/integration/VectorHooksTest.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/includes/Hooks.php b/includes/Hooks.php index dc12e08c..ca646d9b 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -214,6 +214,8 @@ class Hooks { * * @param SkinTemplate $sk * @param array &$content_navigation + * @suppress PhanTypeArraySuspiciousNullable False positives + * @suppress PhanTypePossiblyInvalidDimOffset False positives */ private static function updateUserLinksDropdownItems( $sk, &$content_navigation ) { // For logged-in users in modern Vector, rearrange some links in the personal toolbar. @@ -224,10 +226,9 @@ class Hooks { ); // watchlist may be disabled if $wgGroupPermissions['*']['viewmywatchlist'] = false; // See [[phab:T299671]] - $wlItem = $content_navigation['user-menu']['watchlist'] ?? false; - if ( $wlItem ) { + if ( isset( $content_navigation['user-menu']['watchlist'] ) ) { self::makeMenuItemCollapsible( - $wlItem + $content_navigation['user-menu']['watchlist'] ); } // Remove logout link from user-menu and recreate it in SkinVector, @@ -260,6 +261,7 @@ class Hooks { * * @param SkinTemplate $sk * @param array &$content_navigation + * @suppress PhanTypeArraySuspiciousNullable False positives */ private static function updateUserLinksItems( $sk, &$content_navigation ) { $hasUserMenu = $content_navigation['user-menu'] ?? false; diff --git a/tests/phpunit/integration/VectorHooksTest.php b/tests/phpunit/integration/VectorHooksTest.php index 9bb53a4f..3892aeb4 100644 --- a/tests/phpunit/integration/VectorHooksTest.php +++ b/tests/phpunit/integration/VectorHooksTest.php @@ -10,6 +10,7 @@ use HashConfig; use HTMLForm; use MediaWiki\User\UserOptionsManager; use MediaWikiIntegrationTestCase; +use ReflectionMethod; use ResourceLoaderContext; use RuntimeException; use Title; @@ -568,4 +569,56 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase { 'List item other than watch or unwatch should not have an "icon" class' ); } + + /** + * @covers ::updateUserLinksDropdownItems + */ + public function testUpdateUserLinksDropdownItems() { + $updateUserLinksDropdownItems = new ReflectionMethod( + Hooks::class, + 'updateUserLinksDropdownItems' + ); + $updateUserLinksDropdownItems->setAccessible( true ); + $skin = new SkinVector( [ 'name' => 'vector' ] ); + // Anon user + $skin->getUser()->setId( '1' ); + $contentAnon = [ + 'user-menu' => [ + 'anonuserpage' => [ 'class' => [], 'icon' => 'anonuserpage' ], + 'createaccount' => [ 'class' => [], 'icon' => 'createaccount' ], + 'login' => [ 'class' => [], 'icon' => 'login' ], + 'login-private' => [ 'class' => [], 'icon' => 'login-private' ], + ], + ]; + $updateUserLinksDropdownItems->invokeArgs( null, [ $skin, &$contentAnon ] ); + $this->assertTrue( + count( $contentAnon['user-menu'] ) === 0, + 'Anon user page, create account, login, and login private links are removed from anon user links dropdown' + ); + // Registered user + $skin->getUser()->setId( '1' ); + $contentRegistered = [ + 'user-menu' => [ + 'userpage' => [ 'class' => [], 'icon' => 'userpage' ], + 'watchlist' => [ 'class' => [], 'icon' => 'watchlist' ], + 'logout' => [ 'class' => [], 'icon' => 'logout' ], + ], + ]; + $updateUserLinksDropdownItems->invokeArgs( null, [ $skin, &$contentRegistered ] ); + $this->assertContains( 'user-links-collapsible-item', $contentRegistered['user-menu']['userpage']['class'], + 'User page link in user links dropdown requires collapsible class' + ); + $this->assertContains( 'mw-ui-icon-before', $contentRegistered['user-menu']['userpage']['link-class'], + 'User page link in user links dropdown requires icon classes' + ); + $this->assertContains( 'user-links-collapsible-item', $contentRegistered['user-menu']['watchlist']['class'], + 'Watchlist link in user links dropdown requires collapsible class' + ); + $this->assertContains( 'mw-ui-icon-before', $contentRegistered['user-menu']['watchlist']['link-class'], + 'Watchlist link in user links dropdown requires icon classes' + ); + $this->assertFalse( isset( $contentRegistered['user-menu']['logout'] ), + 'Logout link in user links dropdown is not set' + ); + } }