From 87adbfe046d41d321226c827a887a369f0415bcd Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Tue, 21 Jan 2020 15:27:26 -0800 Subject: [PATCH 1/6] Add label with ambiguous lookup result --- src/labels/CustomLabels.labels | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/labels/CustomLabels.labels b/src/labels/CustomLabels.labels index e0e7f27..17e1f9b 100644 --- a/src/labels/CustomLabels.labels +++ b/src/labels/CustomLabels.labels @@ -503,6 +503,14 @@ day: 'day' label displayed on PersonalSiteContactInfo if the lookup and email succeed Message sent. Please check your email for a message that contains a link to your Volunteer information. Thank you. + + labelContactLookupAmbiguous + PersonalSiteContactInfo + en_US + false + label displayed on PersonalSiteContactInfo after lookup is complete + If your record was found an email message that contains a link to your Volunteer information will be sent. Thank you. + labelCopySchedule NewAndEditVRS From d7e0436780120a0552f8493ad10a75f60bf0f1a7 Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Tue, 21 Jan 2020 15:42:44 -0800 Subject: [PATCH 2/6] Update label references --- .../VOL_CTRL_PersonalSiteContactLookup.cls | 2 +- ...OL_CTRL_PersonalSiteContactLookup_TEST.cls | 235 ++++++++++-------- 2 files changed, 131 insertions(+), 106 deletions(-) diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls index 12fc45c..0506db4 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls @@ -79,7 +79,7 @@ global with sharing class VOL_CTRL_PersonalSiteContactLookup { list listCon = VOL_SharedCode.LookupContact(contact, null); if (listCon == null || listCon.size() == 0) { - strResult = System.Label.labelContactLookupNotFound; + strResult = System.Label.labelContactLookupAmbiguous; } else { SendEmailToContact(listCon[0]); } diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls index 3c95e0f..507dc82 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls @@ -30,62 +30,80 @@ @isTest private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST { + //==================== TEST METHOD(s) ====================================== - //==================== 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(); - @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 + ); - 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; - Contact contactRecord = UTIL_UnitTest.createContact(); - insert contactRecord; + personalSiteLookupCtrl.SendEmailToContact(contactRecord); + accessMock.assertMethodCalled('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(); - personalSiteLookupCtrl.SendEmailToContact(contactRecord); - accessMock.assertMethodCalled('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.'); - } + email.setSubject(expectedSubject); + email.setPlainTextBody(expectedDescription); - @IsTest - private static void shouldReturnDefaultClosedStatus() { - final String defaultClosedStatus = 'Completed'; + 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.' + ); + } - System.assertEquals(defaultClosedStatus, VOL_CTRL_PersonalSiteContactLookup.getClosedTaskStatus(), 'Expected the system to find the default closed task status.'); - } + @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'; + @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.'); - } + 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 - /******************************************************************************************************* - * @description test the visualforce page controller, running as the Sites Guest User, if such as user - * exists. if not, will run under the current user. - * @return void - ********************************************************************************************************/ - private static void testPageWithSitesGuestUser() { - /** Without the ability to create sharing records in apex when owd sharing is public read/write or controlled by parent, we can no longer + } + + @isTest(SeeAllData=true) // SeeAllData so the CSS file is viewable + /******************************************************************************************************* + * @description test the visualforce page controller, running as the Sites Guest User, if such as user + * exists. if not, will run under the current user. + * @return void + ********************************************************************************************************/ + private static void testPageWithSitesGuestUser() { + /** Without the ability to create sharing records in apex when owd sharing is public read/write or controlled by parent, we can no longer * run this test as the guest user and will only run it as the admin. Commenting out so that we can reinstate * if / when the ability to do so becomes available * list listU = [Select Username, UserType, Name, IsActive, Id From User where IsActive = true and UserType = 'Guest' and @@ -98,64 +116,71 @@ private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST { } } else { */ - system.debug('Running test as system.'); - TestPersonalSiteContactLookup(); - //} - } + system.debug('Running test as system.'); + TestPersonalSiteContactLookup(); + //} + } + + private static void TestPersonalSiteContactLookup() { + //point to our VF page + PageReference p = new PageReference('Page.PersonalSiteContactLookup'); + Test.setCurrentPageReference(p); + + //instantiate the controller for this page + VOL_CTRL_PersonalSiteContactLookup ctrl = new VOL_CTRL_PersonalSiteContactLookup(); + system.assertNotEquals(null, ctrl.strURLtoCSSFile); + system.assertNotEquals(null, ctrl.contact); + system.assertNotEquals(null, ctrl.strLanguage); + system.assertEquals(null, ctrl.strResult); + system.assertNotEquals(null, ctrl.emailTemplateId); + system.assert(ctrl.orgWideEmailId == ctrl.orgWideEmailId); + + // test empty contact + ctrl.contact.Firstname = null; + ctrl.contact.Lastname = null; + ctrl.contact.Email = null; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals( + System.Label.labelContactLookupAmbiguous, + ctrl.strResult + ); + + // test bogus contact + ctrl.contact.Firstname = 'Not There'; + ctrl.contact.Lastname = 'Not There'; + ctrl.contact.Email = 'NotThere@NotThere.com'; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals( + System.Label.labelContactLookupAmbiguous, + ctrl.strResult + ); + + // create a temp contact + Contact con = new Contact(); + con.FirstName = 'LookupTestFirstName'; + con.LastName = 'LookupTestLastName'; + con.Email = 'LookupTestEmail@email.com'; + insert con; + + // test existing contact + ctrl.contact.FirstName = 'LookupTestFirstName'; + ctrl.contact.LastName = 'LookupTestLastName'; + ctrl.contact.Email = 'LookupTestEmail@email.com'; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals(System.Label.labelContactLookupSuccess, ctrl.strResult); - private static void TestPersonalSiteContactLookup() { - - //point to our VF page - PageReference p = new PageReference('Page.PersonalSiteContactLookup'); - Test.setCurrentPageReference(p); - - //instantiate the controller for this page - VOL_CTRL_PersonalSiteContactLookup ctrl = new VOL_CTRL_PersonalSiteContactLookup(); - system.assertNotEquals(null, ctrl.strURLtoCSSFile); - system.assertNotEquals(null, ctrl.contact); - system.assertNotEquals(null, ctrl.strLanguage); - system.assertEquals(null, ctrl.strResult); - system.assertNotEquals(null, ctrl.emailTemplateId); - system.assert(ctrl.orgWideEmailId == ctrl.orgWideEmailId); - - // test empty contact - ctrl.contact.Firstname = null; - ctrl.contact.Lastname = null; - ctrl.contact.Email = null; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals(System.Label.labelContactLookupNotFound, ctrl.strResult); - - // test bogus contact - ctrl.contact.Firstname = 'Not There'; - ctrl.contact.Lastname = 'Not There'; - ctrl.contact.Email = 'NotThere@NotThere.com'; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals(System.Label.labelContactLookupNotFound, ctrl.strResult); - - // create a temp contact - Contact con = new Contact(); - con.FirstName = 'LookupTestFirstName'; - con.LastName = 'LookupTestLastName'; - con.Email = 'LookupTestEmail@email.com'; - insert con; - - // test existing contact - ctrl.contact.FirstName = 'LookupTestFirstName'; - ctrl.contact.LastName = 'LookupTestLastName'; - ctrl.contact.Email = 'LookupTestEmail@email.com'; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals(System.Label.labelContactLookupSuccess, ctrl.strResult); - - // test error email handling - con.Email = null; - if (VOL_SharedCode.IsNPSPInstalled) { - con.put('npe01__AlternateEmail__c', null); - con.put('npe01__HomeEmail__c', null); - con.put('npe01__WorkEmail__c', null); - } - update con; - ctrl.SendEmailToContact(con); - system.assertNotEquals(System.Label.labelContactLookupSuccess, ctrl.strResult); + // test error email handling + con.Email = null; + if (VOL_SharedCode.IsNPSPInstalled) { + con.put('npe01__AlternateEmail__c', null); + con.put('npe01__HomeEmail__c', null); + con.put('npe01__WorkEmail__c', null); } - -} \ No newline at end of file + update con; + ctrl.SendEmailToContact(con); + system.assertNotEquals( + System.Label.labelContactLookupSuccess, + ctrl.strResult + ); + } +} From 6a0b55504a06602804787f81af4e63c3e3a90e72 Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Tue, 21 Jan 2020 16:06:50 -0800 Subject: [PATCH 3/6] Update label reference on success --- .../VOL_CTRL_PersonalSiteContactLookup.cls | 304 ++++++++++-------- ...OL_CTRL_PersonalSiteContactLookup_TEST.cls | 7 +- 2 files changed, 170 insertions(+), 141 deletions(-) diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls index 0506db4..e63da41 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls @@ -29,159 +29,185 @@ */ global with sharing class VOL_CTRL_PersonalSiteContactLookup { - @TestVisible - private static VOL_Access access = VOL_Access.getInstance(); + @TestVisible + private static VOL_Access access = VOL_Access.getInstance(); - // constructor - global VOL_CTRL_PersonalSiteContactLookup() { + // constructor + global VOL_CTRL_PersonalSiteContactLookup() { + // initial values for potential parameters + strLanguage = 'en-us'; + strResult = null; - // initial values for potential parameters - strLanguage = 'en-us'; - strResult = null; + // handle optional page parameters (must use string, not ID, to handle null) + map params = ApexPages.currentPage().getParameters(); + string p = params.get('Language'); + if (p != null && p != '') + strLanguage = p; + } - // handle optional page parameters (must use string, not ID, to handle null) - map params = ApexPages.currentPage().getParameters(); - string p = params.get('Language'); - if (p != null && p != '') strLanguage = p; - } - - // returns the correct URL for the CSS override file - global string strURLtoCSSFile { - get { - if (strURLtoCSSFile == null) { - list listDocs = [SELECT Name, Id From Document WHERE Name = 'VolunteersPersonalSiteCSS.css' LIMIT 1 ]; - if (listDocs.size() > 0) { - Document doc = listDocs[0]; - string imageid = doc.id; - imageid = imageid.substring(0,15); - strURLToCSSFile = '/servlet/servlet.FileDownload?file=' + imageid; - } - } - return strURLtoCSSFile; - } - - set; + // returns the correct URL for the CSS override file + global string strURLtoCSSFile { + get { + if (strURLtoCSSFile == null) { + list listDocs = [ + SELECT Name, Id + FROM Document + WHERE Name = 'VolunteersPersonalSiteCSS.css' + LIMIT 1 + ]; + if (listDocs.size() > 0) { + Document doc = listDocs[0]; + string imageid = doc.id; + imageid = imageid.substring(0, 15); + strURLToCSSFile = '/servlet/servlet.FileDownload?file=' + imageid; + } + } + return strURLtoCSSFile; } - - global Contact contact { - get { - if (contact == null) contact = new Contact(); - return contact; - } - set; + set; + } + + global Contact contact { + get { + if (contact == null) + contact = new Contact(); + return contact; } + set; + } - global string strResult { get; set; } - global string strLanguage { get; set; } - - // the action method called from the page to lookup the contact, and to send them email if found. - global PageReference LookupContact() { - - list listCon = VOL_SharedCode.LookupContact(contact, null); - if (listCon == null || listCon.size() == 0) { - strResult = System.Label.labelContactLookupAmbiguous; - } else { - SendEmailToContact(listCon[0]); - } - return null; + global string strResult { get; set; } + global string strLanguage { get; set; } + + // the action method called from the page to lookup the contact, and to send them email if found. + global PageReference LookupContact() { + list listCon = VOL_SharedCode.LookupContact(contact, null); + if (listCon == null || listCon.size() == 0) { + strResult = System.Label.labelContactLookupAmbiguous; + } else { + SendEmailToContact(listCon[0]); } - - // get the Id of the Email Template to use - public ID emailTemplateId { - get { - if (emailTemplateId == null) { - list listT = [select Id from EmailTemplate where Name='Volunteers Personal Site Contact Lookup' limit 1]; - if (listT.size() > 0) emailTemplateId = listT[0].Id; - } - return emailTemplateId; - } - set; + return null; + } + + // get the Id of the Email Template to use + public ID emailTemplateId { + get { + if (emailTemplateId == null) { + list listT = [ + SELECT Id + FROM EmailTemplate + WHERE Name = 'Volunteers Personal Site Contact Lookup' + LIMIT 1 + ]; + if (listT.size() > 0) + emailTemplateId = listT[0].Id; + } + return emailTemplateId; } - - // get the Id of the Org-wide Email Address to use - public ID orgWideEmailId { - get { - if (orgWideEmailId == null) { - string strOwa = VOL_SharedCode.VolunteersSettings.Personal_Site_Org_Wide_Email_Name__c; - if (strOwa != null) { - list listOwa = [select id from OrgWideEmailAddress where DisplayName = :strOwa]; - if (listOwa.size() > 0) orgWideEmailId = listOwa[0].Id; - } - } - return orgWideEmailId; + set; + } + + // get the Id of the Org-wide Email Address to use + public ID orgWideEmailId { + get { + if (orgWideEmailId == null) { + string strOwa = VOL_SharedCode.VolunteersSettings.Personal_Site_Org_Wide_Email_Name__c; + if (strOwa != null) { + list listOwa = [ + SELECT id + FROM OrgWideEmailAddress + WHERE DisplayName = :strOwa + ]; + if (listOwa.size() > 0) + orgWideEmailId = listOwa[0].Id; } - set; - } - - // send the email to the passed in contact - public void SendEmailToContact(Contact con) { - try { - strResult = null; - Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); - mail.setTargetObjectId(con.Id); - mail.setSaveAsActivity(false); - mail.setTemplateID(emailTemplateId); - if (orgWideEmailId != null) { - mail.setOrgWideEmailAddressId(orgWideEmailId); - } - list listSER; - listSER = Messaging.sendEmail(new Messaging.Email[] { mail }, false); - if (listSER[0].isSuccess()) { - strResult = System.Label.labelContactLookupSuccess; - Task taskRecord = toTask(mail); - access.insertRecords(new List{ taskRecord }); - - } else { - list listSEE = listSER[0].getErrors(); - for (Messaging.SendEmailError see : listSEE) { - ID objId = see.getTargetObjectId(); - if (objId == null) { - // see if we can parse it out of the error string. - string strErr = see.getMessage(); - integer ich = strErr.lastIndexOf(' for id : '); - if (ich > = 0) { - objId = strErr.substring(ich + 10); - } - } - - // Ensure the user has access to the object and fields before querying - UTIL_Describe.checkReadAccess('Contact', new Set{'Id', 'Name', 'Email'}); - - list listCon = [select Name, Email from Contact where Id =: objId]; - string strDetails = ''; - if (listCon.size() > 0) - strDetails = ' ' + listCon[0].Name + ' (' + listCon[0].Email + ') '; - strResult += see.getStatusCode() + ': ' + see.getMessage() + strDetails; - } - } - } catch (Exception e) { - ApexPages.addMessages(e); - } + } + return orgWideEmailId; } + set; + } - @TestVisible - private static Task toTask(Messaging.SingleEmailMessage email) { - final String subType = 'Email'; + // send the email to the passed in contact + public void SendEmailToContact(Contact con) { + try { + strResult = null; + Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); + mail.setTargetObjectId(con.Id); + mail.setSaveAsActivity(false); + mail.setTemplateID(emailTemplateId); + if (orgWideEmailId != null) { + mail.setOrgWideEmailAddressId(orgWideEmailId); + } + list listSER; + listSER = Messaging.sendEmail(new List{ mail }, false); + if (listSER[0].isSuccess()) { + strResult = System.Label.labelContactLookupAmbiguous; + Task taskRecord = toTask(mail); + access.insertRecords(new List{ taskRecord }); + } else { + list listSEE = listSER[0].getErrors(); + for (Messaging.SendEmailError see : listSEE) { + ID objId = see.getTargetObjectId(); + if (objId == null) { + // see if we can parse it out of the error string. + string strErr = see.getMessage(); + integer ich = strErr.lastIndexOf(' for id : '); + if (ich >= 0) { + objId = strErr.substring(ich + 10); + } + } - Task taskRecord = new Task( - WhoId = email.getTargetObjectId(), - Subject = subType + ': ' + email.getSubject(), - ActivityDate = System.today(), - Status = getClosedTaskStatus(), - Description = email.getPlainTextBody(), - TaskSubtype = subType - ); + // Ensure the user has access to the object and fields before querying + UTIL_Describe.checkReadAccess( + 'Contact', + new Set{ 'Id', 'Name', 'Email' } + ); - return taskRecord; + list listCon = [ + SELECT Name, Email + FROM Contact + WHERE Id = :objId + ]; + string strDetails = ''; + if (listCon.size() > 0) + strDetails = ' ' + listCon[0].Name + ' (' + listCon[0].Email + ') '; + strResult += + see.getStatusCode() + + ': ' + + see.getMessage() + + strDetails; + } + } + } catch (Exception e) { + ApexPages.addMessages(e); } + } - @TestVisible - private static String getClosedTaskStatus() { - List closedTaskStatuses = [SELECT ApiName FROM TaskStatus WHERE IsClosed = true]; + @TestVisible + private static Task toTask(Messaging.SingleEmailMessage email) { + final String subType = 'Email'; - return closedTaskStatuses.isEmpty() ? - null : - closedTaskStatuses[0].ApiName; - } + 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 closedTaskStatuses = [ + SELECT ApiName + FROM TaskStatus + WHERE IsClosed = true + ]; -} \ No newline at end of file + return closedTaskStatuses.isEmpty() ? null : closedTaskStatuses[0].ApiName; + } +} diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls index 507dc82..9e595df 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls @@ -167,7 +167,10 @@ private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST { ctrl.contact.LastName = 'LookupTestLastName'; ctrl.contact.Email = 'LookupTestEmail@email.com'; system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals(System.Label.labelContactLookupSuccess, ctrl.strResult); + system.assertEquals( + System.Label.labelContactLookupAmbiguous, + ctrl.strResult + ); // test error email handling con.Email = null; @@ -179,7 +182,7 @@ private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST { update con; ctrl.SendEmailToContact(con); system.assertNotEquals( - System.Label.labelContactLookupSuccess, + System.Label.labelContactLookupAmbiguous, ctrl.strResult ); } From 63323f1565879d91b028b710bfeb84a45d98bb95 Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Tue, 21 Jan 2020 16:14:16 -0800 Subject: [PATCH 4/6] Updated formatting --- .../VOL_CTRL_PersonalSiteContactLookup.cls | 304 ++++++++---------- ...OL_CTRL_PersonalSiteContactLookup_TEST.cls | 238 ++++++-------- 2 files changed, 244 insertions(+), 298 deletions(-) diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls index e63da41..0d0ef2f 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup.cls @@ -29,185 +29,159 @@ */ global with sharing class VOL_CTRL_PersonalSiteContactLookup { - @TestVisible - private static VOL_Access access = VOL_Access.getInstance(); + @TestVisible + private static VOL_Access access = VOL_Access.getInstance(); - // constructor - global VOL_CTRL_PersonalSiteContactLookup() { - // initial values for potential parameters - strLanguage = 'en-us'; - strResult = null; + // constructor + global VOL_CTRL_PersonalSiteContactLookup() { - // handle optional page parameters (must use string, not ID, to handle null) - map params = ApexPages.currentPage().getParameters(); - string p = params.get('Language'); - if (p != null && p != '') - strLanguage = p; - } + // initial values for potential parameters + strLanguage = 'en-us'; + strResult = null; - // returns the correct URL for the CSS override file - global string strURLtoCSSFile { - get { - if (strURLtoCSSFile == null) { - list listDocs = [ - SELECT Name, Id - FROM Document - WHERE Name = 'VolunteersPersonalSiteCSS.css' - LIMIT 1 - ]; - if (listDocs.size() > 0) { - Document doc = listDocs[0]; - string imageid = doc.id; - imageid = imageid.substring(0, 15); - strURLToCSSFile = '/servlet/servlet.FileDownload?file=' + imageid; - } - } - return strURLtoCSSFile; - } - set; - } - - global Contact contact { - get { - if (contact == null) - contact = new Contact(); - return contact; + // handle optional page parameters (must use string, not ID, to handle null) + map params = ApexPages.currentPage().getParameters(); + string p = params.get('Language'); + if (p != null && p != '') strLanguage = p; + } + + // returns the correct URL for the CSS override file + global string strURLtoCSSFile { + get { + if (strURLtoCSSFile == null) { + list listDocs = [SELECT Name, Id From Document WHERE Name = 'VolunteersPersonalSiteCSS.css' LIMIT 1 ]; + if (listDocs.size() > 0) { + Document doc = listDocs[0]; + string imageid = doc.id; + imageid = imageid.substring(0,15); + strURLToCSSFile = '/servlet/servlet.FileDownload?file=' + imageid; + } + } + return strURLtoCSSFile; + } + + set; } - set; - } - - global string strResult { get; set; } - global string strLanguage { get; set; } - - // the action method called from the page to lookup the contact, and to send them email if found. - global PageReference LookupContact() { - list listCon = VOL_SharedCode.LookupContact(contact, null); - if (listCon == null || listCon.size() == 0) { - strResult = System.Label.labelContactLookupAmbiguous; - } else { - SendEmailToContact(listCon[0]); + + global Contact contact { + get { + if (contact == null) contact = new Contact(); + return contact; + } + set; } - return null; - } - // get the Id of the Email Template to use - public ID emailTemplateId { - get { - if (emailTemplateId == null) { - list listT = [ - SELECT Id - FROM EmailTemplate - WHERE Name = 'Volunteers Personal Site Contact Lookup' - LIMIT 1 - ]; - if (listT.size() > 0) - emailTemplateId = listT[0].Id; - } - return emailTemplateId; + global string strResult { get; set; } + global string strLanguage { get; set; } + + // the action method called from the page to lookup the contact, and to send them email if found. + global PageReference LookupContact() { + + list listCon = VOL_SharedCode.LookupContact(contact, null); + if (listCon == null || listCon.size() == 0) { + strResult = System.Label.labelContactLookupAmbiguous; + } else { + SendEmailToContact(listCon[0]); + } + return null; } - set; - } - - // get the Id of the Org-wide Email Address to use - public ID orgWideEmailId { - get { - if (orgWideEmailId == null) { - string strOwa = VOL_SharedCode.VolunteersSettings.Personal_Site_Org_Wide_Email_Name__c; - if (strOwa != null) { - list listOwa = [ - SELECT id - FROM OrgWideEmailAddress - WHERE DisplayName = :strOwa - ]; - if (listOwa.size() > 0) - orgWideEmailId = listOwa[0].Id; + + // get the Id of the Email Template to use + public ID emailTemplateId { + get { + if (emailTemplateId == null) { + list listT = [select Id from EmailTemplate where Name='Volunteers Personal Site Contact Lookup' limit 1]; + if (listT.size() > 0) emailTemplateId = listT[0].Id; + } + return emailTemplateId; } - } - return orgWideEmailId; + set; } - set; - } - - // send the email to the passed in contact - public void SendEmailToContact(Contact con) { - try { - strResult = null; - Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); - mail.setTargetObjectId(con.Id); - mail.setSaveAsActivity(false); - mail.setTemplateID(emailTemplateId); - if (orgWideEmailId != null) { - mail.setOrgWideEmailAddressId(orgWideEmailId); - } - list listSER; - listSER = Messaging.sendEmail(new List{ mail }, false); - if (listSER[0].isSuccess()) { - strResult = System.Label.labelContactLookupAmbiguous; - Task taskRecord = toTask(mail); - access.insertRecords(new List{ taskRecord }); - } else { - list listSEE = listSER[0].getErrors(); - for (Messaging.SendEmailError see : listSEE) { - ID objId = see.getTargetObjectId(); - if (objId == null) { - // see if we can parse it out of the error string. - string strErr = see.getMessage(); - integer ich = strErr.lastIndexOf(' for id : '); - if (ich >= 0) { - objId = strErr.substring(ich + 10); + + // get the Id of the Org-wide Email Address to use + public ID orgWideEmailId { + get { + if (orgWideEmailId == null) { + string strOwa = VOL_SharedCode.VolunteersSettings.Personal_Site_Org_Wide_Email_Name__c; + if (strOwa != null) { + list listOwa = [select id from OrgWideEmailAddress where DisplayName = :strOwa]; + if (listOwa.size() > 0) orgWideEmailId = listOwa[0].Id; + } } - } - - // Ensure the user has access to the object and fields before querying - UTIL_Describe.checkReadAccess( - 'Contact', - new Set{ 'Id', 'Name', 'Email' } - ); - - list listCon = [ - SELECT Name, Email - FROM Contact - WHERE Id = :objId - ]; - string strDetails = ''; - if (listCon.size() > 0) - strDetails = ' ' + listCon[0].Name + ' (' + listCon[0].Email + ') '; - strResult += - see.getStatusCode() + - ': ' + - see.getMessage() + - strDetails; + return orgWideEmailId; } - } - } catch (Exception e) { - ApexPages.addMessages(e); + set; + } + + // send the email to the passed in contact + public void SendEmailToContact(Contact con) { + try { + strResult = null; + Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); + mail.setTargetObjectId(con.Id); + mail.setSaveAsActivity(false); + mail.setTemplateID(emailTemplateId); + if (orgWideEmailId != null) { + mail.setOrgWideEmailAddressId(orgWideEmailId); + } + list listSER; + listSER = Messaging.sendEmail(new Messaging.Email[] { mail }, false); + if (listSER[0].isSuccess()) { + strResult = System.Label.labelContactLookupAmbiguous; + Task taskRecord = toTask(mail); + access.insertRecords(new List{ taskRecord }); + + } else { + list listSEE = listSER[0].getErrors(); + for (Messaging.SendEmailError see : listSEE) { + ID objId = see.getTargetObjectId(); + if (objId == null) { + // see if we can parse it out of the error string. + string strErr = see.getMessage(); + integer ich = strErr.lastIndexOf(' for id : '); + if (ich > = 0) { + objId = strErr.substring(ich + 10); + } + } + + // Ensure the user has access to the object and fields before querying + UTIL_Describe.checkReadAccess('Contact', new Set{'Id', 'Name', 'Email'}); + + list listCon = [select Name, Email from Contact where Id =: objId]; + string strDetails = ''; + if (listCon.size() > 0) + strDetails = ' ' + listCon[0].Name + ' (' + listCon[0].Email + ') '; + strResult += see.getStatusCode() + ': ' + see.getMessage() + strDetails; + } + } + } catch (Exception e) { + ApexPages.addMessages(e); + } } - } - @TestVisible - private static Task toTask(Messaging.SingleEmailMessage email) { - final String subType = 'Email'; + @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 - ); + Task taskRecord = new Task( + WhoId = email.getTargetObjectId(), + Subject = subType + ': ' + email.getSubject(), + ActivityDate = System.today(), + Status = getClosedTaskStatus(), + Description = email.getPlainTextBody(), + TaskSubtype = subType + ); - return taskRecord; - } + return taskRecord; + } - @TestVisible - private static String getClosedTaskStatus() { - List closedTaskStatuses = [ - SELECT ApiName - FROM TaskStatus - WHERE IsClosed = true - ]; + @TestVisible + private static String getClosedTaskStatus() { + List closedTaskStatuses = [SELECT ApiName FROM TaskStatus WHERE IsClosed = true]; + + return closedTaskStatuses.isEmpty() ? + null : + closedTaskStatuses[0].ApiName; + } - return closedTaskStatuses.isEmpty() ? null : closedTaskStatuses[0].ApiName; - } -} +} \ No newline at end of file diff --git a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls index 9e595df..70723d7 100644 --- a/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls +++ b/src/classes/VOL_CTRL_PersonalSiteContactLookup_TEST.cls @@ -30,80 +30,62 @@ @isTest 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(); + //==================== TEST METHOD(s) ====================================== - VOL_Access_TEST.Stub accessMock = new VOL_Access_TEST.Stub(); - VOL_CTRL_PersonalSiteContactLookup.access = (VOL_Access) Test.createStub( - VOL_Access.class, - accessMock - ); + @IsTest + private static void shouldInsertTaskRecordsWhenEmailSent() { + PageReference personalSiteLookupPage = new PageReference('Page.PersonalSiteContactLookup'); + Test.setCurrentPageReference(personalSiteLookupPage); + VOL_CTRL_PersonalSiteContactLookup personalSiteLookupCtrl = new VOL_CTRL_PersonalSiteContactLookup(); - Contact contactRecord = UTIL_UnitTest.createContact(); - insert contactRecord; + VOL_Access_TEST.Stub accessMock = new VOL_Access_TEST.Stub(); + VOL_CTRL_PersonalSiteContactLookup.access = (VOL_Access) Test.createStub(VOL_Access.class, accessMock); - personalSiteLookupCtrl.SendEmailToContact(contactRecord); - accessMock.assertMethodCalled('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(); + Contact contactRecord = UTIL_UnitTest.createContact(); + insert contactRecord; - 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.' - ); - } + personalSiteLookupCtrl.SendEmailToContact(contactRecord); + accessMock.assertMethodCalled('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'; + @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.' - ); - } + 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'; + @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.' - ); + 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 - /******************************************************************************************************* - * @description test the visualforce page controller, running as the Sites Guest User, if such as user - * exists. if not, will run under the current user. - * @return void - ********************************************************************************************************/ - private static void testPageWithSitesGuestUser() { - /** Without the ability to create sharing records in apex when owd sharing is public read/write or controlled by parent, we can no longer + + @isTest(SeeAllData=true) // SeeAllData so the CSS file is viewable + /******************************************************************************************************* + * @description test the visualforce page controller, running as the Sites Guest User, if such as user + * exists. if not, will run under the current user. + * @return void + ********************************************************************************************************/ + private static void testPageWithSitesGuestUser() { + /** Without the ability to create sharing records in apex when owd sharing is public read/write or controlled by parent, we can no longer * run this test as the guest user and will only run it as the admin. Commenting out so that we can reinstate * if / when the ability to do so becomes available * list listU = [Select Username, UserType, Name, IsActive, Id From User where IsActive = true and UserType = 'Guest' and @@ -116,74 +98,64 @@ private with sharing class VOL_CTRL_PersonalSiteContactLookup_TEST { } } else { */ - system.debug('Running test as system.'); - TestPersonalSiteContactLookup(); - //} - } - - private static void TestPersonalSiteContactLookup() { - //point to our VF page - PageReference p = new PageReference('Page.PersonalSiteContactLookup'); - Test.setCurrentPageReference(p); - - //instantiate the controller for this page - VOL_CTRL_PersonalSiteContactLookup ctrl = new VOL_CTRL_PersonalSiteContactLookup(); - system.assertNotEquals(null, ctrl.strURLtoCSSFile); - system.assertNotEquals(null, ctrl.contact); - system.assertNotEquals(null, ctrl.strLanguage); - system.assertEquals(null, ctrl.strResult); - system.assertNotEquals(null, ctrl.emailTemplateId); - system.assert(ctrl.orgWideEmailId == ctrl.orgWideEmailId); - - // test empty contact - ctrl.contact.Firstname = null; - ctrl.contact.Lastname = null; - ctrl.contact.Email = null; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals( - System.Label.labelContactLookupAmbiguous, - ctrl.strResult - ); - - // test bogus contact - ctrl.contact.Firstname = 'Not There'; - ctrl.contact.Lastname = 'Not There'; - ctrl.contact.Email = 'NotThere@NotThere.com'; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals( - System.Label.labelContactLookupAmbiguous, - ctrl.strResult - ); - - // create a temp contact - Contact con = new Contact(); - con.FirstName = 'LookupTestFirstName'; - con.LastName = 'LookupTestLastName'; - con.Email = 'LookupTestEmail@email.com'; - insert con; - - // test existing contact - ctrl.contact.FirstName = 'LookupTestFirstName'; - ctrl.contact.LastName = 'LookupTestLastName'; - ctrl.contact.Email = 'LookupTestEmail@email.com'; - system.assertEquals(null, ctrl.LookupContact()); - system.assertEquals( - System.Label.labelContactLookupAmbiguous, - ctrl.strResult - ); + system.debug('Running test as system.'); + TestPersonalSiteContactLookup(); + //} + } - // test error email handling - con.Email = null; - if (VOL_SharedCode.IsNPSPInstalled) { - con.put('npe01__AlternateEmail__c', null); - con.put('npe01__HomeEmail__c', null); - con.put('npe01__WorkEmail__c', null); + private static void TestPersonalSiteContactLookup() { + + //point to our VF page + PageReference p = new PageReference('Page.PersonalSiteContactLookup'); + Test.setCurrentPageReference(p); + + //instantiate the controller for this page + VOL_CTRL_PersonalSiteContactLookup ctrl = new VOL_CTRL_PersonalSiteContactLookup(); + system.assertNotEquals(null, ctrl.strURLtoCSSFile); + system.assertNotEquals(null, ctrl.contact); + system.assertNotEquals(null, ctrl.strLanguage); + system.assertEquals(null, ctrl.strResult); + system.assertNotEquals(null, ctrl.emailTemplateId); + system.assert(ctrl.orgWideEmailId == ctrl.orgWideEmailId); + + // test empty contact + ctrl.contact.Firstname = null; + ctrl.contact.Lastname = null; + ctrl.contact.Email = null; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals(System.Label.labelContactLookupAmbiguous, ctrl.strResult); + + // test bogus contact + ctrl.contact.Firstname = 'Not There'; + ctrl.contact.Lastname = 'Not There'; + ctrl.contact.Email = 'NotThere@NotThere.com'; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals(System.Label.labelContactLookupAmbiguous, ctrl.strResult); + + // create a temp contact + Contact con = new Contact(); + con.FirstName = 'LookupTestFirstName'; + con.LastName = 'LookupTestLastName'; + con.Email = 'LookupTestEmail@email.com'; + insert con; + + // test existing contact + ctrl.contact.FirstName = 'LookupTestFirstName'; + ctrl.contact.LastName = 'LookupTestLastName'; + ctrl.contact.Email = 'LookupTestEmail@email.com'; + system.assertEquals(null, ctrl.LookupContact()); + system.assertEquals(System.Label.labelContactLookupAmbiguous, ctrl.strResult); + + // test error email handling + con.Email = null; + if (VOL_SharedCode.IsNPSPInstalled) { + con.put('npe01__AlternateEmail__c', null); + con.put('npe01__HomeEmail__c', null); + con.put('npe01__WorkEmail__c', null); + } + update con; + ctrl.SendEmailToContact(con); + system.assertNotEquals(System.Label.labelContactLookupAmbiguous, ctrl.strResult); } - update con; - ctrl.SendEmailToContact(con); - system.assertNotEquals( - System.Label.labelContactLookupAmbiguous, - ctrl.strResult - ); - } -} + +} \ No newline at end of file From 867f5e217267a07a8ff9eefa36e1517a7a71849e Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Wed, 22 Jan 2020 08:18:36 -0800 Subject: [PATCH 5/6] Updated messaging from docs review --- src/labels/CustomLabels.labels | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/labels/CustomLabels.labels b/src/labels/CustomLabels.labels index 17e1f9b..b04c739 100644 --- a/src/labels/CustomLabels.labels +++ b/src/labels/CustomLabels.labels @@ -509,7 +509,7 @@ day: 'day' en_US false label displayed on PersonalSiteContactInfo after lookup is complete - If your record was found an email message that contains a link to your Volunteer information will be sent. Thank you. + If your contact information was found, we'll send a unique volunteer link to the email we have on file. labelCopySchedule From 9ac61708d53dec78ba99895e18a885735f392a89 Mon Sep 17 00:00:00 2001 From: Dennis Warren Date: Wed, 22 Jan 2020 08:45:53 -0800 Subject: [PATCH 6/6] Prevent automation from removing labels --- src/classes/VOL_SharedCode.cls | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/classes/VOL_SharedCode.cls b/src/classes/VOL_SharedCode.cls index ee3bea7..ad57431 100644 --- a/src/classes/VOL_SharedCode.cls +++ b/src/classes/VOL_SharedCode.cls @@ -879,6 +879,8 @@ global with sharing class VOL_SharedCode { str = Label.labelFindVolunteersCriteria; str = Label.labelFindVolunteersHelpAssign; str = Label.labelVolunteersWizardNewCampaignTitle; + str = Label.labelContactLookupSuccess; + str = Label.labelContactLookupNotFound; } } \ No newline at end of file