diff --git a/application/dashboardlayout.class.inc.php b/application/dashboardlayout.class.inc.php index d9fa2362d2..813b16c4ec 100644 --- a/application/dashboardlayout.class.inc.php +++ b/application/dashboardlayout.class.inc.php @@ -137,7 +137,7 @@ abstract class DashboardLayoutMultiCol extends DashboardLayout /** @var \Dashlet $oDashlet */ foreach ($aDashlets as $oDashlet) { if ($oDashlet::IsVisible()) { - $oDashboardGrid->AddDashlet($oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams), $oDashlet->GetID(), get_class($oDashlet), $oDashlet->GetNormalizedProperties()); + $oDashboardGrid->AddDashlet($oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams), $oDashlet->GetID(), get_class($oDashlet), $oDashlet->GetDenormalizedProperties()); //$oDashboardColumn->AddUIBlock($oDashlet->DoRender($oPage, $bEditMode, true /* bEnclosingDiv */, $aExtraParams)); } } diff --git a/application/dashlet.class.inc.php b/application/dashlet.class.inc.php index 77ed69fae0..f414fe20b3 100644 --- a/application/dashlet.class.inc.php +++ b/application/dashlet.class.inc.php @@ -506,7 +506,7 @@ EOF $this->sDashletType = $sDashletType; } - public function GetNormalizedProperties(): ?array + public function GetDenormalizedProperties(): ?array { return XMLNormalizer::GetInstance()->Denormalize($this->aProperties, get_class($this), 'Dashlet'); } diff --git a/sources/PropertyType/Serializer/XMLFormat/XMLFormatCSV.php b/sources/PropertyType/Serializer/XMLFormat/XMLFormatCSV.php index d8382722c5..511b11efba 100644 --- a/sources/PropertyType/Serializer/XMLFormat/XMLFormatCSV.php +++ b/sources/PropertyType/Serializer/XMLFormat/XMLFormatCSV.php @@ -14,18 +14,22 @@ class XMLFormatCSV extends AbstractXMLFormat { public function Normalize($value, AbstractValueType $oValueType): mixed { - return implode(',', $value); + return $value; } public function EncodeToDOMNode(mixed $normalizedValue, DesignElement $oDOMNode, AbstractValueType $oValueType): void { + if (is_array($normalizedValue)) { + $normalizedValue = implode(',', $normalizedValue); + } $oTextNode = $oDOMNode->ownerDocument->createTextNode($normalizedValue); $oDOMNode->appendChild($oTextNode); } public function DecodeFromDOMNode(DesignElement $oDOMNode, AbstractValueType $oValueType): mixed { - return $oDOMNode->GetText(''); + $value = $oDOMNode->GetText(''); + return explode(',', $value); } public function Denormalize($normalizedValue, AbstractValueType $oValueType): mixed diff --git a/sources/PropertyType/Serializer/XMLSerializer.php b/sources/PropertyType/Serializer/XMLSerializer.php index 37e1e7d818..6e111982e1 100644 --- a/sources/PropertyType/Serializer/XMLSerializer.php +++ b/sources/PropertyType/Serializer/XMLSerializer.php @@ -36,11 +36,11 @@ class XMLSerializer $this->SerializeForPropertyType($value, $oParentNode, $sPropertyTypeXML); } - public function Unserialize(DesignElement $oDOMNode, string $sId, string $sType): mixed + public function Deserialize(DesignElement $oDOMNode, string $sId, string $sType): mixed { $sPropertyTypeXML = PropertyTypeCompiler::GetInstance()->GetXMLContent($sId, $sType); - return $this->UnserializeForPropertyType($oDOMNode, $sPropertyTypeXML); + return $this->DeserializeForPropertyType($oDOMNode, $sPropertyTypeXML); } public function SerializeForPropertyType(mixed $value, DesignElement $oParentNode, string $sPropertyTypeXML): void @@ -50,7 +50,7 @@ class XMLSerializer XMLEncoder::GetInstance()->EncodeForPropertyType($normalizedValue, $oParentNode, $sPropertyTypeXML); } - public function UnserializeForPropertyType(DesignElement $oParentNode, string $sPropertyTypeXML): mixed + public function DeserializeForPropertyType(DesignElement $oParentNode, string $sPropertyTypeXML): mixed { $normalizedValue = XMLEncoder::GetInstance()->DecodeForPropertyType($oParentNode, $sPropertyTypeXML); diff --git a/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLNormalizerTest.php b/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLNormalizerTest.php new file mode 100644 index 0000000000..0c6ef30a08 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLNormalizerTest.php @@ -0,0 +1,170 @@ +RegisterService('ModelReflection', new ModelReflectionRuntime()); + + $oDOMDocument = new PropertyTypeDesign(); + $oDOMDocument->preserveWhiteSpace = false; + $oDOMDocument->formatOutput = true; + + /** @var \Combodo\iTop\DesignElement $oRootNode */ + $oRootNode = $oDOMDocument->createElement('root'); + $oDOMDocument->appendChild($oRootNode); + + $actualValue = Combodo\iTop\PropertyType\Serializer\XMLNormalizer::GetInstance()->NormalizeForPropertyType($denormalizedValue, $sPropertyTypeXML); + + $this->assertEquals($normalizedValue, $actualValue); + } + + public function XMLNormalizerProvider() + { + return [ + 'Basic test should serialize to XML' => [ + 'denormalizedValue' => 'text', + 'sPropertyTypeXML' => << + + Dashlet + + + +XML, + 'normalizedValue' => 'text', + ], + 'Collection of values as CSV' => [ + 'denormalizedValue' => ['Contact', 'Organization'], + 'sPropertyTypeXML' => << + + Dashlet + + + + + + +XML, + 'normalizedValue' => ['Contact', 'Organization'], + ], + 'Collection of values as id attribute' => [ + 'denormalizedValue' => ['Contact', 'Organization'], + 'sPropertyTypeXML' => << + + Dashlet + + + item + + + + + +XML, + 'normalizedValue' => ['Contact', 'Organization'], + ], + 'Collection of tree as flat array' => [ + 'denormalizedValue' => [ + [ + 'title_property' => 'title_a', + 'class_property' => 'class_a', + ], + [ + 'title_property' => 'title_b', + 'class_property' => 'class_b', + ], + ], + 'sPropertyTypeXML' => << + + Dashlet + + + item_count + item_\$rank\$_\$id\$ + + + + + + + + test + + + + +XML, + 'normalizedValue' => [ + 'item_count' => 2, + 'item_0_title_property' => 'title_a', + 'item_0_class_property' => 'class_a', + 'item_1_title_property' => 'title_b', + 'item_1_class_property' => 'class_b', + ], + ], + 'Property tree' => [ + 'denormalizedValue' => ['title_property' => 'title', 'class_property' => 'class'], + 'sPropertyTypeXML' => << + + Dashlet + + + + + + + + test + + + + +XML, + 'normalizedValue' => ['title_property' => 'title', 'class_property' => 'class'], + ], + ]; + } + + /** + * @dataProvider XMLNormalizerProvider + * + * @param $sInputXMLContent + * @param string $sPropertyTypeXML + * @param $expectedValue + * + * @return void + */ + public function testDenormalizeXML($denormalizedValue, string $sPropertyTypeXML, $normalizedValue) + { + ServiceLocator::GetInstance()->RegisterService('ModelReflection', new ModelReflectionRuntime()); + + $aActualValue = Combodo\iTop\PropertyType\Serializer\XMLNormalizer::GetInstance()->DenormalizeForPropertyType($normalizedValue, $sPropertyTypeXML); + + $this->assertEquals($denormalizedValue, $aActualValue); + } +} diff --git a/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLSerializerTest.php b/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLSerializerTest.php index 41d4c14c5d..ee2adce924 100644 --- a/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLSerializerTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/PropertyType/Serializer/XMLSerializerTest.php @@ -191,7 +191,7 @@ XML, /** @var \Combodo\iTop\DesignElement $oRoot */ $oRoot = $oDoc->firstChild; - $aActualValue = Combodo\iTop\PropertyType\Serializer\XMLSerializer::GetInstance()->UnserializeForPropertyType($oRoot, $sPropertyTypeXML); + $aActualValue = Combodo\iTop\PropertyType\Serializer\XMLSerializer::GetInstance()->DeserializeForPropertyType($oRoot, $sPropertyTypeXML); $this->assertEquals($normalizedValue, $aActualValue); }