1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
<?php
require_once __DIR__.'/../Base.php';
use Kanboard\Model\TaskFinder;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\Subtask;
use Kanboard\Model\Comment;
use Kanboard\Model\User;
use Kanboard\Model\File;
use Kanboard\Model\Project;
use Kanboard\Model\Task;
use Kanboard\Model\ProjectPermission;
use Kanboard\Model\UserNotification;
use Kanboard\Model\UserNotificationFilter;
use Kanboard\Model\UserNotificationType;
use Kanboard\Subscriber\UserNotificationSubscriber;
class UserNotificationTest extends Base
{
public function testEnableDisableNotification()
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
$this->assertEquals(2, $u->create(array('username' => 'user1')));
$this->assertTrue($pp->addMember(1, 2));
$this->assertEmpty($n->getUsersWithNotificationEnabled(1));
$n->enableNotification(2);
$this->assertNotEmpty($n->getUsersWithNotificationEnabled(1));
$n->disableNotification(2);
$this->assertEmpty($n->getUsersWithNotificationEnabled(1));
}
public function testReadWriteSettings()
{
$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' => 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(UserNotificationFilter::FILTER_CREATOR, $settings['notifications_filter']);
$this->assertEquals(array('email'), $settings['notification_types']);
$this->assertEmpty($settings['notification_projects']);
$n->saveSettings(1, array(
'notifications_enabled' => 0,
));
$settings = $n->readSettings(1);
$this->assertNotEmpty($settings);
$this->assertEquals(0, $settings['notifications_enabled']);
$n->saveSettings(1, array(
'notifications_enabled' => 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(UserNotificationFilter::FILTER_ASSIGNEE, $settings['notifications_filter']);
$this->assertEquals(array('email', 'web'), $settings['notification_types']);
$this->assertEquals(array(1), $settings['notification_projects']);
}
public function testGetProjectMembersWithNotifications()
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
// Email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
// No email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1)));
// Email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user3', 'email' => 'user3@here', 'notifications_enabled' => 1)));
// No email + notifications disabled
$this->assertNotFalse($u->create(array('username' => 'user4')));
// Nobody is member of any projects
$this->assertEmpty($pp->getMembers(1));
$this->assertEmpty($n->getUsersWithNotificationEnabled(1));
// We allow all users to be member of our projects
$this->assertTrue($pp->addMember(1, 1));
$this->assertTrue($pp->addMember(1, 2));
$this->assertTrue($pp->addMember(1, 3));
$this->assertTrue($pp->addMember(1, 4));
$this->assertNotEmpty($pp->getMembers(1));
$users = $n->getUsersWithNotificationEnabled(1);
$this->assertNotEmpty($users);
$this->assertCount(3, $users);
$this->assertEquals('user1@here', $users[0]['email']);
$this->assertEquals('', $users[1]['email']);
$this->assertEquals('user3@here', $users[2]['email']);
}
public function testGetUsersWithNotificationsWhenEverybodyAllowed()
{
$u = new User($this->container);
$p = new Project($this->container);
$n = new UserNotification($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'is_everybody_allowed' => 1)));
$this->assertTrue($pp->isEverybodyAllowed(1));
// Email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
// No email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1)));
// Email + Notifications enabled
$this->assertNotFalse($u->create(array('username' => 'user3', 'email' => 'user3@here', 'notifications_enabled' => 1)));
// No email + notifications disabled
$this->assertNotFalse($u->create(array('username' => 'user4')));
$users = $n->getUsersWithNotificationEnabled(1);
$this->assertNotEmpty($users);
$this->assertCount(3, $users);
$this->assertEquals('user1@here', $users[0]['email']);
$this->assertEquals('', $users[1]['email']);
$this->assertEquals('user3@here', $users[2]['email']);
}
public function testSendNotifications()
{
$u = new User($this->container);
$n = new UserNotification($this->container);
$p = new Project($this->container);
$tc = new TaskCreation($this->container);
$tf = new TaskFinder($this->container);
$pp = new ProjectPermission($this->container);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'is_everybody_allowed' => 1)));
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$this->assertTrue($u->update(array('id' => 1, 'email' => 'test@localhost')));
$this->assertTrue($pp->isEverybodyAllowed(1));
$n->saveSettings(1, array(
'notifications_enabled' => 1,
'notifications_filter' => UserNotificationFilter::FILTER_NONE,
'notification_types' => array('web' => 1, 'email' => 1),
));
$notifier = $this
->getMockBuilder('Stdclass')
->setMethods(array('notifyUser'))
->getMock();
$notifier
->expects($this->exactly(2))
->method('notifyUser');
$this->container['userNotificationType']
->expects($this->at(0))
->method('getType')
->with($this->equalTo('email'))
->will($this->returnValue($notifier));
$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)));
}
}
|