Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert task manually instead of using the flag on email #452

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/classes/VOL_CTRL_PersonalSiteContactLookup.cls
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/

global with sharing class VOL_CTRL_PersonalSiteContactLookup {
@TestVisible
private static VOL_Access access = VOL_Access.getInstance();

// constructor
global VOL_CTRL_PersonalSiteContactLookup() {
Expand Down Expand Up @@ -117,7 +119,7 @@ global with sharing class VOL_CTRL_PersonalSiteContactLookup {
strResult = null;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id);
mail.setSaveAsActivity(true);
mail.setSaveAsActivity(false);
mail.setTemplateID(emailTemplateId);
if (orgWideEmailId != null) {
mail.setOrgWideEmailAddressId(orgWideEmailId);
Expand All @@ -126,6 +128,9 @@ global with sharing class VOL_CTRL_PersonalSiteContactLookup {
listSER = Messaging.sendEmail(new Messaging.Email[] { mail }, false);
if (listSER[0].isSuccess()) {
strResult = System.Label.labelContactLookupSuccess;
Task taskRecord = toTask(mail);
access.insertRecords(new List<SObject>{ taskRecord });

} else {
list<Messaging.SendEmailError> listSEE = listSER[0].getErrors();
for (Messaging.SendEmailError see : listSEE) {
Expand All @@ -152,6 +157,31 @@ global with sharing class VOL_CTRL_PersonalSiteContactLookup {
} catch (Exception e) {
ApexPages.addMessages(e);
}
}
}

@TestVisible
private static Task toTask(Messaging.SingleEmailMessage email) {
final String subType = 'Email';

Task taskRecord = new Task(
WhoId = email.getTargetObjectId(),
Subject = subType + ': ' + email.getSubject(),
ActivityDate = System.today(),
Status = getClosedTaskStatus(),
Description = email.getPlainTextBody(),
TaskSubtype = subType
);

return taskRecord;
}

@TestVisible
private static String getClosedTaskStatus() {
List<TaskStatus> closedTaskStatuses = [SELECT ApiName FROM TaskStatus WHERE IsClosed = true];

return closedTaskStatuses.isEmpty() ?
null :
closedTaskStatuses[0].ApiName;
}

}
47 changes: 46 additions & 1 deletion src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,51 @@
private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST {

//==================== TEST METHOD(s) ======================================

@IsTest
private static void shouldInsertTaskRecordsWhenEmailSent() {
PageReference personalSiteLookupPage = new PageReference('Page.PersonalSiteContactLookup');
Test.setCurrentPageReference(personalSiteLookupPage);
VOL_CTRL_PersonalSiteContactLookup personalSiteLookupCtrl = new VOL_CTRL_PersonalSiteContactLookup();

VOL_Access_TEST.Stub accessMock = new VOL_Access_TEST.Stub();
VOL_CTRL_PersonalSiteContactLookup.access = (VOL_Access) Test.createStub(VOL_Access.class, accessMock);

Contact contactRecord = UTIL_UnitTest.createContact();
insert contactRecord;

personalSiteLookupCtrl.SendEmailToContact(contactRecord);
accessMock.assertMethodCalledWith('insertRecords', Task.SObjectType);
}
@IsTest
private static void shouldConvertEmailToTask() {
String expectedSubject = 'This is the subject';
String expectedDescription = 'This the description.';
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

email.setSubject(expectedSubject);
email.setPlainTextBody(expectedDescription);

Task actualTask = VOL_CTRL_PersonalSiteContactLookup.toTask(email);
System.assert(actualTask.Subject.contains(expectedSubject), 'Expected the Task subject to contain the Email\'s subject.');
System.assert(actualTask.Description.contains(expectedDescription), 'Expected the Task subject to contain the Email\'s subject.');
}

@IsTest
private static void shouldReturnDefaultClosedStatus() {
final String defaultClosedStatus = 'Completed';

System.assertEquals(defaultClosedStatus, VOL_CTRL_PersonalSiteContactLookup.getClosedTaskStatus(), 'Expected the system to find the default closed task status.');
}

@IsTest
private static void shouldReturnDefaultClosedStatusWhenRunAsGuest() {
final String defaultClosedStatus = 'Completed';

System.runAs(UTIL_UnitTest.guestUser) {
System.assertEquals(defaultClosedStatus, VOL_CTRL_PersonalSiteContactLookup.getClosedTaskStatus(), 'Expected the system to find the default closed task status when ran as a guest user.');
}
}

@isTest(SeeAllData=true) // SeeAllData so the CSS file is viewable
/*******************************************************************************************************
Expand Down Expand Up @@ -91,7 +136,7 @@ private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST {
insert con;

// test existing contact
ctrl.contact.FirstName = 'LookupTestFirstName';
ctrl.contact.FirstName = 'LookupTestFirstName';
ctrl.contact.LastName = 'LookupTestLastName';
ctrl.contact.Email = '[email protected]';
system.assertEquals(null, ctrl.LookupContact());
Expand Down