-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathTaskControllerTest.php
More file actions
389 lines (327 loc) · 15 KB
/
TaskControllerTest.php
File metadata and controls
389 lines (327 loc) · 15 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
<?php
namespace Tests\Feature\Api;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\TaskDraft;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class TaskControllerTest extends TestCase
{
use RequestHelper;
const API_TASK_BY_CASE = '/tasks-by-case';
const TASK_BY_CASE_STRUCTURE = [
'id',
'element_name',
'user_id',
'process_id',
'completed_at',
'due_at',
'process_request_id',
];
/**
* Test indexCase without case_number.
*
* @return void
*/
public function testIndexCaseRequiresCaseNumber()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Call the endpoint without the 'case_number' parameter
$response = $this->apiCall('GET', self::API_TASK_BY_CASE);
// Check if the response returns a 400 error due to missing 'case_number'
$response->assertStatus(422)
->assertJson(['message' => 'The Case number field is required.']);
}
/**
* Test indexCase returns active tasks related to the case_number with pagination.
*
* @return void
*/
public function testIndexCaseReturnsActiveTasksForCaseNumberPagination()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create();
ProcessRequestToken::factory(12)->create([
'user_id' => $user->id,
'status' => 'ACTIVE',
'process_request_id' => $processRequest->id, // id del ProcessRequest
]);
// Call the endpoint with the 'case_number' parameter page 1
$filter = "?case_number=$processRequest->case_number&page=1&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(5, $response->json('data'));
// Call the endpoint with the 'case_number' parameter page 2
$filter = "?case_number=$processRequest->case_number&page=2&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(5, $response->json('data'));
// Call the endpoint with the 'case_number' parameter page 3
$filter = "?case_number=$processRequest->case_number&page=3&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(2, $response->json('data'));
}
/**
* Test indexCase returns completed tasks related to the case_number.
*
* @return void
*/
public function testIndexCaseReturnsInactiveTasksForCaseNumber()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create();
ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'status' => 'CLOSED',
'process_request_id' => $processRequest->id, // id del ProcessRequest
]);
// Call the endpoint with the 'case_number' parameter
$filter = "?case_number=$processRequest->case_number&status=CLOSED";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(1, $response->json('data'));
$response->assertJsonStructure([
'data' => ['*' => self::TASK_BY_CASE_STRUCTURE],
'meta',
]);
}
/**
* Test indexCase returns completed tasks related to the case_number.
*
* @return void
*/
public function testIndexCaseReturnsInactiveTasksForCaseNumberPagination()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create();
ProcessRequestToken::factory(12)->create([
'user_id' => $user->id,
'status' => 'CLOSED',
'process_request_id' => $processRequest->id, // id del ProcessRequest
]);
// Call the endpoint with the 'case_number' parameter page 1
$filter = "?case_number=$processRequest->case_number&status=CLOSED&page=1&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(5, $response->json('data'));
// Call the endpoint with the 'case_number' parameter page 2
$filter = "?case_number=$processRequest->case_number&status=CLOSED&page=2&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(5, $response->json('data'));
// Call the endpoint with the 'case_number' parameter page 3
$filter = "?case_number=$processRequest->case_number&status=CLOSED&page=3&per_page=5";
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(2, $response->json('data'));
}
/**
* Test indexCase returns completed tasks related to the case_number.
*
* @return void
*/
public function testIndexCaseReturnsWithData()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create();
ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'status' => 'CLOSED',
'process_request_id' => $processRequest->id, // id del ProcessRequest
]);
// Call the endpoint with the 'case_number' parameter
$filter = "?case_number=$processRequest->case_number&status=CLOSED&includeScreen=" . true;
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(1, $response->json('data'));
$response->assertJsonStructure([
'data' => ['*' => array_merge(self::TASK_BY_CASE_STRUCTURE, ['taskData'])],
'meta',
]);
}
/**
* Test verify the corresponding data for each task
*
* @return void
*/
public function testTasksByCaseReturnsCorrectData()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);
// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create();
$data1 = ['form_input_1' => 'value a', 'form_text_area_1' => 'value a'];
$token1 = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'status' => 'CLOSED',
'process_request_id' => $processRequest->id,
'data' => $data1,
]);
$data2 = ['form_input_1' => 'value b', 'form_text_area_1' => 'value b'];
$token2 = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'status' => 'CLOSED',
'process_request_id' => $processRequest->id,
'data' => $data2,
]);
// Call the endpoint with the 'case_number' parameter
$filter = "?case_number=$processRequest->case_number&status=CLOSED&includeScreen=" . true;
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(2, $response->json('data'));
$response->assertJsonStructure([
'data' => ['*' => array_merge(self::TASK_BY_CASE_STRUCTURE, ['taskData'])],
'meta',
]);
// Validate the data returned in the response
$responseData = $response->json('data');
// Validate the first token's data
$this->assertEquals($token1->id, $responseData[0]['id']);
$this->assertEquals($data1['form_input_1'], $responseData[0]['taskData']['form_input_1']);
$this->assertEquals($data1['form_text_area_1'], $responseData[0]['taskData']['form_text_area_1']);
// Validate the second token's data
$this->assertEquals($token2->id, $responseData[1]['id']);
$this->assertEquals($data2['form_input_1'], $responseData[1]['taskData']['form_input_1']);
$this->assertEquals($data2['form_text_area_1'], $responseData[1]['taskData']['form_text_area_1']);
}
public function testShowTaskIncludesNewProperty()
{
$user = User::factory()->create();
$this->actingAs($user);
$comments = 'This is a comment';
$task = ProcessRequestToken::factory()->create([
'comments' => $comments,
]);
$this->assertEquals($comments, $task->comments);
}
private function taskShowUrl(ProcessRequestToken $task): string
{
return route('api.tasks.show', $task);
}
/**
* When a task has no draft, the include=draft response key should be null.
*/
public function testIncludeDraftReturnsNullWhenNoDraftExists()
{
$task = ProcessRequestToken::factory()->create(['user_id' => $this->user->id]);
$response = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$response->assertStatus(200);
$response->assertJsonPath('draft', null);
}
/**
* When drafts are enabled and a draft exists, it is returned as-is and not deleted.
*/
public function testIncludeDraftReturnsDraftWhenDraftsEnabled()
{
Config::set('app.task_drafts_enabled', true);
$task = ProcessRequestToken::factory()->create(['user_id' => $this->user->id]);
$draft = TaskDraft::factory()->create([
'task_id' => $task->id,
'data' => ['field' => 'value'],
]);
$response = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$response->assertStatus(200);
$response->assertJsonPath('draft.id', $draft->id);
$this->assertDatabaseHas('task_drafts', ['id' => $draft->id]);
}
/**
* When drafts are disabled and merge_draft_on_restore is true, the draft is
* deleted after being accessed and null is returned to the frontend.
* This is the quick-fill scenario: a draft was created to carry data into the
* screen, but since drafts are disabled it must be consumed exactly once.
*/
public function testIncludeDraftDeletesDraftAndReturnsNullWhenDraftsDisabledAndMergeOnRestoreEnabled()
{
Config::set('app.task_drafts_enabled', false);
Config::set('app.screen.merge_draft_on_restore', true);
$task = ProcessRequestToken::factory()->create(['user_id' => $this->user->id]);
$draft = TaskDraft::factory()->create([
'task_id' => $task->id,
'data' => ['field' => 'value'],
]);
$response = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$response->assertStatus(200);
$response->assertJsonPath('draft', null);
$this->assertDatabaseMissing('task_drafts', ['id' => $draft->id]);
}
/**
* When drafts are disabled but merge_draft_on_restore is false, the draft is
* returned as-is and NOT deleted, preserving it for future accesses.
*/
public function testIncludeDraftReturnsDraftWithoutDeletingWhenDraftsDisabledAndMergeOnRestoreDisabled()
{
Config::set('app.task_drafts_enabled', false);
Config::set('app.screen.merge_draft_on_restore', false);
$task = ProcessRequestToken::factory()->create(['user_id' => $this->user->id]);
$draft = TaskDraft::factory()->create([
'task_id' => $task->id,
'data' => ['field' => 'value'],
]);
$response = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$response->assertStatus(200);
$response->assertJsonPath('draft.id', $draft->id);
$this->assertDatabaseHas('task_drafts', ['id' => $draft->id]);
}
/**
* Quick-fill end-to-end: an inbox rule creates a draft to pre-populate the screen
* (task_drafts_enabled=false, merge_draft_on_restore=true).
*
* The draft must be consumed exactly once:
* - First request: draft data is present in the response (the frontend merges it)
* and the record is immediately deleted.
* - Second request: no draft record exists, so null is returned cleanly without errors.
*
* This protects the quick-fill feature from regressions: if the draft were not deleted
* on the first access the data would be re-merged on every subsequent page load; if
* an error were thrown on the second access the task screen would break.
*/
public function testQuickFillDraftIsConsumedExactlyOnce()
{
Config::set('app.task_drafts_enabled', false);
Config::set('app.screen.merge_draft_on_restore', true);
$task = ProcessRequestToken::factory()->create(['user_id' => $this->user->id]);
// Simulates the draft created by an inbox rule with make_draft=true
$draft = TaskDraft::factory()->create([
'task_id' => $task->id,
'data' => ['prefilled_field' => 'inbox rule value'],
]);
// First access — frontend receives null so it knows to merge from the draft data
// that was embedded in the response before deletion
$firstResponse = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$firstResponse->assertStatus(200);
$firstResponse->assertJsonPath('draft', null);
$this->assertDatabaseMissing('task_drafts', ['id' => $draft->id]);
// Second access — draft is already gone; must return null without errors
$secondResponse = $this->apiCall('GET', $this->taskShowUrl($task), ['include' => 'draft']);
$secondResponse->assertStatus(200);
$secondResponse->assertJsonPath('draft', null);
}
}