N°7216 import improves error handling missing or null data (#612)

Co-authored-by: Thomas Casteleyn <thomas.casteleyn@super-visions.com>
This commit is contained in:
Anne-Catherine
2025-01-30 16:14:14 +01:00
committed by GitHub
parent 8a604c643e
commit 8e0e01ad40
6 changed files with 121 additions and 46 deletions

View File

@@ -222,30 +222,36 @@ class BulkChangeTest extends ItopDataTestCase
* @param $aExtKeys
* @param $aReconcilKeys
*/
public function testBulkChangeWithExistingData($aInitData, $aCsvData, $aAttributes, $aExtKeys, $aReconcilKeys, $aResult, $aResultHTML= null) {
public function testBulkChangeWithExistingData($aInitData, $aCsvData, $aAttributes, $aExtKeys, $aReconcilKeys, $aResult, $aResultHTML= null ) {
//change value during the test
$db_core_transactions_enabled=MetaModel::GetConfig()->Get('db_core_transactions_enabled');
MetaModel::GetConfig()->Set('db_core_transactions_enabled',false);
if (is_array($aInitData) && sizeof($aInitData) != 0) {
/** @var Server $oServer */
$oServer = $this->createObject('Server', array(
'name' => $aInitData[1],
'status' => $aInitData[2],
'org_id' => $aInitData[0],
'name' => $aInitData[1],
'status' => $aInitData[2],
'org_id' => $aInitData[0],
'purchase_date' => $aInitData[3],
));
$aCsvData[0][2]=$oServer->GetKey();
$aResult[2]=$oServer->GetKey();
if ($aResult["id"]==="{Id of the server created by the test}") {
$aResult["id"]=$oServer->GetKey();
if ($aResultHTML!==null){
$aResultHTML[2]=$oServer->GetKey();
$aResultHTML["id"]=$oServer->GetKey();
if(array_key_exists('id', $aAttributes)) {
$iColumnForId = $aAttributes['id'];
$aCsvData[0][$iColumnForId] = $oServer->GetKey();
$aResult[$iColumnForId] = $oServer->GetKey();
if ($aResult["id"] === "{Id of the server created by the test}") {
$aResult["id"] = $oServer->GetKey();
if ($aResultHTML !== null) {
$aResultHTML[$iColumnForId] = $oServer->GetKey();
$aResultHTML["id"] = $oServer->GetKey();
}
}
}
$this->debug("oServer->GetKey():".$oServer->GetKey());
}
$oBulk = new BulkChange(
"Server",
$aCsvData,
@@ -270,7 +276,7 @@ class BulkChangeTest extends ItopDataTestCase
$this->debug('GetCLIValue:'.$oCell->GetCLIValue());
$this->debug("aResult:".$aResult[$i]);
$this->assertEquals( $aResult[$i], $oCell->GetCLIValue(), "failure on " . get_class($oCell) . ' cell type for cell number ' . $i );
if (null !== $aResultHTML) {
if (null !== $aResultHTML && array_key_exists($i, $aResultHTML)) {
$this->assertEquals($aResultHTML[$i], $oCell->GetHTMLValue(), "failure on " . get_class($oCell) . ' cell type for cell number ' . $i);
}
} else if ($i === "__ERRORS__") {
@@ -294,37 +300,39 @@ class BulkChangeTest extends ItopDataTestCase
["1", "Server1", "production", ""],
"csvData" =>
[[">Demo", "Server1"]],
"attributes"=>
["name" => 1],
"extKeys"=>
["org_id" => ["name" => 0]],
"reconcilKeys"=>
["name"],
"expectedResult"=>
[
0 => ">Demo",
"org_id" => "n/a",
1 => "Server1",
"id" => "Invalid value for attribute",
"__STATUS__" => "Issue: ambiguous reconciliation",
"__ERRORS__" => "Allowed 'status' value(s): stock,implementation,production,obsolete",
],
"attributes"=>
["name" => 1],
"extKeys"=>
["org_id" => ["name" => 0]],
"reconcilKeys"=>
["name"],
"expectedResult"=>
[
0 => ">Demo",
"org_id" => "n/a",
1 => "Server1",
"id" => "Invalid value for attribute",
"name" => "Invalid value for attribute",
"__STATUS__" => "Issue: ambiguous reconciliation",
"__ERRORS__" => "Object not found",
],
"expectedResultHTML"=>
[
0 => "&gt;Demo",
"org_id" => "n/a",
1 => "Server1",
"id" => "Invalid value for attribute",
"name" => "Invalid value for attribute",
"__STATUS__" => "Issue: ambiguous reconciliation",
"__ERRORS__" => "Allowed 'status' value(s): stock,implementation,production,obsolete",
"__ERRORS__" => "Object not found",
],
],
"Case 6 - 1 : Unexpected value (update)" => [
"initData"=>
["1", ">ServerTest", "production", ""],
"csvData"=>
"csvData"=>
[["Demo", ">ServerTest", "key - will be automatically overwritten by test", ">BadValue", ""]],
"attributes"=>
"attributes"=>
["name" => 1, "id" => 2, "status" => 3, "purchase_date" => 4],
"extKeys"=>
["org_id" => ["name" => 0]],
@@ -354,7 +362,6 @@ class BulkChangeTest extends ItopDataTestCase
"__STATUS__" => "Issue: Unexpected attribute value(s)",
"__ERRORS__" => "Allowed 'status' value(s): stock,implementation,production,obsolete",
],
],
"Case 6 - 2 : Unexpected value (update)" => [
"initData"=>
@@ -396,9 +403,9 @@ class BulkChangeTest extends ItopDataTestCase
"initData"=>
[],
"csvData"=>
[["Demo", ">ServerTest", "<svg onclick\"alert(1)\">", ""]],
[["Demo", ">ServerTest", "<svg onclick\"alert(1)\">", 1]],
"attributes"=>
["name" => 1, "status" => 2, "purchase_date" => 3],
["name" => 1, "status" => 2, 'id'=> 3],
"extKeys"=>
["org_id" => ["name" => 0]],
"reconcilKeys"=>
@@ -410,7 +417,7 @@ class BulkChangeTest extends ItopDataTestCase
"org_id" => "3",
1 => "\">ServerTest\"",
2 => '\'<svg onclick"alert(1)">\' is an invalid value',
3 => "",
3 => "1",
"__STATUS__" => "Issue: Unexpected attribute value(s)",
"__ERRORS__" => "Allowed 'status' value(s): stock,implementation,production,obsolete",
],
@@ -421,7 +428,7 @@ class BulkChangeTest extends ItopDataTestCase
"org_id" => "3",
1 => "&gt;ServerTest",
2 => "'&lt;svg onclick&quot;alert(1)&quot;&gt;' is an invalid value",
3 => "",
3 => "1",
"__STATUS__" => "Issue: Unexpected attribute value(s)",
"__ERRORS__" => "Allowed 'status' value(s): stock,implementation,production,obsolete",
],
@@ -580,11 +587,52 @@ class BulkChangeTest extends ItopDataTestCase
[ "id" => "{Id of the server created by the test}",
0 => "Demo", "org_id" => "n/a", 1 => ">ServerTest", 2 => "1", 3 => "production", 4 => "'2020-20-03' is an invalid value", "id" => 1, "__STATUS__" => "Issue: wrong date format"],
],
"Case 11 : Missing AttributeDateTime cell should issue an error" => [
"initData"=>
["1", "ServerTest", "production", "2020-02-01"],
"csvData"=>
[["1"]],
"attributes"=>
["id" => 0,"purchase_date" => 1],
"extKeys"=>
[],
"reconcilKeys"=>
["id"],
"expectedResult"=>
[ 1 => "'' is an invalid value", "id" => 1, "__STATUS__" => 'Issue: Not the expected number of columns (found: 1, expected: 2)'],
],
"Case 12 : Missing AttributeEnum cell should issue an error" => [
"initData"=>
["1", "ServerTest", "production", "2020-02-01"],
"csvData"=>
[[ "1"]], // missing status
"attributes"=>
[ "id" => 0, "status" => 1],
"extKeys"=>
[],
"reconcilKeys"=>
["id"],
"expectedResult"=>
[ 1 => "'' is an invalid value", "id" => 1, "__STATUS__" => 'Issue: Not the expected number of columns (found: 1, expected: 2)'],
],
"Case 13 : Missing AttributeExternalKey cell should issue an error" => [
"initData"=>
["1", "ServerTest", "production", "2020-02-01"],
"csvData"=>
[[ "1"]], // missing org_id
"attributes"=>
[ "id" => 0],
"extKeys"=>
["org_id" => ["name" => 1]],
"reconcilKeys"=>
["id"],
"expectedResult"=>
[ 1 => "'' is an invalid value", "id" => 1, "__STATUS__" => 'Issue: Not the expected number of columns (found: 1, expected: 2)'],
],
];
}
/**
* test $oBulk->Process with new server and new organization datas
*
@@ -618,7 +666,7 @@ class BulkChangeTest extends ItopDataTestCase
'org_id' => $oOrganisation->GetKey(),
'purchase_date' => $aInitData["serverPurchaseDate"],
));
$aCsvData[0][2]=$oServer->GetKey();
$aCsvData[0][2] = $oServer->GetKey();
$aResult[2]=$oServer->GetKey();
if ($aResult["id"]==="{Id of the server created by the test}") {
$aResult["id"]=$oServer->GetKey();
@@ -648,7 +696,7 @@ class BulkChangeTest extends ItopDataTestCase
$this->debug('GetCLIValue:'.$oCell->GetCLIValue());
$this->debug("aResult:".$aResult[$i]);
$this->assertEquals($aResult[$i], $oCell->GetCLIValue(), "$i cell is incorrect");
if (null !== $aResultHTML) {
if (null !== $aResultHTML && array_key_exists($i, $aResultHTML)) {
$this->assertEquals($aResultHTML[$i], $oCell->GetHTMLValue());
}
} elseif ($i === "__STATUS__") {
@@ -675,7 +723,7 @@ class BulkChangeTest extends ItopDataTestCase
"csvData" =>
[["Demo", ">Server1"]],
"attributes"=>
["name" => 1],
["name" => 1,"id" => 2],
"extKeys"=>
["org_id" => ["name" => 0]],
"reconcilKeys"=>