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
RstrunkRstrunk 

Test a VF controller

I'm not sure how to write a test class for a VF page and custom controller.  

 

 

HERE IS THE CONTROLLER

 

public class UploadContentController 
{
    public ContentVersion contentToUpload {get;set;}
    public ContentVersion contentToUpload1 {get;set;}
    Id memberId;
    public Blob fileContent {get;set;}
    public UploadContentController(ApexPages.StandardController controller) 
    {
        memberId = controller.getId();
        contentToUpload = new ContentVersion();
        contentToUpload1  = new ContentVersion();
    }
    public PageReference uploadContents() 
    {
        List<ContentWorkSpace> CWList = [SELECT Id, Name From ContentWorkspace WHERE Name = 'Contract Documents'];
        contentToUpload.VersionData = fileContent;
        insert contentToUpload;
      
        contentToUpload = [SELECT ContentDocumentId,ContractDocumentType__c FROM ContentVersion WHERE Id = :contentToUpload.Id];
        ContentWorkspaceDoc cwd = new ContentWorkspaceDoc();
        cwd.ContentDocumentId = contentToUpload.ContentDocumentId;
        cwd.ContentWorkspaceId = CWList.get(0).Id;
        insert cwd;
        List<Contract> con=[select id,accountid from contract where id=:memberId limit 1];
        contentToUpload.ContractDocumentType__c=contentToUpload1.ContractDocumentType__c;
        contentToUpload.RelatedAccount__c=con[0].accountid;
        contentToUpload.RelatedContract__c=con[0].id;
        update contentToUpload;
        PageReference pg = new PageReference('/' + memberId);
        pg.setRedirect(true);
        return pg;
    }
}

 

 

HERE IS THE VF PAGE

 

<apex:page standardController="Contract" extensions="UploadContentController">
<apex:sectionHeader title="Upload to Contract Library" subtitle="Upload Approved Contracts only."/>

<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection title="Upload a File">
        
            <apex:panelGrid columns="2">
                Select File :
                <apex:inputFile fileName="{!contentToUpload.PathOnClient}" value="{!fileContent}"/>
           </apex:panelGrid>
           <apex:panelGrid columns="2">
           Contract Document Type:
           <apex:inputField id="pick" value="{!contentToUpload1.ContractDocumentType__c}"/>
           </apex:panelGrid>

        </apex:pageBlockSection>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Upload" action="{!uploadContents}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

I can write tests for triggers and such, but I am not too savvy on VF.

 

Any help would be appreciated.

 

Thanks, 

GlynAGlynA

Hey Robert,

 

The trick is knowing how to construct your controller extension.

 

// you can include parameters in the URL if your extension uses them
Test.setCurrentPage( new PageReference( '/apex/<your URL here>' ) );

// initialize a standard controller with your test record
ApexPages.StandardController sc = new ApexPages.StandardController( <your sObject record here> );

// create an extension instance using the standard controller
UploadContentController myExt = new UploadContentController( sc );

// invoke the methods of the extension to test them
PageReference page = myExt.uploadContents();

// you can also write to extension properties to simulate
// the user input on the page

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator