-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathCommentTest.php
More file actions
368 lines (318 loc) · 12 KB
/
Copy pathCommentTest.php
File metadata and controls
368 lines (318 loc) · 12 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
<?php
namespace Tests\Feature\Api;
use Database\Seeders\PermissionSeeder;
use Faker\Factory as Faker;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Hash;
use ProcessMaker\Models\Comment;
use ProcessMaker\Models\Permission;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use ProcessMaker\Providers\AuthServiceProvider;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class CommentTest extends TestCase
{
use RequestHelper;
const API_TEST_URL = '/comments';
const API_COMMENT_BY_CASE = '/comments-by-case';
const STRUCTURE = [
'id',
'user_id',
'commentable_id',
'commentable_type',
'subject',
'body',
'hidden',
'type',
'updated_at',
'created_at',
'case_number',
];
protected function withUserSetup()
{
// Seed the permissions table.
Artisan::call('db:seed', ['--class' => 'PermissionSeeder']);
// Reboot our AuthServiceProvider. This is necessary so that it can
// pick up the new permissions and setup gates for each of them.
$asp = new AuthServiceProvider(app());
$asp->boot();
}
public function testGetCommentListAdministrator()
{
$model = ProcessRequestToken::factory()->create();
Comment::factory()->count(10)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => false,
]);
Comment::factory()->count(5)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => true,
]);
$response = $this->apiCall('GET', self::API_TEST_URL, [
'commentable_type' => get_class($model),
'commentable_id' => $model->getKey(),
]);
$json = $response->json('data');
$this->assertCount(15, $json);
}
public function testGetCommentListNoAdministrator()
{
$model = ProcessRequest::factory()->create();
Comment::factory()->count(10)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => false,
]);
Comment::factory()->count(5)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => true,
]);
$this->user = User::factory()->create([
'password' => Hash::make('password'),
'is_administrator' => false,
]);
// Make user a participant
ProcessRequestToken::factory()->create([
'user_id' => $this->user->id,
'process_request_id' => $model->id,
]);
$response = $this->apiCall('GET', self::API_TEST_URL, [
'commentable_type' => get_class($model),
'commentable_id' => $model->getKey(),
]);
$json = $response->json('data');
$this->assertCount(10, $json);
}
public function testGetCommentByType()
{
$this->user = User::factory()->create([
'password' => Hash::make('password'),
'is_administrator' => false,
]);
$model = ProcessRequestToken::factory()->create(['user_id' => $this->user->getKey()]);
Comment::factory()->count(10)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => false,
]);
Comment::factory()->count(5)->create([
'commentable_id' => $model->getKey(),
'commentable_type' => get_class($model),
'hidden' => true,
]);
$model2 = ProcessRequest::factory()->create();
Comment::factory()->count(10)->create([
'commentable_id' => $model2->getKey(),
'commentable_type' => get_class($model2),
'hidden' => false,
]);
Comment::factory()->count(5)->create([
'commentable_id' => $model2->getKey(),
'commentable_type' => get_class($model2),
'hidden' => true,
]);
$response = $this->apiCall('GET', self::API_TEST_URL, [
'commentable_type' => get_class($model),
'commentable_id' => $model->getKey(),
]);
$json = $response->json('data');
$this->assertCount(10, $json);
}
/**
* Test verify the parameter required for create form
*/
public function testNotCreatedForParameterRequired()
{
//Post should have the parameter required
$response = $this->apiCall('POST', self::API_TEST_URL, []);
//Validate the header status code
$response->assertStatus(422);
$this->assertArrayHasKey('message', $response->json());
}
public function testCreateComment()
{
$comment = Comment::factory()->make();
$response = $this->apiCall('POST', self::API_TEST_URL, $comment->toArray());
//Validate the header status code
$response->assertStatus(201);
//Validate the header status code
$response->assertStatus(201);
$response->assertJsonStructure(self::STRUCTURE);
}
/**
* Get a comment
*/
public function testGetComment()
{
//get the id from the factory
$comment = Comment::factory()->create()->id;
//load api
$response = $this->apiCall('GET', self::API_TEST_URL . '/' . $comment);
//Validate the status is correct
$response->assertStatus(200);
//verify structure
$response->assertJsonStructure(self::STRUCTURE);
}
/**
* Delete comment
*/
public function testDeleteComment()
{
//Remove comment
$url = self::API_TEST_URL . '/' . Comment::factory()->create(['user_id' => $this->user->getKey()])->id;
$response = $this->apiCall('DELETE', $url);
//Validate the header status code
$response->assertStatus(204);
}
/**
* The comment does not exist
*/
public function testDeleteCommentNotExist()
{
//Comment not exist
$url = self::API_TEST_URL . '/' . Comment::factory()->make()->id;
$response = $this->apiCall('DELETE', $url);
//Validate the header status code
$response->assertStatus(405);
}
/**
* Test indexCase without case_number.
*
* @return void
*/
public function testIndexCaseRequiresCaseNumber()
{
// Call the endpoint without the 'case_number' parameter
$response = $this->apiCall('GET', self::API_COMMENT_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 comments by case
*/
public function testGetCommentByTypeByCase()
{
$this->user = User::factory()->create([
'password' => Hash::make('password'),
'is_administrator' => false,
]);
// Create a request1 then a task related to the request
$request1 = ProcessRequest::factory()->create();
$task1 = ProcessRequestToken::factory()->create([
'user_id' => $this->user->getKey(),
'process_request_id' => $request1->getKey(),
]);
Comment::factory()->count(2)->create([
'commentable_id' => $request1->getKey(),
'commentable_type' => get_class($request1),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
Comment::factory()->count(2)->create([
'commentable_id' => $task1->getKey(),
'commentable_type' => get_class($task1),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
// Create a request2 with the same case_number then a task related to the request
$request2 = ProcessRequest::factory()->create([
'parent_request_id' => $request1->getKey(),
]);
$task2 = ProcessRequestToken::factory()->create([
'user_id' => $this->user->getKey(),
'process_request_id' => $request2->getKey(),
]);
Comment::factory()->count(2)->create([
'commentable_id' => $request2->getKey(),
'commentable_type' => get_class($request2),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
Comment::factory()->count(2)->create([
'commentable_id' => $task2->getKey(),
'commentable_type' => get_class($task2),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
// Load api
$filter = "?case_number=$request1->case_number";
$response = $this->apiCall('GET', self::API_COMMENT_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(8, $response->json('data'));
}
/**
* Test comments by case with pagination
*/
public function testGetCommentByTypeByCasePagination()
{
$this->user = User::factory()->create([
'password' => Hash::make('password'),
'is_administrator' => false,
]);
// Create a request1 then a task related to the request
$request1 = ProcessRequest::factory()->create();
$task1 = ProcessRequestToken::factory()->create([
'user_id' => $this->user->getKey(),
'process_request_id' => $request1->getKey(),
]);
Comment::factory()->count(2)->create([
'commentable_id' => $request1->getKey(),
'commentable_type' => get_class($request1),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
Comment::factory()->count(2)->create([
'commentable_id' => $task1->getKey(),
'commentable_type' => get_class($task1),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
// Create a request2 with the same case_number then a task related to the request
$request2 = ProcessRequest::factory()->create([
'parent_request_id' => $request1->getKey(),
]);
$task2 = ProcessRequestToken::factory()->create([
'user_id' => $this->user->getKey(),
'process_request_id' => $request2->getKey(),
]);
Comment::factory()->count(2)->create([
'commentable_id' => $request2->getKey(),
'commentable_type' => get_class($request2),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
Comment::factory()->count(2)->create([
'commentable_id' => $task2->getKey(),
'commentable_type' => get_class($task2),
'case_number' => $request1->case_number,
'type' => 'LOG',
'hidden' => false,
]);
// Call the endpoint with the 'case_number' parameter page 1
$filter = "?case_number=$request1->case_number&page=1&per_page=5";
$response = $this->apiCall('GET', self::API_COMMENT_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=$request1->case_number&page=2&per_page=5";
$response = $this->apiCall('GET', self::API_COMMENT_BY_CASE . $filter);
// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(3, $response->json('data'));
}
}