I attempted to break up the class based on the different scenarios that might come up, but now I'm only getting 1 case and 1 email message as it's not taking the queried user Id's.
@isTest
public class TestMasterCaseTrigger {
public static final String HELPEMAIL = '[email protected]';
public static User SystemUser = [SELECT Id, Name FROM User WHERE Name = 'System' LIMIT 1];
public static User nonSystemUser = [SELECT Id, Name FROM User WHERE Name = 'Joe Schmoe' LIMIT 1];
public static User nonSystemUser2 = [SELECT Id, Name FROM User WHERE Name = 'Jack Schmoe' LIMIT 1];
static testMethod void testEmailOnClosedCaseSystemUser() {
createContact('testFirstName', 'testLastName', HELPEMAIL);
createCase('System User Test', 'New', SystemUser.Id);
Case systemCase = [SELECT Id, Status, OwnerId FROM Case WHERE OwnerId=:SystemUser.Id LIMIT 1];
createEmailMessage('[email protected]', systemCase.Id, 'System User', 'System User Body', HELPEMAIL);
System.debug('Status of systemCase before update: ' + systemCase.Status);
System.debug('systemCase: '+ systemCase.Id);
systemCase.Status = 'Closed';
update systemCase;
Case systemCaseAfterUpdate = [SELECT Id, Status, OwnerId FROM Case WHERE OwnerId=:SystemUser.Id LIMIT 1];
System.debug('Emails sent on SystemCase Closed: '+ Limits.getEmailInvocations());
System.debug('Status of systemCase after update: ' + systemCaseAfterUpdate.Status);
System.assert( Limits.getEmailInvocations()==1,'System should not send emails');
}
static testMethod void testEmailOnClosedCaseNonSystemUser(){
createContact('testFirstName', 'testLastName', HELPEMAIL);
createCase('Non System User test', 'New', nonSystemUser2.Id);
Case nonSystemCase = [SELECT Id, Status, OwnerId FROM Case WHERE OwnerId=:nonSystemUser2.Id AND Status != 'Closed' LIMIT 1];
createEmailMessage('[email protected]', nonSystemCase.Id, 'nonSystem Subject', 'nonSystem Body', HELPEMAIL);
System.debug('Status of nonSystemCase before update: ' + nonSystemCase.Status);
System.debug('nonSystemCase: '+ nonSystemCase.Id);
nonSystemCase.Status = 'Closed';
update nonSystemCase;
Case nonSystemCaseAfterUpdate = [SELECT Id, Status, OwnerId FROM Case WHERE OwnerId=:nonSystemUser2.Id AND Status = 'Closed' LIMIT 1];
System.debug('Emails sent on nonSystemCase Closed: '+ Limits.getEmailInvocations());
System.debug('Status of nonSystemCase after update: ' + nonSystemCaseAfterUpdate.Status);
System.assert( Limits.getEmailInvocations()==2,'Non System should send 2 emails');
}
static testMethod void testEmailOnClosedCaseAlreadyClosed(){
createContact('testFirstName', 'testLastName', HELPEMAIL);
createCase('Case Closed Already Test', 'Closed', nonSystemUser.Id);
Case alreadyClosedCase = [SELECT Id, Status, OwnerId FROM Case WHERE OwnerId=:nonSystemUser.Id AND Status = 'Closed' LIMIT 1];
createEmailMessage('[email protected]', alreadyClosedCase.Id, 'Already Closed Test', 'Already Closed Body', HELPEMAIL);
System.debug('Status of alreadyClosedCase before update: ' + alreadyClosedCase.Status);
System.debug('alreadyClosedCase: '+ alreadyClosedCase.Id);
alreadyClosedCase.Status = 'Closed';
update alreadyClosedCase;
System.debug('Emails sent on already closed case: '+ Limits.getEmailInvocations());
System.debug('Status of alreadyClosedCase after update: ' + alreadyClosedCase.Status);
System.assert( Limits.getEmailInvocations()==0,'Already closed Cases should not send emails');
}
public static void createCase (String subject, String status, Id owner) {
Case caseCreate = new Case(
Subject = subject,
Status = status,
OwnerId = owner);
insert caseCreate;
}
public static void createContact(String fName, String lName, String emailAddress){
Contact contactCreate = new Contact(
FirstName = fName,
LastName = lName,
email = emailAddress);
insert contactCreate;
}
public static void createEmailMessage(String fromEmailAddress, Id parent, String isSubject, String body, String toAddress){
EmailMessage caseEmailMessage = new EmailMessage(
FromAddress = fromEmailAddress,
ParentId = parent,
Subject = isSubject,
TextBody = body,
ToAddress = toAddress);
insert caseEmailMessage;
}
}
Any help would be greatly appreciated.