StandardSetController test coverage
<apex:page standardController="Contact" extensions="ContactMassSendSurveyController" recordSetVar="contacts" showHeader="false" standardStylesheets="false">
<apex:slds />
<!-- styles and js libraries for date picker -->
<apex:stylesheet value="{!URLFOR($Resource.DateTimePicker, 'src/css/bootstrap.css')}"/>
<apex:stylesheet value="{!URLFOR($Resource.DateTimePicker, 'src/css/bootstrap-datetimepicker.css')}"/>
<apex:includeScript value="{!URLFOR($Resource.DateTimePicker, 'src/js/jquery-3.2.1.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.DateTimePicker, 'src/js/bootstrap.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.DateTimePicker, 'src/js/moment.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.DateTimePicker, 'src/js/moment-with-locales.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.DateTimePicker, 'src/js/bootstrap-datetimepicker.js')}"/>
<!-- end -->
<apex:form styleClass="slds-p-around--medium">
<script type="text/javascript">
// submitForm() is called with the button at the bottom of the page
function submitForm() {
sendSurvey(
document.getElementById("selectedSurvey").options[document.getElementById("selectedSurvey").selectedIndex].value,
document.getElementById("sendDateTime").value
);
}
// makes it so users can't type in the sendDateTime input, they need to use the picker.
$(function () {
$('#datetimepicker').datetimepicker({ignoreReadonly : true});
});
</script>
<apex:actionFunction name="sendSurvey" action="{!sendSurveys}" reRender="">
<apex:param name="selectedSurvey" value=""/>
<apex:param name="sendDateTime" value=""/>
</apex:actionFunction>
<div class="slds-scope">
<div class="slds-grid slds-wrap slds-size_3-of-7">
<main class="slds-col slds-size--1-of-1 slds-small-size--1-of-2 slds-medium-size--8-of-12 slds-large-size--4-of-5">
<div class="slds-form-element">
<label class="slds-form-element__label" for="select-01">Select A Survey</label>
<div class="slds-form-element__control">
<div class="slds-select_container">
<select id="selectedSurvey" class="slds-select">
<apex:repeat value="{!surveys}" var="survey">
<option value="{!survey.value}">{!survey.label}</option>
</apex:repeat>
</select>
</div>
</div>
</div>
<br />
<div class='slds-form-element'>
<label class="slds-form-element__label" for="select-01">Select Date/Time</label>
<div class='input-group date' id='datetimepicker'>
<input id="sendDateTime"
type='text'
class="form-control"
readonly="true"
/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<button onclick="submitForm(); return false;">Send Survey</button>
</main>
</div>
</div>
</apex:form>
</apex:page>
/***********************************
Touchpoint Solutions
author: Tyler Zika
link: http://www.touchpointcrm.com
email: support@touchpointcrm.com
date: 07/03/17
description: get picklist values
***********************************/
public with sharing class ContactMassSendSurveyController
{
private ApexPages.StandardSetController standardSetController;
// constructor.
public ContactMassSendSurveyController(ApexPages.StandardSetController standardSetController)
{
this.standardSetController = standardSetController;
}
// get a list of surveys for the form picklist
public List<SelectOption> getSurveys() {
List<SelectOption> surveys = new List<SelectOption>();
for (SEO__CSEO_Survey__c s : [SELECT Id, Name FROM SEO__CSEO_Survey__c])
{
surveys.add(new SelectOption(s.Id , s.Name));
}
return surveys;
}
public PageReference sendSurveys()
{
List<Contact> selectedContacts = (List<Contact>) standardSetController.getSelected();
String selectedSurvey = Apexpages.currentPage().getParameters().get('selectedSurvey');
DateTime sendDateTime = DateTime.parse(Apexpages.currentPage().getParameters().get('sendDateTime'));
// an empty list to add records to
List<SEO__Survey_Sent__c> surveysToSend = new List<SEO__Survey_Sent__c>();
for(Contact c : selectedContacts) {
SEO__Survey_Sent__c s = new SEO__Survey_Sent__c ();
s.SEO__Contact__c = c.Id;
s.SEO__Survey__c = selectedSurvey;
s.SEO__Cust_Survey_Email__c = [SELECT Email FROM Contact WHERE Id =: c.Id].Email;
s.SEO__Survey_Sent__c = sendDateTime;
surveysToSend.add(s);
}
insert surveysToSend;
return standardSetController.cancel();
}
}
@isTest
public class ContactMassSendSurveyControllerTest {
static testMethod void massSendSurvey(){
//Create Test Data
Contact c = new Contact(FirstName='John', LastName='Doe', Email='john@doe.email');
insert c;
SEO__CSEO_Survey__c survey = new SEO__CSEO_Survey__c(Name = 'Test Survey');
insert survey;
Test.startTest();
// load the page
PageReference pageRef = Page.ContactMassSendSurvey;
pageRef.getParameters().put('selectedSurvey', survey.Id);
pageRef.getParameters().put('sendDateTime', '01/01/2001 7:32 PM');
Test.setCurrentPageReference(pageRef);
// load the extension
ContactMassSendSurveyController controller = new ContactMassSendSurveyController(new ApexPages.StandardSetController([SELECT Id FROM Contact]));
controller.getSurveys();
controller.sendSurveys();
Test.stopTest();
}
}