From a3c911e93eaacfb48f0aa7ae3ad9463345d9bc2f Mon Sep 17 00:00:00 2001 From: Eric Espie Date: Wed, 29 Jan 2025 09:32:21 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B08139=20-=20:white=5Fcheck=5Fmark:=20Avoi?= =?UTF-8?q?d=20double=20writing=20in=20lifecycle=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/dbobject.class.php | 2 + .../DBObjectWithModifiedDataModelTest.php | 84 ++++++++++ .../core/DBObject/Delta/dbobjecttest.xml | 155 +++++++++++++++++- 3 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectWithModifiedDataModelTest.php diff --git a/core/dbobject.class.php b/core/dbobject.class.php index 869957265..f0030a860 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -4538,6 +4538,8 @@ abstract class DBObject implements iDisplay } else { $aBackupValues[$sAttCode] = $value; } + } else { + $aBackupValues[$sAttCode] = $oAttDef->GetNullValue(); } } diff --git a/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectWithModifiedDataModelTest.php b/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectWithModifiedDataModelTest.php new file mode 100644 index 000000000..cda459023 --- /dev/null +++ b/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectWithModifiedDataModelTest.php @@ -0,0 +1,84 @@ +Set('log_level_min', [LogChannels::DM_CRUD => 'Debug', LogChannels::EVENT_SERVICE => 'Trace']); + } + } + + protected function tearDown(): void + { + if (is_file(APPROOT.static::$sLogFile)) { + $sLog = file_get_contents(APPROOT.static::$sLogFile); + echo "--- error.log\n$sLog\n\n"; + @unlink(APPROOT.static::$sLogFile); + } + + parent::tearDown(); + } + + public function testStimulusStoppingActionPreventObjectModification() + { + // Given + $sObjectKey = $this->GivenObjectInDB('TestDBObject', ['name' => 'parent', 'status' => 'new']); + $oParent = MetaModel::GetObject('TestDBObject', $sObjectKey); + + // When actions ApplyStimulus then next action fails + $oParent->ApplyStimulus('ev_assign'); + $oParent->Reload(); + + // Then + // Check status... + $this->assertEquals('new', $oParent->Get('status'), 'The status should have remained unmodified due to action failure'); + } + + public function testCallingApplyStimulusWithinActionsWorks() + { + // Given + $sObjectKey = $this->GivenObjectInDB('TestDBObject', ['name' => 'parent', 'status' => 'assigned']); + $oParent = MetaModel::GetObject('TestDBObject', $sObjectKey); + + // When action ApplyStimulus + $oParent->ApplyStimulus('ev_reassign'); + $oParent->Reload(); + + // Then + // Check that status has changed to the final status + $this->assertEquals('resolved', $oParent->Get('status'), 'The status should have been modified to resolved (the final state after a nested stimulus)'); + } +} + diff --git a/tests/php-unit-tests/unitary-tests/core/DBObject/Delta/dbobjecttest.xml b/tests/php-unit-tests/unitary-tests/core/DBObject/Delta/dbobjecttest.xml index 20c31528e..2b2a9d5ca 100644 --- a/tests/php-unit-tests/unitary-tests/core/DBObject/Delta/dbobjecttest.xml +++ b/tests/php-unit-tests/unitary-tests/core/DBObject/Delta/dbobjecttest.xml @@ -1,5 +1,5 @@ - + cmdbAbstractObject @@ -12,7 +12,77 @@ + + status + + + + + 3 + HIGHLIGHT_CLASS_NONE + images/user-request-closed.svg + + + + + + + + + + + + + assigned + + + Assign + + + + Assign2 + + + + + + + + new + + + + resolved + + + Resolve + + + + + + assigned + + + Reassign + + + + + + + + + + closed + + assigned + + + + + name @@ -38,9 +108,90 @@ 0 true + + true + rank + + + new + 10 + + + assigned + 60 + + + resolved + 100 + + + status + new + false + - + + + + false + public + LifecycleAction + + + ApplyStimulus('ev_assign'); + return true; + }]]> + + + + false + public + LifecycleAction + + + + + + + false + public + LifecycleAction + + + ApplyStimulus('ev_resolve'); + return true; + }]]> + + + + false + public + LifecycleAction + + + ApplyStimulus('ev_resolve'); + return true; + }]]> + + \ No newline at end of file