2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2019-10-23 10:23:09 +00:00
|
|
|
* @covers \EchoNotificationController
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2014-07-29 23:54:00 +00:00
|
|
|
class NotificationControllerTest extends MediaWikiTestCase {
|
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
public function evaluateUserLocatorsProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-29 23:54:00 +00:00
|
|
|
'With no options no users are notified',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
|
|
|
],
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-29 23:54:00 +00:00
|
|
|
'Does not error when given non-existant user-locator',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'not-callable' ],
|
|
|
|
],
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-29 23:54:00 +00:00
|
|
|
'Calls selected locator and returns result',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ [ 123 ] ],
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 123 => 123 ];
|
2015-10-01 13:48:52 +00:00
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-30 03:18:48 +00:00
|
|
|
'evaluates multiple locators',
|
2014-07-29 23:54:00 +00:00
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ [ 123 ], [ 456 ] ],
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 123 => 123 ];
|
2015-10-01 13:48:52 +00:00
|
|
|
},
|
|
|
|
function () {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 456 => 456 ];
|
2015-10-01 13:48:52 +00:00
|
|
|
},
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-30 03:18:48 +00:00
|
|
|
'Passes parameters to locateFromEventExtra in expected manner',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ [ 123 ] ],
|
2014-07-30 03:18:48 +00:00
|
|
|
// event user locator config
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2019-02-19 10:35:54 +00:00
|
|
|
[ [ EchoUserLocator::class, 'locateFromEventExtra' ], [ 'other-user' ] ],
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2014-07-30 03:18:48 +00:00
|
|
|
// additional setup
|
2015-10-01 13:48:52 +00:00
|
|
|
function ( $test, $event ) {
|
2014-07-30 03:18:48 +00:00
|
|
|
$event->expects( $test->any() )
|
|
|
|
->method( 'getExtraParam' )
|
|
|
|
->with( 'other-user' )
|
|
|
|
->will( $test->returnValue( 123 ) );
|
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-30 03:18:48 +00:00
|
|
|
* @dataProvider evaluateUserLocatorsProvider
|
2014-07-29 23:54:00 +00:00
|
|
|
*/
|
2014-07-30 03:18:48 +00:00
|
|
|
public function testEvaluateUserLocators( $message, $expect, $locatorConfigForEventType, $setup = null ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgEchoNotifications' => [
|
|
|
|
'unit-test' => [
|
Allow certain users to be excluded
Right now, if certain users should be excluded, that would have
to be part of the user-locators already. This is annoying because
it's hard to write "generic" user locators when you want to exclude
just a couple of people in certain cases.
In Flow, for example, we have user-locators for users watching a
board or topic. We don't want to send the notification to people
that have also been mentioned in that post (they'll get a separate
notification). We could build that exception into those
user-locators, but then we couldn't re-use them in other places...
This basically means we couldn't use EchoUserLocator::locateUsersWatchingTitle,
we would have to roll our own that also excludes mentioned users.
Instead, this lets you add 'user-filters' (that functionality
actually exists already, but is not currently exposed), which
lists users to not send the notification to, even though they could
be in a user-locator.
Bug: T125428
Change-Id: Ifa0e2d3283f57624af4c5ec264f9f66223508e83
2016-02-02 13:16:39 +00:00
|
|
|
EchoAttributeManager::ATTR_LOCATORS => $locatorConfigForEventType
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
] );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-07-29 23:54:00 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getType' )
|
|
|
|
->will( $this->returnValue( 'unit-test' ) );
|
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
if ( $setup !== null ) {
|
|
|
|
$setup( $this, $event );
|
|
|
|
}
|
|
|
|
|
Allow certain users to be excluded
Right now, if certain users should be excluded, that would have
to be part of the user-locators already. This is annoying because
it's hard to write "generic" user locators when you want to exclude
just a couple of people in certain cases.
In Flow, for example, we have user-locators for users watching a
board or topic. We don't want to send the notification to people
that have also been mentioned in that post (they'll get a separate
notification). We could build that exception into those
user-locators, but then we couldn't re-use them in other places...
This basically means we couldn't use EchoUserLocator::locateUsersWatchingTitle,
we would have to roll our own that also excludes mentioned users.
Instead, this lets you add 'user-filters' (that functionality
actually exists already, but is not currently exposed), which
lists users to not send the notification to, even though they could
be in a user-locator.
Bug: T125428
Change-Id: Ifa0e2d3283f57624af4c5ec264f9f66223508e83
2016-02-02 13:16:39 +00:00
|
|
|
$result = EchoNotificationController::evaluateUserCallable( $event, EchoAttributeManager::ATTR_LOCATORS );
|
2014-07-30 03:18:48 +00:00
|
|
|
$this->assertEquals( $expect, array_map( 'array_keys', $result ), $message );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEvaluateUserLocatorPassesParameters() {
|
|
|
|
$test = $this;
|
2015-10-01 13:48:52 +00:00
|
|
|
$callback = function ( $event, $firstOption, $secondOption ) use ( $test ) {
|
2019-02-19 10:35:54 +00:00
|
|
|
$test->assertInstanceOf( EchoEvent::class, $event );
|
2014-07-30 03:18:48 +00:00
|
|
|
$test->assertEquals( 'first', $firstOption );
|
|
|
|
$test->assertEquals( 'second', $secondOption );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-07-30 03:18:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self::testEvaluateUserLocators(
|
|
|
|
__FUNCTION__,
|
2016-12-05 18:51:07 +00:00
|
|
|
[ [] ],
|
|
|
|
[ [ $callback, 'first', 'second' ] ]
|
2014-07-30 03:18:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsersToNotifyForEventProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-07-30 03:18:48 +00:00
|
|
|
'Filters anonymous users',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2014-07-30 03:18:48 +00:00
|
|
|
// users returned from locator
|
2016-12-05 18:51:07 +00:00
|
|
|
[ User::newFromName( '4.5.6.7', false ) ],
|
|
|
|
],
|
2014-07-30 03:18:48 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-30 03:18:48 +00:00
|
|
|
'Filters duplicate users',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123 ],
|
2014-07-30 03:18:48 +00:00
|
|
|
// users returned from locator
|
2016-12-05 18:51:07 +00:00
|
|
|
[ User::newFromId( 123 ), User::newFromId( 123 ) ],
|
|
|
|
],
|
2014-07-30 03:18:48 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
2014-07-30 03:18:48 +00:00
|
|
|
'Filters non-user objects',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 123 ],
|
2014-07-30 03:18:48 +00:00
|
|
|
// users returned from locator
|
2020-08-10 10:55:36 +00:00
|
|
|
[ null, 'foo', User::newFromId( 123 ), (object)[], 456 ],
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-30 03:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUsersToNotifyForEventProvider
|
|
|
|
*/
|
|
|
|
public function testGetUsersToNotifyForEvent(
|
|
|
|
$message,
|
|
|
|
$expect,
|
|
|
|
$users
|
|
|
|
) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgEchoNotifications' => [
|
|
|
|
'unit-test' => [
|
Allow certain users to be excluded
Right now, if certain users should be excluded, that would have
to be part of the user-locators already. This is annoying because
it's hard to write "generic" user locators when you want to exclude
just a couple of people in certain cases.
In Flow, for example, we have user-locators for users watching a
board or topic. We don't want to send the notification to people
that have also been mentioned in that post (they'll get a separate
notification). We could build that exception into those
user-locators, but then we couldn't re-use them in other places...
This basically means we couldn't use EchoUserLocator::locateUsersWatchingTitle,
we would have to roll our own that also excludes mentioned users.
Instead, this lets you add 'user-filters' (that functionality
actually exists already, but is not currently exposed), which
lists users to not send the notification to, even though they could
be in a user-locator.
Bug: T125428
Change-Id: Ifa0e2d3283f57624af4c5ec264f9f66223508e83
2016-02-02 13:16:39 +00:00
|
|
|
EchoAttributeManager::ATTR_LOCATORS => function () use ( $users ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
return $users;
|
|
|
|
},
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
] );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-07-30 03:18:48 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getType' )
|
|
|
|
->will( $this->returnValue( 'unit-test' ) );
|
|
|
|
|
|
|
|
$result = EchoNotificationController::getUsersToNotifyForEvent( $event );
|
2016-12-05 18:51:07 +00:00
|
|
|
$ids = [];
|
2014-07-30 03:18:48 +00:00
|
|
|
foreach ( $result as $user ) {
|
|
|
|
$ids[] = $user->getId();
|
|
|
|
}
|
|
|
|
$this->assertEquals( $expect, $ids, $message );
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
2014-08-02 06:52:16 +00:00
|
|
|
|
|
|
|
public function testDoesNotDeliverDisabledEvent() {
|
2019-02-19 10:35:54 +00:00
|
|
|
$event = $this->getMockBuilder( EchoEvent::class )
|
2014-08-02 06:52:16 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'isEnabledEvent' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
// Assume it would have to check the event type to
|
|
|
|
// determine how to deliver
|
|
|
|
$event->expects( $this->never() )
|
|
|
|
->method( 'getType' );
|
|
|
|
|
|
|
|
EchoNotificationController::notify( $event, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEventNotifyTypesProvider() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2014-08-02 06:52:16 +00:00
|
|
|
'Selects the `all` configuration by default',
|
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'web' ],
|
2014-08-02 06:52:16 +00:00
|
|
|
// event type
|
|
|
|
'bar',
|
|
|
|
// default notification types configuration
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'web' => true ],
|
2019-04-22 22:35:31 +00:00
|
|
|
// per-category notification type availability
|
|
|
|
[
|
|
|
|
'f' => [ 'email' => true ]
|
|
|
|
],
|
|
|
|
// event types
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
'foo' => [
|
2019-04-22 22:35:31 +00:00
|
|
|
'category' => 'f',
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2019-04-22 22:35:31 +00:00
|
|
|
'bar' => [
|
|
|
|
'category' => 'b',
|
|
|
|
]
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
|
|
|
|
[
|
2019-04-22 22:35:31 +00:00
|
|
|
'Overrides `all` configuration with event category configuration',
|
2014-08-02 06:52:16 +00:00
|
|
|
// expected result
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'web' ],
|
2014-08-02 06:52:16 +00:00
|
|
|
// event type
|
|
|
|
'foo',
|
|
|
|
// default notification types configuration
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'web' => true, 'email' => true ],
|
2019-04-22 22:35:31 +00:00
|
|
|
// per-category notification type availability
|
|
|
|
[
|
|
|
|
'f' => [ 'email' => false ],
|
|
|
|
'b' => [ 'sms' => true ],
|
|
|
|
],
|
|
|
|
// event types
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
'foo' => [
|
2019-04-22 22:35:31 +00:00
|
|
|
'category' => 'f',
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'bar' => [
|
2019-04-22 22:35:31 +00:00
|
|
|
'category' => 'b',
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
2014-08-02 06:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getEventNotifyTypesProvider
|
|
|
|
*/
|
2019-04-22 22:35:31 +00:00
|
|
|
public function testGetEventNotifyTypes(
|
2020-06-27 10:05:03 +00:00
|
|
|
$message,
|
|
|
|
$expect,
|
|
|
|
$type,
|
|
|
|
array $defaultNotifyTypeAvailability,
|
|
|
|
array $notifyTypeAvailabilityByCategory,
|
|
|
|
array $notifications
|
2019-04-22 22:35:31 +00:00
|
|
|
) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->setMwGlobals( [
|
BREAKING CHANGE: Change $wgEchoDefaultNotificationTypes to be logical
Merge and deploy at the *same time* as:
* BounceHandler - I3c669945080d8e1f67880bd8a31af7f88a70904d
* mediawiki-config - I13817c139967ed9e230cfb0c87c5de66da793c96
Despite claiming to be about categories, $wgEchoDefaultNotificationTypes
was actually configuring both categories and types (which go inside
categories).
For example, 'thank-you-edit' is a type, but 'emailuser' is both
a category and a type (when used as a category, this has special
effects at Special:Preferences).
Since types and categories can and sometimes do have the same names,
this leaves no way to properly and clearly configure them. It also
makes it difficult to document what is going on (as required by
T132127).
Split into three variables:
$wgDefaultNotifyTypeAvailability - Applies unless overriden
$wgNotifyTypeAvailabilityByCategory - By category; this can be and is
displayed at Special:Preferences
$wgNotifyTypeAvailabilityByNotificationType - By type; this cannot
be displayed at Special:Preferences. To avoid confusing the user,
we introduce a restriction (which was previously followed in practice,
AFAICT) that types can only be overridden if the category is not
displayed in preferences.
Otherwise, it can look to the user like a category is on/off, but the
types within might have the opposite state.
Due to this configuration change, this is a breaking change, and needs
coordinated deployments.
This also lays the groundwork for T132127
Also change terminology to consistently use "notify type" for web/email.
It was mixing between that and output format (which unfortunately
sounds like the API format, e.g. 'model').
Bug: T132820
Bug: T132127
Change-Id: I09f39f5fc5f13f3253af9f7819bca81f1601da93
2016-04-19 02:54:15 +00:00
|
|
|
'wgDefaultNotifyTypeAvailability' => $defaultNotifyTypeAvailability,
|
2019-04-22 22:35:31 +00:00
|
|
|
'wgNotifyTypeAvailabilityByCategory' => $notifyTypeAvailabilityByCategory,
|
BREAKING CHANGE: Change $wgEchoDefaultNotificationTypes to be logical
Merge and deploy at the *same time* as:
* BounceHandler - I3c669945080d8e1f67880bd8a31af7f88a70904d
* mediawiki-config - I13817c139967ed9e230cfb0c87c5de66da793c96
Despite claiming to be about categories, $wgEchoDefaultNotificationTypes
was actually configuring both categories and types (which go inside
categories).
For example, 'thank-you-edit' is a type, but 'emailuser' is both
a category and a type (when used as a category, this has special
effects at Special:Preferences).
Since types and categories can and sometimes do have the same names,
this leaves no way to properly and clearly configure them. It also
makes it difficult to document what is going on (as required by
T132127).
Split into three variables:
$wgDefaultNotifyTypeAvailability - Applies unless overriden
$wgNotifyTypeAvailabilityByCategory - By category; this can be and is
displayed at Special:Preferences
$wgNotifyTypeAvailabilityByNotificationType - By type; this cannot
be displayed at Special:Preferences. To avoid confusing the user,
we introduce a restriction (which was previously followed in practice,
AFAICT) that types can only be overridden if the category is not
displayed in preferences.
Otherwise, it can look to the user like a category is on/off, but the
types within might have the opposite state.
Due to this configuration change, this is a breaking change, and needs
coordinated deployments.
This also lays the groundwork for T132127
Also change terminology to consistently use "notify type" for web/email.
It was mixing between that and output format (which unfortunately
sounds like the API format, e.g. 'model').
Bug: T132820
Bug: T132127
Change-Id: I09f39f5fc5f13f3253af9f7819bca81f1601da93
2016-04-19 02:54:15 +00:00
|
|
|
'wgEchoNotifications' => $notifications,
|
2020-06-27 10:05:03 +00:00
|
|
|
'wgEchoNotificationCategories' => array_fill_keys(
|
|
|
|
array_keys( $notifyTypeAvailabilityByCategory ),
|
|
|
|
[ 'priority' => 4 ]
|
|
|
|
),
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2014-08-02 06:52:16 +00:00
|
|
|
$result = EchoNotificationController::getEventNotifyTypes( $type );
|
|
|
|
$this->assertEquals( $expect, $result, $message );
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|