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
softcloud2009softcloud2009 

Test Class for ApexPages.KnowledgeArticleVersionStandardController

Is there an example on how to write a test class for this controller: Apex Customization for Submitting Articles from Cases

 

public class AgentContributionArticleController {
    // The constructor must take a ApexPages.KnowledgeArticleVersionStandardController as an argument
    public AgentContributionArticleController(ApexPages.KnowledgeArticleVersionStandardController ctl) {
        SObject article = ctl.getRecord();   //this is the SObject for the new article. 
                                             //It can optionally be cast to the proper article type, e.g. FAQ__kav article = (FAQ__kav) ctl.getRecord();
        
        String sourceId = ctl.getSourceId(); //this returns the id of the case that was closed.
        Case c = [select subject, description from Case where id=:sourceId];
        
        article.put('title', 'From Case: '+c.subject);  //this overrides the default behavior of pre-filling the title of the article with the subject of the closed case. 
        article.put('Details__c',c.description);  
        
        ctl.selectDataCategory('Geography','USA');  //Only one category per category group can be specified.
        ctl.selectDataCategory('Topics','Maintenance');                        
    }

 

https://login.salesforce.com/help/doc/en/knowledge_caseclose_apex.htm

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
softcloud2009softcloud2009

its available in the VisualForce Developer books:

 

public static testMethod void testAgentContributionArticleController() {

String caseSubject = 'my test';

String caseDesc = 'my test description';

Case c = new Case();

c.subject= caseSubject;

c.description = caseDesc;

insert c;

String caseId = c.id;

System.debug('Created Case: ' + caseId);

ApexPages.currentPage().getParameters().put('sourceId', caseId);

ApexPages.currentPage().getParameters().put('sfdc.override', '1');

ApexPages.KnowledgeArticleVersionStandardController ctl =

new ApexPages.KnowledgeArticleVersionStandardController(new FAQ__kav());

new AgentContributionArticleController(ctl);

System.assertEquals(caseId, ctl.getSourceId());

System.assertEquals('From Case: '+caseSubject, ctl.getRecord().get('title'));

System.assertEquals(caseDesc, ctl.getRecord().get('details__c'));

}

All Answers

softcloud2009softcloud2009

its available in the VisualForce Developer books:

 

public static testMethod void testAgentContributionArticleController() {

String caseSubject = 'my test';

String caseDesc = 'my test description';

Case c = new Case();

c.subject= caseSubject;

c.description = caseDesc;

insert c;

String caseId = c.id;

System.debug('Created Case: ' + caseId);

ApexPages.currentPage().getParameters().put('sourceId', caseId);

ApexPages.currentPage().getParameters().put('sfdc.override', '1');

ApexPages.KnowledgeArticleVersionStandardController ctl =

new ApexPages.KnowledgeArticleVersionStandardController(new FAQ__kav());

new AgentContributionArticleController(ctl);

System.assertEquals(caseId, ctl.getSourceId());

System.assertEquals('From Case: '+caseSubject, ctl.getRecord().get('title'));

System.assertEquals(caseDesc, ctl.getRecord().get('details__c'));

}

This was selected as the best answer
StephenDavidsonStephenDavidson

I have modified AgentCOntributionArticleController as follows:

 

public class AgentContributionArticleController {
    // The constructor must take a ApexPages.KnowledgeArticleVersionStandardController as an argument
    public AgentContributionArticleController(ApexPages.KnowledgeArticleVersionStandardController ctl) {
        SObject article = ctl.getRecord();   
		String sourceId = ctl.getSourceId();
        Case c = [select subject, description from Case where id=:sourceId];
//  I added this code -- begin
        CaseComment cc = [select LastModifiedDate, LastModifiedBy.Id, LastModifiedBy.Name, IsPublished, CreatedDate, CreatedBy.Id, CreatedBy.Name, CommentBody From CaseComment where ParentId = :sourceId order by LastModifiedDate desc limit 1];
        //check article type with if statement
	    if (article instanceof troubleshootingobj__kav){
            System.Debug('in troubleshootingobj__kav if'); 
            String [] all = cc.CommentBody.split(':',-1);
            if (all.size() != 4 ){
                article.put('title','Some instructions go here...');
                article.put('summary','Some instructions go here...');
                article.put('symptom__c','Some instructions go here...');
                article.put('solution__c','Some instructions gohere...');

                }
            else {
                for (Integer i = 0; i < all.size(); i++) {
                    if (i == 0){
                      article.put('title',all.get(i));
                    }
                    else if (i==1){
                      article.put('summary',all.get(1));
                    }
                    else  if (i == 2){
                    article.put('Symptom__c',all.get(2));
                    }
                    else  if (i == 3){
                    article.put('Solution__c',all.get(3));
                    }
                  }
                } 
           }
// I added this code -- end
    article.put('title', 'From Case: '+c.subject);  //this overrides the default behavior of pre-filling the title of the article with the subject of the closed case. 
    article.put('Details__c',c.description);  
              
}

 Can anyone provide some guidance as to how to modify the default test class for the changes I have made?

 

Here is the default test class. 

public static testMethod void testAgentContributionArticleController() {
String caseSubject = 'my test';
String caseDesc = 'my test description';
Case c = new Case();
c.subject= caseSubject;
c.description = caseDesc;
insert c;
String caseId = c.id;
System.debug('Created Case: ' + caseId);
ApexPages.currentPage().getParameters().put('sourceId', caseId);
ApexPages.currentPage().getParameters().put('sfdc.override', '1');
ApexPages.KnowledgeArticleVersionStandardController ctl =
new ApexPages.KnowledgeArticleVersionStandardController(new FAQ__kav());
new AgentContributionArticleController(ctl);
System.assertEquals(caseId, ctl.getSourceId());
System.assertEquals('From Case: '+caseSubject, ctl.getRecord().get('title'));
System.assertEquals(caseDesc, ctl.getRecord().get('details__c'));
}

 I have been trying to figure it out myself but I have enough on my hands actually writing the code.  Test code makes my head spin...  ANy hints or some direction on this testing, totally appreciated.

 

StephenDavidsonStephenDavidson

Ok.  SO how much would someone charge me to either write the tests or to provide enough guidance to get me up and running? 

StephenDavidsonStephenDavidson

Seriously.  Maybe a one hour joint session.  Help me get a good test class and I pay you and learn and you teach me and delight at what a wonderful student I am.  WIn/win!