Save as XML serialized

This commit is contained in:
Eric Espie
2026-01-21 17:27:16 +01:00
parent 945d2f32c5
commit e909c41445
27 changed files with 536 additions and 919 deletions

View File

@@ -1,170 +0,0 @@
<?php
/*
* @copyright Copyright (C) 2010-2026 Combodo SAS
* @license http://opensource.org/licenses/AGPL-3.0
*/
use Combodo\iTop\PropertyType\Compiler\PropertyTypeCompiler;
use Combodo\iTop\PropertyType\PropertyTypeDesign;
use Combodo\iTop\Service\DependencyInjection\ServiceLocator;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
class XMLNormalizerTest extends ItopDataTestCase
{
/**
* @dataProvider XMLNormalizerProvider
*
* @param $denormalizedValue
* @param string $sPropertyTypeXML
* @param string $normalizedValue
*
* @return void
* @throws \DOMException
*/
public function testNormalizeXML($denormalizedValue, string $sPropertyTypeXML, $normalizedValue)
{
ServiceLocator::GetInstance()->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' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="basic_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-Label">
</definition>
</property_type>
XML,
'normalizedValue' => 'text',
],
'Collection of values as CSV' => [
'denormalizedValue' => ['Contact', 'Organization'],
'sPropertyTypeXML' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="basic_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-CollectionOfValues">
<xml-format xsi:type="Combodo-XMLFormat-CSV"/>
<value-type xsi:type="Combodo-ValueType-Class">
</value-type>
</definition>
</property_type>
XML,
'normalizedValue' => ['Contact', 'Organization'],
],
'Collection of values as id attribute' => [
'denormalizedValue' => ['Contact', 'Organization'],
'sPropertyTypeXML' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="class_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-CollectionOfValues">
<xml-format xsi:type="Combodo-XMLFormat-ValueAsId">
<tag-name>item</tag-name>
</xml-format>
<value-type xsi:type="Combodo-ValueType-Class">
</value-type>
</definition>
</property_type>
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' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="collection_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-Collection">
<xml-format xsi:type="Combodo-XMLFormat-FlatArray">
<count-tag>item_count</count-tag>
<tag-format>item_\$rank\$_\$id\$</tag-format>
</xml-format>
<prototype>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
</prototype>
</definition>
</property_type>
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' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="property_tree_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-PropertyTree">
<nodes>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
</nodes>
</definition>
</property_type>
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);
}
}

View File

@@ -145,21 +145,40 @@ XML,
</root>
XML,
],
'Property tree' => [
'normalizedValue' => ['title_property' => 'title', 'class_property' => 'class'],
'Collection of tree as flat array in a property tree' => [
'normalizedValue' => [
'collection' => [
[
'title_property' => 'title_a',
'class_property' => 'class_a',
],
[
'title_property' => 'title_b',
'class_property' => 'class_b',
],
],
],
'sPropertyTypeXML' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="property_tree_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<property_type id="collection_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-PropertyTree">
<nodes>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
<definition xsi:type="Combodo-ValueType-PropertyTree">
<nodes>
<node id="collection" xsi:type="Combodo-ValueType-Collection">
<xml-format xsi:type="Combodo-XMLFormat-FlatArray">
<count-tag>item_count</count-tag>
<tag-format>item_\$rank\$_\$id\$</tag-format>
</xml-format>
<prototype>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
</prototype>
</node>
</nodes>
</definition>
</property_type>
@@ -167,8 +186,11 @@ XML,
'sXMLContent' => <<<XML
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title_property>title</title_property>
<class_property>class</class_property>
<item_count>2</item_count>
<item_0_title_property>title_a</item_0_title_property>
<item_0_class_property>class_a</item_0_class_property>
<item_1_title_property>title_b</item_1_title_property>
<item_1_class_property>class_b</item_1_class_property>
</root>
XML,
],
@@ -243,6 +265,79 @@ XML,
<class_property>class_b</class_property>
</item>
</root>
XML,
],
'Property tree' => [
'normalizedValue' => [
'title_property' => 'title',
'sub_tree' => [
'sub_tree_property' => 'my_class',
],
'items' => [
'a' => [
'title_property' => 'title_a',
'class_property' => 'class_a',
],
'b' => [
'title_property' => 'title_b',
'class_property' => 'class_b',
],
],
],
'sPropertyTypeXML' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="property_tree_test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Combodo-PropertyType" xsi:noNamespaceSchemaLocation = "https://www.combodo.com/itop-schema/3.3">
<extends>Dashlet</extends>
<definition xsi:type="Combodo-ValueType-PropertyTree">
<nodes>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="sub_tree" xsi:type="Combodo-ValueType-PropertyTree">
<label>My sub tree</label>
<nodes>
<node id="sub_tree_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
</nodes>
</node>
<node id="items" xsi:type="Combodo-ValueType-Collection">
<xml-format xsi:type="Combodo-XMLFormat-CollectionWithId">
<tag-name>item</tag-name>
</xml-format>
<prototype>
<node id="title_property" xsi:type="Combodo-ValueType-Label">
<label>UI:BasicTest:Prop-Title</label>
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:BasicTest:Prop-Class</label>
<categories-csv>test</categories-csv>
</node>
</prototype>
</node>
</nodes>
</definition>
</property_type>
XML,
'sXMLContent' => <<<XML
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title_property>title</title_property>
<sub_tree>
<sub_tree_property>my_class</sub_tree_property>
</sub_tree>
<items>
<item id="a">
<title_property>title_a</title_property>
<class_property>class_a</class_property>
</item>
<item id="b">
<title_property>title_b</title_property>
<class_property>class_b</class_property>
</item>
</items>
</root>
XML,
],
];