mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-09-23 10:22:05 +00:00
Merge "Remove deprecated User method getGroups"
This commit is contained in:
commit
55942a49d2
|
@ -9,12 +9,20 @@ use MediaWiki\Storage\NameTableStore;
|
|||
return [
|
||||
|
||||
'EchoAttributeManager' => function ( MediaWikiServices $services ): EchoAttributeManager {
|
||||
$userGroupManager = $services->getUserGroupManager();
|
||||
$echoConfig = $services->getConfigFactory()->makeConfig( 'Echo' );
|
||||
$notifications = $echoConfig->get( 'EchoNotifications' );
|
||||
$categories = $echoConfig->get( 'EchoNotificationCategories' );
|
||||
$typeAvailability = $echoConfig->get( 'DefaultNotifyTypeAvailability' );
|
||||
$typeAvailabilityByCategory = $echoConfig->get( 'NotifyTypeAvailabilityByCategory' );
|
||||
return new EchoAttributeManager( $notifications, $categories, $typeAvailability, $typeAvailabilityByCategory );
|
||||
|
||||
return new EchoAttributeManager(
|
||||
$notifications,
|
||||
$categories,
|
||||
$typeAvailability,
|
||||
$typeAvailabilityByCategory,
|
||||
$userGroupManager
|
||||
);
|
||||
},
|
||||
|
||||
'EchoPushNotificationServiceClient' => function ( MediaWikiServices $services ):
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\User\UserGroupManager;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
|
||||
/**
|
||||
* An object that manages attributes of echo notifications: category, elegibility,
|
||||
* group, section etc.
|
||||
*/
|
||||
class EchoAttributeManager {
|
||||
/**
|
||||
* @var UserGroupManager
|
||||
*/
|
||||
private $userGroupManager;
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
|
@ -59,12 +66,14 @@ class EchoAttributeManager {
|
|||
* @param array[] $notifyTypeAvailabilityByCategory Associative array with
|
||||
* categories as keys and value an associative array as with
|
||||
* $defaultNotifyTypeAvailability.
|
||||
* @param UserGroupManager $userGroupManager
|
||||
*/
|
||||
public function __construct(
|
||||
array $notifications,
|
||||
array $categories,
|
||||
array $defaultNotifyTypeAvailability,
|
||||
array $notifyTypeAvailabilityByCategory
|
||||
array $notifyTypeAvailabilityByCategory,
|
||||
UserGroupManager $userGroupManager
|
||||
) {
|
||||
// Extensions can define their own notifications and categories
|
||||
$this->notifications = $notifications;
|
||||
|
@ -72,6 +81,7 @@ class EchoAttributeManager {
|
|||
|
||||
$this->defaultNotifyTypeAvailability = $defaultNotifyTypeAvailability;
|
||||
$this->notifyTypeAvailabilityByCategory = $notifyTypeAvailabilityByCategory;
|
||||
$this->userGroupManager = $userGroupManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,12 +189,12 @@ class EchoAttributeManager {
|
|||
* See if a user is eligible to receive a certain type of notification
|
||||
* (based on user groups, not user preferences)
|
||||
*
|
||||
* @param User $user
|
||||
* @param UserIdentity $user
|
||||
* @param string $category A notification category defined in $wgEchoNotificationCategories
|
||||
* @return bool
|
||||
*/
|
||||
public function getCategoryEligibility( $user, $category ) {
|
||||
$usersGroups = $user->getGroups();
|
||||
$usersGroups = $this->userGroupManager->getUserGroups( $user );
|
||||
if ( isset( $this->categories[$category]['usergroups'] ) ) {
|
||||
$allowedGroups = $this->categories[$category]['usergroups'];
|
||||
if ( !array_intersect( $usersGroups, $allowedGroups ) ) {
|
||||
|
|
|
@ -210,9 +210,6 @@ class EchoNotificationMapperTest extends MediaWikiTestCase {
|
|||
$user->expects( $this->any() )
|
||||
->method( 'getOption' )
|
||||
->will( $this->returnValue( true ) );
|
||||
$user->expects( $this->any() )
|
||||
->method( 'getGroups' )
|
||||
->will( $this->returnValue( [ 'echo_group' ] ) );
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\User\UserGroupManager;
|
||||
|
||||
/**
|
||||
* @covers \EchoAttributeManager
|
||||
*/
|
||||
|
@ -54,7 +56,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
* @dataProvider getUserLocatorsProvider
|
||||
*/
|
||||
public function testGetUserLocators( $message, $expect, $type, $notifications ) {
|
||||
$manager = new EchoAttributeManager( $notifications, [], [], [] );
|
||||
$manager = new EchoAttributeManager( $notifications, [], [], [], $this->mockUserGroupManager() );
|
||||
|
||||
$result = $manager->getUserCallable( $type, EchoAttributeManager::ATTR_LOCATORS );
|
||||
$this->assertEquals( $expect, $result, $message );
|
||||
|
@ -71,7 +73,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
'priority' => 10
|
||||
]
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertTrue( $manager->getCategoryEligibility( $this->mockUser(), 'category_one' ) );
|
||||
$category = [
|
||||
'category_one' => [
|
||||
|
@ -81,7 +83,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
]
|
||||
]
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertFalse( $manager->getCategoryEligibility( $this->mockUser(), 'category_one' ) );
|
||||
}
|
||||
|
||||
|
@ -96,10 +98,10 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
'priority' => 10
|
||||
]
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'category_one' );
|
||||
|
||||
$manager = new EchoAttributeManager( $notif, [], [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, [], [], [], $this->mockUserGroupManager() );
|
||||
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'other' );
|
||||
|
||||
$notif = [
|
||||
|
@ -112,7 +114,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
'priority' => 10
|
||||
]
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'other' );
|
||||
}
|
||||
|
||||
|
@ -134,7 +136,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
],
|
||||
'category_four' => []
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertSame( 6, $manager->getCategoryPriority( 'category_one' ) );
|
||||
$this->assertSame( 10, $manager->getCategoryPriority( 'category_two' ) );
|
||||
$this->assertSame( 10, $manager->getCategoryPriority( 'category_three' ) );
|
||||
|
@ -168,7 +170,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
],
|
||||
'category_four' => []
|
||||
];
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [] );
|
||||
$manager = new EchoAttributeManager( $notif, $category, [], [], $this->mockUserGroupManager() );
|
||||
$this->assertSame( 6, $manager->getNotificationPriority( 'event_one' ) );
|
||||
$this->assertSame( 10, $manager->getNotificationPriority( 'event_two' ) );
|
||||
$this->assertSame( 10, $manager->getNotificationPriority( 'event_three' ) );
|
||||
|
@ -220,7 +222,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
* @dataProvider getEventsForSectionProvider
|
||||
*/
|
||||
public function testGetEventsForSection( $expected, $notificationTypes, $section, $message ) {
|
||||
$am = new EchoAttributeManager( $notificationTypes, [], [], [] );
|
||||
$am = new EchoAttributeManager( $notificationTypes, [], [], [], $this->mockUserGroupManager() );
|
||||
$actual = $am->getEventsForSection( $section );
|
||||
$this->assertEquals( $expected, $actual, $message );
|
||||
}
|
||||
|
@ -274,7 +276,8 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$notif,
|
||||
$category,
|
||||
$defaultNotifyTypeAvailability,
|
||||
$notifyTypeAvailabilityByCategory
|
||||
$notifyTypeAvailabilityByCategory,
|
||||
$this->mockUserGroupManager()
|
||||
);
|
||||
$this->assertEquals(
|
||||
[ 'event_two', 'event_four' ],
|
||||
|
@ -330,7 +333,8 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$notif,
|
||||
$category,
|
||||
$defaultNotifyTypeAvailability,
|
||||
$notifyTypeAvailabilityByCategory
|
||||
$notifyTypeAvailabilityByCategory,
|
||||
$this->mockUserGroupManager()
|
||||
);
|
||||
$expected = [ 'event_one', 'event_three', 'event_four' ];
|
||||
$actual = $manager->getUserEnabledEventsbySections( $this->mockUser(), 'web', [ 'alert' ] );
|
||||
|
@ -403,7 +407,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$categories,
|
||||
$notifications
|
||||
) {
|
||||
$am = new EchoAttributeManager( $notifications, $categories, [], [] );
|
||||
$am = new EchoAttributeManager( $notifications, $categories, [], [], $this->mockUserGroupManager() );
|
||||
$actualMapping = $am->getEventsByCategory();
|
||||
$this->assertEquals( $expectedMapping, $actualMapping, $message );
|
||||
}
|
||||
|
@ -457,7 +461,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$notifyTypeAvailabilityByCategory
|
||||
) {
|
||||
$am = new EchoAttributeManager( [], [], $defaultNotifyTypeAvailability,
|
||||
$notifyTypeAvailabilityByCategory );
|
||||
$notifyTypeAvailabilityByCategory, $this->mockUserGroupManager() );
|
||||
$actual = $am->isNotifyTypeAvailableForCategory( $categoryName, $notifyType );
|
||||
$this->assertEquals( $expected, $actual, $message );
|
||||
}
|
||||
|
@ -510,7 +514,7 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$categoryName,
|
||||
$notifyType
|
||||
) {
|
||||
$am = new EchoAttributeManager( [], $categories, [], [] );
|
||||
$am = new EchoAttributeManager( [], $categories, [], [], $this->mockUserGroupManager() );
|
||||
$actual = $am->isNotifyTypeDismissableForCategory( $categoryName, $notifyType );
|
||||
$this->assertEquals( $expected, $actual, $message );
|
||||
}
|
||||
|
@ -529,10 +533,19 @@ class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
|
|||
$user->expects( $this->any() )
|
||||
->method( 'getOption' )
|
||||
->will( $this->returnValue( true ) );
|
||||
$user->expects( $this->any() )
|
||||
->method( 'getGroups' )
|
||||
->will( $this->returnValue( [ 'echo_group' ] ) );
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock object of UserGroupManager
|
||||
* @return UserGroupManager
|
||||
*/
|
||||
protected function mockUserGroupManager() {
|
||||
$userGroupManager = $this->createNoOpMock( UserGroupManager::class, [ 'getUserGroups' ] );
|
||||
$userGroupManager->method( 'getUserGroups' )
|
||||
->willReturn( [ 'echo_group' ] );
|
||||
|
||||
return $userGroupManager;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -109,10 +109,9 @@ class EchoUserNotificationGatewayTest extends MediaWikiUnitTestCase {
|
|||
|
||||
/**
|
||||
* Mock object of User
|
||||
* @param string $group
|
||||
* @return User
|
||||
*/
|
||||
protected function mockUser( $group = 'echo_group' ) {
|
||||
protected function mockUser() {
|
||||
$user = $this->getMockBuilder( User::class )
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
@ -122,9 +121,6 @@ class EchoUserNotificationGatewayTest extends MediaWikiUnitTestCase {
|
|||
$user->expects( $this->any() )
|
||||
->method( 'getOption' )
|
||||
->will( $this->returnValue( true ) );
|
||||
$user->expects( $this->any() )
|
||||
->method( 'getGroups' )
|
||||
->will( $this->returnValue( [ $group ] ) );
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue