mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-24 16:04:35 +00:00
f995de90c8
There are a variety of generic strategies you could define to choose which users should be notified about an event, such as 'users watching the title' or 'talk page owner' (User_talk only). This adds a new way to generically implement these in Echo and expose them to other extensions rather than having each extension implement these generic strategies themselves. This patch only converts one notification, edit-user-talk. The remaining notifications will be converted in future patches. The first user of this will be Flow for notifying all users watching a particular talk page in I4e46a9c003fbdde274b20ac7aef8455eab4a5222 The users watching title implementation provided here is minimalist, a larger refactor to accomidate pages with thousands of watchers is being handled in I3d3fa9328f348bb48682d3658622952ce82d3925 Change-Id: I19bb6a794d22565f3bb5421de92426d390197796
231 lines
5.7 KiB
PHP
231 lines
5.7 KiB
PHP
<?php
|
|
|
|
class EchoAttributeManagerTest extends MediaWikiTestCase {
|
|
|
|
public function testNewFromGlobalVars() {
|
|
$this->assertInstanceOf( 'EchoAttributeManager', EchoAttributeManager::newFromGlobalVars() );
|
|
}
|
|
|
|
public static function getUserLocatorsProvider() {
|
|
return array(
|
|
array(
|
|
'No errors when requesting unknown type',
|
|
// expected result
|
|
array(),
|
|
// event type
|
|
'foo',
|
|
// notification configuration
|
|
array(),
|
|
),
|
|
|
|
array(
|
|
'Returns selected notification configuration',
|
|
// expected result
|
|
array( 'woot!' ),
|
|
// event type
|
|
'magic',
|
|
// notification configuration
|
|
array(
|
|
'foo' => array(
|
|
'user-locators' => array( 'frown' ),
|
|
),
|
|
'magic' => array(
|
|
'user-locators' => array( 'woot!' ),
|
|
),
|
|
),
|
|
),
|
|
|
|
array(
|
|
'Accepts user-locators as string and returns array',
|
|
// expected result
|
|
array( 'sagen' ),
|
|
// event type
|
|
'challah',
|
|
// notification configuration
|
|
array(
|
|
'challah' => array(
|
|
'user-locators' => 'sagen',
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider getUserLocatorsProvider
|
|
*/
|
|
public function testGetUserLocators( $message, $expect, $type, $notifications) {
|
|
$manager = new EchoAttributeManager( $notifications, array() );
|
|
|
|
$result = $manager->getUserLocators( $type );
|
|
$this->assertEquals( $expect, $result, $message );
|
|
}
|
|
|
|
public function testGetCategoryEligibility() {
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_one'
|
|
),
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 10
|
|
)
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertTrue( $manager->getCategoryEligibility( $this->mockUser(), 'category_one' ) );
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 10,
|
|
'usergroups' => array(
|
|
'sysop'
|
|
)
|
|
)
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertFalse( $manager->getCategoryEligibility( $this->mockUser(), 'category_one' ) );
|
|
}
|
|
|
|
public function testGetNotificationCategory() {
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_one'
|
|
),
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 10
|
|
)
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'category_one' );
|
|
|
|
$manager = new EchoAttributeManager( $notif, array() );
|
|
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'other' );
|
|
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_two'
|
|
),
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 10
|
|
)
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertEquals( $manager->getNotificationCategory( 'event_one' ), 'other' );
|
|
}
|
|
|
|
public function testGetCategoryPriority() {
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_two'
|
|
),
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 6
|
|
),
|
|
'category_two' => array (
|
|
'priority' => 100
|
|
),
|
|
'category_three' => array (
|
|
'priority' => -10
|
|
),
|
|
'category_four' => array ()
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertEquals( 6, $manager->getCategoryPriority( 'category_one' ) );
|
|
$this->assertEquals( 10, $manager->getCategoryPriority( 'category_two' ) );
|
|
$this->assertEquals( 10, $manager->getCategoryPriority( 'category_three' ) );
|
|
$this->assertEquals( 10, $manager->getCategoryPriority( 'category_four' ) );
|
|
}
|
|
|
|
public function testGetNotificationPriority() {
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_one'
|
|
),
|
|
'event_two' => array (
|
|
'category' => 'category_two'
|
|
),
|
|
'event_three' => array (
|
|
'category' => 'category_three'
|
|
),
|
|
'event_four' => array (
|
|
'category' => 'category_four'
|
|
)
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 6
|
|
),
|
|
'category_two' => array (
|
|
'priority' => 100
|
|
),
|
|
'category_three' => array (
|
|
'priority' => -10
|
|
),
|
|
'category_four' => array ()
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertEquals( 6, $manager->getNotificationPriority( 'event_one' ) );
|
|
$this->assertEquals( 10, $manager->getNotificationPriority( 'event_two' ) );
|
|
$this->assertEquals( 10, $manager->getNotificationPriority( 'event_three' ) );
|
|
$this->assertEquals( 10, $manager->getNotificationPriority( 'event_four' ) );
|
|
}
|
|
|
|
public function testGetUserEnabledEvents() {
|
|
$notif = array(
|
|
'event_one' => array (
|
|
'category' => 'category_one'
|
|
),
|
|
'event_two' => array (
|
|
'category' => 'category_two'
|
|
),
|
|
'event_three' => array (
|
|
'category' => 'category_three'
|
|
),
|
|
);
|
|
$category = array(
|
|
'category_one' => array (
|
|
'priority' => 10,
|
|
'usergroups' => array(
|
|
'sysop'
|
|
)
|
|
),
|
|
'category_two' => array (
|
|
'priority' => 10,
|
|
'usergroups' => array(
|
|
'echo_group'
|
|
)
|
|
),
|
|
'category_three' => array (
|
|
'priority' => 10,
|
|
),
|
|
);
|
|
$manager = new EchoAttributeManager( $notif, $category );
|
|
$this->assertEquals( $manager->getUserEnabledEvents( $this->mockUser(), 'web' ), array( 'event_two', 'event_three' ) );
|
|
}
|
|
|
|
/**
|
|
* Mock object of User
|
|
*/
|
|
protected function mockUser() {
|
|
$user = $this->getMockBuilder( 'User' )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$user->expects( $this->any() )
|
|
->method( 'getID' )
|
|
->will( $this->returnValue( 1 ) );
|
|
$user->expects( $this->any() )
|
|
->method( 'getOption' )
|
|
->will( $this->returnValue( true ) );
|
|
$user->expects( $this->any() )
|
|
->method( 'getGroups' )
|
|
->will( $this->returnValue( array( 'echo_group' ) ) );
|
|
return $user;
|
|
}
|
|
}
|