From b0dfc95f547a8d8fb8b36c35b63fcb73b33bbf48 Mon Sep 17 00:00:00 2001 From: David Durieux Date: Fri, 12 Apr 2024 11:01:41 +0200 Subject: [PATCH] Many fixes --- inc/deployaction.class.php | 19 ++++--------------- inc/deploypackageitem.class.php | 19 ++++++++++++++++--- install/update.php | 3 +++ tests/Unit/Deploy/DeployactionTest.php | 17 +++++++++-------- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/inc/deployaction.class.php b/inc/deployaction.class.php index ec7f3855b..f5ec7bc17 100644 --- a/inc/deployaction.class.php +++ b/inc/deployaction.class.php @@ -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); @@ -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; diff --git a/inc/deploypackageitem.class.php b/inc/deploypackageitem.class.php index 4ababfc07..3930623ff 100644 --- a/inc/deploypackageitem.class.php +++ b/inc/deploypackageitem.class.php @@ -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; } @@ -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; } diff --git a/install/update.php b/install/update.php index ebae65215..03d827b57 100644 --- a/install/update.php +++ b/install/update.php @@ -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"); diff --git a/tests/Unit/Deploy/DeployactionTest.php b/tests/Unit/Deploy/DeployactionTest.php index 0fb7bd59c..42e3d0180 100644 --- a/tests/Unit/Deploy/DeployactionTest.php +++ b/tests/Unit/Deploy/DeployactionTest.php @@ -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); @@ -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); @@ -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); @@ -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, @@ -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); @@ -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);