summaryrefslogtreecommitdiff
path: root/tests/units/Model/ProjectPermissionTest.php
blob: 0f5996b59c021f7f3bf8b05e1d3193ec4abe4ae6 (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
<?php

require_once __DIR__.'/../Base.php';

use Kanboard\Model\ProjectPermission;
use Kanboard\Model\Project;
use Kanboard\Model\User;
use Kanboard\Model\Group;
use Kanboard\Model\GroupMember;
use Kanboard\Model\ProjectGroupRole;
use Kanboard\Model\ProjectUserRole;
use Kanboard\Core\Security\Role;

class ProjectPermissionTest extends Base
{
    public function testGetQueryByRole()
    {
        $userModel = new User($this->container);
        $projectModel = new Project($this->container);
        $groupModel = new Group($this->container);
        $groupMemberModel = new GroupMember($this->container);
        $groupRoleModel = new ProjectGroupRole($this->container);
        $userRoleModel = new ProjectUserRole($this->container);
        $projectPermission = new ProjectPermission($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1')));
        $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2')));
        $this->assertEquals(3, $projectModel->create(array('name' => 'Project 3')));

        $this->assertEquals(2, $userModel->create(array('username' => 'user 1')));
        $this->assertEquals(3, $userModel->create(array('username' => 'user 2')));
        $this->assertEquals(4, $userModel->create(array('username' => 'user 3')));
        $this->assertEquals(5, $userModel->create(array('username' => 'user 4')));

        $this->assertTrue($userRoleModel->addUser(1, 2, Role::PROJECT_MANAGER));
        $this->assertTrue($userRoleModel->addUser(1, 3, Role::PROJECT_MANAGER));
        $this->assertTrue($userRoleModel->addUser(1, 4, Role::PROJECT_MEMBER));
        $this->assertTrue($userRoleModel->addUser(1, 5, Role::PROJECT_MEMBER));

        $this->assertTrue($userRoleModel->addUser(2, 2, Role::PROJECT_MEMBER));
        $this->assertTrue($userRoleModel->addUser(2, 3, Role::PROJECT_MEMBER));
        $this->assertTrue($userRoleModel->addUser(2, 5, Role::PROJECT_MANAGER));

        $this->assertTrue($userRoleModel->addUser(3, 4, Role::PROJECT_MANAGER));
        $this->assertTrue($userRoleModel->addUser(3, 5, Role::PROJECT_VIEWER));

        $this->assertEmpty($projectPermission->getQueryByRole(array(), Role::PROJECT_MANAGER)->findAll());

        $users = $projectPermission->getQueryByRole(array(1, 2), Role::PROJECT_MANAGER)->findAll();
        $this->assertCount(3, $users);
        $this->assertEquals('user 1', $users[0]['username']);
        $this->assertEquals('Project 1', $users[0]['project_name']);
        $this->assertEquals('user 2', $users[1]['username']);
        $this->assertEquals('Project 1', $users[1]['project_name']);
        $this->assertEquals('user 4', $users[2]['username']);
        $this->assertEquals('Project 2', $users[2]['project_name']);

        $users = $projectPermission->getQueryByRole(array(1), Role::PROJECT_MANAGER)->findAll();
        $this->assertCount(2, $users);
        $this->assertEquals('user 1', $users[0]['username']);
        $this->assertEquals('Project 1', $users[0]['project_name']);
        $this->assertEquals('user 2', $users[1]['username']);
        $this->assertEquals('Project 1', $users[1]['project_name']);

        $users = $projectPermission->getQueryByRole(array(1, 2, 3), Role::PROJECT_MEMBER)->findAll();
        $this->assertCount(4, $users);
        $this->assertEquals('user 3', $users[0]['username']);
        $this->assertEquals('Project 1', $users[0]['project_name']);
        $this->assertEquals('user 4', $users[1]['username']);
        $this->assertEquals('Project 1', $users[1]['project_name']);
        $this->assertEquals('user 1', $users[2]['username']);
        $this->assertEquals('Project 2', $users[2]['project_name']);
        $this->assertEquals('user 2', $users[3]['username']);
        $this->assertEquals('Project 2', $users[3]['project_name']);

        $users = $projectPermission->getQueryByRole(array(1, 2, 3), Role::PROJECT_VIEWER)->findAll();
        $this->assertCount(1, $users);
        $this->assertEquals('user 4', $users[0]['username']);
        $this->assertEquals('Project 3', $users[0]['project_name']);
    }

    public function testDuplicate()
    {
        $userModel = new User($this->container);
        $projectModel = new Project($this->container);
        $groupModel = new Group($this->container);
        $groupMemberModel = new GroupMember($this->container);
        $groupRoleModel = new ProjectGroupRole($this->container);
        $userRoleModel = new ProjectUserRole($this->container);
        $projectPermission = new ProjectPermission($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'Project 1')));
        $this->assertEquals(2, $projectModel->create(array('name' => 'Project 2')));

        $this->assertEquals(2, $userModel->create(array('username' => 'user 1', 'name' => 'User #1')));
        $this->assertEquals(3, $userModel->create(array('username' => 'user 2')));
        $this->assertEquals(4, $userModel->create(array('username' => 'user 3')));
        $this->assertEquals(5, $userModel->create(array('username' => 'user 4')));
        $this->assertEquals(6, $userModel->create(array('username' => 'user 5', 'name' => 'User #5')));

        $this->assertEquals(1, $groupModel->create('Group C'));
        $this->assertEquals(2, $groupModel->create('Group B'));
        $this->assertEquals(3, $groupModel->create('Group A'));

        $this->assertTrue($groupMemberModel->addUser(1, 4));
        $this->assertTrue($groupMemberModel->addUser(2, 5));
        $this->assertTrue($groupMemberModel->addUser(3, 3));
        $this->assertTrue($groupMemberModel->addUser(3, 2));

        $this->assertTrue($groupRoleModel->addGroup(1, 1, Role::PROJECT_VIEWER));
        $this->assertTrue($groupRoleModel->addGroup(1, 3, Role::PROJECT_MANAGER));

        $this->assertTrue($userRoleModel->addUser(1, 5, Role::PROJECT_MANAGER));
        $this->assertTrue($userRoleModel->addUser(1, 6, Role::PROJECT_MEMBER));

        $this->assertTrue($projectPermission->duplicate(1, 2));

        $this->assertCount(2, $userRoleModel->getUsers(2));
        $this->assertCount(3, $groupRoleModel->getUsers(2));
    }
}