-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathProcessExecutionTest.php
More file actions
370 lines (334 loc) · 13.1 KB
/
Copy pathProcessExecutionTest.php
File metadata and controls
370 lines (334 loc) · 13.1 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
<?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\ProcessRequestToken;
use ProcessMaker\Models\ProcessTaskAssignment;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
/**
* Test the process execution with requests
*
* @group process_tests
*/
class ProcessExecutionTest 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'] = Process::getProcessTemplate('SingleTask.bpmn');
$process = Process::factory()->create($data);
//Assign the task to $this->user
$taskId = 'UserTaskUID';
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 = ['foo' => 'bar'];
$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');
// Verify that the start event data was stored in the task
$task = ProcessRequestToken::where([
'process_request_id' => $request['id'],
'status' => 'TRIGGERED',
])->first();
$this->assertEquals(['foo'=>'bar'], $task->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
);
}
/**
* Test to get the list of available start events of the process.
*/
public function testGetListOfStartEvents()
{
$route = route('api.processes.show', [$this->process->id, 'include' => 'events']);
$response = $this->apiCall('GET', $route);
//Check the inclusion of events
$response->assertJsonStructure(['events' => [['id', 'name']]]);
}
/**
* Test to start a process without sending an event identifier.
*/
public function testStartProcessEmptyEventId()
{
$route = route('api.process_events.trigger', [$this->process->id]);
$data = [];
$response = $this->apiCall('POST', $route, $data);
$response->assertStatus(404);
}
/**
* Test to start a process without sending an non-existent event.
*/
public function testStartProcessWithNonExistingEventId()
{
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'non-existent']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
$response->assertStatus(404);
}
/**
* Try to close an already closed 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);
}
/**
* Get the list of processes to start with categories and start events.
*/
public function testGetListProcessesToStart()
{
$this->actingAs($this->user);
//Create two additional processes
$this->createTestProcess();
$uncProcess = $this->createTestProcess(['process_category_id' => null]);
//Get the start event id
$uncProcessEvents = $uncProcess->events;
//Get the list of processes (with and without category) and its start events
$route = route('api.processes.index', ['include' => 'events,category', 'order_by' => 'name']);
$response = $this->apiCall('GET', $route);
//Check the inclusion of events
$response->assertJsonStructure(['data' => ['*' => ['events' => [['id', 'name']], 'category']]]);
$data = $response->json('data');
$list = ['Uncategorized' => []];
foreach ($data as $process) {
$categoryName = $process['category'] ? $process['category']['name'] : 'Uncategorized';
$list[$categoryName][] = $process;
}
$this->assertArraySubset(
[
'Uncategorized' => [
[
'name' => $uncProcess->name,
'events' => [
['id' => $uncProcessEvents[0]['id']],
],
],
],
], $list);
}
/**
* 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');
$response = $this->actingAs($foo, 'api')->json('GET', $route);
$tasks = $response->json('data');
// Assert the first user "foo" got the task
$this->assertEquals(count($tasks), 1);
$task_id = $tasks[0]['id'];
//Get the active tasks of the request for the other user
$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
$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(1, count($tasks));
// 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']);
}
}