function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RGK.ax912RGK.ax912 

Help with Test Class

Hi

 

I am really struggling trying to write the test class for this class, if anyone can help  or point me in the right direction it would be greatly appreciated.

 

Thanks

 

public class CaseArticleSfdcController{
    Case record;
    CaseArticle[] articles;
    String[] caseArticleIds;
    String fields;
    String caselanguage;
    Public String zzLang;
    Map<Id,List<FAQ__kav>> faqs;
   
    public Map<Id,List<FAQ__kav>> getFaqs(){
    zzLang=Caselanguage;
        if(faqs==null){
            if( hasArticles() ){
                String flds='';
                if( hasFields() ) flds=','+String.join(getFieldsArray(),',');
                String selectClause='SELECT Id'+flds+' ';
                String fromClause='FROM FAQ__kav ';
                String whereClause='WHERE Language = \'' + zzLang +'\' and IsVisibleInPkb= true and PublishStatus=\'Online\' and KnowledgeArticleId IN (\''+ String.join(getKnowledgeArticleIds(),'\',\'') +'\')';

//                String whereClause='WHERE Language = \'en_US\' and PublishStatus=\'Online\' and KnowledgeArticleId IN (\''+ String.join(getKnowledgeArticleIds(),'\',\'') +'\')';
               

                String qry=selectClause    +    fromClause    +    whereClause;
                Map<Id,List<FAQ__kav>> faqMap=new Map<Id,List<FAQ__kav>>();
                List<Faq__kav> results;
                system.debug('-----qry: '+qry);
                try{
                    results=database.query(qry);
                }catch(CaseArticleException e){
                   
                    ApexPages.addMessages(e);
                }
                if(results!=null){
                    for(Faq__kav f : results){
                        if(!faqMap.containsKey(f.KnowledgeArticleId)) faqMap.put(f.KnowledgeArticleId,new List<FAQ__kav>());
                        faqMap.get(f.KnowledgeArticleId).add(f);
                    }
                    faqs=faqMap;
                }
            } else ApexPages.addMessages(new CaseArticleException('Missing articles'));
        }
        return faqs;
    }
    public String[] getCaseArticleIds(){
        return caseArticleIds;
    }
    public void setCaseArticleIds(String[] s){
        caseArticleIds=s;
    }
    private List<String> getKnowledgeArticleIds(){
        if(    hasArticles()    ){
            List<String> ids=new List<String>();
            for(CaseArticle ca : getArticles())
                ids.add(ca.KnowledgeArticleId);
            return ids;
        } return null;
    }
    public Boolean hasArticles() {
        return getArticles()!=null;
    }
    public Case getRecord(){
        return record;
    }
    public void setRecord(Case c){
        record=c;
    }
    private CaseArticle[] getArticles(){
        if( articles==null ) {
            try{
                articles=[select id, KnowledgeArticleId,CaseId from CaseArticle where CaseId=:record.id];
            } catch(CaseArticleException e){
                ApexPages.addMessages(e);
            }
        }
        return articles;
    }
    private String[] getFieldsArray(){
        if(getFields()!=null)    return getFields().split(',');
        return null;
    }
    public Boolean hasFields(){
        return getFields()!=null;
    }
    public String getFields(){
        return fields;
    }
    public void setFields(String s){
        fields=s;
    }
   
    public String getCaseLanguage(){
        return caselanguage;
    }
    public void setCaseLanguage(String s){
        caselanguage=s;   
    }
    public class CaseArticleException extends Exception{}

  public static Boolean isRunningTest {
    set;
    get {
        if (isRunningTest == null) isRunningTest = false;
        return isRunningTest;
    }
  }   

Jeff MayJeff May

In your test method (which can be in the same Apex file as the controller, you can create an instance of the Page being controlled, then create an instance of the controller. From there, you can call the public controller methods.

 

Here is a general snippet:

 

   @IsTest (SeeAllData=true)
    static void controllerTest(){
        
        SomeObject__c obj = [SELECT Id, Name from SomeObject__c limit 1];

        // load the page       
        PageReference pageRef = Page.myPage;
        pageRef.getParameters().put('Id',obj.Id);
        Test.setCurrentPageReference(pageRef);
        
        // load the extension
        myController ctlr = new myController(new ApexPages.StandardController(obj));
        
        // test the method
        ctlr.doWork();
        ctlr.doSomethingElse();
        
     }

 

RGK.ax912RGK.ax912

Hi

 

Thanks for the suggestion, but in this case I am calling this from a visualforce email template so there is no page involved??

 

 

Jeff MayJeff May

The approach will be the same.  Pass whatever object or parameters your VF uses.