tylerzika
7/31/2017 - 6:58 PM

controller.java

@istest
public class testTakeSurveyController
{
    
    @testSetup static void methodName() {
		SEO__CSEO_Survey__c cntSurvey = new SEO__CSEO_Survey__c(
            Name = 'Contact survey', 
            SEO__Question_1_Threshold__c = 1, 
            SEO__Survey_Status__c = 'Active', 
            SEO__Survey_Type__c = 'Survey with SEO'
        );     
        
        insert cntSurvey;
        
        SEO__CSEO_Survey_Question__c question1 = new SEO__CSEO_Survey_Question__c(
            SEO__CSEO_Survey__c = cntSurvey.Id, 
            SEO__Order__c = 1, 
            SEO__Question__c = '1'
        );
        SEO__CSEO_Survey_Question__c question2 = new SEO__CSEO_Survey_Question__c(
            SEO__CSEO_Survey__c = cntSurvey.Id, 
            SEO__Order__c = 2, 
            SEO__Question__c = '2'
        ); 
        
        insert new List<SEO__CSEO_Survey_Question__c>{question1, question2}; 
            
        SEO__CSEO_Referral_Site__c googleReferralSite = new SEO__CSEO_Referral_Site__c(
        	Name = 'Google',
            SEO__Site_Name__c = 'Google',
            SEO__CSEO_Survey__c = cntSurvey.Id
        );
        
        insert googleReferralSite;

        Account acct = new Account(Name = 'Acme');
        insert acct;
        
        Contact cnt = new COntact(AccountId = acct.Id, Lastname = 'acme@acme.com', Email = 'acme@acme.com');
        insert cnt;  
        
        SEO__Survey_Sent__c sentRecord = new SEO__Survey_Sent__c(
        	SEO__Contact__c = cnt.Id,
            SEO__Survey__c  = cntSurvey.Id
        );
        
        insert sentRecord;        
    }    
    
    @istest
    static void test()
    {
		SEO__CSEO_Survey__c cntSurvey = [SELECT Id FROM SEO__CSEO_Survey__c LIMIT 1];
        Contact cnt = [SELECT Id FROM Contact LIMIT 1];
        SEO__Survey_Sent__c sentRecord = [SELECT Id FROM SEO__Survey_Sent__c LIMIT 1];
		List<SEO__CSEO_Survey_Question__c> questions = [SELECT Id FROM SEO__CSEO_Survey_Question__c];
        SEO__CSEO_Survey_Response_Header__c userResponse = new SEO__CSEO_Survey_Response_Header__c (
        	SEO__Contact__c = cnt.Id,
            SEO__CSEO_Survey__c = cntSurvey.Id,
            SEO__Testimonial__c = 'Great'
        );
            
        insert userResponse;
        
        Test.startTest();
            Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
            PageReference retPage = new PageReference('/apex/CompleteSurvey?id=' + sentRecord.Id);
            Test.setCurrentPage(retPage);
            TakeSurveyController crl = new TakeSurveyController();
        	crl.SurveyHeader.Testimonial__c = 'Great';
            crl.giveFeedback();
            crl.surveyScoreStr = questions[0].Id + '$4;' + questions[1].Id + '$5';
            crl.takeSurvey(); 
        	//crl.createReferralActivityRecord();
        Test.stopTest();        

                
    }
}
public class TakeSurveyController
{
    private String headerId = ApexPages.currentpage().getParameters().get('header');	
    private String surveySentId = ApexPages.currentpage().getParameters().get('id');

    public SEO__CSEO_Survey__c Survey { get; set; }
    public Id surveyId { get; set; }
    
    public List<SEO__CSEO_Survey_Question__c> SurveyQuestions { get; set; }
    private Map<Id, SEO__CSEO_Survey_Question__c> questionmap = new Map<Id, SEO__CSEO_Survey_Question__c>();
    
    public List<SEO__CSEO_Referral_Site__c> SurveySites { get; set; }
    public SEO__CSEO_Survey_Response_Header__c SurveyHeader { get; set; }
    
    public String surveyScoreStr { get; set; }

    public PageReference init() {
      	SEO__Survey_Sent__c sent = [SELECT SEO__Survey__c, SEO__Contact__c
                                  FROM SEO__Survey_Sent__c
                                  WHERE Id = :surveySentId
                                 ];     
      
        SurveyHeader = new SEO__CSEO_Survey_Response_Header__c();
        Survey = [select Id, name, SEO__Background_Image__c, SEO__Hide_Welcome_Page__c, Question_1_Threshold__c, Survey_Type__c, Survey_Status__c, SEO__No_Testimonial_Thank_You__c, SEO__Survey_Welcome_Heading__c, SEO__Testimonial_Request__c, SEO__Testimonial_Thank_You__c, SEO__Welcome_Text__c from SEO__CSEO_Survey__c where Id =:sent.SEO__Survey__c];
        SurveyQuestions = [select Id, Name, SEO__Low_Rating__c, SEO__High_Rating__c, SEO__CSEO_Survey__c, SEO__Order__c, SEO__Question__c, SEO__Status__c from  SEO__CSEO_Survey_Question__c where SEO__CSEO_Survey__c = :sent.SEO__Survey__c and SEO__Status__c = 'Active' order by SEO__Order__c];
        for(SEO__CSEO_Survey_Question__c ques : SurveyQuestions)
        {
            questionmap.put(ques.Id, ques);
        }
        SurveySites = [select Id, name, SEO__Site_Name__c, Site_URL__c from SEO__CSEO_Referral_Site__c where SEO__CSEO_Survey__c = :sent.SEO__Survey__c];              

        if(headerid != null && headerid != '')
        {
            SurveyHeader = [select Id, Name, SEO__Testimonial__c, SEO__Testimonial_Given__c from SEO__CSEO_Survey_Response_Header__c where Id = :headerId];
        }
        
        // if the current url is the welcome page and Hide_Welcome_Page__c is equal to true,
        // redirect to the page where the user can take the survey
        Boolean isWelcomePage = (ApexPages.currentPage().getUrl().substringBefore('?') == '/apex/SEO__SurveyWelcome');
        if(isWelcomePage && Survey.SEO__Hide_Welcome_Page__c) {
            return giveFeedback();
        }        
        return null;
    }    
    
