summaryrefslogtreecommitdiff
path: root/tests/units/TaskPermissionTest.php
blob: 426941ce3cc974e7e1e7be8c58b894041dad9564 (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
<?php

require_once __DIR__.'/Base.php';

use Model\Task;
use Model\TaskFinder;
use Model\TaskPermission;
use Model\Project;
use Model\Category;
use Model\User;

class TaskPermissionTest extends Base
{
    public function testPrepareCreation()
    {
        $t = new Task($this->container);
        $tf = new TaskFinder($this->container);
        $tp = new TaskPermission($this->container);
        $p = new Project($this->container);
        $u = new User($this->container);

        $this->assertTrue($u->create(array('username' => 'toto', 'password' => '123456')));
        $this->assertTrue($u->create(array('username' => 'toto2', 'password' => '123456')));
        $this->assertEquals(1, $p->create(array('name' => 'Project #1')));
        $this->assertEquals(1, $t->create(array('title' => 'Task #1', 'project_id' => 1, 'creator_id' => 1)));
        $this->assertEquals(2, $t->create(array('title' => 'Task #2', 'project_id' => 1, 'creator_id' => 2)));
        $this->assertEquals(3, $t->create(array('title' => 'Task #3', 'project_id' => 1, 'creator_id' => 3)));
        $this->assertEquals(4, $t->create(array('title' => 'Task #4', 'project_id' => 1)));

        // User #1 can remove everything
        $user = $u->getbyId(1);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(1);
        $this->assertNotEmpty($task);
        $this->assertTrue($tp->canRemoveTask($task));

        // User #2 can't remove the task #1
        $user = $u->getbyId(2);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(1);
        $this->assertNotEmpty($task);
        $this->assertFalse($tp->canRemoveTask($task));

        // User #1 can remove everything
        $user = $u->getbyId(1);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(2);
        $this->assertNotEmpty($task);
        $this->assertTrue($tp->canRemoveTask($task));

        // User #2 can remove his own task
        $user = $u->getbyId(2);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(2);
        $this->assertNotEmpty($task);
        $this->assertTrue($tp->canRemoveTask($task));

        // User #1 can remove everything
        $user = $u->getbyId(1);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(3);
        $this->assertNotEmpty($task);
        $this->assertTrue($tp->canRemoveTask($task));

        // User #2 can't remove the task #3
        $user = $u->getbyId(2);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(3);
        $this->assertNotEmpty($task);
        $this->assertFalse($tp->canRemoveTask($task));

        // User #1 can remove everything
        $user = $u->getbyId(1);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(4);
        $this->assertNotEmpty($task);
        $this->assertTrue($tp->canRemoveTask($task));

        // User #2 can't remove the task #4
        $user = $u->getbyId(2);
        $this->assertNotEmpty($user);
        $u->updateSession($user);

        $task = $tf->getbyId(4);
        $this->assertNotEmpty($task);
        $this->assertFalse($tp->canRemoveTask($task));
    }
}