summaryrefslogtreecommitdiff
path: root/tests/units/Model/TaskStatusTest.php
blob: 86f31d704ef358a0efe6531fcdab7a0e8ce5540a (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
134
135
136
<?php

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

use Kanboard\Model\Swimlane;
use Kanboard\Model\Subtask;
use Kanboard\Model\Task;
use Kanboard\Model\TaskCreation;
use Kanboard\Model\TaskFinder;
use Kanboard\Model\TaskStatus;
use Kanboard\Model\Project;

class TaskStatusTest extends Base
{
    public function testCloseBySwimlaneAndColumn()
    {
        $tc = new TaskCreation($this->container);
        $tf = new TaskFinder($this->container);
        $ts = new TaskStatus($this->container);
        $p = new Project($this->container);
        $s = new Swimlane($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'test')));
        $this->assertEquals(1, $s->create(array('name' => 'test', 'project_id' => 1)));
        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
        $this->assertEquals(2, $tc->create(array('title' => 'test', 'project_id' => 1)));
        $this->assertEquals(3, $tc->create(array('title' => 'test', 'project_id' => 1, 'column_id' => 2)));
        $this->assertEquals(4, $tc->create(array('title' => 'test', 'project_id' => 1, 'swimlane_id' => 1)));

        $this->assertEquals(2, $tf->countByColumnAndSwimlaneId(1, 1, 0));
        $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 1, 1));
        $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0));

        $ts->closeTasksBySwimlaneAndColumn(0, 1);
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0));
        $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 1, 1));
        $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0));

        $ts->closeTasksBySwimlaneAndColumn(1, 1);
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0));
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 1));
        $this->assertEquals(1, $tf->countByColumnAndSwimlaneId(1, 2, 0));

        $ts->closeTasksBySwimlaneAndColumn(0, 2);
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 0));
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 1, 1));
        $this->assertEquals(0, $tf->countByColumnAndSwimlaneId(1, 2, 0));
    }

    public function testStatus()
    {
        $tc = new TaskCreation($this->container);
        $tf = new TaskFinder($this->container);
        $ts = new TaskStatus($this->container);
        $p = new Project($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'test')));
        $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));

        // The task must be open

        $this->assertTrue($ts->isOpen(1));

        $task = $tf->getById(1);
        $this->assertNotEmpty($task);
        $this->assertEquals(Task::STATUS_OPEN, $task['is_active']);
        $this->assertEquals(0, $task['date_completed']);
        $this->assertEquals(time(), $task['date_modification'], '', 1);

        // We close the task

        $this->container['dispatcher']->addListener(Task::EVENT_CLOSE, array($this, 'onTaskClose'));
        $this->container['dispatcher']->addListener(Task::EVENT_OPEN, array($this, 'onTaskOpen'));

        $this->assertTrue($ts->close(1));
        $this->assertTrue($ts->isClosed(1));

        $task = $tf->getById(1);
        $this->assertNotEmpty($task);
        $this->assertEquals(Task::STATUS_CLOSED, $task['is_active']);
        $this->assertEquals(time(), $task['date_completed'], 'Bad completion timestamp', 1);
        $this->assertEquals(time(), $task['date_modification'], 'Bad modification timestamp', 1);

        // We open the task again

        $this->assertTrue($ts->open(1));
        $this->assertTrue($ts->isOpen(1));

        $task = $tf->getById(1);
        $this->assertNotEmpty($task);
        $this->assertEquals(Task::STATUS_OPEN, $task['is_active']);
        $this->assertEquals(0, $task['date_completed']);
        $this->assertEquals(time(), $task['date_modification'], '', 1);

        $called = $this->container['dispatcher']->getCalledListeners();
        $this->assertArrayHasKey('task.close.TaskStatusTest::onTaskClose', $called);
        $this->assertArrayHasKey('task.open.TaskStatusTest::onTaskOpen', $called);
    }

    public function onTaskOpen($event)
    {
        $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event);
        $this->assertArrayHasKey('task_id', $event);
        $this->assertNotEmpty($event['task_id']);
    }

    public function onTaskClose($event)
    {
        $this->assertInstanceOf('Kanboard\Event\TaskEvent', $event);
        $this->assertArrayHasKey('task_id', $event);
        $this->assertNotEmpty($event['task_id']);
    }

    public function testThatAllSubtasksAreClosed()
    {
        $ts = new TaskStatus($this->container);
        $tc = new TaskCreation($this->container);
        $s = new Subtask($this->container);
        $p = new Project($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'test1')));
        $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1)));

        $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1)));
        $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1)));

        $this->assertTrue($ts->close(1));

        $subtasks = $s->getAll(1);
        $this->assertNotEmpty($subtasks);

        foreach ($subtasks as $subtask) {
            $this->assertEquals(Subtask::STATUS_DONE, $subtask['status']);
        }
    }
}