summaryrefslogtreecommitdiff
path: root/tests/units/NotificationTest.php
blob: 3728521273985917b7719b28173449b322024fc2 (plain)
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
<?php

require_once __DIR__.'/Base.php';

use Model\User;
use Model\Project;
use Model\ProjectPermission;
use Model\Notification;

class NotificationTest extends Base
{
    public function testGetUsersWithNotification()
    {
        $u = new User($this->container);
        $p = new Project($this->container);
        $n = new Notification($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->getUsersWithNotification(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->getUsersWithNotification(1);

        $this->assertNotEmpty($users);
        $this->assertEquals(2, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);
        $this->assertEquals('user3@here', $users[1]['email']);
    }

    public function testGetUserList()
    {
        $u = new User($this->container);
        $p = new Project($this->container);
        $pp = new ProjectPermission($this->container);
        $n = new Notification($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
        $this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
        $this->assertEquals(3, $p->create(array('name' => 'UnitTest3', 'is_everybody_allowed' => 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')));

        // 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->assertTrue($pp->addMember(2, 1));
        $this->assertTrue($pp->addMember(2, 2));
        $this->assertTrue($pp->addMember(2, 3));
        $this->assertTrue($pp->addMember(2, 4));

        $users = $n->getUsersList(1);
        $this->assertNotEmpty($users);
        $this->assertEquals(2, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);
        $this->assertEquals('user3@here', $users[1]['email']);

        $users = $n->getUsersList(2);
        $this->assertNotEmpty($users);
        $this->assertEquals(2, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);
        $this->assertEquals('user3@here', $users[1]['email']);

        // User 3 choose to receive notification only for project 2
        $n->saveSettings(4, array('notifications_enabled' => 1, 'projects' => array(2 => true)));

        $users = $n->getUsersList(1);
        $this->assertNotEmpty($users);
        $this->assertEquals(1, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);

        $users = $n->getUsersList(2);
        $this->assertNotEmpty($users);
        $this->assertEquals(2, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);
        $this->assertEquals('user3@here', $users[1]['email']);

        // User 1 excluded
        $users = $n->getUsersList(1, array(2));
        $this->assertEmpty($users);

        $users = $n->getUsersList(2, array(2));
        $this->assertNotEmpty($users);
        $this->assertEquals(1, count($users));
        $this->assertEquals('user3@here', $users[0]['email']);

        // Project #3 allow everybody
        $users = $n->getUsersList(3);
        $this->assertNotEmpty($users);
        $this->assertEquals(1, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);

        $users = $n->getUsersWithNotification(3);
        $this->assertNotEmpty($users);
        $this->assertEquals(2, count($users));
        $this->assertEquals('user1@here', $users[0]['email']);
        $this->assertEquals('user3@here', $users[1]['email']);
    }
}