N°9066 - Serialization/Unserialization from XML to Forms

This commit is contained in:
Eric Espie
2026-01-07 14:29:08 +01:00
parent efb1bd765b
commit 154fb5c737
23 changed files with 553 additions and 112 deletions

View File

@@ -9,6 +9,7 @@ namespace Combodo\iTop\Test\UnitTest;
use CMDBSource;
use DeprecatedCallsLog;
use DOMDocument;
use MySQLTransactionNotClosedException;
use ParseError;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@@ -717,4 +718,34 @@ abstract class ItopTestCase extends KernelTestCase
return $this->CallUrl($sUrl, $aPostFields, $aCurlOptions, $bXDebugEnabled);
}
/**
* @param $sXML
*
* @return false|string
*/
protected function CanonicalizeXML($sXML)
{
// Canonicalize the expected XML (to cope with indentation)
$oExpectedDocument = new DOMDocument();
$oExpectedDocument->preserveWhiteSpace = false;
$oExpectedDocument->formatOutput = true;
$oExpectedDocument->loadXML($sXML);
$sSavedXML = $oExpectedDocument->SaveXML();
return str_replace(' encoding="UTF-8"', '', $sSavedXML);
}
/**
* @param $sExpected
* @param $sActual
* @param string $sMessage
*/
protected function AssertEqualiTopXML($sExpected, $sActual, string $sMessage = '')
{
// Note: assertEquals reports the differences in a diff which is easier to interpret (in PHPStorm)
// as compared to the report given by assertEqualXMLStructure
static::assertEquals($this->CanonicalizeXML($sExpected), $this->CanonicalizeXML($sActual), $sMessage);
}
}

View File

@@ -64,36 +64,6 @@ class ModelFactoryTest extends ItopTestCase
return $oFactory;
}
/**
* @param $sXML
*
* @return false|string
*/
protected function CanonicalizeXML($sXML)
{
// Canonicalize the expected XML (to cope with indentation)
$oExpectedDocument = new DOMDocument();
$oExpectedDocument->preserveWhiteSpace = false;
$oExpectedDocument->formatOutput = true;
$oExpectedDocument->loadXML($sXML);
$sSavedXML = $oExpectedDocument->SaveXML();
return str_replace(' encoding="UTF-8"', '', $sSavedXML);
}
/**
* @param $sExpected
* @param $sActual
* @param string $sMessage
*/
protected function AssertEqualiTopXML($sExpected, $sActual, string $sMessage = '')
{
// Note: assertEquals reports the differences in a diff which is easier to interpret (in PHPStorm)
// as compared to the report given by assertEqualXMLStructure
static::assertEquals($this->CanonicalizeXML($sExpected), $this->CanonicalizeXML($sActual), $sMessage);
}
/**
* Assertion ignoring some of the unexpected decoration brought by DOM Elements.
*/

View File

@@ -136,7 +136,7 @@ PHP,
</node>
<node id="class_property" xsi:type="Combodo-ValueType-Class">
<label>UI:Class</label>
<categories-csv>test</categories-csv>
<categories-csv>test</categories-csv>
</node>
<node id="class_attribute_property" xsi:type="Combodo-ValueType-ClassAttribute">
<label>UI:ClassAttribute</label>
@@ -244,6 +244,10 @@ PHP,
<nodes>
<node id="sub_tree_collection" xsi:type="Combodo-ValueType-Collection">
<label>UI:SubTree</label>
<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="string_property" xsi:type="Combodo-ValueType-String">
<label>UI:String</label>
@@ -580,6 +584,7 @@ PHP,
<nodes>
<node id="coll" xsi:type="Combodo-ValueType-CollectionOfValues">
<label>UI:ClassAttributeValue</label>
<xml-format xsi:type="Combodo-XMLFormat-CSV"/>
<value-type xsi:type="Combodo-ValueType-ClassAttributeValue">
<class>Contact</class>
<attribute>status</attribute>
@@ -600,6 +605,22 @@ class FormFor__CollectionOfValuesTest extends Combodo\iTop\Forms\Block\Base\Form
]);
}
}
PHP,
],
'Single value' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_typeproperty_type id="SingleValueTest" 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">
<label>Single Test</label>
</definition>
</property_typeproperty_type>
XML,
'sExpectedPHP' => <<<PHP
class FormFor__SingleValueTest extends Combodo\iTop\Forms\Block\DataModel\LabelFormBlock
{
}
PHP,
],
'test' => [
@@ -731,7 +752,7 @@ XML,
'Missing value-type in node specification' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="RelevanceCondition" 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="WrongDefinition" 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>
@@ -743,13 +764,12 @@ XML,
</property_type>
XML,
'sExpectedClass' => 'Combodo\iTop\PropertyType\PropertyTypeException',
'sExpectedMessage' => 'Node: source_property, missing value-type in node specification',
'sExpectedMessage' => '/property_type[WrongDefinition]/definition/nodes/node[source_property]: Missing value-type in node specification',
],
'Wrong class for value-type in node specification' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="RelevanceCondition" 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="WrongDefinition" 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>
@@ -761,7 +781,71 @@ XML,
</property_type>
XML,
'sExpectedClass' => 'Combodo\iTop\PropertyType\PropertyTypeException',
'sExpectedMessage' => 'Node: source_property, unknown type node class: "Test-Combodo"',
'sExpectedMessage' => '/property_type[WrongDefinition]/definition/nodes/node[source_property]: Unknown value-type node class: "Test-Combodo"',
],
'Wrong class for xml-format in node specification' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="WrongDefinition" 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="coll" xsi:type="Combodo-ValueType-CollectionOfValues">
<label>UI:ClassAttributeValue</label>
<xml-format xsi:type="Combodo-test"/>
<value-type xsi:type="Combodo-ValueType-ClassAttributeValue">
<class>Contact</class>
<attribute>status</attribute>
</value-type>
</node>
</nodes>
</definition>
</property_type>
XML,
'sExpectedClass' => 'Combodo\iTop\PropertyType\Serializer\SerializerException',
'sExpectedMessage' => '/property_type[WrongDefinition]/definition/nodes/node[coll]/xml-format: Unknown type node class: "Combodo-test"',
],
'Missing class for xml-format in node specification' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="WrongDefinition" 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="coll" xsi:type="Combodo-ValueType-CollectionOfValues">
<label>UI:ClassAttributeValue</label>
<xml-format/>
<value-type xsi:type="Combodo-ValueType-ClassAttributeValue">
<class>Contact</class>
<attribute>status</attribute>
</value-type>
</node>
</nodes>
</definition>
</property_type>
XML,
'sExpectedClass' => 'Combodo\iTop\PropertyType\Serializer\SerializerException',
'sExpectedMessage' => '/property_type[WrongDefinition]/definition/nodes/node[coll]/xml-format: Missing xsi:type in node specification',
],
'Missing tag in xml-format ' => [
'sXMLContent' => <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<property_type id="WrongDefinition" 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 xsi:type="Combodo-ValueType-CollectionOfValues">
<xml-format xsi:type="Combodo-XMLFormat-ValueAsId">
</xml-format>
<value-type xsi:type="Combodo-ValueType-Class">
</value-type>
</node>
</nodes>
</definition>
</property_type>
XML,
'sExpectedClass' => 'Combodo\iTop\PropertyType\Serializer\SerializerException',
'sExpectedMessage' => '/property_type[WrongDefinition]/definition/nodes/node/xml-format: Missing <tag-name> element',
],
];
}