-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathProcessManualTaskTest.php
More file actions
295 lines (265 loc) · 10.4 KB
/
Copy pathProcessManualTaskTest.php
File metadata and controls
295 lines (265 loc) · 10.4 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
<?php
namespace Tests\Feature\Api;
use Illuminate\Foundation\Testing\WithFaker;
use ProcessMaker\Models\Group;
use ProcessMaker\Models\GroupMember;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessTaskAssignment;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
/**
* Test the execution of manual tasks
*
* @group process_tests
*/
class ProcessManualTaskTest extends TestCase
{
use RequestHelper;
use WithFaker;
/**
* @var Process
*/
protected $process;
private $requestStructure = [
'id',
'process_id',
'user_id',
'status',
'name',
'initiated_at',
'created_at',
'updated_at',
];
/**
* Initialize the controller tests
*/
protected function withUserSetup()
{
$this->process = $this->createTestProcess();
}
/**
* Create a single task process assigned to $this->user
*/
private function createTestProcess(array $data = [])
{
$data['bpmn'] = file_get_contents(__DIR__ . '/processes/ManualTask.bpmn');
$process = Process::factory()->create($data);
//Assign the task to $this->user
$taskId = 'TaskUID';
ProcessTaskAssignment::factory()->create([
'process_id' => $process->id,
'process_task_id' => $taskId,
'assignment_id' => $this->user->id,
'assignment_type' => User::class,
]);
return $process;
}
/**
* Execute a process
*/
public function testExecuteAProcess()
{
//Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'StartEventUID']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
//Verify status
$response->assertStatus(201);
//Verify the structure
$response->assertJsonStructure($this->requestStructure);
$request = $response->json();
//Get the active tasks of the request
$route = route('api.tasks.index');
$response = $this->apiCall('GET', $route);
$tasks = $response->json('data');
//Complete the task
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'COMPLETED']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
$task = $response->json();
//Check the task is closed
$this->assertEquals('CLOSED', $task['status']);
$this->assertNotNull($task['completed_at']);
//Check the request is completed
$this->assertEquals('COMPLETED', $task['process_request']['status']);
$this->assertNotNull($task['process_request']['completed_at']);
// Check that a log comment was created
$response = $this->apiCall('GET', '/comments', [
'commentable_type' => ProcessRequest::class,
'commentable_id' => $tasks[0]['process_request_id'],
]);
$message = $response->json()['data'][0]['body'];
$this->assertEquals(
$this->user->fullname . ' has completed the task ' . $task['element_name'],
$message
);
}
/**
* Try to close an already closed manual task
*/
public function testCloseAClosedTask()
{
//Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'StartEventUID']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
//Verify status
$response->assertStatus(201);
//Verify the structure
$response->assertJsonStructure($this->requestStructure);
$request = $response->json();
//Get the active tasks of the request
$route = route('api.tasks.index');
$response = $this->apiCall('GET', $route);
$tasks = $response->json('data');
//Complete the task
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'COMPLETED']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
$task = $response->json();
//Check the task is closed
$this->assertEquals('CLOSED', $task['status']);
$this->assertNotNull($task['completed_at']);
//Try to complete the task again
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'COMPLETED']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
$task = $response->json();
$response->assertStatus(422);
}
/**
* Try to update a task status
*/
public function testUpdateTaskInvalidStatus()
{
//Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'StartEventUID']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
//Verify status
$response->assertStatus(201);
//Verify the structure
$response->assertJsonStructure($this->requestStructure);
$request = $response->json();
//Get the active tasks of the request
$route = route('api.tasks.index');
$response = $this->apiCall('GET', $route);
$tasks = $response->json('data');
//Update to a FAILING status
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'FAILING']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
$response->assertStatus(422);
//Update to a *invalid* status
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => '*invalid*']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
$response->assertStatus(422);
}
/**
* Test to get the status information of a task
*/
public function testGetTaskStatusPage()
{
$this->withoutExceptionHandling();
//Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'StartEventUID']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
//Verify status
$response->assertStatus(201);
//Verify the structure
$response->assertJsonStructure($this->requestStructure);
$request = $response->json();
//Get the active tasks of the request
$route = route('api.tasks.index');
$response = $this->apiCall('GET', $route);
$tasks = $response->json('data');
//Get the task information
$route = route('api.tasks.show', [$tasks[0]['id'], 'include' => 'definition']);
$response = $this->apiCall('GET', $route, $data);
$response->assertJsonStructure([
'id',
'status',
'created_at',
'element_id',
'element_name',
'definition' => [
'id',
'name',
],
]);
}
/**
* Test that the task gets assigned to the correct person in a group
*/
public function testTaskAssignedToGroup()
{
$foo = User::factory()->create(
['firstname' => 'Foo', 'status' => 'ACTIVE']
);
$bar = User::factory()->create(
['firstname' => 'Bar', 'status' => 'ACTIVE']
);
$group = Group::factory()->create(
['id' => 999, 'status' => 'ACTIVE']
);
foreach ([$foo, $bar] as $user) {
GroupMember::factory()->create([
'member_id' => $user->id,
'member_type' => User::class,
'group_id' => $group->id,
]);
}
$group_process = Process::factory()->create(['status' => 'ACTIVE']);
$data['bpmn'] = Process::getProcessTemplate('SingleTaskAssignedToGroup.bpmn');
$group_process->update($data);
$taskId = 'node_3';
ProcessTaskAssignment::factory()->create([
'process_id' => $group_process->id,
'process_task_id' => $taskId,
'assignment_id' => $group->id,
'assignment_type' => Group::class,
]);
//Start a process request
$route = route('api.process_events.trigger', [$group_process->id, 'event' => 'node_2']);
$response = $this->apiCall('POST', $route, []);
//Get the active tasks of the request
$route = route('api.tasks.index', ['user_id' => $foo->id]);
$response = $this->actingAs($foo, 'api')->json('GET', $route);
$tasks = $response->json('data');
// Assert the first user "foo" got the task
$this->assertEquals(1, count($tasks));
$task_id = $tasks[0]['id'];
//Get the active tasks of the request for the other user
//Since PR #3470, user_id is required as parameter
$route = route('api.tasks.index', ['user_id' => $bar->id]);
$response = $this->actingAs($bar, 'api')->json('GET', $route);
$tasks = $response->json('data');
// Assert that "bar" did NOT get the task
$this->assertEquals(0, count($tasks));
// Complete the task
$route = route('api.tasks.update', [$task_id, 'status' => 'COMPLETED']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
// Start another request
$route = route('api.process_events.trigger', [$group_process->id, 'event' => 'node_2']);
$response = $this->apiCall('POST', $route, []);
//Get the active tasks of the request
//Since PR #3470, user_id is required as parameter
$route = route('api.tasks.index', ['user_id' => $bar->id]);
$response = $this->actingAs($bar, 'api')->json('GET', $route);
$tasks = $response->json('data');
// Assert the next user "bar" got the task
$this->assertEquals(count($tasks), 1);
// Complete the task
$route = route('api.tasks.update', [$tasks[0]['id'], 'status' => 'COMPLETED']);
$response = $this->apiCall('PUT', $route, ['data' => $data]);
// Start another request
$route = route('api.process_events.trigger', [$group_process->id, 'event' => 'node_2']);
$response = $this->apiCall('POST', $route, []);
//Get the active tasks of the request
$route = route('api.tasks.index', ['user_id' => $foo->id]);
$response = $this->actingAs($foo, 'api')->json('GET', $route);
$tasks = $response->json('data');
// Assert the next user, "foo" again, got the task
$this->assertEquals('completed', $tasks[0]['advanceStatus']);
$this->assertEquals('open', $tasks[1]['advanceStatus']);
}
}