mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-11-23 23:44:53 +00:00
Introduce AttributeManager class
This is a precursor to splitting notifications into alert and message sections. Change-Id: Ic685f7026ab9b41407b51317780bbfadd05bf9f1
This commit is contained in:
parent
77239e17de
commit
267a56398e
1
Echo.php
1
Echo.php
|
@ -56,6 +56,7 @@ $wgAutoloadClasses['MWDbEchoEmailBatch'] = $dir . 'includes/DbEmailBatch.php';
|
|||
$wgAutoloadClasses['MWEchoEmailBundler'] = $dir . 'includes/EmailBundler.php';
|
||||
$wgAutoloadClasses['MWDbEchoEmailBundler'] = $dir . 'includes/DbEmailBundler.php';
|
||||
$wgAutoloadClasses['MWEchoEventLogging'] = $dir . 'includes/EventLogging.php';
|
||||
$wgAutoloadClasses['EchoAttributeManager'] = $dir . 'includes/AttributeManager.php';
|
||||
|
||||
// Database mappers && gateways
|
||||
$wgAutoloadClasses['EchoEventMapper'] = $dir . 'includes/mapper/EventMapper.php';
|
||||
|
|
|
@ -333,9 +333,10 @@ class EchoHooks {
|
|||
if ( isset( $categoryData['no-dismiss'] ) && in_array( 'all' , $categoryData['no-dismiss'] ) ) {
|
||||
continue;
|
||||
}
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
// See if user is eligible to recieve this notification (per user group restrictions)
|
||||
if ( EchoNotificationController::getCategoryEligibility( $user, $category ) ) {
|
||||
$categoriesAndPriorities[$category] = EchoNotificationController::getCategoryPriority( $category );
|
||||
if ( $attributeManager->getCategoryEligibility( $user, $category ) ) {
|
||||
$categoriesAndPriorities[$category] = $attributeManager->getCategoryPriority( $category );
|
||||
}
|
||||
}
|
||||
asort( $categoriesAndPriorities );
|
||||
|
@ -732,9 +733,11 @@ class EchoHooks {
|
|||
// If a user is watching his/her own talk page, do not send talk page watchlist
|
||||
// email notification if the user is receiving Echo talk page notification
|
||||
if ( $title->isTalkPage() && $targetUser->getTalkPage()->equals( $title ) ) {
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$events = $attributeManager->getUserEnabledEvents( $targetUser, 'email' );
|
||||
if (
|
||||
!self::isEchoDisabled( $targetUser )
|
||||
&& in_array( 'edit-user-talk', EchoNotificationController::getUserEnabledEvents( $targetUser, 'email' ) )
|
||||
&& in_array( 'edit-user-talk', $events )
|
||||
) {
|
||||
// Do not send watchlist email notification, the user will receive an Echo notification
|
||||
return false;
|
||||
|
|
|
@ -12,7 +12,8 @@ class EchoNotifier {
|
|||
public static function notifyWithNotification( $user, $event ) {
|
||||
// Only create the notification if the user wants to recieve that type
|
||||
// of notification and they are eligible to recieve it. See bug 47664.
|
||||
$userWebNotifications = EchoNotificationController::getUserEnabledEvents( $user, 'web' );
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
|
||||
if ( !in_array( $event->getType(), $userWebNotifications ) ) {
|
||||
return;
|
||||
}
|
||||
|
@ -40,11 +41,13 @@ class EchoNotifier {
|
|||
return false;
|
||||
}
|
||||
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' );
|
||||
// See if the user wants to receive emails for this category or the user is eligible to receive this email
|
||||
if ( in_array( $event->getType(), EchoNotificationController::getUserEnabledEvents( $user, 'email' ) ) ) {
|
||||
if ( in_array( $event->getType(), $userEmailNotifications ) ) {
|
||||
global $wgEchoEnableEmailBatch, $wgEchoNotifications, $wgNotificationSender, $wgNotificationSenderName, $wgNotificationReplyName, $wgEchoBundleEmailInterval;
|
||||
|
||||
$priority = EchoNotificationController::getNotificationPriority( $event->getType() );
|
||||
$priority = $attributeManager->getNotificationPriority( $event->getType() );
|
||||
|
||||
$bundleString = $bundleHash = '';
|
||||
|
||||
|
|
|
@ -19,11 +19,12 @@ class ApiEchoNotifications extends ApiQueryBase {
|
|||
|
||||
$prop = $params['prop'];
|
||||
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$result = array();
|
||||
if ( in_array( 'list', $prop ) ) {
|
||||
$result = $this->getPropList(
|
||||
$user,
|
||||
EchoNotificationController::getUserEnabledEvents( $user, 'web' ),
|
||||
$attributeManager->getUserEnabledEvents( $user, 'web' ),
|
||||
$params['limit'], $params['continue'], $params['format']
|
||||
);
|
||||
$this->getResult()->setIndexedTagName( $result['list'], 'notification' );
|
||||
|
@ -66,7 +67,7 @@ class ApiEchoNotifications extends ApiQueryBase {
|
|||
|
||||
// Fetch the result for the event types above
|
||||
$notifMapper = new EchoNotificationMapper( MWEchoDbFactory::newFromDefault() );
|
||||
$notifs = $notifMapper->fetchByUser( $user, $limit + 1, $continue, 'web' );
|
||||
$notifs = $notifMapper->fetchByUser( $user, $limit + 1, $continue, $eventTypesToLoad );
|
||||
foreach ( $notifs as $notif ) {
|
||||
$result['list'][$notif->getEvent()->getID()] = EchoDataOutputFormatter::formatOutput( $notif, $format, $user );
|
||||
}
|
||||
|
|
|
@ -7,108 +7,6 @@ class EchoNotificationController {
|
|||
static protected $blacklist;
|
||||
static protected $userWhitelist;
|
||||
|
||||
/**
|
||||
* Get the enabled events for a user, which excludes user-dismissed events
|
||||
* from the general enabled events
|
||||
* @param $user User
|
||||
* @param $outputFormat string
|
||||
* @return array
|
||||
*/
|
||||
public static function getUserEnabledEvents( $user, $outputFormat ) {
|
||||
global $wgEchoNotifications;
|
||||
$eventTypesToLoad = $wgEchoNotifications;
|
||||
foreach ( $eventTypesToLoad as $eventType => $eventData ) {
|
||||
$category = self::getNotificationCategory( $eventType );
|
||||
// Make sure the user is eligible to recieve this type of notification
|
||||
if ( !self::getCategoryEligibility( $user, $category ) ) {
|
||||
unset( $eventTypesToLoad[$eventType] );
|
||||
}
|
||||
if ( !$user->getOption( 'echo-subscriptions-' . $outputFormat . '-' . $category ) ) {
|
||||
unset( $eventTypesToLoad[$eventType] );
|
||||
}
|
||||
}
|
||||
return array_keys( $eventTypesToLoad );
|
||||
}
|
||||
|
||||
/**
|
||||
* See if a user is eligible to recieve a certain type of notification
|
||||
* (based on user groups, not user preferences)
|
||||
*
|
||||
* @param $user User object
|
||||
* @param $notificationType string A notification type defined in $wgEchoNotifications
|
||||
* @return boolean
|
||||
*/
|
||||
public static function getNotificationEligibility( $user, $notificationType ) {
|
||||
$category = self::getNotificationCategory( $notificationType );
|
||||
return self::getCategoryEligibility( $user, $category );
|
||||
}
|
||||
|
||||
/**
|
||||
* See if a user is eligible to recieve a certain type of notification
|
||||
* (based on user groups, not user preferences)
|
||||
*
|
||||
* @param $user User object
|
||||
* @param $category string A notification category defined in $wgEchoNotificationCategories
|
||||
* @return boolean
|
||||
*/
|
||||
public static function getCategoryEligibility( $user, $category ) {
|
||||
global $wgEchoNotificationCategories;
|
||||
$usersGroups = $user->getGroups();
|
||||
if ( isset( $wgEchoNotificationCategories[$category]['usergroups'] ) ) {
|
||||
$allowedGroups = $wgEchoNotificationCategories[$category]['usergroups'];
|
||||
if ( !array_intersect( $usersGroups, $allowedGroups ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority for a specific notification type
|
||||
*
|
||||
* @param $notificationType string A notification type defined in $wgEchoNotifications
|
||||
* @return integer From 1 to 10 (10 is default)
|
||||
*/
|
||||
public static function getNotificationPriority( $notificationType ) {
|
||||
$category = self::getNotificationCategory( $notificationType );
|
||||
return self::getCategoryPriority( $category );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority for a notification category
|
||||
*
|
||||
* @param $category string A notification category defined in $wgEchoNotificationCategories
|
||||
* @return integer From 1 to 10 (10 is default)
|
||||
*/
|
||||
public static function getCategoryPriority( $category ) {
|
||||
global $wgEchoNotificationCategories;
|
||||
if ( isset( $wgEchoNotificationCategories[$category]['priority'] ) ) {
|
||||
$priority = $wgEchoNotificationCategories[$category]['priority'];
|
||||
if ( $priority >= 1 && $priority <= 10 ) {
|
||||
return $priority;
|
||||
}
|
||||
}
|
||||
return 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification category for a notification type
|
||||
*
|
||||
* @param $notificationType string A notification type defined in $wgEchoNotifications
|
||||
* @return String The name of the notification category or 'other' if no
|
||||
* category is explicitly assigned.
|
||||
*/
|
||||
public static function getNotificationCategory( $notificationType ) {
|
||||
global $wgEchoNotifications, $wgEchoNotificationCategories;
|
||||
if ( isset( $wgEchoNotifications[$notificationType]['category'] ) ) {
|
||||
$category = $wgEchoNotifications[$notificationType]['category'];
|
||||
if ( isset( $wgEchoNotificationCategories[$category] ) ) {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
return 'other';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the notification count with Language::formatNum(). In addition, for large count,
|
||||
* return abbreviated version, e.g. 99+
|
||||
|
|
153
includes/AttributeManager.php
Normal file
153
includes/AttributeManager.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* An object that manages attributes of echo notifications: category, elegibility,
|
||||
* group, section etc.
|
||||
*/
|
||||
class EchoAttributeManager {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $notifications;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $categories;
|
||||
|
||||
/**
|
||||
* An array of EchoAttributeManager instance created from global variables
|
||||
* @param EchoAttributeManager[]
|
||||
*/
|
||||
protected static $globalVarInstance = array();
|
||||
|
||||
/**
|
||||
* @param array notification attributes
|
||||
* @param array notification categories
|
||||
*/
|
||||
public function __construct( array $notifications, array $categories ) {
|
||||
// Extensions can define their own notifications and categories
|
||||
$this->notifications = $notifications;
|
||||
$this->categories = $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance from global variables
|
||||
* @return EchoAttributeManager
|
||||
*/
|
||||
public static function newFromGlobalVars() {
|
||||
global $wgEchoNotifications, $wgEchoNotificationCategories;
|
||||
|
||||
// Unit test may alter the global data for test purpose
|
||||
if ( defined( 'MW_PHPUNIT_TEST' ) && MW_PHPUNIT_TEST ) {
|
||||
return new self( $wgEchoNotifications, $wgEchoNotificationCategories );
|
||||
}
|
||||
// A job queue job may run against different wikis, the singleton
|
||||
// instance should be a per wiki singleton
|
||||
$wikiId = wfWikiId();
|
||||
if ( !isset( self::$globalVarInstance[$wikiId] ) ) {
|
||||
self::$globalVarInstance[$wikiId] = new self(
|
||||
$wgEchoNotifications,
|
||||
$wgEchoNotificationCategories
|
||||
);
|
||||
}
|
||||
return self::$globalVarInstance[$wikiId];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the enabled events for a user, which excludes user-dismissed events
|
||||
* from the general enabled events
|
||||
* @param User
|
||||
* @param string web/email
|
||||
* @return string[]
|
||||
*/
|
||||
public function getUserEnabledEvents( User $user, $outputFormat ) {
|
||||
$eventTypesToLoad = $this->notifications;
|
||||
foreach ( $eventTypesToLoad as $eventType => $eventData ) {
|
||||
$category = $this->getNotificationCategory( $eventType );
|
||||
// Make sure the user is eligible to recieve this type of notification
|
||||
if ( !$this->getCategoryEligibility( $user, $category ) ) {
|
||||
unset( $eventTypesToLoad[$eventType] );
|
||||
}
|
||||
if ( !$user->getOption( 'echo-subscriptions-' . $outputFormat . '-' . $category ) ) {
|
||||
unset( $eventTypesToLoad[$eventType] );
|
||||
}
|
||||
}
|
||||
$eventTypes = array_keys( $eventTypesToLoad );
|
||||
|
||||
return $eventTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get alert notification event. Notifications without a section attributes
|
||||
* default to section alert
|
||||
* @return array
|
||||
*/
|
||||
public function getEvents() {
|
||||
return array_keys( $this->notifications );
|
||||
}
|
||||
|
||||
/**
|
||||
* See if a user is eligible to recieve a certain type of notification
|
||||
* (based on user groups, not user preferences)
|
||||
*
|
||||
* @param User
|
||||
* @param string A notification category defined in $wgEchoNotificationCategories
|
||||
* @return boolean
|
||||
*/
|
||||
public function getCategoryEligibility( $user, $category ) {
|
||||
$usersGroups = $user->getGroups();
|
||||
if ( isset( $this->categories[$category]['usergroups'] ) ) {
|
||||
$allowedGroups = $this->categories[$category]['usergroups'];
|
||||
if ( !array_intersect( $usersGroups, $allowedGroups ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority for a specific notification type
|
||||
*
|
||||
* @param string A notification type defined in $wgEchoNotifications
|
||||
* @return integer From 1 to 10 (10 is default)
|
||||
*/
|
||||
public function getNotificationPriority( $notificationType ) {
|
||||
$category = $this->getNotificationCategory( $notificationType );
|
||||
return $this->getCategoryPriority( $category );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority for a notification category
|
||||
*
|
||||
* @param string A notification category defined in $wgEchoNotificationCategories
|
||||
* @return integer From 1 to 10 (10 is default)
|
||||
*/
|
||||
public function getCategoryPriority( $category ) {
|
||||
if ( isset( $this->categories[$category]['priority'] ) ) {
|
||||
$priority = $this->categories[$category]['priority'];
|
||||
if ( $priority >= 1 && $priority <= 10 ) {
|
||||
return $priority;
|
||||
}
|
||||
}
|
||||
return 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification category for a notification type
|
||||
*
|
||||
* @param string A notification type defined in $wgEchoNotifications
|
||||
* @return string The name of the notification category or 'other' if no
|
||||
* category is explicitly assigned.
|
||||
*/
|
||||
public function getNotificationCategory( $notificationType ) {
|
||||
if ( isset( $this->notifications[$notificationType]['category'] ) ) {
|
||||
$category = $this->notifications[$notificationType]['category'];
|
||||
if ( isset( $this->categories[$category] ) ) {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
return 'other';
|
||||
}
|
||||
}
|
|
@ -137,7 +137,9 @@ class MWEchoNotifUser {
|
|||
return (int)$this->cache->get( $memcKey );
|
||||
}
|
||||
|
||||
$count = $this->userNotifGateway->getNotificationCount( $dbSource );
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$eventTypesToLoad = $attributeManager->getUserEnabledEvents( $this->mUser, 'web' );
|
||||
$count = $this->userNotifGateway->getNotificationCount( $dbSource, $eventTypesToLoad );
|
||||
|
||||
$this->cache->set( $memcKey, $count, 86400 );
|
||||
|
||||
|
|
|
@ -74,17 +74,17 @@ class EchoUserNotificationGateway {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param $dbSource string use master or slave storage to pull count
|
||||
* Get notification count for the types specified
|
||||
* @param int use master or slave storage to pull count
|
||||
* @param array event types to retrieve
|
||||
* @return int
|
||||
*/
|
||||
public function getNotificationCount( $dbSource ) {
|
||||
public function getNotificationCount( $dbSource, array $eventTypesToLoad = array() ) {
|
||||
// double check
|
||||
if ( !in_array( $dbSource, array( DB_SLAVE, DB_MASTER ) ) ) {
|
||||
$dbSource = DB_SLAVE;
|
||||
}
|
||||
|
||||
$eventTypesToLoad = EchoNotificationController::getUserEnabledEvents( $this->user, 'web' );
|
||||
|
||||
if ( !$eventTypesToLoad ) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -89,17 +89,23 @@ class EchoNotificationMapper {
|
|||
* @param User the user to get notifications for
|
||||
* @param int The maximum number of notifications to return
|
||||
* @param string Used for offset
|
||||
* @param string Notification distribution type ( web, email, etc.)
|
||||
* @param array Event types to load
|
||||
* @return EchoNotification[]
|
||||
*/
|
||||
public function fetchByUser( User $user, $limit, $continue, $distributionType = 'web' ) {
|
||||
public function fetchByUser( User $user, $limit, $continue, array $eventTypesToLoad = array() ) {
|
||||
$dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
|
||||
|
||||
$eventTypesToLoad = EchoNotificationController::getUserEnabledEvents( $user, $distributionType );
|
||||
if ( !$eventTypesToLoad ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// There is a problem with querying by event type, if a user has only one or none
|
||||
// flow notification and huge amount other notications, the lookup of only flow
|
||||
// notification will result in a slow query. Luckily users won't have that many
|
||||
// notifications. We should have some cron job to remove old notifications so
|
||||
// the notification volume is in a reasonable amount for such case. The other option
|
||||
// is to denormalize notification table with event_type and lookup index.
|
||||
//
|
||||
// Look for notifications with base = 1
|
||||
$conds = array(
|
||||
'notification_user' => $user->getID(),
|
||||
|
|
|
@ -422,7 +422,8 @@ class EchoEvent {
|
|||
* @return string
|
||||
*/
|
||||
public function getCategory() {
|
||||
return EchoNotificationController::getNotificationCategory( $this->type );
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
return $attributeManager->getNotificationCategory( $this->type );
|
||||
}
|
||||
|
||||
public function setType( $type ) {
|
||||
|
|
|
@ -20,13 +20,14 @@ class SpecialNotifications extends SpecialPage {
|
|||
|
||||
$user = $this->getUser();
|
||||
if ( $user->isAnon() ) {
|
||||
$notificationsPageName = $this->getPageTitle()->getPrefixedDBkey();
|
||||
$returnTo = array( 'returnto' => $notificationsPageName );
|
||||
$signupTitle = SpecialPage::getTitleFor( 'UserLogin', 'signup' );
|
||||
$signupURL = $signupTitle->getFullURL( $returnTo );
|
||||
$loginTitle = SpecialPage::getTitleFor( 'UserLogin' );
|
||||
$loginURL = $loginTitle->getFullURL( $returnTo );
|
||||
$anonMsgHtml = $this->msg( 'echo-anon', $signupURL, $loginURL )->parse();
|
||||
// return to this title upon login
|
||||
$returnTo = array( 'returnto' => $this->getPageTitle()->getPrefixedDBkey() );
|
||||
// the html message for anon users
|
||||
$anonMsgHtml = $this->msg(
|
||||
'echo-anon',
|
||||
SpecialPage::getTitleFor( 'UserLogin', 'signup' )->getFullURL( $returnTo ),
|
||||
SpecialPage::getTitleFor( 'UserLogin' )->getFullURL( $returnTo )
|
||||
)->parse();
|
||||
$out->addHTML( Html::rawElement( 'span', array( 'class' => 'plainlinks' ), $anonMsgHtml ) );
|
||||
return;
|
||||
}
|
||||
|
@ -40,7 +41,14 @@ class SpecialNotifications extends SpecialPage {
|
|||
// Pull the notifications
|
||||
$notif = array();
|
||||
$notificationMapper = new EchoNotificationMapper( MWEchoDbFactory::newFromDefault() );
|
||||
$notifications = $notificationMapper->fetchByUser( $user, self::$displayNum + 1, $continue, 'web' );
|
||||
|
||||
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$notifications = $notificationMapper->fetchByUser(
|
||||
$user,
|
||||
/* $limit = */self::$displayNum + 1,
|
||||
$continue,
|
||||
$attributeManager->getUserEnabledEvents( $user, 'web' )
|
||||
);
|
||||
foreach ( $notifications as $notification ) {
|
||||
$notif[] = EchoDataOutputFormatter::formatOutput( $notification, 'html', $user );
|
||||
}
|
||||
|
|
|
@ -69,6 +69,7 @@ class EchoEmailFormatterTest extends MediaWikiTestCase {
|
|||
$methods = get_class_methods( 'EchoEvent' );
|
||||
$methods = array_diff( $methods, array( 'userCan', 'getLinkMessage', 'getLinkDestination' ) );
|
||||
|
||||
$attribManager = EchoAttributeManager::newFromGlobalVars();
|
||||
$event = $this->getMockBuilder( 'EchoEvent' )
|
||||
->disableOriginalConstructor()
|
||||
->setMethods( $methods )
|
||||
|
@ -78,7 +79,7 @@ class EchoEmailFormatterTest extends MediaWikiTestCase {
|
|||
->will( $this->returnValue( $type ) );
|
||||
$event->expects( $this->any() )
|
||||
->method( 'getCategory' )
|
||||
->will( $this->returnValue( EchoNotificationController::getNotificationCategory( $type ) ) );
|
||||
->will( $this->returnValue( $attribManager->getNotificationCategory( $type ) ) );
|
||||
return $event;
|
||||
}
|
||||
|
||||
|
|
175
tests/includes/AttributeManagerTest.php
Normal file
175
tests/includes/AttributeManagerTest.php
Normal file
|
@ -0,0 +1,175 @@
|
|||
<?php
|
||||
|
||||
class EchoAttributeManagerTest extends MediaWikiTestCase {
|
||||
|
||||
public function testNewFromGlobalVars() {
|
||||
$this->assertInstanceOf( 'EchoAttributeManager', EchoAttributeManager::newFromGlobalVars() );
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -27,35 +27,21 @@ class EchoUserNotificationGatewayTest extends MediaWikiTestCase {
|
|||
}
|
||||
|
||||
public function testGetNotificationCount() {
|
||||
global $wgEchoNotificationCategories;
|
||||
$previous = $wgEchoNotificationCategories;
|
||||
|
||||
// Alter the category group so the user is always elegible to
|
||||
// view some notification types.
|
||||
foreach ( $wgEchoNotificationCategories as &$value ) {
|
||||
$value['usergroups'] = array( 'echo_group' );
|
||||
}
|
||||
unset( $value );
|
||||
|
||||
// successful select
|
||||
// unsuccessful select
|
||||
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( array( 'select' => false ) ) );
|
||||
$this->assertEquals( 0, $gateway->getNotificationCount( DB_SLAVE ) );
|
||||
$this->assertEquals( 0, $gateway->getNotificationCount( DB_SLAVE, array( 'event_one' ) ) );
|
||||
|
||||
// successful select of alert
|
||||
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( array( 'select' => array( 1, 2 ) ) ) );
|
||||
$this->assertEquals( 2, $gateway->getNotificationCount( DB_SLAVE, array( 'event_one', 'event_two' ) ) );
|
||||
|
||||
// there is event, should return 0
|
||||
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( array( 'select' => array( 1, 2 ) ) ) );
|
||||
$this->assertEquals( 0, $gateway->getNotificationCount( DB_SLAVE, array() ) );
|
||||
|
||||
// successful select
|
||||
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( array( 'select' => array( 1, 2, 3 ) ) ) );
|
||||
$this->assertEquals( 3, $gateway->getNotificationCount( DB_SLAVE ) );
|
||||
|
||||
// Alter the category group so the user is not elegible to
|
||||
// view any notification types.
|
||||
foreach ( $wgEchoNotificationCategories as &$value ) {
|
||||
$value['usergroups'] = array( 'sysop' );
|
||||
}
|
||||
unset( $value );
|
||||
|
||||
$gateway = new EchoUserNotificationGateway( $this->mockUser(), $this->mockMWEchoDbFactory( array( 'select' => array( 1, 2, 3 ) ) ) );
|
||||
$this->assertEquals( 0, $gateway->getNotificationCount( DB_SLAVE ) );
|
||||
|
||||
$wgEchoNotificationCategories = $previous;
|
||||
$this->assertEquals( 3, $gateway->getNotificationCount( DB_SLAVE, array( 'event_one' ) ) );
|
||||
}
|
||||
|
||||
public function testGetUnreadNotifications() {
|
||||
|
@ -75,7 +61,7 @@ class EchoUserNotificationGatewayTest extends MediaWikiTestCase {
|
|||
/**
|
||||
* Mock object of User
|
||||
*/
|
||||
protected function mockUser() {
|
||||
protected function mockUser( $group = 'echo_group' ) {
|
||||
$user = $this->getMockBuilder( 'User' )
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
@ -87,7 +73,7 @@ class EchoUserNotificationGatewayTest extends MediaWikiTestCase {
|
|||
->will( $this->returnValue( true ) );
|
||||
$user->expects( $this->any() )
|
||||
->method( 'getGroups' )
|
||||
->will( $this->returnValue( array( 'echo_group' ) ) );
|
||||
->will( $this->returnValue( array( $group ) ) );
|
||||
return $user;
|
||||
}
|
||||
|
||||
|
|
|
@ -44,8 +44,13 @@ class EchoNotificationMapperTest extends MediaWikiTestCase {
|
|||
)
|
||||
);
|
||||
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( array ( 'select' => $dbResult ) ) );
|
||||
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '' );
|
||||
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', array() );
|
||||
$this->assertEmpty( $res );
|
||||
|
||||
$notifMapper = new EchoNotificationMapper( $this->mockMWEchoDbFactory( array ( 'select' => $dbResult ) ) );
|
||||
$res = $notifMapper->fetchByUser( $this->mockUser(), 10, '', array( 'test_event' ) );
|
||||
$this->assertTrue( is_array( $res ) );
|
||||
$this->assertGreaterThan( 0, count( $res ) );
|
||||
foreach ( $res as $row ) {
|
||||
$this->assertInstanceOf( 'EchoNotification', $row );
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue