N°5704 - Unit tests on XML assembly (#329)

* Add a complete test suite for XML assembly

* Add a complete test suite for XML assembly

* Dispatched the test of GetDelta into real unit tests

* Add test for GetDelta on a rename operation

* Add comments on a weird case and a case on rename

* Update XML version after rebase from develop to support/2.7

* Fix phpdoc about coverage

* Remove ModelFactory::GetRootDirs and ItopTestCase::RecurseRmDir+CreateTmpDir+RecurseMkDir+RecurseCopy, that were meant to be introduced in iTop 3.0 and have been copied here by mistake, when rebasing the branch from develop to 2.7.0

* Update test/ItopTestCase.php

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>

* Update test/setup/ModelFactoryTest.php

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>

* Update test/ItopTestCase.php

Co-authored-by: Molkobain <lajarige.guillaume@free.fr>

Co-authored-by: Pierre Goiffon <pierre.goiffon@combodo.com>
Co-authored-by: Molkobain <lajarige.guillaume@free.fr>
This commit is contained in:
Romain Quetiez
2022-11-08 19:43:07 +01:00
committed by GitHub
parent 0cf8d731bb
commit cd010afb48
3 changed files with 1305 additions and 156 deletions

View File

@@ -1658,162 +1658,6 @@ EOF
return $aResult;
}
public function TestAlteration()
{
$sDOMOriginal = 'undefined';
$sDOMModified = 'undefined';
$sDOMRebuilt = 'undefined';
$sDeltaXML = 'undefined';
try
{
$sHeader = '<?xml version="1.0" encoding="utf-8"?'.'>';
$sOriginalXML =
<<<EOF
$sHeader
<itop_design>
<a id="first a">
<b>Text</b>
<c id="1">
<d>D1</d>
<d>D2</d>
</c>
</a>
<a id="second a">
<parent>first a</parent>
</a>
<a id="third a">
<parent>first a</parent>
<x>blah</x>
</a>
</itop_design>
EOF;
$this->oDOMDocument = new MFDocument();
$this->oDOMDocument->loadXML($sOriginalXML);
// DOM Get the original values, then modify its contents by the mean of the API
$oRoot = $this->GetNodes('//itop_design')->item(0);
//$oRoot->Dump();
$sDOMOriginal = $oRoot->Dump(true);
$oNode = $oRoot->GetNodes('a/b')->item(0);
$oNew = $this->oDOMDocument->CreateElement('b', 'New text');
$oNode->parentNode->RedefineChildNode($oNew);
$oNode = $oRoot->GetNodes('a/c')->item(0);
$oNewC = $this->oDOMDocument->CreateElement('c');
$oNewC->setAttribute('id', '1');
$oNode->parentNode->RedefineChildNode($oNewC);
$oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'x'));
$oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'y'));
$oNewC->appendChild($this->oDOMDocument->CreateElement('d', 'z'));
$oNamedNode = $this->oDOMDocument->CreateElement('z');
$oNamedNode->setAttribute('id', 'abc');
$oNewC->AddChildNode($oNamedNode);
$oNewC->AddChildNode($this->oDOMDocument->CreateElement('r', 'to be replaced'));
// Alter this "modified node", no flag should be set in its subnodes
$oNewC->Rename('blah');
$oNewC->Rename('foo');
$oNewC->AddChildNode($this->oDOMDocument->CreateElement('y', '(no flag)'));
$oNewC->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programmatically'));
$oSubNode = $oNewC->GetUniqueElement('z');
$oSubNode->Rename('abcdef');
$oSubNode = $oNewC->GetUniqueElement('x');
$oSubNode->Delete();
$oNewC->RedefineChildNode($this->oDOMDocument->CreateElement('r', 'replacement'));
$oNode = $oRoot->GetNodes("//a[@id='second a']")->item(0);
$oNode->Rename('el 2o A');
$oNode->Rename('el secundo A');
$oNew = $this->oDOMDocument->CreateElement('e', 'Something new here');
$oNode->AddChildNode($oNew);
$oNewA = $this->oDOMDocument->CreateElement('a');
$oNewA->setAttribute('id', 'new a');
$oNode->AddChildNode($oNewA);
$oSubnode = $this->oDOMDocument->CreateElement('parent', 'el secundo A');
$oSubnode->setAttribute('id', 'to be changed');
$oNewA->AddChildNode($oSubnode);
$oNewA->AddChildNode($this->oDOMDocument->CreateElement('f', 'Welcome to the newcomer'));
$oNewA->AddChildNode($this->oDOMDocument->CreateElement('x', 'To delete programmatically'));
// Alter this "new a", as it is new, no flag should be set
$oNewA->Rename('new_a');
$oSubNode = $oNewA->GetUniqueElement('parent');
$oSubNode->Rename('alter ego');
$oNewA->RedefineChildNode($this->oDOMDocument->CreateElement('f', 'dummy data'));
$oSubNode = $oNewA->GetUniqueElement('x');
$oSubNode->Delete();
$oNode = $oRoot->GetNodes("//a[@id='third a']")->item(0);
$oNode->Delete();
//$oRoot->Dump();
$sDOMModified = $oRoot->Dump(true);
// Compute the delta
//
$sDeltaXML = $this->GetDelta();
//echo "<pre>\n";
//echo htmlentities($sDeltaXML);
//echo "</pre>\n";
// Reiterating - try to remake the DOM by applying the computed delta
//
$this->oDOMDocument = new MFDocument();
$this->oDOMDocument->loadXML($sOriginalXML);
$oRoot = $this->GetNodes('//itop_design')->item(0);
//$oRoot->Dump();
echo "<h4>Rebuild the DOM - Delta applied...</h4>\n";
$oDeltaDoc = new MFDocument();
$oDeltaDoc->loadXML($sDeltaXML);
//$oDeltaDoc->Dump();
//$this->oDOMDocument->Dump();
$oDeltaRoot = $oDeltaDoc->childNodes->item(0);
$this->LoadDelta($oDeltaRoot, $this->oDOMDocument);
//$oRoot->Dump();
$sDOMRebuilt = $oRoot->Dump(true);
}
catch (Exception $e)
{
echo "<h1>Exception: ".$e->getMessage()."</h1>\n";
echo "<pre>\n";
debug_print_backtrace();
echo "</pre>\n";
}
$sArrStyle = "font-size: 40;";
echo "<table>\n";
echo " <tr>\n";
echo " <td width=\"50%\">\n";
echo " <h4>DOM - Original values</h4>\n";
echo " <pre>".htmlentities($sDOMOriginal)."</pre>\n";
echo " </td>\n";
echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">&rArr; &rArr; &rArr;</span></td>\n";
echo " </tr>\n";
echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">&dArr;</div></td><td align=\"center\"><span style=\"$sArrStyle\"><span style=\"$sArrStyle\">&dArr;</div></div></td></tr>\n";
echo " <tr>\n";
echo " <td width=\"50%\">\n";
echo " <h4>DOM - Altered with various changes</h4>\n";
echo " <pre>".htmlentities($sDOMModified)."</pre>\n";
echo " </td>\n";
echo " <td width=\"50%\">\n";
echo " <h4>DOM - Rebuilt from the Delta</h4>\n";
echo " <pre>".htmlentities($sDOMRebuilt)."</pre>\n";
echo " </td>\n";
echo " </tr>\n";
echo " <tr><td align=\"center\"><span style=\"$sArrStyle\">&dArr;</div></td><td align=\"center\"><span style=\"$sArrStyle\">&uArr;</div></td></tr>\n";
echo " <td width=\"50%\">\n";
echo " <h4>Delta (Computed by ModelFactory)</h4>\n";
echo " <pre>".htmlentities($sDeltaXML)."</pre>\n";
echo " </td>\n";
echo " <td width=\"50%\" align=\"left\" valign=\"center\"><span style=\"$sArrStyle\">&rArr; &rArr; &rArr;</span></td>\n";
echo " </tr>\n";
echo "</table>\n";
} // TEST !
/**
* Extracts some nodes from the DOM
*

View File

@@ -136,4 +136,40 @@ class ItopTestCase extends TestCase
return $method->invokeArgs($oObject, $aArgs);
}
/**
* @param object $oObject
* @param string $sProperty
*
* @return mixed property
*
* @throws \ReflectionException
* @since 2.7.8 3.0.3 3.1.0
*/
public function GetNonPublicProperty(object $oObject, string $sProperty)
{
$class = new \ReflectionClass(get_class($oObject));
$property = $class->getProperty($sProperty);
$property->setAccessible(true);
return $property->getValue($oObject);
}
/**
* @param object $oObject
* @param string $sProperty
* @param $value
*
* @throws \ReflectionException
* @since 2.7.8 3.0.3 3.1.0
*/
public function SetNonPublicProperty(object $oObject, string $sProperty, $value)
{
$class = new \ReflectionClass(get_class($oObject));
$property = $class->getProperty($sProperty);
$property->setAccessible(true);
$property->setValue($oObject, $value);
}
}

File diff suppressed because it is too large Load Diff