    public PageReference giveFeedback()
    {
        PageReference retPage = new PageReference('/apex/CompleteSurvey');
        if(surveySentId != null && surveySentId != '')
        {
            retPage.getParameters().put('id', surveySentId);
        }
        retPage.setRedirect(true);
        return retPage;
    }
    
    public PageReference takeSurvey()
    {

        SEO__Survey_Sent__c surveySent = [SELECT Id, SEO__Case__c, SEO__Contact__c, SEO__Lead__c, SEO__Opportunity__c  
                                          FROM SEO__Survey_Sent__c 
                                          WHERE Id =:surveySentId];
        
        SurveyHeader.SEO__CSEO_Survey__c = Survey.Id;
        if(surveySent.SEO__Opportunity__c != null && surveySent.SEO__Contact__c != null)
        {
            SurveyHeader.SEO__Opportunity__c = surveySent.SEO__Opportunity__c;
            SurveyHeader.SEO__Contact__c     = surveySent.SEO__Contact__c;
        }
        else if(surveySent.SEO__Case__c != null && surveySent.SEO__Contact__c != null)
        {
            SurveyHeader.SEO__Case__c = surveySent.SEO__Case__c;
            SurveyHeader.SEO__Contact__c = surveySent.SEO__Contact__c;
            
        }
        else if(surveySent.SEO__Contact__c != null)
        {
            SurveyHeader.SEO__Contact__c = surveySent.SEO__Contact__c;
        } 
        else if(surveySent.SEO__Lead__c != null) {
            SurveyHeader.SEO__Lead__c = surveySent.SEO__Lead__c;
        }
        String testimonial = SurveyHeader.Testimonial__c;
        if(testimonial.length() > 0) {
            SurveyHeader.SEO__Testimonial_Given__c = true;
        }  
        
        insert SurveyHeader;
        
        List<SEO__CSEO_Survey_Response_Answers__c> quesAnswers = new List<SEO__CSEO_Survey_Response_Answers__c>();
        Map<Id, Decimal> questionAnswermap = new Map<Id, Decimal>();
        if(surveyScoreStr != null && surveyScoreStr != '')
        {
            for(String surveyItem : surveyScoreStr.split(';'))
            {
                if(surveyItem != null && surveyItem != '')
                {
                    questionAnswermap.put(surveyItem.split('\\$')[0], decimal.valueOf(surveyItem.split('\\$')[1]));
                    
                }
            }
        }
        for(Id quesId : questionmap.keyset())
        {
            if(questionAnswermap.containsKey(quesId))
            {
                SEO__CSEO_Survey_Question__c thisQuestion = questionmap.get(quesId);
                if(thisQuestion != null)
                {
                    SEO__CSEO_Survey_Response_Answers__c quesAnswer = new SEO__CSEO_Survey_Response_Answers__c(SEO__CSEO_Survey_Response_Header__c = SurveyHeader.Id, SEO__Question__c = thisQuestion.SEO__Question__c, SEO__Answer__c = questionAnswermap.get(quesId));
                    quesAnswers.add(quesAnswer);
                }
            }                
        }
        insert quesAnswers;
        

        PageReference retPage = new PageReference('/apex/ThankYou');
        if(surveySentId != null && surveySentId != '')
        {
            retPage.getParameters().put('id', surveySentId);
        }


        if(SurveyHeader.Id != null)
        {	
          
            retPage.getParameters().put('header', SurveyHeader.Id);       
        }
        retPage.setRedirect(true);
        return retPage;
    }
    
    public void createReferralActivityRecord() {
        String siteName = Apexpages.currentPage().getParameters().get('siteName');
        
        SEO__CSEO_Referral_Site__c referralSite = [SELECT Id FROM SEO__CSEO_Referral_Site__c WHERE Name = :siteName AND SEO__CSEO_Survey__c = :surveyId];

        SEO__Referral_Activity__c newReferralActivityRecord = new SEO__Referral_Activity__c();
        newReferralActivityRecord.SEO__Referral_Site__c	 = referralSite.Id;
        newReferralActivityRecord.SEO__Survey_Response_Header__c = headerId;
        newReferralActivityRecord.SEO__Activity_Type__c = 'Clicked Into';
        newReferralActivityRecord.SEO__Activity_Date_Time__c = DateTime.now();
        insert newReferralActivityRecord;
    }

}