Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Controller/Admin/QueuedJobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cake\Http\Exception\NotFoundException;
use Cake\I18n\DateTime;
use Cake\View\JsonView;
use InvalidArgumentException;
use Queue\Queue\TaskFinder;
use RuntimeException;

Expand Down Expand Up @@ -304,14 +305,21 @@ public function data(?int $id = null) {
}

if ($this->request->is(['patch', 'post', 'put'])) {
$queuedJob = $this->QueuedJobs->patchEntity($queuedJob, $this->request->getData());
if ($this->QueuedJobs->save($queuedJob)) {
$this->Flash->success(__d('queue', 'The queued job has been saved.'));
try {
$queuedJob = $this->QueuedJobs->patchEntity($queuedJob, $this->request->getData());
if ($this->QueuedJobs->save($queuedJob)) {
$this->Flash->success(__d('queue', 'The queued job has been saved.'));

return $this->redirect(['action' => 'view', $id]);
}
return $this->redirect(['action' => 'view', $id]);
}

$this->Flash->error(__d('queue', 'The queued job could not be saved. Please try again.'));
$this->Flash->error(__d('queue', 'The queued job could not be saved. Please try again.'));
} catch (InvalidArgumentException $e) {
$this->Flash->error($e->getMessage());

// Preserve the user's invalid input so they can fix it
$queuedJob->data_string = $this->request->getData('data_string');
}
}

$this->set(compact('queuedJob'));
Expand Down
7 changes: 6 additions & 1 deletion src/Model/Behavior/JsonableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,15 @@ public function _decode($val) {
/**
* @param string $val
*
* @throws \InvalidArgumentException
*
* @return array
*/
protected function _fromJson(string $val): array {
$json = json_decode($val, true, JSON_THROW_ON_ERROR);
$json = json_decode($val, true);
if (!is_array($json)) {
throw new InvalidArgumentException('Invalid JSON: ' . json_last_error_msg());
}

return $json;
}
Expand Down
1 change: 1 addition & 0 deletions src/Model/Entity/QueuedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @property int $id
* @property string $job_task
* @property array|null $data
* @property string|null $data_string Virtual property from JsonableBehavior
* @property string|null $job_group
* @property string|null $reference
* @property \Cake\I18n\DateTime $created
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/CostsExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CostsExampleTask extends Task implements AddInterface, AddFromBackendInter
public function add(?string $data): void {
$this->io->out('CakePHP Queue CostsExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/ExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ExampleTask extends Task implements AddInterface, AddFromBackendInterface
public function add(?string $data): void {
$this->io->out('CakePHP Queue Example task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/ExceptionExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ExceptionExampleTask extends Task implements AddInterface, AddFromBackendI
public function add(?string $data): void {
$this->io->out('CakePHP Queue ExceptionExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/MonitorExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MonitorExampleTask extends Task implements AddInterface, AddFromBackendInt
public function add(?string $data): void {
$this->io->out('CakePHP Queue MonitorExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/ProgressExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ProgressExampleTask extends Task implements AddInterface, AddFromBackendIn
public function add(?string $data): void {
$this->io->out('CakePHP Queue ProgressExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add the Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/RetryExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function init(): bool {
public function add(?string $data): void {
$this->io->out('CakePHP Queue RetryExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/SuperExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SuperExampleTask extends Task implements AddInterface, AddFromBackendInter
public function add(?string $data): void {
$this->io->out('CakePHP Queue SuperExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
2 changes: 1 addition & 1 deletion src/Queue/Task/UniqueExampleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UniqueExampleTask extends Task implements AddInterface, AddFromBackendInte
public function add(?string $data): void {
$this->io->out('CakePHP Queue UniqueExample task.');
$this->io->hr();
$this->io->out($this->description());
$this->io->out($this->description() ?? '');
$this->io->out('I will now add an example Job into the Queue.');
$this->io->out(' ');
$this->io->out('To run a Worker use:');
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Controller/Admin/QueuedJobsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ public function testDataPost() {
$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
*
Expand Down
Loading