diff --git a/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php b/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php index 36b4190417..07eb2851e0 100644 --- a/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php +++ b/datamodels/2.x/combodo-data-feature-removal/src/Service/DeletionPlanService.php @@ -102,7 +102,7 @@ class DeletionPlanService * @throws \CoreUnexpectedValue * @throws \MySQLException */ - public function ExecuteDeletionPlan(array $aClasses, int $iUnixTimeLimit = 0, int $iMaxExecutionCount=0): array + public function ExecuteDeletionPlan(array $aClasses, int $iUnixTimeLimit = 0, int $iMaxExecutionCount = -1): array { $oDeletionPlan = $this->GetDeletionPlan($aClasses); @@ -110,7 +110,7 @@ class DeletionPlanService throw new DataFeatureRemovalException("Deletion Plan cannot be executed due to issues"); } - $this->iExecutionCount=0; + $this->iExecutionCount = 0; $aSummary = []; foreach ($oDeletionPlan->ListUpdates() as $sClass => $aToUpdate) { @@ -199,13 +199,12 @@ class DeletionPlanService public function IsTimeLimitExceeded(int $iUnixTimeLimit, int $iMaxExecutionCount): bool { - throw new \Exception(""); - $this->iExecutionCount++; - echo json_encode([$this->iExecutionCount, $iMaxExecutionCount]); - if ($iMaxExecutionCount === $this->iExecutionCount){ - return false; + if (($iMaxExecutionCount !== -1) && ($iMaxExecutionCount <= $this->iExecutionCount)) { + return true; } + $this->iExecutionCount++; + if ($iUnixTimeLimit === 0) { return false; } diff --git a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php index 6f4c8a1a8b..e5b08ff1fc 100644 --- a/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php +++ b/tests/php-unit-tests/unitary-tests/datamodels/2.x/combodo-data-feature-removal/DeletionPlanServiceTest.php @@ -366,12 +366,10 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase EOF); $aClasses = [ 'DFRToRemoveLeaf' ]; - $aRes = DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses, 0, 2); + $aRes = DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses, 0, 3); $aExpected = [ - ['DFRToUpdate', 2, 0 ], + ['DFRToUpdate', 3, 0 ], ['DFRToRemoveLeaf', 0, 0 ], - ['DFRRemovedCollateral', 0, 0 ], - ['DFRRemovedCollateralCascade', 0, 0 ], ]; $this->AssertSummaryEquals($aExpected, $aRes); } @@ -393,16 +391,48 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase EOF); $aClasses = [ 'DFRToRemoveLeaf' ]; - $aRes = DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses, 0, 5); + $aRes = DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses, 0, 8); $aExpected = [ ['DFRToUpdate', 3, 0 ], - ['DFRToRemoveLeaf', 2, 0 ], - ['DFRRemovedCollateral', 0, 0 ], - ['DFRRemovedCollateralCascade', 0, 0 ], + ['DFRToRemoveLeaf', 0, 3 ], + ['DFRRemovedCollateral', 0, 2 ], ]; $this->AssertSummaryEquals($aExpected, $aRes); } + public function testExecuteDeletionPlan_WrongOrderDeletion() + { + $this->GivenDFRTreeInDB(<<Fetch(); + self::assertNotNull($oExpectedObj); + + $aRes = DeletionPlanService::GetInstance()->ExecuteDeletionPlan($aClasses, 0, 5); + $aExpected = [ + ['DFRToRemoveLeaf', 0, 3 ], + ['DFRRemovedCollateral', 0, 2 ], + ]; + + $this->AssertSummaryEquals($aExpected, $aRes); + + $oSet = new \DBObjectSet(\DBObjectSearch::FromOQL("SELECT DFRRemovedCollateral WHERE name='DFRRemovedCollateral_3'")); + $oActualObj = $oSet->Fetch(); + self::assertNotNull($oActualObj, "Deletion plan executed in wrong order: DFRRemovedCollateralCascade/DFRRemovedCollateral are not valid anymore"); + self::assertEquals($oExpectedObj->GetKey(), $oActualObj->GetKey()); + } + public function GetDatamodelDeltaAbsPath(): string { return __DIR__.'/deletionplan_delta.xml'; @@ -412,7 +442,7 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase { $aTree = explode("\n", $sTree); foreach ($aTree as $sLine) { - if (trim($sLine) === ""){ + if (trim($sLine) === "") { continue; } $this->GivenDFRTreeLineInDB($sLine); @@ -423,18 +453,18 @@ class DeletionPlanServiceTest extends ItopCustomDatamodelTestCase private function GivenDFRTreeLineInDB(string $sLine) { list($sLeft, $sRight) = explode('<-', $sLine); - $sLeft=trim($sLeft); + $sLeft = trim($sLeft); $iLeftId = $this->aIdByObjectName[$sLeft] ?? 0; - if ($iLeftId===0){ + if ($iLeftId === 0) { list($sChildClass, ) = explode('_', $sLeft, 2); $iLeftId = $this->GivenObjectInDB($sChildClass, ['name' => $sLeft]); - $this->aIdByObjectName[$sLeft]=$iLeftId; + $this->aIdByObjectName[$sLeft] = $iLeftId; } - $sRight=trim($sRight); + $sRight = trim($sRight); list($sChildClass, ) = explode('_', $sRight, 2); $iRightId = $this->GivenObjectInDB($sChildClass, ['name' => $sRight, 'extkey_id' => $iLeftId]); - $this->aIdByObjectName[$sRight]=$iRightId; + $this->aIdByObjectName[$sRight] = $iRightId; } }