+ Start a Discussion
Christi Kane gobucksChristi Kane gobucks 

how to write a test class for this? I'm very new to this

public class wv6_ContactTriggerHandler {

    public static FINAL String STR_PreventDeleteWithoutMerge_ERROR = Label.PreventDeleteWithoutMerge;
    
    public static FINAL Boolean hasPermission = FeatureManagement.checkPermission('Allow_Contact_Deletes');

    public static void checkDeletePermissions(List<Contact> contactList, Map<Id,Contact> oldContactMap){
        
        if(hasPermission) // user is authorized to delete without using Merge
            return;
        
        for(Contact c : oldContactMap.values()){
            if(!Test.isRunningTest() && c.MasterRecordId == null){
                c.addError(STR_PreventDeleteWithoutMerge_ERROR);                  
            }
        }
        
    }
}
Best Answer chosen by Christi Kane gobucks
Martha VMartha V
There is a trailhead module that explains the pattern for tests with permissions https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/permission-based-tests

Assuming this is part of a Contact trigger on Delete you could do something like this
@isTest
public class wv6_ContactTriggerHandlerTest {

	@TestSetup
	private static void makeData() {
		Contact contact = new Contact(FirstName = 'contactTest', LastName = 'contactTest');
		insert contact;
	}

	@isTest
	private static void testDeleteContactWithPerm() {
		//get the contact inserted in makeData()
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'contactTest' LIMIT 1];

		//create a user or get the id of a user with the custom permission 'Allow_Contact_Deletes'		

		//run the test as the user with permission
		runAs(userWithPermission) {
			Test.startTest;
			try {
				delete contact;
			} catch (Exception e) {
				system.debug(e.getMessage());
			}
			Test.stopTest;
			Integer numberOfContacts = [SELECT Count() FROM Contact WHERE LastName = 'contactTest'];
			System.assertEquals(0, numberOfContacts, 'the contact should have been deleted');
		}
	}

	@isTest
	private static void testDeleteContactWithPerm() {
		//get the contact inserted in makeData()
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'contactTest' LIMIT 1];

		//create a user or get the id of a user with the custom permission 'Allow_Contact_Deletes'		

		Boolean notDeleted = false;
		//run the test as the user with permission
		runAs(userWithPermission) {
			Test.startTest;
			try {
				delete contact;
			} catch (Exception e) {
				notDeleted = true;
			}
			Test.stopTest;
		}
		System.assert(notDeleted, 'the deletion should throw an error');
	}
}

 

All Answers

Martha VMartha V
There is a trailhead module that explains the pattern for tests with permissions https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/permission-based-tests

Assuming this is part of a Contact trigger on Delete you could do something like this
@isTest
public class wv6_ContactTriggerHandlerTest {

	@TestSetup
	private static void makeData() {
		Contact contact = new Contact(FirstName = 'contactTest', LastName = 'contactTest');
		insert contact;
	}

	@isTest
	private static void testDeleteContactWithPerm() {
		//get the contact inserted in makeData()
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'contactTest' LIMIT 1];

		//create a user or get the id of a user with the custom permission 'Allow_Contact_Deletes'		

		//run the test as the user with permission
		runAs(userWithPermission) {
			Test.startTest;
			try {
				delete contact;
			} catch (Exception e) {
				system.debug(e.getMessage());
			}
			Test.stopTest;
			Integer numberOfContacts = [SELECT Count() FROM Contact WHERE LastName = 'contactTest'];
			System.assertEquals(0, numberOfContacts, 'the contact should have been deleted');
		}
	}

	@isTest
	private static void testDeleteContactWithPerm() {
		//get the contact inserted in makeData()
		Contact contact = [SELECT Id FROM Contact WHERE LastName = 'contactTest' LIMIT 1];

		//create a user or get the id of a user with the custom permission 'Allow_Contact_Deletes'		

		Boolean notDeleted = false;
		//run the test as the user with permission
		runAs(userWithPermission) {
			Test.startTest;
			try {
				delete contact;
			} catch (Exception e) {
				notDeleted = true;
			}
			Test.stopTest;
		}
		System.assert(notDeleted, 'the deletion should throw an error');
	}
}

 
This was selected as the best answer
Christi Kane gobucksChristi Kane gobucks
Thanks Martha! I have taken that Trailhead recently (this weekend) and am in the level 1 RAD women code course. I was just introduced to test classes 2 weeks ago.

I appreciate the template! Part of my issue was that I needed to update the contact records to be owned by the user running as, so they could delete (or not) and get the error I expected. Once I did that, it worked! :) #learningdaily