forked from glpi-project/glpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectTaskTest.php
More file actions
1006 lines (880 loc) · 35.6 KB
/
ProjectTaskTest.php
File metadata and controls
1006 lines (880 loc) · 35.6 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
namespace tests\units;
use Glpi\Team\Team;
use Glpi\Tests\DbTestCase;
use Project;
use ProjectTask;
/* Test for inc/projecttask.class.php */
class ProjectTaskTest extends DbTestCase
{
public function testPlanningConflict()
{
$this->login();
$user = getItemByTypeName('User', 'tech');
$users_id = (int) $user->fields['id'];
$ptask = new ProjectTask();
$this->assertSame(
0,
(int) $ptask->add([
'name' => 'test',
])
);
$this->hasSessionMessages(ERROR, ['A linked project is mandatory']);
$project = new Project();
$pid = (int) $project->add([
'name' => 'Test project',
]);
$this->assertGreaterThan(0, $pid);
$this->assertGreaterThan(
0,
(int) $ptask->add([
'name' => 'first test, whole period',
'projects_id' => $pid,
'plan_start_date' => '2019-08-10',
'plan_end_date' => '2019-08-20',
'projecttasktemplates_id' => 0,
])
);
$this->hasNoSessionMessages([ERROR, WARNING]);
$task_id = $ptask->fields['id'];
$team = new \ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
'items_id' => $users_id,
]);
$this->hasNoSessionMessages([ERROR, WARNING]);
$this->assertGreaterThan(0, $tid);
$this->assertGreaterThan(
0,
(int) $ptask->add([
'name' => 'test, subperiod',
'projects_id' => $pid,
'plan_start_date' => '2019-08-13',
'plan_end_date' => '2019-08-14',
'projecttasktemplates_id' => 0,
])
);
$this->hasNoSessionMessages([ERROR, WARNING]);
$team = new \ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
'items_id' => $users_id,
]);
$usr_str = '<a href="' . $user->getFormURLWithID($users_id) . '">' . $user->getName() . '</a>';
$this->hasSessionMessages(
WARNING,
[
"The user $usr_str is busy at the selected timeframe.<br/>- Project task: from 2019-08-13 00:00 to 2019-08-14 00:00:<br/><a href='"
. $ptask->getFormURLWithID($task_id) . "'>first test, whole period</a><br/>",
]
);
$this->assertGreaterThan(0, $tid);
//check when updating. first create a new task out of existing bouds
$this->assertGreaterThan(
0,
(int) $ptask->add([
'name' => 'test subperiod, out of bounds',
'projects_id' => $pid,
'plan_start_date' => '2018-08-13',
'plan_end_date' => '2018-08-24',
'projecttasktemplates_id' => 0,
])
);
$this->hasNoSessionMessages([ERROR, WARNING]);
$team = new \ProjectTaskTeam();
$tid = (int) $team->add([
'projecttasks_id' => $ptask->fields['id'],
'itemtype' => \User::getType(),
'items_id' => $users_id,
]);
$this->hasNoSessionMessages([ERROR, WARNING]);
$this->assertGreaterThan(0, $tid);
$this->assertTrue(
$ptask->update([
'id' => $ptask->fields['id'],
'name' => 'test subperiod, no longer out of bounds',
'projects_id' => $pid,
'plan_start_date' => '2019-08-13',
'plan_end_date' => '2019-08-24',
'projecttasktemplates_id' => 0,
])
);
$this->assertArrayHasKey(WARNING, $_SESSION['MESSAGE_AFTER_REDIRECT']);
$_SESSION['MESSAGE_AFTER_REDIRECT'] = []; //reset
//create reference ticket
$ticket = new \Ticket();
$this->assertGreaterThan(
0,
(int) $ticket->add([
'name' => 'ticket title',
'description' => 'a description',
'content' => '',
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
'_users_id_assign' => getItemByTypeName('User', 'tech', true),
])
);
$this->assertFalse($ticket->isNewItem());
$tid = (int) $ticket->fields['id'];
$ttask = new \TicketTask();
$ttask_id = (int) $ttask->add([
'name' => 'A ticket task in bounds',
'content' => 'A ticket task in bounds',
'tickets_id' => $tid,
'plan' => [
'begin' => '2019-08-11',
'end' => '2019-08-12',
],
'users_id_tech' => $users_id,
'tasktemplates_id' => 0,
]);
$usr_str = '<a href="' . $user->getFormURLWithID($users_id) . '">' . $user->getName() . '</a>';
$this->hasSessionMessages(
WARNING,
[
"The user $usr_str is busy at the selected timeframe.<br/>- Project task: from 2019-08-11 00:00 to 2019-08-12 00:00:<br/><a href='"
. $ptask->getFormURLWithID($task_id) . "'>first test, whole period</a><br/>",
]
);
$this->assertGreaterThan(0, $ttask_id);
}
public function testGetTeamRoles(): void
{
$roles = ProjectTask::getTeamRoles();
$this->assertContains(Team::ROLE_OWNER, $roles);
$this->assertContains(Team::ROLE_MEMBER, $roles);
}
public function testGetTeamRoleName(): void
{
$roles = ProjectTask::getTeamRoles();
foreach ($roles as $role) {
$this->assertNotEmpty(ProjectTask::getTeamRoleName($role));
}
}
/**
* Tests addTeamMember, deleteTeamMember, and getTeamMembers methods
*/
public function testTeamManagement(): void
{
$this->login();
$project_task = new ProjectTask();
$project = new Project();
$projects_id = $project->add([
'name' => 'Team test',
'content' => 'Team test',
]);
$projecttasks_id = $project_task->add([
'projects_id' => $projects_id,
'name' => 'Team test',
'content' => 'Team test',
]);
$this->assertGreaterThan(0, $projecttasks_id);
// Check team members array has keys for all team itemtypes
$team = $project_task->getTeam();
$this->assertEmpty($team);
// Add team members
$this->assertTrue($project_task->addTeamMember(\User::class, 4, ['role' => Team::ROLE_MEMBER]));
// Reload ticket from DB
$project_task->getFromDB($projecttasks_id);
// Check team members
$team = $project_task->getTeam();
$this->assertCount(1, $team);
$this->assertEquals(\User::class, $team[0]['itemtype']);
$this->assertEquals(4, $team[0]['items_id']);
$this->assertEquals(Team::ROLE_MEMBER, $team[0]['role']);
// Delete team members
$this->assertTrue($project_task->deleteTeamMember(\User::class, 4, ['role' => Team::ROLE_MEMBER]));
//Reload ticket from DB
$project_task->getFromDB($projecttasks_id);
$team = $project_task->getTeam();
$this->assertEmpty($team);
// Add team members
$this->assertTrue($project_task->addTeamMember(\Group::class, 5, ['role' => Team::ROLE_MEMBER]));
// Reload ticket from DB
$project_task->getFromDB($projecttasks_id);
// Check team members
$team = $project_task->getTeam();
$this->assertCount(1, $team);
$this->assertEquals(\Group::class, $team[0]['itemtype']);
$this->assertEquals(5, $team[0]['items_id']);
$this->assertEquals(Team::ROLE_MEMBER, $team[0]['role']);
}
public function testTaskMustHaveLinkedProject()
{
// Create a project
$project = $this->createItem('Project', [
'name' => 'Project 1',
]);
// Create a task
$task = $this->createItem('ProjectTask', [
'name' => 'Task 1',
'projects_id' => $project->getID(),
]);
// Update the task with a projects_id at 0
$this->updateItem('ProjectTask', $task->getID(), [
'projects_id' => 0,
], ['projects_id']);
// Reload task from DB
$task->getFromDB($task->getID());
// Check that the task is still linked to the project
$this->assertEquals($project->getID(), $task->fields['projects_id']);
// Check if session has an error message
$this->hasSessionMessages(ERROR, ['A linked project is mandatory']);
}
public function testMoveTaskToAnotherProject()
{
// Create a project
$project1 = $this->createItem('Project', [
'name' => 'Project 1',
]);
// Create a project task
$task = $this->createItem('ProjectTask', [
'projects_id' => $project1->getID(),
'name' => 'Task 1',
]);
// Create a subtask
$subtask = $this->createItem('ProjectTask', [
'projects_id' => $project1->getID(),
'projecttasks_id' => $task->getID(),
'name' => 'Subtask 1',
]);
// Create a subtask of the subtask
$subtask2 = $this->createItem('ProjectTask', [
'projects_id' => $project1->getID(),
'projecttasks_id' => $subtask->getID(),
'name' => 'Subtask 2',
]);
// Create another project
$project2 = $this->createItem('Project', [
'name' => 'Project 2',
]);
// Move the task to another project
$this->updateItem('ProjectTask', $task->getID(), [
'projects_id' => $project2->getID(),
]);
// Reload all items from DB
$task->getFromDB($task->getID());
$subtask->getFromDB($subtask->getID());
$subtask2->getFromDB($subtask2->getID());
// Check all tasks have been moved
$this->assertEquals($project2->getID(), $task->fields['projects_id']);
$this->assertEquals($project2->getID(), $subtask->fields['projects_id']);
$this->assertEquals($project2->getID(), $subtask2->fields['projects_id']);
}
public function testCloneProjectTask()
{
// Create a project
$project = $this->createItem('Project', [
'name' => 'Project 1',
]);
// Create a project task
$task = $this->createItem('ProjectTask', [
'projects_id' => $project->getID(),
'name' => 'Task 1',
]);
// Create a subtask
$subtask = $this->createItem('ProjectTask', [
'projects_id' => $project->getID(),
'projecttasks_id' => $task->getID(),
'name' => 'Subtask 1',
]);
// Create a subtask of the subtask
$subtask2 = $this->createItem('ProjectTask', [
'projects_id' => $project->getID(),
'projecttasks_id' => $subtask->getID(),
'name' => 'Subtask 2',
]);
// Create a user
$user_name = 'Project testClone - User' . random_int(0, 99999);
$this->createItems('User', [['name' => $user_name]]);
$users_id = getItemByTypeName("User", $user_name, true);
// Create a group
$group_name = 'Project testClone - Group' . random_int(0, 99999);
$this->createItems('Group', [['name' => $group_name]]);
$groups_id = getItemByTypeName("Group", $group_name, true);
// Add team to project
$this->createItems('ProjectTaskTeam', [
[
'projecttasks_id' => $task->fields['id'],
'itemtype' => 'User',
'items_id' => $users_id,
],
[
'projecttasks_id' => $task->fields['id'],
'itemtype' => 'Group',
'items_id' => $groups_id,
],
]);
// Clone the task
$clonedTaskId = $task->clone();
$clonedTask = ProjectTask::getById($clonedTaskId);
// Check if the cloned task is in the same project with the same name
$this->assertEquals($project->getID(), $clonedTask->fields['projects_id']);
$this->assertEquals($task->fields['name'] . ' (copy)', $clonedTask->fields['name']);
// Load task team
$project_task_team = new \ProjectTaskTeam();
$team = [];
foreach ($project_task_team->find(['projecttasks_id' => $task->fields['id']]) as $row) {
$team[] = [
'itemtype' => $row['itemtype'],
'items_id' => $row['items_id'],
];
}
// Load clone team
$team_clone = [];
foreach ($project_task_team->find(['projecttasks_id' => $clonedTaskId]) as $row) {
$team_clone[] = [
'itemtype' => $row['itemtype'],
'items_id' => $row['items_id'],
];
}
// Compare teams
$this->assertEquals($team, $team_clone);
// Check if the subtask has been cloned
$clonedSubtask = new ProjectTask();
$clonedSubtask->getFromDBByCrit([
'projects_id' => $project->getID(),
'projecttasks_id' => $clonedTaskId,
]);
$this->assertGreaterThan(0, $clonedSubtask->getID());
$this->assertEquals($project->getID(), $clonedSubtask->fields['projects_id']);
$this->assertEquals($subtask->fields['name'] . ' (copy)', $clonedSubtask->fields['name']);
// Check if the subtask of the subtask has been cloned
$clonedSubtask2 = new ProjectTask();
$clonedSubtask2->getFromDBByCrit([
'projects_id' => $project->getID(),
'projecttasks_id' => $clonedSubtask->getID(),
]);
$this->assertGreaterThan(0, $clonedSubtask2->getID());
$this->assertEquals($project->getID(), $clonedSubtask2->fields['projects_id']);
$this->assertEquals($subtask2->fields['name'] . ' (copy)', $clonedSubtask2->fields['name']);
}
protected function testAutochangeStateProvider()
{
$config = new \Config();
$config->getFromDBByCrit(['name' => 'projecttask_unstarted_states_id']);
$this->updateItem('Config', $config->getID(), ['value' => '1'] + $config->fields);
$config->getFromDBByCrit(['name' => 'projecttask_inprogress_states_id']);
$this->updateItem('Config', $config->getID(), ['value' => '2'] + $config->fields);
$config->getFromDBByCrit(['name' => 'projecttask_completed_states_id']);
$this->updateItem('Config', $config->getID(), ['value' => '3'] + $config->fields);
$this->login(); // must be logged as ProjectTask uses Session::getLoginUserID()
$project = $this->createItem(
'Project',
[
'name' => 'Project 1',
]
);
$task = $this->createItem(
'ProjectTask',
[
'name' => 'Project Task 1',
'auto_projectstates' => 1,
'projects_id' => $project->getID(),
'percent_done' => 0,
]
);
yield [
'input' => [ // Task with percent_done == 0
'id' => $task->getID(),
'percent_done' => '0',
],
'result' => [
'percent_done' => '0',
'projectstates_id' => 1,
],
];
yield [
'input' => [ // Task with percent_done == 50
'id' => $task->getID(),
'percent_done' => '50',
],
'result' => [
'percent_done' => '50',
'projectstates_id' => 2,
],
];
yield [
'input' => [ // Task with percent_done == 100
'id' => $task->getID(),
'percent_done' => '100',
],
'result' => [
'percent_done' => '100',
'projectstates_id' => 3,
],
];
yield [
'input' => [ // Task with percent_done == 25
'id' => $task->getID(),
'percent_done' => '25',
],
'result' => [
'percent_done' => '25',
'projectstates_id' => 2,
],
];
yield [
'input' => [ // Task with percent_done < 0
'id' => $task->getID(),
'percent_done' => '-1',
],
'result' => [
'percent_done' => '-1',
'projectstates_id' => 1,
],
];
}
public function testAutochangeState()
{
$provider = $this->testAutochangeStateProvider();
foreach ($provider as $row) {
$input = $row['input'];
$result = $row['result'];
$projecttask = $this->updateItem('ProjectTask', $input['id'], $input);
$this->assertEquals($result['percent_done'], $projecttask->fields['percent_done']);
$this->assertEquals($result['projectstates_id'], $projecttask->fields['projectstates_id']);
}
}
protected function testAutoSetDateForAddProvider()
{
$_SESSION['glpi_currenttime'] = '2023-10-10 10:10:10';
$project = $this->createItem('Project', [
'name' => 'Project 1',
]);
yield [
/**
* The real_start_date can be an empty string value because the form
* may have already been saved without a defined start time.
*
* Setting an empty value for the real_start_date field allows
* to reproduce the behavior of the interface.
*/
'input' => [ // Task with percent_done < 100 and > 0
'projects_id' => $project->getID(),
'name' => 'Task 1',
'percent_done' => '5',
'real_start_date' => '',
],
'result' => [
'real_start_date' => \Session::getCurrentTime(),
'real_end_date' => null,
],
];
yield [
'input' => [ // Task with percent_done < 100 and > 0
'projects_id' => $project->getID(),
'name' => 'Task 1',
'percent_done' => '5',
],
'result' => [
'real_start_date' => \Session::getCurrentTime(),
'real_end_date' => null,
],
];
yield [
'input' => [ // Task with percent_done = 100
'projects_id' => $project->getID(),
'name' => 'Task 2',
'percent_done' => '100',
],
'result' => [
'real_start_date' => \Session::getCurrentTime(),
'real_end_date' => \Session::getCurrentTime(),
],
];
$tasks = [
[
'name' => 'Task 3',
'percent_done' => '0',
],
[
'name' => 'Task 4',
],
[
'name' => 'Task 5',
'percent_done' => null,
],
[
'name' => 'Task 7',
'real_start_date' => null,
'real_end_date' => null,
],
];
foreach ($tasks as $task) {
$task['projects_id'] = $project->getID();
yield [
'input' => $task,
'result' => [
'real_start_date' => null,
'real_end_date' => null,
],
];
}
yield [
'input' => [ // Task with real_start_date and no real_end_date
'projects_id' => $project->getID(),
'name' => 'Task 6',
'percent_done' => '0',
'real_start_date' => '2024-10-10 10:10:10',
'real_end_date' => null,
],
'result' => [
'real_start_date' => '2024-10-10 10:10:10',
'real_end_date' => null,
],
];
yield [
'input' => [ // Task with empty real_start_date and real_end_date
'projects_id' => $project->getID(),
'name' => 'Task 6',
'real_start_date' => '',
'real_end_date' => '',
],
'result' => [
'real_start_date' => null,
'real_end_date' => null,
],
];
yield [
'input' => [ // Task with percent_done = 100 and real_start_date and real_end_date
'projects_id' => $project->getID(),
'name' => 'Task 8',
'percent_done' => '100',
'real_start_date' => '2023-05-12 11:54:23',
'real_end_date' => '2023-09-05 16:21:46',
],
'result' => [
'real_start_date' => '2023-05-12 11:54:23',
'real_end_date' => '2023-09-05 16:21:46',
],
];
}
public function testAutoSetDateForAdd()
{
$provider = $this->testAutoSetDateForAddProvider();
foreach ($provider as $row) {
$input = $row['input'];
$result = $row['result'];
// Some logic can change the fields values, so we need to skip them
// if the values are different between the input and the result
$skip_fields = array_filter(
array_intersect(array_keys($input), array_keys($result)),
fn($key) => $input[$key] !== $result[$key],
);
// Create a project task with percent_done < 100 and > 0
$task = $this->createItem('ProjectTask', $input, $skip_fields);
// Check if the task has been added with the current date
$this->assertEquals($result['real_start_date'], $task->fields['real_start_date']);
$this->assertEquals($result['real_end_date'], $task->fields['real_end_date']);
}
}
protected function testAutoSetDateForUpdateProvider()
{
// no failure if `percent_done` is null
yield [
'fields' => [
'percent_done' => 0,
],
'input' => [
'percent_done' => null,
],
'result' => [
'percent_done' => 0,
'real_start_date' => null,
'real_end_date' => null,
],
];
// `real_start_date` is automatically set reset when `percent_done` is set to something else than 0%
foreach ([50, 75, 100] as $percent_done) {
yield [
'fields' => [
'percent_done' => 0,
],
'input' => [
'percent_done' => $percent_done,
],
'result' => [
'percent_done' => $percent_done,
'real_start_date' => \Session::getCurrentTime(),
],
];
}
// `real_end_date` is not to current time if already set when `percent_done` is set to 100%
yield [
'fields' => [
'percent_done' => 0,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => '2023-12-22 12:23:35',
],
'input' => [
'percent_done' => 100,
],
'result' => [
'percent_done' => 100,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => '2023-12-22 12:23:35',
],
];
// `real_start_date` is not reset when `percent_done` is set to 100%
// `real_end_date` is set to current time if not already set
$initial_fields_sets = [
[
'percent_done' => 0,
'real_start_date' => '2023-12-12 04:56:35',
],
[
'percent_done' => 30,
'real_start_date' => '2023-12-12 04:56:35',
],
[
'percent_done' => 100,
'real_start_date' => '2023-12-12 04:56:35',
],
];
foreach ($initial_fields_sets as $fields) {
yield [
'fields' => $fields,
'input' => [
'percent_done' => 100,
],
'result' => [
'percent_done' => 100,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => \Session::getCurrentTime(),
],
];
}
// `real_start_date` is not reset when `percent_done` is set below 100%
// but `real_end_date` is
$initial_fields_sets = [
[
'percent_done' => 0,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => '2023-12-22 12:23:35',
],
[
'percent_done' => 30,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => '2023-12-22 12:23:35',
],
[
'percent_done' => 100,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => '2023-12-22 12:23:35',
],
];
foreach ($initial_fields_sets as $fields) {
yield [
'fields' => $fields,
'input' => [
'percent_done' => 50,
],
'result' => [
'percent_done' => 50,
'real_start_date' => '2023-12-12 04:56:35',
'real_end_date' => null,
],
];
}
}
public function testAutoSetDateForUpdate()
{
global $DB;
$provider = $this->testAutoSetDateForUpdateProvider();
foreach ($provider as $row) {
$fields = $row['fields'];
$input = $row['input'];
$result = $row['result'];
// Create a project
$project = $this->createItem(Project::class, [
'name' => 'Project 1',
]);
$task = $this->createItem(ProjectTask::class, ['projects_id' => $project->getId()]);
// Force initial fields (bypass prepareInputForAdd)
$DB->update(ProjectTask::getTable(), $fields, ['id' => $task->getID()]);
$this->updateItem(ProjectTask::class, $task->getID(), $input);
$ptask = new ProjectTask();
$this->assertTrue($ptask->getFromDB($task->getID()));
foreach ($result as $field => $value) {
$this->assertEquals($value, $ptask->fields[$field]);
}
}
}
public function testGetActiveProjectTaskIDsForUser()
{
$this->login();
$entity = getItemByTypeName("Entity", "_test_root_entity", true);
// Create user
$user = $this->createItem(\User::getType(), ['name' => __FUNCTION__ . 'user']);
// Check if a user with no project returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()]));
// Create project
$project = $this->createItem(Project::getType(), [
'name' => 'project',
'entities_id' => $entity,
]);
// Check if a user with a project with no tasks returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()]));
// Create project task
$project_task = $this->createItem(ProjectTask::getType(), [
'projects_id' => $project->getID(),
'name' => 'project task',
]);
// Check if a user with a project with tasks, where the user is not a member of the team, returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()]));
// Create user team
$user_team = $this->createItem(\ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \User::class,
'items_id' => $user->getID(),
]);
// Check if a user with a project with tasks, where the user is a member of the team, returns an array with the task ID
$this->assertEquals(
[['id' => $project_task->getID()]],
ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()])
);
// Create group
$group = $this->createItem(\Group::getType(), ['name' => __FUNCTION__ . 'group']);
// Add user to group
$this->createItem(\Group_User::getType(), ['groups_id' => $group->getID(), 'users_id' => $user->getID()]);
// Create group team
$this->createItem(\ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \Group::class,
'items_id' => $group->getID(),
]);
// Remove user team
$this->deleteItem(\ProjectTaskTeam::getType(), $user_team->getID());
// Check if a user with a project with tasks, where the user is a member of the group and the group is a member of the team, returns an array with the task ID if $search_in_groups is true
$this->assertEquals(
[['id' => $project_task->getID()]],
ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()])
);
// Check if a user with a project with tasks, where the user is a member of the group and the group is a member of the team, returns an empty array if $search_in_groups is false
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()], false));
// Templates should be excluded
$project = $this->updateItem(
Project::getType(),
$project->getID(),
['is_template' => true]
);
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForUser([$user->getID()]));
}
public function testGetActiveProjectTaskIDsForGroup(): void
{
$this->login();
$entity = getItemByTypeName("Entity", "_test_root_entity", true);
// Create group
$group = $this->createItem(\Group::getType(), ['name' => __FUNCTION__ . 'group']);
// Check if a group with no project returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()]));
// Create project
$project = $this->createItem(Project::getType(), [
'name' => 'project',
'entities_id' => $entity,
'groups_id' => $group->getID(),
]);
// Check if a group with a project with no tasks returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()]));
// Create project task
$project_task = $this->createItem(ProjectTask::getType(), [
'projects_id' => $project->getID(),
'name' => 'project task',
]);
// Check if a group with a project with tasks, where the group is not a member of the team, returns an empty array
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()]));
// Create group team
$this->createItem(\ProjectTaskTeam::getType(), [
'projecttasks_id' => $project_task->getID(),
'itemtype' => \Group::class,
'items_id' => $group->getID(),
]);
// Check if a group with a project with tasks, where the group is a member of the team, returns an array with the task ID
$this->assertEquals(
[['id' => $project_task->getID()]],
ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()])
);
// Templates should be excluded
$project = $this->updateItem(
Project::getType(),
$project->getID(),
['is_template' => true]
);
$this->assertEmpty(ProjectTask::getActiveProjectTaskIDsForGroup([$group->getID()]));
}
/**
* Verify that real_end_date is saved and that effective_duration is calculated
*/
public function testAutoSetDate(): void
{
$_SESSION['glpi_currenttime'] = '2024-03-15 10:30:00';
$project = $this->createItem(Project::class, [
'name' => 'Project 1',
]);
$task = $this->createItem(ProjectTask::class, [
'projects_id' => $project->getID(),
'name' => 'Test Task',
'percent_done' => 0,
]);
// 1. When percent_done = 100, real_end_date should be set automatically
$input = ['percent_done' => 100];
$result = $task->autoSetDate($input);
$this->assertEquals('2024-03-15 10:30:00', $result['real_start_date']);
$this->assertEquals('2024-03-15 10:30:00', $result['real_end_date']);
// 2. When using manual dates, effective_duration must be calculated
$input = [
'percent_done' => 100,
'real_start_date' => '2024-03-10 08:00:00',
'real_end_date' => '2024-03-12 18:00:00',
];
$result = $task->autoSetDate($input);
$this->assertArrayHasKey('effective_duration', $result);
$expected_duration = (new \DateTime('2024-03-12 18:00:00'))->getTimestamp()
- (new \DateTime('2024-03-10 08:00:00'))->getTimestamp();
$this->assertEquals($expected_duration, $result['effective_duration']);
// 3. When percent_done drops below 100%, real_end_date must be reset
$task2 = $this->createItem(ProjectTask::class, [
'projects_id' => $project->getID(),
'name' => 'task 2',
'percent_done' => 100,
'real_start_date' => '2024-03-10 08:00:00',
'real_end_date' => '2024-03-12 18:00:00',
]);