Make user notifications pluggable

This commit is contained in:
Frederic Guillot 2015-10-17 09:51:15 -04:00
parent 98b203fe69
commit 73a5b9bc75
25 changed files with 407 additions and 258 deletions

View File

@ -198,7 +198,7 @@ class App extends Base
$this->response->html($this->layout('app/notifications', array(
'title' => t('My notifications'),
'notifications' => $this->webNotification->getAll($user['id']),
'notifications' => $this->userUnreadNotification->getAll($user['id']),
'user' => $user,
)));
}

View File

@ -96,7 +96,7 @@ class User extends Base
$this->projectPermission->addMember($project_id, $user_id);
if (! empty($values['notifications_enabled'])) {
$this->notificationType->saveUserSelectedTypes($user_id, array(NotificationType::TYPE_EMAIL));
$this->userNotificationType->saveSelectedTypes($user_id, array(NotificationType::TYPE_EMAIL));
}
$this->session->flash(t('User created successfully.'));
@ -201,16 +201,16 @@ class User extends Base
if ($this->request->isPost()) {
$values = $this->request->getValues();
$this->notification->saveSettings($user['id'], $values);
$this->userNotification->saveSettings($user['id'], $values);
$this->session->flash(t('User updated successfully.'));
$this->response->redirect($this->helper->url->to('user', 'notifications', array('user_id' => $user['id'])));
}
$this->response->html($this->layout('user/notifications', array(
'projects' => $this->projectPermission->getMemberProjects($user['id']),
'notifications' => $this->notification->readSettings($user['id']),
'types' => $this->notificationType->getTypes(),
'filters' => $this->notificationFilter->getFilters(),
'notifications' => $this->userNotification->readSettings($user['id']),
'types' => $this->userNotificationType->getTypes(),
'filters' => $this->userNotificationFilter->getFilters(),
'user' => $user,
)));
}

View File

@ -8,7 +8,7 @@ namespace Kanboard\Controller;
* @package controller
* @author Frederic Guillot
*/
class Webnotification extends Base
class WebNotification extends Base
{
/**
* Mark all notifications as read
@ -17,9 +17,9 @@ class Webnotification extends Base
*/
public function flush()
{
$user_id = $this->userSession->getId();
$user_id = $this->getUserId();
$this->webNotification->markAllAsRead($user_id);
$this->userUnreadNotification->markAllAsRead($user_id);
$this->response->redirect($this->helper->url->to('app', 'notifications', array('user_id' => $user_id)));
}
@ -30,10 +30,21 @@ class Webnotification extends Base
*/
public function remove()
{
$user_id = $this->userSession->getId();
$user_id = $this->getUserId();
$notification_id = $this->request->getIntegerParam('notification_id');
$this->webNotification->markAsRead($user_id, $notification_id);
$this->userUnreadNotification->markAsRead($user_id, $notification_id);
$this->response->redirect($this->helper->url->to('app', 'notifications', array('user_id' => $user_id)));
}
private function getUserId()
{
$user_id = $this->request->getIntegerParam('user_id');
if (! $this->userSession->isAdmin() && $user_id != $this->userSession->getId()) {
$user_id = $this->userSession->getId();
}
return $user_id;
}
}

View File

@ -49,11 +49,11 @@ use Pimple\Container;
* @property \Kanboard\Model\File $file
* @property \Kanboard\Model\LastLogin $lastLogin
* @property \Kanboard\Model\Link $link
* @property \Kanboard\Model\Notification $notification
* @property \Kanboard\Model\NotificationType $notificationType
* @property \Kanboard\Model\NotificationFilter $notificationFilter
* @property \Kanboard\Model\UserNotification $userNotification
* @property \Kanboard\Model\UserNotificationType $userNotificationType
* @property \Kanboard\Model\UserNotificationFilter $userNotificationFilter
* @property \Kanboard\Model\UserUnreadNotification $userUnreadNotification
* @property \Kanboard\Model\OverdueNotification $overdueNotification
* @property \Kanboard\Model\WebNotification $webNotification
* @property \Kanboard\Model\Project $project
* @property \Kanboard\Model\ProjectActivity $projectActivity
* @property \Kanboard\Model\ProjectAnalytic $projectAnalytic

View File

