diff --git a/force-app/main/default/classes/HH_CampaignDedupeBTN_CTRL.cls b/force-app/main/default/classes/HH_CampaignDedupeBTN_CTRL.cls index eadce8cb670..2f8b4b5933b 100644 --- a/force-app/main/default/classes/HH_CampaignDedupeBTN_CTRL.cls +++ b/force-app/main/default/classes/HH_CampaignDedupeBTN_CTRL.cls @@ -57,6 +57,29 @@ public without sharing class HH_CampaignDedupeBTN_CTRL { //so we inform our action method to perform the update private boolean updateSettingsWithID = false; + @TestVisible + public Boolean hasAccess { + get { + if (hasAccess == null) { + hasAccess = getCurrentUserHasAccess(); + } + return hasAccess; + } + private set; + } + + @TestVisible + private UTIL_Permissions perms { + get { + if (perms == null) { + perms = new UTIL_Permissions(); + } + + return perms; + } + set; + } + /******************************************************************************************************* * @description Constructor * @param controller StandardController to a Campaign @@ -98,6 +121,10 @@ public without sharing class HH_CampaignDedupeBTN_CTRL { public PageReference RunReport(){ Savepoint sp = Database.setSavepoint(); try { + if (!hasAccess) { + throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + } + string ActiveID = campaign.id; ActiveID = ActiveID.substring(0,15); string newPageUrl = ''; @@ -193,8 +220,6 @@ public without sharing class HH_CampaignDedupeBTN_CTRL { ********************************************************************************************************/ public static integer MarkDuplicatesFromList(ID campaignId, list listCM) { - validateAccessPermissions(); - final String DUPE_STATUS_SUFFIX = System.Label.hhCmpDedupeStatus; //check for the Household Duplicate status values we need @@ -293,23 +318,23 @@ public without sharing class HH_CampaignDedupeBTN_CTRL { return returnsize; } - private static void validateAccessPermissions() { - if (!UTIL_Permissions.canUpdate('CampaignMember', 'Status', false)) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); - } - - Set cmsFields = new Set{ - 'CampaignId', - 'Label', - 'HasResponded', - 'SortOrder' - }; - for (String cmsField : cmsFields) { - if (!(UTIL_Permissions.canRead('CampaignMemberStatus', cmsField, false) && - UTIL_Permissions.canCreate('CampaignMemberStatus', cmsField, false))) { - throw new UTIL_Permissions.InsufficientPermissionException(System.Label.commonAccessErrorMessage); + private Boolean getCurrentUserHasAccess() { + Boolean accessOK = false; + + if (UTIL_Permissions.canUpdate('CampaignMember', 'Status', false)) { + Set cmsFields = new Set{ + CampaignMemberStatus.fields.CampaignId, + CampaignMemberStatus.fields.Label, + CampaignMemberStatus.fields.HasResponded, + CampaignMemberStatus.fields.SortOrder + }; + if ((perms.canRead(CampaignMemberStatus.getSObjectType(), cmsFields) && + perms.canCreate(CampaignMemberStatus.getSObjectType(), cmsFields))) { + accessOK = true; } } + + return accessOK; } /******************************************************************************************************* diff --git a/force-app/main/default/classes/HH_CampaignDedupeBTN_TEST.cls b/force-app/main/default/classes/HH_CampaignDedupeBTN_TEST.cls index ca87c4b7fd6..1c12e74f693 100644 --- a/force-app/main/default/classes/HH_CampaignDedupeBTN_TEST.cls +++ b/force-app/main/default/classes/HH_CampaignDedupeBTN_TEST.cls @@ -150,6 +150,7 @@ private class HH_CampaignDedupeBTN_TEST { Test.startTest(); ApexPages.StandardController sc = new ApexPages.StandardController(camp); HH_CampaignDedupeBTN_CTRL deduper = new HH_CampaignDedupeBTN_CTRL(sc); + deduper.hasAccess = true; deduper.RunReport(); Test.stopTest(); diff --git a/force-app/main/default/classes/PotentialDuplicates_TEST.cls b/force-app/main/default/classes/PotentialDuplicates_TEST.cls index 27987c7076a..6f81d20c876 100644 --- a/force-app/main/default/classes/PotentialDuplicates_TEST.cls +++ b/force-app/main/default/classes/PotentialDuplicates_TEST.cls @@ -5,30 +5,53 @@ private with sharing class PotentialDuplicates_TEST { private static void shouldReturnNullWhenNoDuplicatesAreFound() { Id recordId = UTIL_UnitTestData_TEST.mockId(Contact.getSObjectType()); Map data = PotentialDuplicates.getDuplicates(recordId); - System.assertEquals('', data.get('setOfMatches'), - 'There should be no duplicates'); + String setOfMatches = (String) data.get('setOfMatches'); + + List activeContactRules = [ + SELECT Id + from DuplicateRule + WHERE SObjectType = 'Contact' + AND isActive = TRUE + ]; + + if (activeContactRules.isEmpty()) { + System.assertEquals(null, setOfMatches, + 'PotentialDuplicates.getDuplicates() should return null if there are no active Duplicate Rules for Contact'); + } else { + System.assertEquals('', setOfMatches, 'There should be no duplicates'); + } } @IsTest private static void shouldReturnIdsWhenDuplicatesAreFound() { List contactList = UTIL_UnitTestData_TEST.getContacts(3); - for(Contact c : contactList) { + for (Contact c : contactList) { c.FirstName = 'Test'; c.LastName = 'LastName'; c.Email = 'tester@example.com'; } insert contactList; + List activeContactRules = [ + SELECT Id + from DuplicateRule + WHERE SObjectType = 'Contact' + AND isActive = TRUE + ]; + Map data = PotentialDuplicates.getDuplicates(contactList[0].Id); - String setOfMatches = (String)data.get('setOfMatches'); - Integer numberOfMatches = setOfMatches.split(',').size(); + String setOfMatches = (String) data.get('setOfMatches'); - // Duplicates will not be found if encryption is enabled / standard rules deactivated - if (sObjectType.Contact.fields.Name.isEncrypted()) { + if (activeContactRules.isEmpty()) { + System.assertEquals(null, setOfMatches, + 'PotentialDuplicates.getDuplicates() should return null if there are no active Duplicate Rules for Contact'); + } else if (sObjectType.Contact.fields.Name.isEncrypted()) { + // Duplicates will not be found if encryption is enabled / standard rules deactivated System.assertEquals('', setOfMatches, 'No duplicate Ids should be returned if encryption is enabled'); - } - else { + } else { + Integer numberOfMatches = setOfMatches.split(',').size(); System.assertNotEquals('', setOfMatches, 'Duplicate Ids should be returned'); - System.assertEquals(2, numberOfMatches, 'There should be 2 duplicates returned'); } + System.assertEquals(2, numberOfMatches, 'There should be 2 duplicates returned'); + } } } \ No newline at end of file