2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 03:51:15 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Controller\NotificationController;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2021-12-01 18:29:00 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
2018-01-24 00:31:53 +00:00
|
|
|
/**
|
2022-11-02 03:51:15 +00:00
|
|
|
* @covers \MediaWiki\Extension\Notifications\Controller\NotificationController
|
2018-01-24 00:31:53 +00:00
|
|
|
*/
|
2021-10-11 22:42:10 +00:00
|
|
|
class NotificationControllerTest extends MediaWikiIntegrationTestCase {
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static 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
|
2021-05-04 16:06:42 +00:00
|
|
|
static 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
|
|
|
[
|
2021-05-04 16:06:42 +00:00
|
|
|
static function () {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 123 => 123 ];
|
2015-10-01 13:48:52 +00:00
|
|
|
},
|
2021-05-04 16:06:42 +00:00
|
|
|
static 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
|
2021-05-04 16:06:42 +00:00
|
|
|
static 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
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getType' )
|
|
|
|
->willReturn( 'unit-test' );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
if ( $setup !== null ) {
|
|
|
|
$setup( $this, $event );
|
|
|
|
}
|
|
|
|
|
2022-11-02 03:51:15 +00:00
|
|
|
$result = NotificationController::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() {
|
2021-05-14 23:58:55 +00:00
|
|
|
$callback = function ( $event, $firstOption, $secondOption ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$this->assertInstanceOf( Event::class, $event );
|
2021-05-14 23:58:55 +00:00
|
|
|
$this->assertEquals( 'first', $firstOption );
|
|
|
|
$this->assertEquals( 'second', $secondOption );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static 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' => [
|
2021-05-04 16:06:42 +00:00
|
|
|
EchoAttributeManager::ATTR_LOCATORS => static 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
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getType' )
|
|
|
|
->willReturn( 'unit-test' );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
2022-11-02 03:51:15 +00:00
|
|
|
$result = NotificationController::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() {
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'isEnabledEvent' )
|
|
|
|
->willReturn( false );
|
2014-08-02 06:52:16 +00:00
|
|
|
// Assume it would have to check the event type to
|
|
|
|
// determine how to deliver
|
|
|
|
$event->expects( $this->never() )
|
|
|
|
->method( 'getType' );
|
|
|
|
|
2022-11-02 03:51:15 +00:00
|
|
|
NotificationController::notify( $event, false );
|
2014-08-02 06:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
] );
|
2022-11-02 03:51:15 +00:00
|
|
|
$result = NotificationController::getEventNotifyTypes( $type );
|
2014-08-02 06:52:16 +00:00
|
|
|
$this->assertEquals( $expect, $result, $message );
|
|
|
|
}
|
2020-10-09 14:09:21 +00:00
|
|
|
|
|
|
|
public function testEnqueueEvent() {
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getExtraParam' )
|
|
|
|
->willReturn( null );
|
|
|
|
$event->expects( $this->once() )
|
2020-10-09 14:09:21 +00:00
|
|
|
->method( 'getTitle' )
|
2022-09-29 13:41:35 +00:00
|
|
|
->willReturn( Title::newFromText( 'test-title' ) );
|
|
|
|
$event->expects( $this->once() )
|
2020-10-09 14:09:21 +00:00
|
|
|
->method( 'getId' )
|
2022-09-29 13:41:35 +00:00
|
|
|
->willReturn( 42 );
|
2022-11-02 03:51:15 +00:00
|
|
|
NotificationController::enqueueEvent( $event );
|
2022-01-25 21:24:53 +00:00
|
|
|
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
|
|
|
|
$queues = $jobQueueGroup->getQueuesWithJobs();
|
2020-10-09 14:09:21 +00:00
|
|
|
$this->assertCount( 1, $queues );
|
|
|
|
$this->assertEquals( 'EchoNotificationJob', $queues[0] );
|
2022-01-25 21:24:53 +00:00
|
|
|
$job = $jobQueueGroup->pop( 'EchoNotificationJob' );
|
2020-10-09 14:09:21 +00:00
|
|
|
$this->assertEquals( 'Test-title', $job->params[ 'title' ] );
|
|
|
|
$this->assertEquals( 42, $job->params[ 'eventId' ] );
|
|
|
|
}
|
|
|
|
|
2020-10-29 13:27:20 +00:00
|
|
|
public function testNotSupportedDelay() {
|
2022-01-25 21:24:53 +00:00
|
|
|
$queueGroup = $this->getServiceContainer()->getJobQueueGroup();
|
2020-10-29 13:27:20 +00:00
|
|
|
$this->assertCount( 0, $queueGroup->getQueuesWithJobs() );
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getExtraParam' )
|
2020-10-29 13:27:20 +00:00
|
|
|
->will( $this->returnValueMap(
|
|
|
|
[
|
|
|
|
[ 'delay', null, 120 ],
|
|
|
|
[ 'rootJobSignature', null, 'test-signature' ],
|
|
|
|
[ 'rootJobTimestamp', null, wfTimestamp() ]
|
|
|
|
]
|
|
|
|
) );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->expects( $this->once() )
|
2020-10-29 13:27:20 +00:00
|
|
|
->method( 'getTitle' )
|
2022-09-29 13:41:35 +00:00
|
|
|
->willReturn( Title::newFromText( 'test-title' ) );
|
|
|
|
$event->method( 'getId' )
|
|
|
|
->willReturn( 42 );
|
2022-11-02 03:51:15 +00:00
|
|
|
NotificationController::enqueueEvent( $event );
|
2020-10-29 13:27:20 +00:00
|
|
|
|
|
|
|
$this->assertCount( 0, $queueGroup->getQueuesWithJobs() );
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:09:21 +00:00
|
|
|
public function testEventParams() {
|
|
|
|
$rootJobTimestamp = wfTimestamp();
|
|
|
|
MWTimestamp::setFakeTime( 0 );
|
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
$event = $this->createMock( Event::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->method( 'getExtraParam' )
|
2020-10-09 14:09:21 +00:00
|
|
|
->will( $this->returnValueMap(
|
|
|
|
[
|
|
|
|
[ 'delay', null, 10 ],
|
|
|
|
[ 'rootJobSignature', null, 'test-signature' ],
|
|
|
|
[ 'rootJobTimestamp', null, $rootJobTimestamp ]
|
|
|
|
]
|
|
|
|
) );
|
2022-09-29 13:41:35 +00:00
|
|
|
$event->expects( $this->once() )
|
2020-10-09 14:09:21 +00:00
|
|
|
->method( 'getId' )
|
2022-09-29 13:41:35 +00:00
|
|
|
->willReturn( 42 );
|
2020-10-09 14:09:21 +00:00
|
|
|
|
2022-11-02 03:51:15 +00:00
|
|
|
$params = NotificationController::getEventParams( $event );
|
2020-10-09 14:09:21 +00:00
|
|
|
$expectedParams = [
|
|
|
|
'eventId' => 42,
|
|
|
|
'rootJobSignature' => 'test-signature',
|
|
|
|
'rootJobTimestamp' => $rootJobTimestamp,
|
|
|
|
'jobReleaseTimestamp' => 10
|
|
|
|
];
|
|
|
|
$this->assertArrayEquals( $expectedParams, $params );
|
|
|
|
}
|
2021-12-01 18:29:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider PageLinkedTitleMutedByUserDataProvider
|
2023-05-20 18:35:53 +00:00
|
|
|
* @param int $mockArticleID
|
|
|
|
* @param int[] $mockMutedTitlePreferences
|
2021-12-01 18:29:00 +00:00
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testIsPageLinkedTitleMutedByUser(
|
2023-05-20 18:35:53 +00:00
|
|
|
int $mockArticleID, array $mockMutedTitlePreferences, $expected ): void {
|
|
|
|
$title = $this->getMockTitle( $mockArticleID );
|
|
|
|
$user = $this->getMockUser();
|
|
|
|
$userOptionsLookup = $this->getUserOptionsLookupMock( $mockMutedTitlePreferences );
|
2022-11-02 03:51:15 +00:00
|
|
|
$wrapper = TestingAccessWrapper::newFromClass( NotificationController::class );
|
2022-09-29 13:41:35 +00:00
|
|
|
$wrapper->mutedPageLinkedTitlesCache = $this->createMock( MapCacheLRU::class );
|
2021-12-01 18:29:00 +00:00
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
|
|
|
$this->assertSame(
|
|
|
|
$expected,
|
|
|
|
$wrapper->isPageLinkedTitleMutedByUser( $title, $user )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-20 18:35:53 +00:00
|
|
|
public static function PageLinkedTitleMutedByUserDataProvider(): array {
|
2021-12-01 18:29:00 +00:00
|
|
|
return [
|
|
|
|
[
|
2023-05-20 18:35:53 +00:00
|
|
|
123,
|
|
|
|
[],
|
2021-12-01 18:29:00 +00:00
|
|
|
false
|
|
|
|
],
|
|
|
|
[
|
2023-05-20 18:35:53 +00:00
|
|
|
123,
|
|
|
|
[ 123, 456, 789 ],
|
2021-12-01 18:29:00 +00:00
|
|
|
true
|
|
|
|
],
|
|
|
|
[
|
2023-05-20 18:35:53 +00:00
|
|
|
456,
|
|
|
|
[ 489 ],
|
2021-12-01 18:29:00 +00:00
|
|
|
false
|
|
|
|
]
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMockTitle( int $articleID ) {
|
2022-09-29 13:41:35 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
2021-12-01 18:29:00 +00:00
|
|
|
$title->method( 'getArticleID' )
|
|
|
|
->willReturn( $articleID );
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMockUser() {
|
2022-09-29 13:41:35 +00:00
|
|
|
$user = $this->createMock( User::class );
|
2021-12-01 18:29:00 +00:00
|
|
|
$user->method( 'getId' )
|
|
|
|
->willReturn( 456 );
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserOptionsLookupMock( $mutedTitlePreferences = [] ) {
|
|
|
|
$userOptionsLookupMock = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookupMock->method( 'getOption' )
|
|
|
|
->willReturn( implode( "\n", $mutedTitlePreferences ) );
|
|
|
|
return $userOptionsLookupMock;
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|