@ -1,22 +0,0 @@
<?php
namespace Kanboard\Core;
/**
* Notification Interface
*
* @package core
* @author Frederic Guillot
*/
interface NotificationInterface
{
/**
* Send notification to someone
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data);
}

View File

@ -18,7 +18,7 @@ class User extends \Kanboard\Core\Base
*/
public function hasNotifications()
{
return $this->webNotification->hasNotifications($this->userSession->getId());
return $this->userUnreadNotification->hasNotifications($this->userSession->getId());
}
/**

View File

@ -2,72 +2,82 @@
namespace Kanboard\Model;
use Pimple\Container;
/**
* Notification Type model
* Notification Type
*
* @package model
* @author Frederic Guillot
*/
class NotificationType extends Base
abstract class NotificationType extends Base
{
/**
* SQL table name
* Mail transport instances
*
* @var string
* @access private
* @var \Pimple\Container
*/
const TABLE = 'user_has_notification_types';
private $classes;
/**
* Types
* Mail transport instances
*
* @var string
* @access private
* @var array
*/
const TYPE_WEB = 'web';
const TYPE_EMAIL = 'email';
private $labels = array();
/**
* Get all notification types
* Constructor
*
* @access public
* @param \Pimple\Container $container
*/
public function __construct(Container $container)
{
parent::__construct($container);
$this->classes = new Container;
}
/**
* Add a new notification type
*
* @access public
* @param string $type
* @param string $label
* @param string $class
*/
public function setType($type, $label, $class)
{
$container = $this->container;
$this->labels[$type] = $label;
$this->classes[$type] = function() use ($class, $container) {
return new $class($container);
};
}
/**
* Get mail notification type instance
*
* @access public
* @param string $type
* @return NotificationInterface
*/
public function getType($type)
{
return $this->classes[$type];
}
/**
* Get all notification types with labels
*
* @access public
* @return array
*/
public function getTypes()
{
return array(
self::TYPE_EMAIL => t('Email'),
self::TYPE_WEB => t('Web'),
);
}
/**
* Get selected notification types for a given user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getUserSelectedTypes($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('notification_type')->findAllByColumn('notification_type');
}
/**
* Save notification types for a given user
*
* @access public
* @param integer $user_id
* @param string[] $types
* @return boolean
*/
public function saveUserSelectedTypes($user_id, array $types)
{
$results = array();
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
foreach ($types as $type) {
$results[] = $this->db->table(self::TABLE)->insert(array('user_id' => $user_id, 'notification_type' => $type));
}
return ! in_array(false, $results);
return $this->labels;
}
}

View File

@ -22,7 +22,7 @@ class OverdueNotification extends Base
foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
// Get the list of users that should receive notifications for each projects
$users = $this->notification->getUsersWithNotificationEnabled($project_id);
$users = $this->userNotification->getUsersWithNotificationEnabled($project_id);
foreach ($users as $user) {
$this->sendUserOverdueTaskNotifications($user, $project_tasks);
@ -44,13 +44,13 @@ class OverdueNotification extends Base
$user_tasks = array();
foreach ($tasks as $task) {
if ($this->notificationFilter->shouldReceiveNotification($user, array('task' => $task))) {
if ($this->userNotificationFilter->shouldReceiveNotification($user, array('task' => $task))) {
$user_tasks[] = $task;
}
}
if (! empty($user_tasks)) {
$this->notification->sendUserNotification(
$this->userNotification->sendUserNotification(
$user,
Task::EVENT_OVERDUE,
array('tasks' => $user_tasks, 'project_name' => $tasks[0]['project_name'])

View File

@ -5,12 +5,12 @@ namespace Kanboard\Model;
use Kanboard\Core\Translator;
/**
* Notification model
* User Notification
*
* @package model
* @author Frederic Guillot
*/
class Notification extends Base
class UserNotification extends Base
{
/**
* Send notifications to people
@ -22,12 +22,11 @@ class Notification extends Base
public function sendNotifications($event_name, array $event_data)
{
$logged_user_id = $this->userSession->isLogged() ? $this->userSession->getId() : 0;
$users = $this->notification->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
$users = $this->getUsersWithNotificationEnabled($event_data['task']['project_id'], $logged_user_id);
if (! empty($users)) {
foreach ($users as $user) {
if ($this->notificationFilter->shouldReceiveNotification($user, $event_data)) {
if ($this->userNotificationFilter->shouldReceiveNotification($user, $event_data)) {
$this->sendUserNotification($user, $event_name, $event_data);
}
}
@ -57,9 +56,8 @@ class Notification extends Base
Translator::load($this->config->get('application_language', 'en_US'));
}
foreach ($this->notificationType->getUserSelectedTypes($user['id']) as $type) {
$className = strtolower($type).'Notification';
$this->$className->send($user, $event_name, $event_data);
foreach ($this->userNotificationType->getSelectedTypes($user['id']) as $type) {
$this->userNotificationType->getType($type)->notifyUser($user, $event_name, $event_data);
}
}
@ -118,13 +116,13 @@ class Notification extends Base
if (isset($values['notifications_enabled']) && $values['notifications_enabled'] == 1) {
$this->enableNotification($user_id);
$filter = empty($values['notifications_filter']) ? NotificationFilter::FILTER_BOTH : $values['notifications_filter'];
$filter = empty($values['notifications_filter']) ? UserNotificationFilter::FILTER_BOTH : $values['notifications_filter'];
$projects = empty($values['notification_projects']) ? array() : array_keys($values['notification_projects']);
$types = empty($values['notification_types']) ? array() : array_keys($values['notification_types']);
$this->notificationFilter->saveUserFilter($user_id, $filter);
$this->notificationFilter->saveUserSelectedProjects($user_id, $projects);
$this->notificationType->saveUserSelectedTypes($user_id, $types);
$this->userNotificationFilter->saveFilter($user_id, $filter);
$this->userNotificationFilter->saveSelectedProjects($user_id, $projects);
$this->userNotificationType->saveSelectedTypes($user_id, $types);
}
else {
$this->disableNotification($user_id);
@ -143,8 +141,8 @@ class Notification extends Base
public function readSettings($user_id)
{
$values = $this->db->table(User::TABLE)->eq('id', $user_id)->columns('notifications_enabled', 'notifications_filter')->findOne();
$values['notification_types'] = $this->notificationType->getUserSelectedTypes($user_id);
$values['notification_projects'] = $this->notificationFilter->getUserSelectedProjects($user_id);
$values['notification_types'] = $this->userNotificationType->getSelectedTypes($user_id);
$values['notification_projects'] = $this->userNotificationFilter->getSelectedProjects($user_id);
return $values;
}

View File

@ -3,12 +3,12 @@
namespace Kanboard\Model;
/**
* Notification Filter model
* User Notification Filter
*
* @package model
* @author Frederic Guillot
*/
class NotificationFilter extends Base
class UserNotificationFilter extends Base
{
/**
* SQL table name
@ -50,7 +50,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @return integer
*/
public function getUserSelectedFilter($user_id)
public function getSelectedFilter($user_id)
{
return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('notifications_filter');
}
@ -62,7 +62,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @param string $filter
*/
public function saveUserFilter($user_id, $filter)
public function saveFilter($user_id, $filter)
{
$this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
'notifications_filter' => $filter,
@ -76,7 +76,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @return array
*/
public function getUserSelectedProjects($user_id)
public function getSelectedProjects($user_id)
{
return $this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->findAllByColumn('project_id');
}
@ -88,7 +88,7 @@ class NotificationFilter extends Base
* @param integer $user_id
* @param integer[] $project_ids
*/
public function saveUserSelectedProjects($user_id, array $project_ids)
public function saveSelectedProjects($user_id, array $project_ids)
{
$this->db->table(self::PROJECT_TABLE)->eq('user_id', $user_id)->remove();
@ -188,7 +188,7 @@ class NotificationFilter extends Base
*/
public function filterProject(array $user, array $event_data)
{
$projects = $this->getUserSelectedProjects($user['id']);
$projects = $this->getSelectedProjects($user['id']);
if (! empty($projects)) {
return in_array($event_data['task']['project_id'], $projects);

View File

@ -0,0 +1,51 @@
<?php
namespace Kanboard\Model;
/**
* User Notification Type
*
* @package model
* @author Frederic Guillot
*/
class UserNotificationType extends NotificationType
{
/**
* SQL table name
*
* @var string
*/
const TABLE = 'user_has_notification_types';
/**
* Get selected notification types for a given user
*
* @access public
* @param integer $user_id
* @return array
*/
public function getSelectedTypes($user_id)
{
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->asc('notification_type')->findAllByColumn('notification_type');
}
/**
* Save notification types for a given user
*
* @access public
* @param integer $user_id
* @param string[] $types
* @return boolean
*/
public function saveSelectedTypes($user_id, array $types)
{
$results = array();
$this->db->table(self::TABLE)->eq('user_id', $user_id)->remove();
foreach ($types as $type) {
$results[] = $this->db->table(self::TABLE)->insert(array('user_id' => $user_id, 'notification_type' => $type));
}
return ! in_array(false, $results);
}
}

View File

@ -2,15 +2,13 @@
namespace Kanboard\Model;
use Kanboard\Core\NotificationInterface;
/**
* Web Notification model
* User Unread Notification
*
* @package model
* @author Frederic Guillot
*/
class WebNotification extends Base implements NotificationInterface
class UserUnreadNotification extends Base
{
/**
* SQL table name
@ -23,14 +21,14 @@ class WebNotification extends Base implements NotificationInterface
* Add unread notification to someone
*
* @access public
* @param array $user
* @param integer $user_id
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
public function create($user_id, $event_name, array $event_data)
{
$this->db->table(self::TABLE)->insert(array(
'user_id' => $user['id'],
'user_id' => $user_id,
'date_creation' => time(),
'event_name' => $event_name,
'event_data' => json_encode($event_data),

View File

@ -1,26 +1,30 @@
<?php
namespace Kanboard\Model;
namespace Kanboard\Notification;
use Kanboard\Core\NotificationInterface;
use Kanboard\Core\Base;
use Kanboard\Model\Task;
use Kanboard\Model\File;
use Kanboard\Model\Comment;
use Kanboard\Model\Subtask;
/**
* Email Notification model
* Email Notification
*
* @package model
* @package notification
* @author Frederic Guillot
*/
class EmailNotification extends Base implements NotificationInterface
class Mail extends Base implements NotificationInterface
{
/**
* Send email notification to someone
* Send notification to a user
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function send(array $user, $event_name, array $event_data)
public function notifyUser(array $user, $event_name, array $event_data)
{
if (! empty($user['email'])) {
$this->emailClient->send(
@ -32,6 +36,19 @@ class EmailNotification extends Base implements NotificationInterface
}
}
/**
* Send notification to a project
*
* @access public
* @param array $project
* @param string $event_name
* @param array $event_data
*/
public function notifyProject(array $project, $event_name, array $event_data)
{
}
/**
* Get the mail content for a given template name
*

View File

@ -0,0 +1,32 @@
<?php
namespace Kanboard\Notification;
/**
* Notification Interface
*
* @package core
* @author Frederic Guillot
*/
interface NotificationInterface
{
/**
* Send notification to a user
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function notifyUser(array $user, $event_name, array $event_data);
/**
* Send notification to a project
*
* @access public
* @param array $project
* @param string $event_name
* @param array $event_data
*/
public function notifyProject(array $project, $event_name, array $event_data);
}

39
app/Notification/Web.php Normal file
View File

@ -0,0 +1,39 @@
<?php
namespace Kanboard\Notification;
use Kanboard\Core\Base;
/**
* Web Notification
*
* @package notification
* @author Frederic Guillot
*/
class Web extends Base implements NotificationInterface
{
/**
* Send notification to a user
*
* @access public
* @param array $user
* @param string $event_name
* @param array $event_data
*/
public function notifyUser(array $user, $event_name, array $event_data)
{
$this->userUnreadNotification->create($user['id'], $event_name, $event_data);
}
/**
* Send notification to a project
*
* @access public
* @param array $project
* @param string $event_name
* @param array $event_data
*/
public function notifyProject(array $project, $event_name, array $event_data)
{
}
}

View File

@ -8,9 +8,7 @@ use Kanboard\Core\ObjectStorage\FileStorage;
use Kanboard\Core\Paginator;
use Kanboard\Core\OAuth2;
use Kanboard\Core\Tool;
use Kanboard\Model\Config;
use Kanboard\Model\Project;
use Kanboard\Model\Webhook;
use Kanboard\Model\UserNotificationType;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use League\HTMLToMarkdown\HtmlConverter;
@ -32,12 +30,7 @@ class ClassProvider implements ServiceProviderInterface
'File',
'LastLogin',
'Link',
'Notification',
'NotificationType',
'NotificationFilter',
'OverdueNotification',
'WebNotification',
'EmailNotification',
'Project',
'ProjectActivity',
'ProjectAnalytic',
@ -68,6 +61,10 @@ class ClassProvider implements ServiceProviderInterface
'User',
'UserImport',
'UserSession',
'UserNotification',
'UserNotificationType',
'UserNotificationFilter',
'UserUnreadNotification',
'Webhook',
),
'Formatter' => array(
@ -131,6 +128,13 @@ class ClassProvider implements ServiceProviderInterface
return $mailer;
};
$container['userNotificationType'] = function($container) {
$type = new UserNotificationType($container);
$type->setType('email', t('Email'), '\Kanboard\Notification\Mail');
$type->setType('web', t('Web'), '\Kanboard\Notification\Web');
return $type;
};
$container['pluginLoader'] = new Loader($container);
$container['cspRules'] = array('style-src' => "'self' 'unsafe-inline'", 'img-src' => '* data:');

View File

@ -32,7 +32,7 @@ class NotificationSubscriber extends \Kanboard\Core\Base implements EventSubscri
public function execute(GenericEvent $event, $event_name)
{
$this->notification->sendNotifications($event_name, $this->getEventData($event));
$this->userNotification->sendNotifications($event_name, $this->getEventData($event));
}
public function getEventData(GenericEvent $event)

View File

@ -8,7 +8,7 @@
<ul>
<li>
<i class="fa fa-check-square-o fa-fw"></i>
<?= $this->url->link(t('Mark all as read'), 'webnotification', 'flush', array('user_id' => $user['id'])) ?>
<?= $this->url->link(t('Mark all as read'), 'webNotification', 'flush', array('user_id' => $user['id'])) ?>
</li>
</ul>
</div>
@ -53,7 +53,7 @@
</td>
<td>
<i class="fa fa-check fa-fw"></i>
<?= $this->url->link(t('Mark as read'), 'webnotification', 'remove', array('user_id' => $user['id'], 'notification_id' => $notification['id'])) ?>
<?= $this->url->link(t('Mark as read'), 'webNotification', 'remove', array('user_id' => $user['id'], 'notification_id' => $notification['id'])) ?>
</td>
</tr>
<?php endforeach ?>

View File

@ -88,6 +88,12 @@ abstract class Base extends PHPUnit_Framework_TestCase
$this->container['logger']->setLogger(new File('/dev/null'));
$this->container['httpClient'] = new FakeHttpClient;
$this->container['emailClient'] = $this->getMockBuilder('EmailClient')->setMethods(array('send'))->getMock();
$this->container['userNotificationType'] = $this
->getMockBuilder('\Kanboard\Model\UserNotificationType')
->setConstructorArgs(array($this->container))
->setMethods(array('getType'))
->getMock();
}
public function tearDown()

View File

@ -1,26 +0,0 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\NotificationType;
class NotificationTypeTest extends Base
{
public function testGetTypes()
{
$nt = new NotificationType($this->container);
$this->assertEquals(array('email' => 'Email', 'web' => 'Web'), $nt->getTypes());
}
public function testGetUserNotificationTypes()
{
$nt = new NotificationType($this->container);
$types = $nt->getUserSelectedTypes(1);
$this->assertEmpty($types);
$this->assertTrue($nt->saveUserSelectedTypes(1, array('email', 'web')));
$types = $nt->getUserSelectedTypes(1);
$this->assertNotEmpty($types);
$this->assertEquals(array('email', 'web'), $types);
}
}

View File

@ -4,107 +4,107 @@ require_once __DIR__.'/../Base.php';
use Kanboard\Model\User;
use Kanboard\Model\Project;
use Kanboard\Model\NotificationFilter;
use Kanboard\Model\Notification;
use Kanboard\Model\UserNotificationFilter;
use Kanboard\Model\UserNotification;
class NotificationFilterTest extends Base
class UserNotificationFilterTest extends Base
{
public function testGetFilters()
{
$nf = new NotificationFilter($this->container);
$nf = new UserNotificationFilter($this->container);
$filters = $nf->getFilters();
$this->assertArrayHasKey(NotificationFilter::FILTER_NONE, $filters);
$this->assertArrayHasKey(NotificationFilter::FILTER_BOTH, $filters);
$this->assertArrayHasKey(NotificationFilter::FILTER_CREATOR, $filters);
$this->assertArrayHasKey(NotificationFilter::FILTER_ASSIGNEE, $filters);
$this->assertArrayHasKey(UserNotificationFilter::FILTER_NONE, $filters);
$this->assertArrayHasKey(UserNotificationFilter::FILTER_BOTH, $filters);
$this->assertArrayHasKey(UserNotificationFilter::FILTER_CREATOR, $filters);
$this->assertArrayHasKey(UserNotificationFilter::FILTER_ASSIGNEE, $filters);
}
public function testSaveProjectFilter()
{
$nf = new NotificationFilter($this->container);
$nf = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
$this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
$this->assertEmpty($nf->getUserSelectedProjects(1));
$nf->saveUserSelectedProjects(1, array(1, 2));
$this->assertEquals(array(1, 2), $nf->getUserSelectedProjects(1));
$this->assertEmpty($nf->getSelectedProjects(1));
$nf->saveSelectedProjects(1, array(1, 2));
$this->assertEquals(array(1, 2), $nf->getSelectedProjects(1));
}
public function testSaveUserFilter()
{
$nf = new NotificationFilter($this->container);
$nf = new UserNotificationFilter($this->container);
$this->assertEquals(NotificationFilter::FILTER_BOTH, $nf->getUserSelectedFilter(1));
$nf->saveUserFilter(1, NotificationFilter::FILTER_CREATOR);
$this->assertEquals(NotificationFilter::FILTER_CREATOR, $nf->getUserSelectedFilter(1));
$this->assertEquals(UserNotificationFilter::FILTER_BOTH, $nf->getSelectedFilter(1));
$nf->saveFilter(1, UserNotificationFilter::FILTER_CREATOR);
$this->assertEquals(UserNotificationFilter::FILTER_CREATOR, $nf->getSelectedFilter(1));
}
public function testFilterNone()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$this->assertTrue($n->filterNone($u->getById(2), array()));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_BOTH)));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_BOTH)));
$this->assertFalse($n->filterNone($u->getById(3), array()));
}
public function testFilterCreator()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => NotificationFilter::FILTER_CREATOR)));
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => UserNotificationFilter::FILTER_CREATOR)));
$this->assertTrue($n->filterCreator($u->getById(2), array('task' => array('creator_id' => 2))));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_CREATOR)));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_CREATOR)));
$this->assertFalse($n->filterCreator($u->getById(3), array('task' => array('creator_id' => 1))));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$this->assertFalse($n->filterCreator($u->getById(4), array('task' => array('creator_id' => 2))));
}
public function testFilterAssignee()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => NotificationFilter::FILTER_ASSIGNEE)));
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => UserNotificationFilter::FILTER_ASSIGNEE)));
$this->assertTrue($n->filterAssignee($u->getById(2), array('task' => array('owner_id' => 2))));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_ASSIGNEE)));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_ASSIGNEE)));
$this->assertFalse($n->filterAssignee($u->getById(3), array('task' => array('owner_id' => 1))));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$this->assertFalse($n->filterAssignee($u->getById(4), array('task' => array('owner_id' => 2))));
}
public function testFilterBoth()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => NotificationFilter::FILTER_BOTH)));
$this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => UserNotificationFilter::FILTER_BOTH)));
$this->assertTrue($n->filterBoth($u->getById(2), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
$this->assertTrue($n->filterBoth($u->getById(2), array('task' => array('owner_id' => 0, 'creator_id' => 2))));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_BOTH)));
$this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_BOTH)));
$this->assertFalse($n->filterBoth($u->getById(3), array('task' => array('owner_id' => 1, 'creator_id' => 1))));
$this->assertFalse($n->filterBoth($u->getById(3), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$this->assertFalse($n->filterBoth($u->getById(4), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
}
public function testFilterProject()
{
$u = new User($this->container);
$n = new Notification($this->container);
$nf = new NotificationFilter($this->container);
$n = new UserNotification($this->container);
$nf = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
@ -114,7 +114,7 @@ class NotificationFilterTest extends Base
$this->assertTrue($nf->filterProject($u->getById(1), array()));
// User that select only some projects
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$n->saveSettings(2, array('notifications_enabled' => 1, 'notification_projects' => array(2 => true)));
$this->assertFalse($nf->filterProject($u->getById(2), array('task' => array('project_id' => 1))));
@ -124,10 +124,10 @@ class NotificationFilterTest extends Base
public function testFilterUserWithNoFilter()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_NONE)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_NONE)));
$this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1))));
}
@ -135,10 +135,10 @@ class NotificationFilterTest extends Base
public function testFilterUserWithAssigneeFilter()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_ASSIGNEE)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_ASSIGNEE)));
$this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'owner_id' => 2))));
$this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'owner_id' => 1))));
@ -147,10 +147,10 @@ class NotificationFilterTest extends Base
public function testFilterUserWithCreatorFilter()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_CREATOR)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_CREATOR)));
$this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 2))));
$this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 1))));
@ -159,10 +159,10 @@ class NotificationFilterTest extends Base
public function testFilterUserWithBothFilter()
{
$u = new User($this->container);
$n = new NotificationFilter($this->container);
$n = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_BOTH)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_BOTH)));
$this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 2, 'owner_id' => 3))));
$this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 0, 'owner_id' => 2))));
@ -173,14 +173,14 @@ class NotificationFilterTest extends Base
public function testFilterUserWithBothFilterAndProjectSelected()
{
$u = new User($this->container);
$n = new Notification($this->container);
$nf = new NotificationFilter($this->container);
$n = new UserNotification($this->container);
$nf = new UserNotificationFilter($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
$this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => NotificationFilter::FILTER_BOTH)));
$this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => UserNotificationFilter::FILTER_BOTH)));
$n->saveSettings(2, array('notifications_enabled' => 1, 'notification_projects' => array(2 => true)));

