From d0707429577b57f465b0f501909e7b7659825e80 Mon Sep 17 00:00:00 2001 From: Jon Davis Date: Fri, 24 Jul 2015 14:17:08 -0700 Subject: [PATCH] Issue #212 - Ensure Rollup Summary validations are enforced on updates Had two options here - refactor to a common validation method that is called by both flavors of onValidate or enable Configuration.OldOnUpdateValidateBehaviour. Decided to go with the former to avoid legacy based code. --- rolluptool/src/classes/RollupSummaries.cls | 22 ++++++++++-- .../src/classes/RollupSummariesTest.cls | 35 ++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/rolluptool/src/classes/RollupSummaries.cls b/rolluptool/src/classes/RollupSummaries.cls index 3f24de17..3ebd2882 100644 --- a/rolluptool/src/classes/RollupSummaries.cls +++ b/rolluptool/src/classes/RollupSummaries.cls @@ -85,8 +85,8 @@ public class RollupSummaries extends fflib_SObjectDomain /** * Validations for inserts and updates of records - **/ - public override void onValidate() + **/ + private void validateCommon() { // Cache Apex Describes and calculate child object tigger names Set rollupTriggerNames = new Set(); @@ -219,6 +219,24 @@ public class RollupSummaries extends fflib_SObjectDomain } } + /** + * Validations for inserts of records + **/ + public override void onValidate() + { + // invoke validation that should occur for insert & update + validateCommon(); + } + + /** + * Validations for updates of records + **/ + public override void onValidate(Map existingRecords) + { + // invoke validation that should occur for insert & update + validateCommon(); + } + private static final String MSG_INVALID_CRITERIA = 'Relationship Criteria \'\'{0}\'\' is not valid, see SOQL documentation http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_conditionexpression.htm, error is \'\'{1}\'\''; public class Constructor implements fflib_SObjectDomain.IConstructable diff --git a/rolluptool/src/classes/RollupSummariesTest.cls b/rolluptool/src/classes/RollupSummariesTest.cls index 3543130e..3f5bb6ca 100644 --- a/rolluptool/src/classes/RollupSummariesTest.cls +++ b/rolluptool/src/classes/RollupSummariesTest.cls @@ -88,7 +88,40 @@ private class RollupSummariesTest System.assertEquals(1, fflib_SObjectDomain.Errors.getAll().size()); System.assertEquals('Object does not exist.', fflib_SObjectDomain.Errors.getAll()[0].message); System.assertEquals(LookupRollupSummary__c.ChildObject__c, ((fflib_SObjectDomain.FieldError)fflib_SObjectDomain.Errors.getAll()[0]).field); - } + } + + /** + * Issue https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/212 + **/ + private testmethod static void testUpdateBadChildBigName() + { + // Test supported? + if(!TestContext.isSupported()) + return; + + String rsId = fflib_IDGenerator.generate(LookupRollupSummary__c.SObjectType); + + LookupRollupSummary__c oldRollupSummary = new LookupRollupSummary__c(Id=rsId); + oldRollupSummary.Name = 'Max Birthday for Contacts related to an Account'; + oldRollupSummary.ParentObject__c = 'Account'; + oldRollupSummary.ChildObject__c = 'Contact'; + oldRollupSummary.RelationShipField__c = 'AccountId'; + oldRollupSummary.RelationShipCriteria__c = null; + oldRollupSummary.FieldToAggregate__c = 'LastCURequestDate'; + oldRollupSummary.AggregateOperation__c = 'Count'; + oldRollupSummary.AggregateResultField__c = 'AnnualRevenue'; + oldRollupSummary.Active__c = false; + oldRollupSummary.CalculationMode__c = 'Realtime'; + + LookupRollupSummary__c newRollupSummary = oldRollupSummary.clone(true, true, true, true); + newRollupSummary.ChildObject__c = 'BadBadBadBadBadBadBadBadBadBad'; + fflib_SObjectDomain.Test.Database.onUpdate(new LookupRollupSummary__c[] { newRollupSummary }, new Map { oldRollupSummary.Id => oldRollupSummary } ); + + fflib_SObjectDomain.triggerHandler(RollupSummaries.class); + System.assertEquals(1, fflib_SObjectDomain.Errors.getAll().size()); + System.assertEquals('Object does not exist.', fflib_SObjectDomain.Errors.getAll()[0].message); + System.assertEquals(LookupRollupSummary__c.ChildObject__c, ((fflib_SObjectDomain.FieldError)fflib_SObjectDomain.Errors.getAll()[0]).field); + } /** * Issue https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/97