summaryrefslogtreecommitdiff
path: root/tests/units/Model/ProjectModelTest.php
blob: 03ba708e76c5d49cf89658fd201931ce45551927 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php

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

use Kanboard\Core\Translator;
use Kanboard\Subscriber\ProjectModificationDateSubscriber;
use Kanboard\Model\ProjectModel;
use Kanboard\Model\UserModel;
use Kanboard\Model\TaskModel;
use Kanboard\Model\TaskCreationModel;
use Kanboard\Model\ConfigModel;
use Kanboard\Model\CategoryModel;

class ProjectModelTest extends Base
{
    public function testCreationForAllLanguages()
    {
        $projectModel = new ProjectModel($this->container);

        foreach ($this->container['languageModel']->getLanguages() as $locale => $language) {
            Translator::unload();
            Translator::load($locale);
            $this->assertNotFalse($projectModel->create(array('name' => 'UnitTest '.$locale)), 'Unable to create project with '.$locale.':'.$language);
        }

        Translator::unload();
    }

    public function testCreation()
    {
        $projectModel = new ProjectModel($this->container);

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

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals(1, $project['is_active']);
        $this->assertEquals(0, $project['is_public']);
        $this->assertEquals(0, $project['is_private']);
        $this->assertEquals(time(), $project['last_modified'], '', 1);
        $this->assertEmpty($project['token']);
        $this->assertEmpty($project['start_date']);
        $this->assertEmpty($project['end_date']);
    }

