2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class NotificationControllerTest extends MediaWikiTestCase {
|
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
public function evaluateUserLocatorsProvider() {
|
2014-07-29 23:54:00 +00:00
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'With no options no users are notified',
|
|
|
|
// expected result
|
|
|
|
array(),
|
|
|
|
// event user locator config
|
|
|
|
array(),
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'Does not error when given non-existant user-locator',
|
|
|
|
// expected result
|
|
|
|
array(),
|
|
|
|
// event user locator config
|
|
|
|
array( 'not-callable' ),
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'Calls selected locator and returns result',
|
|
|
|
// expected result
|
2014-07-30 03:18:48 +00:00
|
|
|
array( array( 123 ) ),
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
|
|
|
return array( 123 => 123 );
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
2014-07-30 03:18:48 +00:00
|
|
|
'evaluates multiple locators',
|
2014-07-29 23:54:00 +00:00
|
|
|
// expected result
|
2014-07-30 03:18:48 +00:00
|
|
|
array( array( 123 ), array( 456 ) ),
|
2014-07-29 23:54:00 +00:00
|
|
|
// event user locator config
|
|
|
|
array(
|
2015-10-01 13:48:52 +00:00
|
|
|
function () {
|
|
|
|
return array( 123 => 123 );
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
return array( 456 => 456 );
|
|
|
|
},
|
2014-07-29 23:54:00 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
array(
|
|
|
|
'Passes parameters to locateFromEventExtra in expected manner',
|
|
|
|
// expected result
|
|
|
|
array( array( 123 ) ),
|
|
|
|
// event user locator config
|
|
|
|
array(
|
|
|
|
array( 'EchoUserLocator::locateFromEventExtra', array( 'other-user' ) ),
|
|
|
|
),
|
|
|
|
// 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 ) );
|
|
|
|
}
|
|
|
|
),
|
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 ) {
|
2014-07-29 23:54:00 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
'wgEchoNotifications' => array(
|
|
|
|
'unit-test' => array(
|
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
|
2014-07-29 23:54:00 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->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 ) {
|
2014-07-30 03:18:48 +00:00
|
|
|
$test->assertInstanceOf( 'EchoEvent', $event );
|
|
|
|
$test->assertEquals( 'first', $firstOption );
|
|
|
|
$test->assertEquals( 'second', $secondOption );
|
|
|
|
|
|
|
|
return array();
|
|
|
|
};
|
|
|
|
|
|
|
|
self::testEvaluateUserLocators(
|
|
|
|
__FUNCTION__,
|
|
|
|
array( array() ),
|
|
|
|
array( array( $callback, 'first', 'second' ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUsersToNotifyForEventProvider() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'Filters anonymous users',
|
|
|
|
// expected result
|
|
|
|
array(),
|
|
|
|
// users returned from locator
|
|
|
|
array( User::newFromName( '4.5.6.7', false ) ),
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'Filters duplicate users',
|
|
|
|
// expected result
|
|
|
|
array( 123 ),
|
|
|
|
// users returned from locator
|
|
|
|
array( User::newFromId( 123 ), User::newFromId( 123 ) ),
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'Filters non-user objects',
|
|
|
|
// expected result
|
|
|
|
array( 123 ),
|
|
|
|
// users returned from locator
|
|
|
|
array( null, 'foo', User::newFromId( 123 ), new stdClass, 456 ),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getUsersToNotifyForEventProvider
|
|
|
|
*/
|
|
|
|
public function testGetUsersToNotifyForEvent(
|
|
|
|
$message,
|
|
|
|
$expect,
|
|
|
|
$users
|
|
|
|
) {
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
'wgEchoNotifications' => array(
|
|
|
|
'unit-test' => array(
|
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;
|
|
|
|
},
|
2014-07-30 03:18:48 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
) );
|
|
|
|
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getType' )
|
|
|
|
->will( $this->returnValue( 'unit-test' ) );
|
|
|
|
|
|
|
|
$result = EchoNotificationController::getUsersToNotifyForEvent( $event );
|
|
|
|
$ids = array();
|
|
|
|
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() {
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->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() {
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'Selects the `all` configuration by default',
|
|
|
|
// expected result
|
|
|
|
array( 'web' ),
|
|
|
|
// event type
|
|
|
|
'bar',
|
|
|
|
// default notification types configuration
|
|
|
|
array(
|
|
|
|
'all' => array( 'web' => true ),
|
|
|
|
'foo' => array( 'email' => true ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
array(
|
|
|
|
'Overrides `all` configuration with event type configuration',
|
|
|
|
// expected result
|
|
|
|
array( 'web' ),
|
|
|
|
// event type
|
|
|
|
'foo',
|
|
|
|
// default notification types configuration
|
|
|
|
array(
|
|
|
|
'all' => array( 'web' => true, 'email' => true ),
|
|
|
|
'foo' => array( 'email' => false ),
|
|
|
|
'bar' => array( 'sms' => true ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getEventNotifyTypesProvider
|
|
|
|
*/
|
|
|
|
public function testGetEventNotifyTypes( $message, $expect, $type, array $notificationTypes ) {
|
|
|
|
$this->setMwGlobals( array(
|
|
|
|
'wgEchoDefaultNotificationTypes' => $notificationTypes,
|
|
|
|
) );
|
|
|
|
$result = EchoNotificationController::getEventNotifyTypes( $type );
|
|
|
|
$this->assertEquals( $expect, $result, $message );
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|