From 78d524d443d9b94633a39583a79fa1a36842cdb2 Mon Sep 17 00:00:00 2001 From: xtophe38 Date: Thu, 2 Jan 2025 11:32:32 +0100 Subject: [PATCH 1/3] Make sure relative paths can be used when json collector is launched from outside its root directory --- core/jsoncollector.class.inc.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/jsoncollector.class.inc.php b/core/jsoncollector.class.inc.php index c9c472a..2bae2af 100644 --- a/core/jsoncollector.class.inc.php +++ b/core/jsoncollector.class.inc.php @@ -150,7 +150,11 @@ public function Prepare() Utils::Log(LOG_DEBUG, 'Source sFileJson: '.$this->sFileJson); Utils::Log(LOG_INFO, 'Synchro URL (target): '.Utils::GetConfigurationValue('itop_url', array())); } else { - $this->sFileJson = file_get_contents($this->sFilePath); + $this->sFileJson = file_get_contents($this->sFilePath); + if ($this->sFileJson === false) { + $this->sFilePath = APPROOT.$this->sFilePath; + $this->sFileJson = file_get_contents($this->sFilePath); + } Utils::Log(LOG_DEBUG, 'Source sFileJson: '.$this->sFileJson); Utils::Log(LOG_INFO, 'Synchro URL (target): '.Utils::GetConfigurationValue('itop_url', array())); } From bb42d49fd9124e23e1f9860749ee922b0db75233 Mon Sep 17 00:00:00 2001 From: xtophe38 Date: Thu, 2 Jan 2025 16:34:17 +0100 Subject: [PATCH 2/3] Add test for jsonfile parameter defined with relative path --- test/JsonCollectorTest.php | 1 + .../dataTest.json | 38 +++++++++++++++++++ .../expected_generated.csv | 4 ++ .../params.distrib.xml | 34 +++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 test/single_json/json_file_with_relative_path/dataTest.json create mode 100644 test/single_json/json_file_with_relative_path/expected_generated.csv create mode 100644 test/single_json/json_file_with_relative_path/params.distrib.xml diff --git a/test/JsonCollectorTest.php b/test/JsonCollectorTest.php index 266bf90..b9ff42b 100644 --- a/test/JsonCollectorTest.php +++ b/test/JsonCollectorTest.php @@ -119,6 +119,7 @@ public function OrgCollectorProvider() "sort of object xpath parsing via an index" => [ "format_json_5" ], "first row nullified function" => [ "nullified_json_1" ], "another row nullified function" => [ "nullified_json_2" ], + "json file with relative path" => [ "json_file_with_relative_path" ], ]; } diff --git a/test/single_json/json_file_with_relative_path/dataTest.json b/test/single_json/json_file_with_relative_path/dataTest.json new file mode 100644 index 0000000..07f1a38 --- /dev/null +++ b/test/single_json/json_file_with_relative_path/dataTest.json @@ -0,0 +1,38 @@ +{ + "objects": { + "Person::1": { + "key": "1", + "name": {"bob": "My last name"}, + "status": "active", + "org_id": "Blala", + "email": "my.email@foo.org", + "phone": "+00 000 000 000", + "notify": "yes", + "function": "", + "first_nameyo": "My first name" + }, + "Person::2": { + "key": "2", + "name": {"bob": "Picasso"}, + "status": "active", + "org_id": "Demo", + "email": "pablo@demo.com", + "phone": "", + "notify": "yes", + "function": "", + "first_nameyo": "Pablo" + }, + "Person::3": { + "key": "3", + "name": {"bob": "Dali"}, + "status": "active", + "email": "dali@demo.com", + "phone": "", + "notify": "yes", + "function": "", + "first_nameyo": "Salvador" + } + }, + "code": 0, + "message": "Found: 1" +} \ No newline at end of file diff --git a/test/single_json/json_file_with_relative_path/expected_generated.csv b/test/single_json/json_file_with_relative_path/expected_generated.csv new file mode 100644 index 0000000..fdf85aa --- /dev/null +++ b/test/single_json/json_file_with_relative_path/expected_generated.csv @@ -0,0 +1,4 @@ +primary_key;name;status;first_name;email;phone;function;org_id +1;"My last name";active;"My first name";my.email@foo.org;"+00 000 000 000";;Blala +2;Picasso;active;Pablo;pablo@demo.com;123456789;;Demo +3;Dali;active;Salvador;dali@demo.com;123456789;;Demo diff --git a/test/single_json/json_file_with_relative_path/params.distrib.xml b/test/single_json/json_file_with_relative_path/params.distrib.xml new file mode 100644 index 0000000..ccdc98c --- /dev/null +++ b/test/single_json/json_file_with_relative_path/params.distrib.xml @@ -0,0 +1,34 @@ + + + + + collectors/dataTest.json + objects/* + + key + name/bob + status + first_nameyo + email + phone + mobile + function + employeenumber + org_id + + + Demo + active + 123456789 + + + + + $prefix$ + synchro_data_person_1 + + From a8d075bced66f11b1eb139d5635f8d80bf5abcc1 Mon Sep 17 00:00:00 2001 From: xtophe38 Date: Thu, 2 Jan 2025 16:37:13 +0100 Subject: [PATCH 3/3] Provider function must be static for PHP 8.2+ --- test/JsonCollectorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/JsonCollectorTest.php b/test/JsonCollectorTest.php index b9ff42b..a2ba7d1 100644 --- a/test/JsonCollectorTest.php +++ b/test/JsonCollectorTest.php @@ -108,7 +108,7 @@ public function testOrgCollector($sAdditionalDir = '') $this->assertEquals($sExpected_content, file_get_contents(APPROOT."/data/ITopPersonJsonCollector-1.csv")); } - public function OrgCollectorProvider() + public static function OrgCollectorProvider() { return [ "default_value" => [ "default_value" ], @@ -168,7 +168,7 @@ public function testJsonErrors($sAdditionalDir, $sErrorMsg, $sExceptionMsg = fal } } - public function ErrorFileProvider() + public static function ErrorFileProvider() { return [ "error_json_1" => [