    public function testProjectDate()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->update(array(
            'id' => 1,
            'start_date' => '2016-08-31',
            'end_date' => '08/31/2016',
        )));

        $project = $projectModel->getById(1);
        $this->assertEquals('2016-08-31', $project['start_date']);
        $this->assertEquals('2016-08-31', $project['end_date']);
    }

    public function testCreationWithDuplicateName()
    {
        $projectModel = new ProjectModel($this->container);

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

    public function testCreationWithStartAndDate()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest', 'start_date' => '2015-01-01', 'end_date' => '2015-12-31')));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals('2015-01-01', $project['start_date']);
        $this->assertEquals('2015-12-31', $project['end_date']);
    }

    public function testCreationWithDefaultCategories()
    {
        $projectModel = new ProjectModel($this->container);
        $configModel = new ConfigModel($this->container);
        $categoryModel = new CategoryModel($this->container);

        // Multiple categories correctly formatted

        $this->assertTrue($configModel->save(array('project_categories' => 'Test1, Test2')));
        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest1')));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);

        $categories = $categoryModel->getAll(1);
        $this->assertNotEmpty($categories);
        $this->assertEquals(2, count($categories));
        $this->assertEquals('Test1', $categories[0]['name']);
        $this->assertEquals('Test2', $categories[1]['name']);

        // Single category

        $this->assertTrue($configModel->save(array('project_categories' => 'Test1')));
        $this->container['memoryCache']->flush();
        $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest2')));

        $project = $projectModel->getById(2);
        $this->assertNotEmpty($project);

        $categories = $categoryModel->getAll(2);
        $this->assertNotEmpty($categories);
        $this->assertEquals(1, count($categories));
        $this->assertEquals('Test1', $categories[0]['name']);

        // Multiple categories badly formatted

        $this->assertTrue($configModel->save(array('project_categories' => 'ABC, , DEF 3,  ')));
        $this->container['memoryCache']->flush();
        $this->assertEquals(3, $projectModel->create(array('name' => 'UnitTest3')));

        $project = $projectModel->getById(3);
        $this->assertNotEmpty($project);

        $categories = $categoryModel->getAll(3);
        $this->assertNotEmpty($categories);
        $this->assertEquals(2, count($categories));
        $this->assertEquals('ABC', $categories[0]['name']);
        $this->assertEquals('DEF 3', $categories[1]['name']);

        // No default categories
        $this->assertTrue($configModel->save(array('project_categories' => '  ')));
        $this->container['memoryCache']->flush();
        $this->assertEquals(4, $projectModel->create(array('name' => 'UnitTest4')));

        $project = $projectModel->getById(4);
        $this->assertNotEmpty($project);

        $categories = $categoryModel->getAll(4);
        $this->assertEmpty($categories);
    }

    public function testUpdateLastModifiedDate()
    {
        $projectModel = new ProjectModel($this->container);
        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));

        $now = time();

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals($now, $project['last_modified'], 'Wrong Timestamp', 1);

        sleep(1);
        $this->assertTrue($projectModel->updateModificationDate(1));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertGreaterThan($now, $project['last_modified']);
    }

    public function testGetAllIds()
    {
        $projectModel = new ProjectModel($this->container);

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

        $this->assertEmpty($projectModel->getAllByIds(array()));
        $this->assertNotEmpty($projectModel->getAllByIds(array(1, 2)));
        $this->assertCount(1, $projectModel->getAllByIds(array(1)));
    }

    public function testIsLastModified()
    {
        $projectModel = new ProjectModel($this->container);
        $taskCreationModel = new TaskCreationModel($this->container);

        $now = time();

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

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals($now, $project['last_modified']);

        sleep(1);

        $listener = new ProjectModificationDateSubscriber($this->container);
        $this->container['dispatcher']->addListener(TaskModel::EVENT_CREATE_UPDATE, array($listener, 'execute'));

        $this->assertEquals(1, $taskCreationModel->create(array('title' => 'Task #1', 'project_id' => 1)));

        $called = $this->container['dispatcher']->getCalledListeners();
        $this->assertArrayHasKey(TaskModel::EVENT_CREATE_UPDATE.'.Kanboard\Subscriber\ProjectModificationDateSubscriber::execute', $called);

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertTrue($projectModel->isModifiedSince(1, $now));
    }

    public function testRemove()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->remove(1));
        $this->assertFalse($projectModel->remove(1234));
    }

    public function testEnable()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->disable(1));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals(0, $project['is_active']);

        $this->assertFalse($projectModel->disable(1111));
    }

    public function testDisable()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->disable(1));
        $this->assertTrue($projectModel->enable(1));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals(1, $project['is_active']);

        $this->assertFalse($projectModel->enable(1234567));
    }

    public function testEnablePublicAccess()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->enablePublicAccess(1));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals(1, $project['is_public']);
        $this->assertNotEmpty($project['token']);

        $this->assertFalse($projectModel->enablePublicAccess(123));
    }

    public function testDisablePublicAccess()
    {
        $projectModel = new ProjectModel($this->container);

        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest')));
        $this->assertTrue($projectModel->enablePublicAccess(1));
        $this->assertTrue($projectModel->disablePublicAccess(1));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals(0, $project['is_public']);
        $this->assertEmpty($project['token']);

        $this->assertFalse($projectModel->disablePublicAccess(123));
    }

    public function testIdentifier()
    {
        $projectModel = new ProjectModel($this->container);

        // Creation
        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest1', 'identifier' => 'test1')));
        $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest2')));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals('TEST1', $project['identifier']);

        $project = $projectModel->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('', $project['identifier']);

        // Update
        $this->assertTrue($projectModel->update(array('id' => '2', 'identifier' => 'test2')));

        $project = $projectModel->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('TEST2', $project['identifier']);

        $project = $projectModel->getByIdentifier('test1');
        $this->assertNotEmpty($project);
        $this->assertEquals('TEST1', $project['identifier']);

        $project = $projectModel->getByIdentifier('');
        $this->assertFalse($project);
    }

    public function testEmail()
    {
        $projectModel = new ProjectModel($this->container);

        // Creation
        $this->assertEquals(1, $projectModel->create(array('name' => 'UnitTest1', 'email' => 'test1@localhost')));
        $this->assertEquals(2, $projectModel->create(array('name' => 'UnitTest2')));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals('test1@localhost', $project['email']);

        $project = $projectModel->getById(2);
        $this->assertNotEmpty($project);
        $this->assertEquals('', $project['email']);

        // Update
        $this->assertTrue($projectModel->update(array('id' => '1', 'email' => 'test1@here')));

        $project = $projectModel->getById(1);
        $this->assertNotEmpty($project);
        $this->assertEquals('test1@here', $project['email']);

        $project = $projectModel->getByEmail('test1@here');
        $this->assertEquals(1, $project['id']);

        $project = $projectModel->getByEmail('test1@localhost');
        $this->assertEmpty($project);

        $project = $projectModel->getByEmail('');
        $this->assertFalse($project);
    }

    public function testThatProjectCreatorAreAlsoOwner()
    {
        $projectModel = new ProjectModel($this->container);
        $userModel = new UserModel($this->container);

        $this->assertEquals(2, $userModel->create(array('username' => 'user1', 'name' => 'Me')));
        $this->assertEquals(1, $projectModel->create(array('name' => 'My project 1'), 2));
        $this->assertEquals(2, $projectModel->create(array('name' => 'My project 2')));

        $project = $projectModel->getByIdWithOwner(1);
        $this->assertNotEmpty($project);
        $this->assertSame('My project 1', $project['name']);
        $this->assertSame('Me', $project['owner_name']);
        $this->assertSame('user1', $project['owner_username']);
        $this->assertEquals(2, $project['owner_id']);

        $project = $projectModel->getByIdWithOwner(2);
        $this->assertNotEmpty($project);
        $this->assertSame('My project 2', $project['name']);
        $this->assertEquals('', $project['owner_name']);
        $this->assertEquals('', $project['owner_username']);
        $this->assertEquals(0, $project['owner_id']);
    }

    public function testGetList()
    {
        $projectModel = new ProjectModel($this->container);

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

        $this->assertEquals(array(0 => 'None', 1 => 'Project B'), $projectModel->getList());
        $this->assertEquals(array(1 => 'Project B'), $projectModel->getList(false));
        $this->assertEquals(array(2 => 'Project A', 1 => 'Project B'), $projectModel->getList(false, false));
        $this->assertEquals(array(0 => 'None', 2 => 'Project A', 1 => 'Project B'), $projectModel->getList(true, false));
    }
}