Skip to content

Commit

Permalink
Many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ddurieux committed Apr 12, 2024
1 parent f170625 commit b0dfc95
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
19 changes: 4 additions & 15 deletions inc/deployaction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,10 @@ function check_data_from_form() {
function add_item($params) {
//append prepared data to new entry
$id = $params['id'];
$actiontype = $params['actionstype'];
unset($params['id']);
$new_entry[$params['actionstype']] = $params;
unset($params['actionstype']);
$new_entry[$actiontype] = $params;

//get current order json
$data = json_decode($this->getJson($id), true);
Expand All @@ -599,26 +601,13 @@ function add_item($params) {
*/
function save_item($params) {
$tmp = [];
$fields = ['list', 'from', 'to', 'exec', 'name', 'logLineLimit'];
$fields = ['list', 'from', 'to', 'exec', 'name', 'logLineLimit', 'retChecks'];
foreach ($fields as $field) {
if (isset($params[$field])) {
$tmp[$field] = $params[$field];
}
}

//process ret checks
if (isset($params['retchecks_type']) && !empty($params['retchecks_type'])) {
foreach ($params['retchecks_type'] as $index => $type) {
//if type == '0', this means nothing is selected
if ($type !== '0') {
$tmp['retChecks'][] = [
'type' => $type,
'values' => [$params['retchecks_value'][$index]]
];
}
}
}

//append prepared data to new entry
$entry[$params['actionstype']] = $tmp;

Expand Down
19 changes: 16 additions & 3 deletions inc/deploypackageitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,23 @@ function prepareDataToSave($params, $entry) {
//get current order json
$data = json_decode($this->getJson($params['id']), true);

if (isset($params['index'])) {
// TODO remove this because used only for tests, so not test the else
$index = $params['index'];
} else {
$index = filter_input(INPUT_POST, 'index');
if (is_null($index) || !is_numeric($index)) {
return $data;
}
$index = intval($index);
}
//unset index
unset($data['jobs'][$this->json_name][$params['index']]);
unset($data['jobs'][$this->json_name][$index]);

//add new data at index position
//(array_splice for insertion, ex : http://stackoverflow.com/a/3797526)
array_splice($data['jobs'][$this->json_name],
$params['index'], 0, [$entry]);
$index, 0, [$entry]);

return $data;
}
Expand Down Expand Up @@ -344,8 +354,11 @@ function updateOrderJson($packages_id, $data) {
);
$error = 1;
} else {
$error = $pfDeployPackage->update(['id' => $packages_id,
$ret = $pfDeployPackage->update(['id' => $packages_id,
'json' => Toolbox::addslashes_deep($json)]);
if (!$ret) {
$error = 1;
}
}
return $error;
}
Expand Down
3 changes: 3 additions & 0 deletions install/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -10251,6 +10251,9 @@ function migrateTablesFromFusinvDeploy ($migration) {

$packages = $DB->request('glpi_plugin_fusioninventory_deploypackages');
foreach ($packages as $order_config) {
// Fix problem with have only one \ in some part of the json string
$order_config['json'] = preg_replace('/\\\\/', '\\\\\\\\', $order_config['json']);

$json_order = json_decode($order_config['json']);
//print("deployorders fixer : actual order structure for ID ".$order_config['id']."\n" . print_r($json_order,true) ."\n");

Expand Down
17 changes: 9 additions & 8 deletions tests/Unit/Deploy/DeployactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ public function testAdd_item() {
$action = new PluginFusioninventoryDeployAction();
$params = ['id' => $packages_id,
'actionstype' => 'cmd',
'name' => 'Command ls',
'exec' => 'ls -lah',
'name' => 'Command ls',
'logLineLimit' => 100,
'retChecks' => ['type' => 'okCode', 'values' => [127]],
];
$action->add_item($params);
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100}}],"userinteractions":[]},"associatedFiles":[]}';
$ret = $action->add_item($params);
$this->assertTrue($ret);
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100,"retChecks":{"type":"okCode","values":[127]}}}],"userinteractions":[]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);

Expand All @@ -127,7 +128,7 @@ public function testAdd_item() {
];
$action->add_item($params);

$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":"100"}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}}],"userinteractions":[]},"associatedFiles":[]}';
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100,"retChecks":{"type":"okCode","values":[127]}}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}}],"userinteractions":[]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);

Expand All @@ -139,7 +140,7 @@ public function testAdd_item() {
];
$action->add_item($params);

$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":"100"}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}}],"userinteractions":[]},"associatedFiles":[]}';
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100,"retChecks":{"type":"okCode","values":[127]}}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}}],"userinteractions":[]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);

Expand All @@ -150,7 +151,7 @@ public function testAdd_item() {
];
$action->add_item($params);

$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":"100"}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}}],"userinteractions":[]},"associatedFiles":[]}';
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100,"retChecks":{"type":"okCode","values":[127]}}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}}],"userinteractions":[]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);
$params = ['id' => $packages_id,
Expand All @@ -160,7 +161,7 @@ public function testAdd_item() {
];
$action->add_item($params);

$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":"100"}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}},{"delete":{"to":"/tmp/foo","name":"Delete directory /tmp/foo"}}],"userinteractions":[]},"associatedFiles":[]}';
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -lah","name":"Command ls","logLineLimit":100,"retChecks":{"type":"okCode","values":[127]}}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}},{"delete":{"to":"/tmp/foo","name":"Delete directory /tmp/foo"}}],"userinteractions":[]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);

Expand Down Expand Up @@ -189,7 +190,7 @@ public function testSave_item() {
'retChecks' => ['type' => 'okCode', 'values' => [127]],
];
$action->save_item($params);
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -la","name":"Command ls -la \'s","logLineLimit":"100"}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}},{"delete":{"to":"/tmp/foo","name":"Delete directory /tmp/foo"}}]},"associatedFiles":[]}';
$expected = '{"jobs":{"checks":[],"associatedFiles":[],"actions":[{"cmd":{"exec":"ls -la","name":"Command ls -la \'s","logLineLimit":"100","retChecks":{"type":"okCode","values":[127]}}},{"move":{"from":"*","to":"/tmp/","name":"Move to /tmp"}},{"copy":{"from":"*","to":"/tmp/","name":"Copy to /tmp"}},{"mkdir":{"to":"/tmp/foo","name":"Create directory /tmp/foo"}},{"delete":{"to":"/tmp/foo","name":"Delete directory /tmp/foo"}}]},"associatedFiles":[]}';
$json = Toolbox::stripslashes_deep($action->getJson($packages_id));
$this->assertEquals($expected, $json);

Expand Down

0 comments on commit b0dfc95

Please sign in to comment.