View File

@ -11,18 +11,18 @@ use Kanboard\Model\File;
use Kanboard\Model\Project;
use Kanboard\Model\Task;
use Kanboard\Model\ProjectPermission;
use Kanboard\Model\Notification;
use Kanboard\Model\NotificationFilter;
use Kanboard\Model\NotificationType;
use Kanboard\Subscriber\NotificationSubscriber;
use Kanboard\Model\UserNotification;
use Kanboard\Model\UserNotificationFilter;
use Kanboard\Model\UserNotificationType;
use Kanboard\Subscriber\UserNotificationSubscriber;
class NotificationTest extends Base
class UserNotificationTest extends Base
{
public function testEnableDisableNotification()
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new Notification($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
@ -38,23 +38,23 @@ class NotificationTest extends Base
public function testReadWriteSettings()
{
$n = new Notification($this->container);
$n = new UserNotification($this->container);
$p = new Project($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
$n->saveSettings(1, array(
'notifications_enabled' => 1,
'notifications_filter' => NotificationFilter::FILTER_CREATOR,
'notification_types' => array(NotificationType::TYPE_EMAIL => 1),
'notifications_filter' => UserNotificationFilter::FILTER_CREATOR,
'notification_types' => array('email' => 1),
'notification_projects' => array(),
));
$settings = $n->readSettings(1);
$this->assertNotEmpty($settings);
$this->assertEquals(1, $settings['notifications_enabled']);
$this->assertEquals(NotificationFilter::FILTER_CREATOR, $settings['notifications_filter']);
$this->assertEquals(array(NotificationType::TYPE_EMAIL), $settings['notification_types']);
$this->assertEquals(UserNotificationFilter::FILTER_CREATOR, $settings['notifications_filter']);
$this->assertEquals(array('email'), $settings['notification_types']);
$this->assertEmpty($settings['notification_projects']);
$n->saveSettings(1, array(
@ -67,16 +67,16 @@ class NotificationTest extends Base
$n->saveSettings(1, array(
'notifications_enabled' => 1,
'notifications_filter' => NotificationFilter::FILTER_ASSIGNEE,
'notification_types' => array(NotificationType::TYPE_WEB => 1, NotificationType::TYPE_EMAIL => 1),
'notifications_filter' => UserNotificationFilter::FILTER_ASSIGNEE,
'notification_types' => array('web' => 1, 'email' => 1),
'notification_projects' => array(1 => 1),
));
$settings = $n->readSettings(1);
$this->assertNotEmpty($settings);
$this->assertEquals(1, $settings['notifications_enabled']);
$this->assertEquals(NotificationFilter::FILTER_ASSIGNEE, $settings['notifications_filter']);
$this->assertEquals(array(NotificationType::TYPE_EMAIL, NotificationType::TYPE_WEB), $settings['notification_types']);
$this->assertEquals(UserNotificationFilter::FILTER_ASSIGNEE, $settings['notifications_filter']);
$this->assertEquals(array('email', 'web'), $settings['notification_types']);
$this->assertEquals(array(1), $settings['notification_projects']);
}
@ -84,7 +84,7 @@ class NotificationTest extends Base
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new Notification($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
@ -125,7 +125,7 @@ class NotificationTest extends Base
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new Notification($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'is_everybody_allowed' => 1)));
@ -155,7 +155,7 @@ class NotificationTest extends Base
public function testSendNotifications()
{
$u = new User($this->container);
$n = new Notification($this->container);
$n = new UserNotification($this->container);
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
@ -168,29 +168,30 @@ class NotificationTest extends Base
$n->saveSettings(1, array(
'notifications_enabled' => 1,
'notifications_filter' => NotificationFilter::FILTER_NONE,
'notification_types' => array(NotificationType::TYPE_WEB => 1, NotificationType::TYPE_EMAIL => 1),
'notifications_filter' => UserNotificationFilter::FILTER_NONE,
'notification_types' => array('web' => 1, 'email' => 1),
));
$this->container['emailNotification'] = $this
->getMockBuilder('\Kanboard\Model\EmailNotification')
->setConstructorArgs(array($this->container))
->setMethods(array('send'))
$notifier = $this
->getMockBuilder('Stdclass')
->setMethods(array('notifyUser'))
->getMock();
$this->container['webNotification'] = $this
->getMockBuilder('\Kanboard\Model\WebNotification')
->setConstructorArgs(array($this->container))
->setMethods(array('send'))
->getMock();
$notifier
->expects($this->exactly(2))
->method('notifyUser');
$this->container['emailNotification']
->expects($this->once())
->method('send');
$this->container['userNotificationType']
->expects($this->at(0))
->method('getType')
->with($this->equalTo('email'))
->will($this->returnValue($notifier));
$this->container['webNotification']
->expects($this->once())
->method('send');
$this->container['userNotificationType']
->expects($this->at(1))
->method('getType')
->with($this->equalTo('web'))
->will($this->returnValue($notifier));
$n->sendNotifications(Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
}

View File

@ -0,0 +1,30 @@
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\UserNotificationType;
class UserNotificationTypeTest extends Base
{
public function testGetTypes()
{
$nt = new UserNotificationType($this->container);
$this->assertEmpty($nt->getTypes());
$nt->setType('email', 'Email', 'Something');
$nt->setType('web', 'Web', 'Something');
$this->assertEquals(array('email' => 'Email', 'web' => 'Web'), $nt->getTypes());
}
public function testGetUserNotificationTypes()
{
$nt = new UserNotificationType($this->container);
$types = $nt->getSelectedTypes(1);
$this->assertEmpty($types);
$this->assertTrue($nt->saveSelectedTypes(1, array('email', 'web')));
$types = $nt->getSelectedTypes(1);
$this->assertNotEmpty($types);
$this->assertEquals(array('email', 'web'), $types);
}
}

View File

@ -10,14 +10,14 @@ use Kanboard\Model\User;
use Kanboard\Model\File;
use Kanboard\Model\Task;
use Kanboard\Model\Project;
use Kanboard\Model\WebNotification;
use Kanboard\Model\UserUnreadNotification;
use Kanboard\Subscriber\NotificationSubscriber;
class WebNotificationTest extends Base
class UserUnreadNotificationTest extends Base
{
public function testGetTitle()
{
$wn = new WebNotification($this->container);
$wn = new UserUnreadNotification($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -59,7 +59,7 @@ class WebNotificationTest extends Base
public function testHasNotification()
{
$wn = new WebNotification($this->container);
$wn = new UserUnreadNotification($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -70,14 +70,14 @@ class WebNotificationTest extends Base
$this->assertFalse($wn->hasNotifications(1));
$wn->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$this->assertTrue($wn->hasNotifications(1));
}
public function testMarkAllAsRead()
{
$wn = new WebNotification($this->container);
$wn = new UserUnreadNotification($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -86,7 +86,7 @@ class WebNotificationTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$wn->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$this->assertTrue($wn->hasNotifications(1));
$this->assertTrue($wn->markAllAsRead(1));
@ -97,7 +97,7 @@ class WebNotificationTest extends Base
public function testMarkAsRead()
{
$wn = new WebNotification($this->container);
$wn = new UserUnreadNotification($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -106,7 +106,7 @@ class WebNotificationTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$wn->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$this->assertTrue($wn->hasNotifications(1));
@ -119,7 +119,7 @@ class WebNotificationTest extends Base
public function testGetAll()
{
$wn = new WebNotification($this->container);
$wn = new UserUnreadNotification($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -128,8 +128,8 @@ class WebNotificationTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$wn->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$wn->create(1, Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$this->assertEmpty($wn->getAll(2));

View File

@ -11,14 +11,14 @@ use Kanboard\Model\File;
use Kanboard\Model\Project;
use Kanboard\Model\Task;
use Kanboard\Model\ProjectPermission;
use Kanboard\Model\EmailNotification;
use Kanboard\Notification\Mail;
use Kanboard\Subscriber\NotificationSubscriber;
class EmailNotificationTest extends Base
class MailTest extends Base
{
public function testGetMailContent()
{
$en = new EmailNotification($this->container);
$en = new Mail($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -63,7 +63,7 @@ class EmailNotificationTest extends Base
public function testSendWithEmailAddress()
{
$en = new EmailNotification($this->container);
$en = new Mail($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -89,12 +89,12 @@ class EmailNotificationTest extends Base
$this->stringContains('test')
);
$en->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$en->notifyUser($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
}
public function testSendWithoutEmailAddress()
{
$en = new EmailNotification($this->container);
$en = new Mail($this->container);
$p = new Project($this->container);
$tf = new TaskFinder($this->container);
$tc = new TaskCreation($this->container);
@ -113,6 +113,6 @@ class EmailNotificationTest extends Base
->expects($this->never())
->method('send');
$en->send($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
$en->notifyUser($u->getById(1), Task::EVENT_CREATE, array('task' => $tf->getDetails(1)));
}
}