tylerzika
7/20/2017 - 11:04 PM

standard set test

standard set test

@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;
      
        // 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();
    }
}
public with sharing class ContactMassSendSurveyController 
{    
    private ApexPages.StandardSetController standardController;
    
    // constructor.
    public ContactMassSendSurveyController(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        // when page loads, load the date/time picker the current time.
		//sendSurveyform = new SEO__Survey_Sent__c(SEO__Survey_Sent__c = Datetime.now());
    }
    
    // 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>) standardController.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 standardController.cancel();   
    }
}