-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathTaskAssignmentByVariableTest.php
More file actions
344 lines (292 loc) · 15.9 KB
/
TaskAssignmentByVariableTest.php
File metadata and controls
344 lines (292 loc) · 15.9 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
<?php
namespace Tests\Feature\Api;
use ProcessMaker\Exception\ThereIsNoProcessManagerAssignedException;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\GroupMember;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\ProcessTaskAssignment;
use ProcessMaker\Models\Screen;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class TaskAssignmentByVariableTest extends TestCase
{
use RequestHelper;
public function testProcessVariableAssignmentWithSimpleIds()
{
// Create users of a group and a user without group
$user = User::factory()->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'usersVariable', 'groupsVariable', '', false);
// The first assignment should be to user (is the first created user)
$response = $this->startTestProcess($process, ['usersVariable' => $user->id, 'groupsVariable' => $group->id]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($user->id, $task->user_id);
// The second assignment should be to the first user of the created group
$response = $this->startTestProcess($process, ['usersVariable' => $user->id, 'groupsVariable' => $group->id]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($group->users->first()->id, $task->user_id);
}
public function testProcessVariableAssignmentWithArrayOfIds()
{
// Create users of a group and a user without group
$users = User::factory(2)->create(['status'=>'ACTIVE']);
$group1 = $this->createGroup(6);
$group2 = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'usersVariable', 'groupsVariable', '', false);
// The first assignment should be to user (is the first created user)
$response = $this->startTestProcess($process, [
'usersVariable' => $users->pluck('id')->toArray(),
'groupsVariable' => [$group1->id, $group2->id],
]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($users->first()->id, $task->user_id);
// The second assignment should be to the second user
$response = $this->startTestProcess($process, [
'usersVariable' => $users->pluck('id')->toArray(),
'groupsVariable' => [$group1->id, $group2->id],
]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($users->get(1)->id, $task->user_id);
// The third assignment should be to the first user of the first created group
$response = $this->startTestProcess($process, [
'usersVariable' => $users->pluck('id')->toArray(),
'groupsVariable' => [$group1->id, $group2->id],
]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($group1->users->first()->id, $task->user_id);
}
public function testProcessVariableAssignmentWithInvalidUsers()
{
// Create users of a group and a user without group
$users = User::factory(2)->create(['status'=>'INACTIVE']);
$group1 = $this->createGroup(6, 'INACTIVE');
$group2 = $this->createGroup(5, 'INACTIVE');
$process = $this->createProcess('process_variable', 'usersVariable', 'groupsVariable', '', false);
// The first assignment should be to user (is the first created user)
$response = $this->startTestProcess($process, [
'usersVariable' => $users->pluck('id')->toArray(),
'groupsVariable' => [$group1->id, $group2->id],
]);
$requestId = $response['id'];
$assignedTasks = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])
->get()
->count();
$this->assertEquals(0, $assignedTasks);
}
public function testSelfServiceWithProcessVariableAssignment()
{
$user = User::factory()->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'usersVariable', 'groupsVariable', '', true);
$response = $this->startTestProcess($process, ['usersVariable' => $user->id, 'groupsVariable' => [$group->id]]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals(1, $task->is_self_service);
// As it is self service the user must be null
$this->assertEquals(null, $task->user_id);
// The column self_service_groups should have the configured groups and users
$this->assertEquals(['users' => [$user->id], 'groups' =>[$group->id]], $task->self_service_groups);
}
public function testSelfServiceClaims()
{
// Create users of a group and a user without group
$user = User::factory()->create(['status'=>'ACTIVE']);
$userWithoutClaim = User::factory()->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'usersVariable', 'groupsVariable', '', true);
$response = $this->startTestProcess($process, ['usersVariable' => $user->id, 'groupsVariable' => [$group->id]]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
// Assert some users that can claim the task
$updateTaskUrl = route('api.tasks.update', [$task->id]);
$this->user = $user;
$response = $this->apiCall('put', $updateTaskUrl, [
'is_self_service' => false,
'user_id' => $this->user->id,
]);
$response->assertStatus(200);
// Reset task assignment
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$task['is_self_service'] = true;
$task['user_id'] = null;
$task->save();
//with a user in the group
$updateTaskUrl = route('api.tasks.update', [$task->id]);
$this->user = $group->users->last();
$response = $this->apiCall('put', $updateTaskUrl, [
'is_self_service' => false,
'user_id' => $this->user->id,
]);
$response->assertStatus(200);
// Reset task assignment
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$task['is_self_service'] = true;
$task['user_id'] = null;
$task->save();
// Assert that a user that is not in the assignment can't claim the task
$this->user = $userWithoutClaim;
$response = $this->apiCall('put', $updateTaskUrl, [
'is_self_service' => false,
'user_id' => $this->user->id,
]);
$response->assertStatus(403);
}
public function testSelfServiceWithUserGroupAssignment()
{
// Create users of a group and a user without group
$users = User::factory()->count(3)->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('user_group', implode(',', $users->pluck('id')->toArray()), $group->id, '', true);
$response = $this->startTestProcess($process, []);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals(1, $task->is_self_service);
// As it is self service the user must be null
$this->assertEquals(null, $task->user_id);
// The column self_service_groups should have the configured groups and users
$this->assertEquals(['users' => $users->pluck('id')->toArray(), 'groups' =>[$group->id]], $task->self_service_groups);
}
public function testSelfServiceWithExpressionAssignment()
{
// Create users of a group and a user without group
$users = User::factory()->count(5)->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$rules = [
['type' => 'user', 'assignee' => $users->get(0)->id, 'expression' => 'TestVar<10'],
['type' => 'user', 'assignee' => $users->get(1)->id, 'expression' => 'TestVar<10'],
['type' => 'user', 'assignee' => $users->get(2)->id, 'expression' => 'TestVar>10'],
['type' => 'group', 'assignee' => $group->id, 'expression' => 'TestVar<10'],
['type' => 'user', 'assignee' => $users->get(3)->id, 'expression' => null],
];
$process = $this->createProcess('rule_expression', '', '', $rules, true);
$response = $this->startTestProcess($process, ['TestVar' => 5]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
// Assert that the task has been stored correctly in the database
$this->assertEquals(1, $task->is_self_service);
$this->assertEquals(null, $task->user_id);
$this->assertEquals(['users' => [$users->get(0)->id, $users->get(1)->id], 'groups' =>[$group->id]], $task->self_service_groups);
// Now we'll test that the default assignment rule runs correctly
$response = $this->startTestProcess($process, ['TestVar' => 10]);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
// Assert that the task has been stored correctly in the database
$this->assertEquals(1, $task->is_self_service);
$this->assertEquals(null, $task->user_id);
$this->assertEquals(['users' => [$users->get(3)->id], 'groups' =>[]], $task->self_service_groups);
}
// tests assignment using object values (FOUR-8708)
public function testProcessVariableAssignmentWithObjectVariable()
{
// test with variables with simple ids
$user = User::factory()->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'obj.assign.usersVariable', 'obj.assign.groupsVariable', '', false);
// the assignment should go to the user
$dataObj = ['obj'=>['assign'=>['usersVariable'=>$user->id, 'groupsVariable'=>$group->id]]];
$response = $this->startTestProcess($process, $dataObj);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($user->id, $task->user_id);
// test with variables with array of ids
// Create users of a group and a user without group
$users = User::factory(2)->create(['status'=>'ACTIVE']);
$group1 = $this->createGroup(6);
$group2 = $this->createGroup(5);
$process = $this->createProcess('process_variable', 'obj.assign.usersVariable', 'obj.assign.groupsVariable', '', false);
$dataObj = ['obj'=>['assign'=>['usersVariable'=>$users->pluck('id')->toArray(), 'groupsVariable'=>[$group1->id, $group2->id]]]];
// The assignment should be to user (the first created user)
$response = $this->startTestProcess($process, $dataObj);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($users->first()->id, $task->user_id);
}
public function testProcessVariableAssignmentWithLiteralId()
{
// Create users of a group and a user without group
$user = User::factory()->create(['status'=>'ACTIVE']);
$group = $this->createGroup(5);
$process = $this->createProcess('process_variable', $user->id, $group->id, '', false);
// The first assignment should be to user (is the first created user)
$response = $this->startTestProcess($process, []);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($user->id, $task->user_id);
// The second assignment should be to the first user of the created group
$response = $this->startTestProcess($process, []);
$requestId = $response['id'];
$task = ProcessRequestToken::where(['process_request_id' => $requestId, 'status' => 'ACTIVE'])->firstOrFail();
$this->assertEquals($group->users->first()->id, $task->user_id);
}
/**
* Creates a process in which the assignment of the first task is configured based on the parameters of this function
*
* @param $assignment assignment type for the task
* @param $assignedUsers variable that has the list of userIds
* @param $assignedGroups variable that has the list of groupIds
* @param $rules list of rules whe $assignment is 'assignment_rules'
* @param $isSelfService if the assignment is self service
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
*/
private function createProcess($assignment, $assignedUsers, $assignedGroups, $rules, $isSelfService)
{
$bpmn = file_get_contents(__DIR__ . '/processes/AssignmentByProcessVariable.bpmn');
$bpmn = str_replace('[ASSIGNMENT]', $assignment, $bpmn);
$bpmn = str_replace('[ASSIGNED_USERS]', $assignedUsers, $bpmn);
$bpmn = str_replace('[ASSIGNED_GROUPS]', $assignedGroups, $bpmn);
$bpmn = str_replace('[IS_SELF_SERVICE]', $isSelfService ? 'true' : 'false', $bpmn);
if ($rules) {
$rulesAsJson = json_encode($rules);
$bpmn = str_replace('[ASSIGNMENT_RULES]', htmlspecialchars($rulesAsJson), $bpmn);
}
$process = Process::factory()->create([
'bpmn' => $bpmn,
]);
if ($assignment === 'user_group') {
foreach (explode(',', $assignedUsers) as $userId) {
ProcessTaskAssignment::factory()->create([
'process_id' => $process->id,
'process_task_id' => 'task1_node',
'assignment_id' => $userId,
'assignment_type' => User::class,
]);
}
foreach (explode(',', $assignedGroups) as $groupId) {
ProcessTaskAssignment::factory()->create([
'process_id' => $process->id,
'process_task_id' => 'task1_node',
'assignment_id' => $groupId,
'assignment_type' => Group::class,
]);
}
}
return $process;
}
private function createGroup($numberOfUsers = 1, $status = 'ACTIVE')
{
$groupUsers = User::factory()->count($numberOfUsers)->create(['status'=>$status]);
$group = Group::factory()->create();
foreach ($groupUsers as $groupUser) {
GroupMember::factory()->create([
'member_id' => $groupUser->id,
'member_type' => User::class,
'group_id' => $group->id,
]);
}
return $group;
}
private function startTestProcess($process, $processData)
{
$route = route('api.process_events.trigger', [$process->id, 'event' => 'start_node']);
return $this->apiCall('POST', $route, $processData)->json();
}
}