diff --git a/application/cmdbabstract.class.inc.php b/application/cmdbabstract.class.inc.php index 7c08e2500..1bc54d72e 100644 --- a/application/cmdbabstract.class.inc.php +++ b/application/cmdbabstract.class.inc.php @@ -5954,6 +5954,16 @@ JS $this->FireEvent(EVENT_DB_CHECK_TO_DELETE, ['deletion_plan' => $oDeletionPlan]); } + /** + * @return void + * @throws \CoreException + * @since 3.1.2 + */ + final protected function FireEventAboutToDelete(): void + { + $this->FireEvent(EVENT_DB_ABOUT_TO_DELETE); + } + /** * @return void * @throws \CoreException diff --git a/application/datamodel.application.xml b/application/datamodel.application.xml index cc0102b47..54400e526 100644 --- a/application/datamodel.application.xml +++ b/application/datamodel.application.xml @@ -270,6 +270,23 @@ + + An object is about to be deleted from the database + + cmdbAbstractObject + + cmdbAbstractObject::OnDelete + + + The object about to be deleted + DBObject + + + Debug string + string + + + An object has been deleted into the database diff --git a/core/dbobject.class.php b/core/dbobject.class.php index d5db209fc..65d2f5d25 100644 --- a/core/dbobject.class.php +++ b/core/dbobject.class.php @@ -4108,6 +4108,8 @@ abstract class DBObject implements iDisplay return; } + $this->SetReadOnly("No modification allowed before delete"); + $this->FireEventAboutToDelete(); $oKPI = new ExecutionKPI(); $this->OnDelete(); $oKPI->ComputeStatsForExtension($this, 'OnDelete'); @@ -6627,6 +6629,14 @@ abstract class DBObject implements iDisplay { } + /** + * @return void + * @since 3.1.2 + */ + protected function FireEventAboutToDelete(): void + { + } + /** * @return void * @since 3.1.0 diff --git a/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php b/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php index 024cfcc1b..67ba6e8f1 100644 --- a/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php +++ b/tests/php-unit-tests/unitary-tests/core/CRUDEventTest.php @@ -24,6 +24,7 @@ use Server; use Team; use UserRequest; use utils; +use const EVENT_DB_ABOUT_TO_DELETE; use const EVENT_DB_AFTER_DELETE; use const EVENT_DB_AFTER_WRITE; use const EVENT_DB_BEFORE_WRITE; @@ -422,9 +423,10 @@ class CRUDEventTest extends ItopDataTestCase // 1 delete for UserRequest, 3 delete for lnkFunctionalCIToTicket $this->assertEquals(4, self::$aEventCalls[EVENT_DB_CHECK_TO_DELETE]); + $this->assertEquals(4, self::$aEventCalls[EVENT_DB_ABOUT_TO_DELETE]); $this->assertEquals(4, self::$aEventCalls[EVENT_DB_AFTER_DELETE]); - $this->assertArrayNotHasKey(EVENT_DB_LINKS_CHANGED, self::$aEventCalls, 'no relation with the with_php_compute attribute !'); - $this->assertEquals(8, self::$iEventCalls); + $this->assertArrayNotHasKey(EVENT_DB_LINKS_CHANGED, self::$aEventCalls, 'Event not to be sent on delete'); + $this->assertEquals(12, self::$iEventCalls); } @@ -725,6 +727,7 @@ class CRUDEventReceiver extends ClassesWithDebug $this->oTestCase->EventService_RegisterListener(EVENT_DB_CHECK_TO_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_BEFORE_WRITE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_AFTER_WRITE, [$this, 'OnEvent']); + $this->oTestCase->EventService_RegisterListener(EVENT_DB_ABOUT_TO_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_AFTER_DELETE, [$this, 'OnEvent']); $this->oTestCase->EventService_RegisterListener(EVENT_DB_LINKS_CHANGED, [$this, 'OnEvent']);