-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathQueuedJobsControllerTest.php
More file actions
490 lines (388 loc) · 13.4 KB
/
Copy pathQueuedJobsControllerTest.php
File metadata and controls
490 lines (388 loc) · 13.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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
declare(strict_types=1);
namespace Queue\Test\TestCase\Controller\Admin;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Http\Exception\NotFoundException;
use Cake\I18n\DateTime;
use Cake\TestSuite\IntegrationTestTrait;
use Laminas\Diactoros\UploadedFile;
use Shim\TestSuite\TestCase;
/**
* @uses \Queue\Controller\Admin\QueuedJobsController
*/
class QueuedJobsControllerTest extends TestCase {
use IntegrationTestTrait;
/**
* @return void
*/
public function setUp(): void {
parent::setUp();
$this->loadPlugins(['Queue', 'Search']);
$this->disableErrorHandlerMiddleware();
}
/**
* @var array
*/
protected array $fixtures = [
'plugin.Queue.QueuedJobs',
'plugin.Queue.QueueProcesses',
];
/**
* Test index method
*
* @return void
*/
public function testIndex() {
$this->createJob();
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index']);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testEdit() {
$job = $this->createJob();
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'edit', $job->id]);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testDelete() {
$job = $this->createJob();
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'delete', $job->id]);
$this->assertResponseCode(302);
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
$queuedJob = $queuedJobs->find()->where(['id' => $job->id])->first();
$this->assertNull($queuedJob);
}
/**
* @return void
*/
public function testEditPost() {
$job = $this->createJob();
$data = [
'priority' => 8,
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'edit', $job->id], $data);
$this->assertResponseCode(302);
$queuedJobs = $this->fetchTable('Queue.QueuedJobs');
/** @var \Queue\Model\Entity\QueuedJob $modifiedJob */
$modifiedJob = $queuedJobs->get($job->id);
$this->assertSame(8, $modifiedJob->priority);
}
/**
* @return void
*/
public function testData() {
$job = $this->createJob(['data' => '{"verbose":true,"count":22,"string":"string"}']);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'data', $job->id]);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testDataPost() {
$job = $this->createJob();
$data = [
'data_string' => <<<JSON
{
"class": "App\\\\Command\\\\RealNotificationCommand",
"args": [
"--verbose",
"-d"
]
}
JSON,
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'data', $job->id], $data);
$this->assertResponseCode(302);
/** @var \Queue\Model\Entity\QueuedJob $job */
$job = $this->fetchTable('Queue.QueuedJobs')->get($job->id);
$expected = '{"class":"App\\\\Command\\\\RealNotificationCommand","args":["--verbose","-d"]}';
$this->assertSame($expected, $job->data);
}
/**
* @return void
*/
public function testDataPostInvalidJson(): void {
$job = $this->createJob(['data' => '{"valid":"json"}']);
$this->enableRetainFlashMessages();
$data = [
'data_string' => 'not valid json {',
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'data', $job->id], $data);
$this->assertResponseCode(200);
$this->assertFlashMessage('Invalid JSON: Syntax error');
// Verify original data was not modified
/** @var \Queue\Model\Entity\QueuedJob $job */
$job = $this->fetchTable('Queue.QueuedJobs')->get($job->id);
$this->assertSame('{"valid":"json"}', $job->data);
}
/**
* Test index method
*
* @return void
*/
public function testStats() {
Configure::write('Queue.isStatisticEnabled', true);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'stats']);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testHeatmap(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'heatmap']);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testHeatmapNotEnabled(): void {
Configure::write('Queue.isStatisticEnabled', false);
$this->expectException(NotFoundException::class);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'heatmap']);
}
/**
* @return void
*/
public function testHeatmapWithFilters(): void {
Configure::write('Queue.isStatisticEnabled', true);
$this->createJob(['job_task' => 'Queue.Example']);
$this->get([
'prefix' => 'Admin',
'plugin' => 'Queue',
'controller' => 'QueuedJobs',
'action' => 'heatmap',
'?' => [
'metric' => 'completed',
'days' => 7,
'job_type' => 'Queue.Example',
],
]);
$this->assertResponseCode(200);
$heatmapData = $this->viewVariable('heatmapData');
$this->assertArrayHasKey('grid', $heatmapData);
$this->assertArrayHasKey('summary', $heatmapData);
$this->assertCount(7, $heatmapData['grid']); // 7 days of week
$metric = $this->viewVariable('metric');
$this->assertSame('completed', $metric);
$days = $this->viewVariable('days');
$this->assertSame(7, $days);
$jobType = $this->viewVariable('jobType');
$this->assertSame('Queue.Example', $jobType);
}
/**
* Test index method
*
* @return void
*/
public function testTest() {
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'test']);
$this->assertResponseCode(200);
}
/**
* Test index method
*
* @return void
*/
public function testIndexSearch() {
$incompleteJob = $this->createJob();
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
$completedJob = $queuedJobs->get($incompleteJob->id);
$completedJob->fetched = new DateTime('-1 hour');
$completedJob->completed = new DateTime();
$queuedJobs->saveOrFail($completedJob);
$this->createJob(['job_task' => 'bar']);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index', '?' => ['status' => 'completed']]);
$this->assertResponseCode(200);
$queuedJobs = $this->viewVariable('queuedJobs');
$this->assertCount(1, $queuedJobs, 'Should only show completed jobs');
$jobTasks = collection($queuedJobs)->extract('job_task')->toList();
$this->assertContains('foo', $jobTasks);
$this->assertNotContains('bar', $jobTasks);
}
/**
* The status filter supports running, failed and aborted in addition to
* completed/in_progress/scheduled, so the dashboard stat cards can link to
* the matching job list.
*
* @return void
*/
public function testIndexSearchByRunningFailedAborted() {
$this->createJob(['job_task' => 'waiting']);
$this->createJob(['job_task' => 'running', 'fetched' => new DateTime('-1 minute')]);
$this->createJob(['job_task' => 'failing', 'failure_message' => 'boom', 'attempts' => 1]);
$this->createJob(['job_task' => 'dead', 'failure_message' => 'boom', 'attempts' => 3, 'status' => 'aborted']);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index', '?' => ['status' => 'pending']]);
$this->assertResponseCode(200);
$tasks = collection($this->viewVariable('queuedJobs'))->extract('job_task')->toList();
$this->assertSame(['waiting'], $tasks);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index', '?' => ['status' => 'running']]);
$this->assertResponseCode(200);
$tasks = collection($this->viewVariable('queuedJobs'))->extract('job_task')->toList();
$this->assertSame(['running'], $tasks);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index', '?' => ['status' => 'failed']]);
$tasks = collection($this->viewVariable('queuedJobs'))->extract('job_task')->toList();
sort($tasks);
$this->assertSame(['dead', 'failing'], $tasks);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'index', '?' => ['status' => 'aborted']]);
$tasks = collection($this->viewVariable('queuedJobs'))->extract('job_task')->toList();
$this->assertSame(['dead'], $tasks);
}
/**
* @return void
*/
public function testView() {
$queuedJob = $this->createJob();
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'view', $queuedJob->id]);
$this->assertResponseCode(200);
}
/**
* @return void
*/
public function testViewJson() {
$queuedJob = $this->createJob();
$this->requestAsJson();
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'view', $queuedJob->id]);
$this->assertResponseCode(200);
$content = (string)$this->_response->getBody();
$json = json_decode($content, true);
$this->assertNotEmpty($json);
}
/**
* Test clone method
*
* @return void
*/
public function testClone() {
$job = $this->createJob();
// Mark as completed so it can be cloned
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
$job->fetched = new DateTime('-1 hour');
$job->completed = new DateTime();
$queuedJobs->saveOrFail($job);
$countBefore = $queuedJobs->find()->count();
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'clone', $job->id]);
$this->assertResponseCode(302);
$this->assertRedirect(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'Queue', 'action' => 'index']);
$countAfter = $queuedJobs->find()->count();
$this->assertSame($countBefore + 1, $countAfter, 'A new job should be created');
}
/**
* Test execute method (GET)
*
* @return void
*/
public function testExecute() {
Configure::write('debug', true);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'execute']);
$this->assertResponseCode(200);
}
/**
* Test execute method (POST)
*
* @return void
*/
public function testExecutePost() {
Configure::write('debug', true);
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
$countBefore = $queuedJobs->find()->count();
$data = [
'command' => 'echo "test"',
'amount' => 1,
'escape' => '1',
'log' => '0',
'exit_code' => '',
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'execute'], $data);
$this->assertResponseCode(302);
$countAfter = $queuedJobs->find()->count();
$this->assertSame($countBefore + 1, $countAfter, 'A new job should be created');
}
/**
* Test execute method throws 404 when not in debug mode
*
* @return void
*/
public function testExecuteNotDebug() {
Configure::write('debug', false);
$this->disableErrorHandlerMiddleware();
$this->expectException(NotFoundException::class);
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'execute']);
}
/**
* Test migrate method (GET)
*
* @return void
*/
public function testMigrate() {
$this->get(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'migrate']);
$this->assertResponseCode(200);
}
/**
* Test migrate method (POST)
*
* @return void
*/
public function testMigratePost() {
// Create a job with old-style task name (without plugin prefix)
$this->createJob(['job_task' => 'ProgressExample']);
$data = [
'tasks' => [
'ProgressExample' => '1',
],
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'migrate'], $data);
$this->assertResponseCode(302);
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
/** @var \Queue\Model\Entity\QueuedJob $queuedJob */
$queuedJob = $queuedJobs->find()->where(['job_task' => 'Queue.ProgressExample'])->first();
$this->assertNotNull($queuedJob, 'Job should be migrated to use Queue. prefix');
}
/**
* Test view method
*
* @return void
*/
public function testImport() {
$jsonFile = TESTS . 'test_files' . DS . 'queued-job.json';
$data = [
'file' => new UploadedFile($jsonFile, 1, 0, 'queued-job.json', 'application/json'),
];
$this->post(['prefix' => 'Admin', 'plugin' => 'Queue', 'controller' => 'QueuedJobs', 'action' => 'import'], $data);
$this->assertResponseCode(302);
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
/** @var \Queue\Model\Entity\QueuedJob $queuedJob */
$queuedJob = $queuedJobs->find()->orderByDesc('id')->firstOrFail();
$this->assertSame('Webhook', $queuedJob->job_task);
$this->assertSame('web-hook-102803234', $queuedJob->reference);
}
/**
* Helper method for skipping tests that need a real connection.
*
* @return void
*/
protected function _needsConnection() {
$config = ConnectionManager::getConfig('test');
$skip = !str_contains((string)$config['driver'], 'Mysql') && !str_contains((string)$config['driver'], 'Postgres');
$this->skipIf($skip, 'Only Mysql/Postgres is working yet for this.');
}
/**
* @param array $data
*
* @return \Queue\Model\Entity\QueuedJob
*/
protected function createJob(array $data = []) {
$data += [
'job_task' => 'foo',
];
$queuedJobs = $this->getTableLocator()->get('Queue.QueuedJobs');
$queuedJob = $queuedJobs->newEntity($data);
$queuedJobs->saveOrFail($queuedJob);
return $queuedJob;
}
}