From 63c0a1871cc05a9fc5bc594cf8ceebb43edc7ef2 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Wed, 23 Mar 2022 12:01:09 +0000 Subject: [PATCH] Added ability to find if a DmlDefiner will delete other records --- .../classes/services/dml/DmlChildContext.cls | 6 +++++ .../classes/services/dml/DmlDefiner.cls | 10 +++++++ .../services/dml/DmlDefinerOptions.cls | 10 +++++++ .../classes/services/dml/DmlRecord.cls | 6 +++++ .../dml/tests/DmlDefinerOptionsTest.cls | 26 +++++++++++++++++++ .../services/dml/tests/DmlDefinerTest.cls | 20 ++++++++++++++ 6 files changed, 78 insertions(+) diff --git a/framework/default/ortoo-core/default/classes/services/dml/DmlChildContext.cls b/framework/default/ortoo-core/default/classes/services/dml/DmlChildContext.cls index f5a6be8ca14..75aa6567021 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/DmlChildContext.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/DmlChildContext.cls @@ -105,4 +105,10 @@ public inherited sharing class DmlChildContext uow.registerRelationship( child, relatedByField, parent ); } } + + @testVisible + private SobjectField getRelatedByField() + { + return relatedByField; + } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/services/dml/DmlDefiner.cls b/framework/default/ortoo-core/default/classes/services/dml/DmlDefiner.cls index 4edabfe2c85..e04639f6209 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/DmlDefiner.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/DmlDefiner.cls @@ -188,6 +188,16 @@ public inherited sharing class DmlDefiner return sobjects; } + /** + * States if this definer is configured to ensure that other records are deleted + * + * @return Boolean Will other records be deleted + */ + public Boolean getWillDeleteOtherRecords() { + Contract.assert( options != null, 'getWillDeleteOtherRecords was called when options was null' ); + return options.getWillDeleteOtherRecords(); + } + @testVisible private List getDmlRecords() { return recordsToDml; diff --git a/framework/default/ortoo-core/default/classes/services/dml/DmlDefinerOptions.cls b/framework/default/ortoo-core/default/classes/services/dml/DmlDefinerOptions.cls index 58bb3709b41..8ebd3c091cf 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/DmlDefinerOptions.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/DmlDefinerOptions.cls @@ -43,4 +43,14 @@ public inherited sharing class DmlDefinerOptions return new DmlDefinerOptions() .setOtherRecordsMode( OtherRecordsOption.IGNORE_RECORDS ); } + + /** + * States if this definer will instruct to delete other records + * + * @return Boolean Should other records be deleted? + */ + public Boolean getWillDeleteOtherRecords() + { + return getOtherRecordsMode() == OtherRecordsOption.DELETE_RECORDS; + } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/services/dml/DmlRecord.cls b/framework/default/ortoo-core/default/classes/services/dml/DmlRecord.cls index 51055ae1b36..d331046d0d4 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/DmlRecord.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/DmlRecord.cls @@ -283,4 +283,10 @@ public virtual inherited sharing class DmlRecord checkTypeIsValid( childRecordType ); return childContextsByType.get( childRecordType ); } + + @testVisible + private Boolean getWillDeleteOtherRecords( String childRecordType ) + { + return getChildDefiner( childRecordType )?.getWillDeleteOtherRecords(); + } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerOptionsTest.cls b/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerOptionsTest.cls index 9bb441ad244..51cb51010ba 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerOptionsTest.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerOptionsTest.cls @@ -39,4 +39,30 @@ private without sharing class DmlDefinerOptionsTest ortoo_Asserts.assertContains( 'setOtherRecordsMode called with a null otherRecordsMode', exceptionMessage, 'setOtherRecordsMode, when called with null, will throw an exception' ); } + + @isTest + private static void getWillDeleteOtherRecords_whenRecordModeIsDelete_returnsTrue() // NOPMD: Test method name format + { + DmlDefinerOptions options = new DmlDefinerOptions(); + + Test.startTest(); + options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.DELETE_RECORDS ); + Boolean got = options.getWillDeleteOtherRecords(); + Test.stopTest(); + + System.assertEquals( true, got, 'getWillDeleteOtherRecords, when the record mode is DELETE_RECORDS, will return true' ); + } + + @isTest + private static void getWillDeleteOtherRecords_whenRecordModeIsIgnore_returnsFalse() // NOPMD: Test method name format + { + DmlDefinerOptions options = new DmlDefinerOptions(); + + Test.startTest(); + options.setOtherRecordsMode( DmlDefinerOptions.OtherRecordsOption.IGNORE_RECORDS ); + Boolean got = options.getWillDeleteOtherRecords(); + Test.stopTest(); + + System.assertEquals( false, got, 'getWillDeleteOtherRecords, when the record mode is IGNORE_RECORDS, will return false' ); + } } \ No newline at end of file diff --git a/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerTest.cls b/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerTest.cls index 8a38bcfa890..69ee840dab9 100644 --- a/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerTest.cls +++ b/framework/default/ortoo-core/default/classes/services/dml/tests/DmlDefinerTest.cls @@ -311,4 +311,24 @@ private without sharing class DmlDefinerTest ortoo_Asserts.assertContains( 'addPreSaveAction called with a null action', exceptionMessage, 'addPreSaveAction, when given null, will throw an exception' ); } + + @isTest + private static void getWillDeleteOtherRecords_willReturnTheValueFromTheDefiner() // NOPMD: Test method name format + { + DmlDefiner dmlDefiner = new DmlDefiner(); + + Amoss_Instance optionsController = new Amoss_Instance( DmlDefinerOptions.class ); + optionsController + .expects( 'getWillDeleteOtherRecords' ) + .returning( true ); + + Test.startTest(); + dmlDefiner.setOptions( (DmlDefinerOptions)optionsController.generateDouble() ); + Boolean got = dmlDefiner.getWillDeleteOtherRecords(); + Test.stopTest(); + + optionsController.verify(); + + System.assertEquals( true, got, 'getWillDeleteOtherRecords, will call getWillDeleteOtherRecords on the options and return the result' ); + } } \ No newline at end of file