From 8652a6459ed23fe7a77ffbede3e215805bfb2638 Mon Sep 17 00:00:00 2001 From: Simon Nowak Date: Tue, 19 Sep 2023 17:44:02 +0200 Subject: [PATCH] unit test to reproduce the issue --- .../DomainProcessInjectionTest.cls | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/sfdx-source/core/test/classes/framework-domain-process-injection/DomainProcessInjectionTest.cls b/sfdx-source/core/test/classes/framework-domain-process-injection/DomainProcessInjectionTest.cls index cb8cf42..fa9a22c 100644 --- a/sfdx-source/core/test/classes/framework-domain-process-injection/DomainProcessInjectionTest.cls +++ b/sfdx-source/core/test/classes/framework-domain-process-injection/DomainProcessInjectionTest.cls @@ -339,6 +339,54 @@ public class DomainProcessInjectionTest } } + + + @isTest + private static void whenTriggerFiredMultipleTimeThenClassToInjectNotRecursiveIfNeeded() { + List config = new List { + new DomainProcessBinding__mdt( + Label = 'PreventRecursiveInTriggerCriteria', + ClassToInject__c = 'DomainProcessInjectionTest.PreventRecursiveInTriggerCriteriaTest', + IsActive__c = true, + OrderOfExecution__c = 100853107.10, + ProcessContext__c = 'TriggerExecution', + RelatedDomainBindingSObject__c = 'Account', + TriggerOperation__c = 'Before_Update', + ExecuteAsynchronous__c = false, + Type__c = 'Criteria', + PreventRecursive__c = true + ), + + new DomainProcessBinding__mdt( + Label = 'PreventRecursiveInTriggerAction', + ClassToInject__c = 'DomainProcessInjectionTest.PreventRecursiveInTriggerActionTest', + IsActive__c = true, + OrderOfExecution__c = 100853107.10, + ProcessContext__c = 'TriggerExecution', + RelatedDomainBindingSObject__c = 'Account', + TriggerOperation__c = 'Before_Update', + ExecuteAsynchronous__c = false, + Type__c = 'Action', + PreventRecursive__c = true + ) + + }; + + + DomainProcessCoordinator.setMockDomainProcessBindings(config); + + List accList = new List { + new Account(Name='Test', Id = fflib_idgenerator.generate(Account.SObjectType) ) + }; + fflib_SObjectDomain.Test.Database.onUpdate(accList, new Map(accList)); + fflib_SObjectDomain.triggerHandler(DomainProcessInjectionTest.AccountsTestDomainConstructor.class); + fflib_SObjectDomain.triggerHandler(DomainProcessInjectionTest.AccountsTestDomainConstructor.class); + + List result = DomainProcessInjectionTest.lastConstructed.getRecords(); + System.Assert.areEqual('Test&', ((Account) result[0]).Name, 'Account Name should have been modified only once'); + + } + public with sharing class AccountsTestDomain extends ApplicationSObjectDomain { @@ -449,6 +497,39 @@ public class DomainProcessInjectionTest } } + public class PreventRecursiveInTriggerCriteriaTest implements IDomainProcessCriteria + { + private list records = new list(); + + public IDomainProcessCriteria setRecordsToEvaluate(List records) + { + this.records.clear(); + this.records.addAll( (list)records ); + + return this; + } + + public List run() + { + return this.records; + } + } + + public class PreventRecursiveInTriggerActionTest extends DomainProcessAbstractAction + { + public override void runInProcess() + { + Account accountRecord = null; + + for ( SObject record : this.records ) + { + accountRecord = (Account)record; + accountRecord.Name = accountRecord.Name+'&'; + DomainProcessInjectionTest.processedRecords.add(accountRecord); + } + } + } + public static final ApplicationSObjectUnitOfWork applicationSObjectUOW= new ApplicationSObjectUnitOfWork( new List{