summaryrefslogtreecommitdiff
path: root/tests/units/ProjectDuplicationTest.php
blob: bb8e0408434f782057910d477d701087300b9f17 (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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php

require_once __DIR__.'/Base.php';

use Model\Action;
use Model\Project;
use Model\Category;
use Model\ProjectPermission;
use Model\ProjectDuplication;
use Model\User;
use Model\Swimlane;
use Model\Task;
use Model\TaskCreation;
use Model\TaskFinder;

class ProjectDuplicationTest extends Base
{
    public function testProjectName()
    {
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals('test (Clone)', $pd->getClonedProjectName('test'));
        
        $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 50))));
        $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 50)));

        $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 60))));
        $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 60)));
    }

    public function testCopyProjectWithLongName()
    {
        $p = new Project($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => str_repeat('a', 50))));
        $this->assertEquals(2, $pd->duplicate(1));

        $project = $p->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals(str_repeat('a', 42).' (Clone)', $project['name']);
    }

    public function testClonePublicProject()
    {
        $p = new Project($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'Public')));
        $this->assertEquals(2, $pd->duplicate(1));

        $project = $p->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('Public (Clone)', $project['name']);
        $this->assertEquals(0, $project['is_private']);
        $this->assertEquals(0, $project['is_public']);
        $this->assertEmpty($project['token']);
    }

    public function testClonePrivateProject()
    {
        $p = new Project($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true));
        $this->assertEquals(2, $pd->duplicate(1));

        $project = $p->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('Private (Clone)', $project['name']);
        $this->assertEquals(1, $project['is_private']);
        $this->assertEquals(0, $project['is_public']);
        $this->assertEmpty($project['token']);

        $pp = new ProjectPermission($this->container);

        $this->assertEquals(array(1 => 'admin'), $pp->getMembers(1));
        $this->assertEquals(array(1 => 'admin'), $pp->getMembers(2));
    }

    public function testCloneProjectWithCategories()
    {
        $p = new Project($this->container);
        $c = new Category($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'P1')));

        $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1)));
        $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1)));
        $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1)));

        $this->assertEquals(2, $pd->duplicate(1));

        $project = $p->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('P1 (Clone)', $project['name']);

        $categories = $c->getAll(2);
        $this->assertNotempty($categories);
        $this->assertEquals(3, count($categories));

        $this->assertEquals(4, $categories[0]['id']);
        $this->assertEquals('C1', $categories[0]['name']);

        $this->assertEquals(5, $categories[1]['id']);
        $this->assertEquals('C2', $categories[1]['name']);

        $this->assertEquals(6, $categories[2]['id']);
        $this->assertEquals('C3', $categories[2]['name']);
    }

    public function testCloneProjectWithUsers()
    {
        $p = new Project($this->container);
        $c = new Category($this->container);
        $pp = new ProjectPermission($this->container);
        $u = new User($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(2, $u->create(array('username' => 'unittest1', 'password' => 'unittest')));
        $this->assertEquals(3, $u->create(array('username' => 'unittest2', 'password' => 'unittest')));
        $this->assertEquals(4, $u->create(array('username' => 'unittest3', 'password' => 'unittest')));

        $this->assertEquals(1, $p->create(array('name' => 'P1')));
        $this->assertTrue($pp->addMember(1, 2));
        $this->assertTrue($pp->addMember(1, 4));
        $this->assertTrue($pp->addManager(1, 3));
        $this->assertTrue($pp->isMember(1, 2));
        $this->assertTrue($pp->isMember(1, 3));
        $this->assertTrue($pp->isMember(1, 4));
        $this->assertFalse($pp->isManager(1, 2));
        $this->assertTrue($pp->isManager(1, 3));
        $this->assertFalse($pp->isManager(1, 4));

        $this->assertEquals(2, $pd->duplicate(1));

        $project = $p->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('P1 (Clone)', $project['name']);

        $this->assertEquals(3, count($pp->getMembers(2)));
        $this->assertTrue($pp->isMember(2, 2));
        $this->assertTrue($pp->isMember(2, 3));
        $this->assertTrue($pp->isMember(2, 4));
        $this->assertFalse($pp->isManager(2, 2));
        $this->assertTrue($pp->isManager(2, 3));
        $this->assertFalse($pp->isManager(2, 4));
    }

    public function testCloneProjectWithActionTaskAssignCurrentUser()
    {
        $p = new Project($this->container);
        $a = new Action($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'P1')));
        
        $this->assertTrue($a->create(array(
            'project_id' => 1,
            'event_name' => Task::EVENT_MOVE_COLUMN,
            'action_name' => 'TaskAssignCurrentUser',
            'params' => array('column_id' => 2),
        )));

        $this->assertEquals(2, $pd->duplicate(1));

        $actions = $a->getAllByProject(2);

        $this->assertNotEmpty($actions);
        $this->assertEquals('TaskAssignCurrentUser', $actions[0]['action_name']);
        $this->assertNotEmpty($actions[0]['params']);
        $this->assertEquals(6, $actions[0]['params'][0]['value']);
    }

    public function testCloneProjectWithActionTaskAssignColorCategory()
    {
        $p = new Project($this->container);
        $a = new Action($this->container);
        $c = new Category($this->container);
        $pd = new ProjectDuplication($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'P1')));

        $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1)));
        $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1)));
        $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1)));
        
        $this->assertTrue($a->create(array(
            'project_id' => 1,
            'event_name' => Task::EVENT_CREATE_UPDATE,
            'action_name' => 'TaskAssignColorCategory',
            'params' => array('color_id' => 'blue', 'category_id' => 2),
        )));

        $this->assertEquals(2, $pd->duplicate(1));

        $actions = $a->getAllByProject(2);

        $this->assertNotEmpty($actions);
        $this->assertEquals('TaskAssignColorCategory', $actions[0]['action_name']);
        $this->assertNotEmpty($actions[0]['params']);
        $this->assertEquals('blue', $actions[0]['params'][0]['value']);
        $this->assertEquals(5, $actions[0]['params'][1]['value']);
    }

    public function testCloneProjectWithSwimlanesAndTasks()
    {
        $p = new Project($this->container);
        $pd = new ProjectDuplication($this->container);
        $s = new Swimlane($this->container);
        $tc = new TaskCreation($this->container);
        $tf = new TaskFinder($this->container);

        $this->assertEquals(1, $p->create(array('name' => 'P1')));

        // create initial swimlanes
        $this->assertEquals(1, $s->create(1, 'S1'));
        $this->assertEquals(2, $s->create(1, 'S2'));
        $this->assertEquals(3, $s->create(1, 'S3'));

        $default_swimlane1 = $s->getDefault(1);
        $default_swimlane1['default_swimlane'] = 'New Default';

        $this->assertTrue($s->updateDefault($default_swimlane1));

        //create initial tasks
        $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
        $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1)));
        $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));

        $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, function() {});
        $this->container['dispatcher']->addListener(Task::EVENT_CREATE, function() {});

        $this->assertEquals(2, $pd->duplicate(1, array('category', 'action', 'swimlane', 'task')));

        // Check if Swimlanes have been duplicated
        $swimlanes = $s->getAll(2);

        $this->assertCount(3, $swimlanes);
        $this->assertEquals(4, $swimlanes[0]['id']);
        $this->assertEquals('S1', $swimlanes[0]['name']);
        $this->assertEquals(5, $swimlanes[1]['id']);
        $this->assertEquals('S2', $swimlanes[1]['name']);
        $this->assertEquals(6, $swimlanes[2]['id']);
        $this->assertEquals('S3', $swimlanes[2]['name']);
        $new_default = $s->getDefault(2);
        $this->assertEquals('New Default', $new_default['default_swimlane']);

        // Check if Tasks have been duplicated

        $tasks = $tf->getAll(2);

        $this->assertCount(3, $tasks);
        $this->assertEquals(4, $tasks[0]['id']);
        $this->assertEquals('T1', $tasks[0]['title']);
        $this->assertEquals(5, $tasks[1]['id']);
        $this->assertEquals('T2', $tasks[1]['title']);
        $this->assertEquals(6, $tasks[2]['id']);
        $this->assertEquals('T3', $tasks[2]['title']);

        // Drop project
        unset($tasks);
        unset($swimlanes);
        unset($new_default);

        $p->remove(2);

        $this->assertFalse($p->exists(2));
        $this->assertCount(0, $s->getAll(2));
        $this->assertCount(0, $tf->getAll(2));

        // Check duplication with Swimlanes only
        $this->assertEquals(2, $pd->duplicate(1, array('category', 'action', 'swimlane')));

        // Check if Swimlanes have been duplicated
        $swimlanes = $s->getAll(2);

        $this->assertCount(3, $swimlanes);
        $this->assertEquals(4, $swimlanes[0]['id']);
        $this->assertEquals('S1', $swimlanes[0]['name']);
        $this->assertEquals(5, $swimlanes[1]['id']);
        $this->assertEquals('S2', $swimlanes[1]['name']);
        $this->assertEquals(6, $swimlanes[2]['id']);
        $this->assertEquals('S3', $swimlanes[2]['name']);
        $new_default = $s->getDefault(2);
        $this->assertEquals('New Default', $new_default['default_swimlane']);

        // Check if Tasks have NOT been duplicated
        $this->assertCount(0, $tf->getAll(2));

        // Drop project
        unset($tasks);
        unset($swimlanes);
        unset($new_default);

        $p->remove(2);

        $this->assertFalse($p->exists(2));
        $this->assertCount(0, $s->getAll(2));
        $this->assertCount(0, $tf->getAll(2));

        // Check duplication with Tasks only
        $this->assertEquals(2, $pd->duplicate(1, array('category', 'action', 'task')));

        // Check if Swimlanes have NOT been duplicated
        $this->assertCount(0, $s->getAll(2));

        // Check if Tasks have been duplicated
        $tasks = $tf->getAll(2);

        $this->assertCount(3, $tasks);
        $this->assertEquals(4, $tasks[0]['id']);
        $this->assertEquals('T1', $tasks[0]['title']);
        $this->assertEquals(5, $tasks[1]['id']);
        $this->assertEquals('T2', $tasks[1]['title']);
        $this->assertEquals(6, $tasks[2]['id']);
        $this->assertEquals('T3', $tasks[2]['title']);
    }
}