+ Start a Discussion
Simon234Simon234 

What kind of test can I write to check the file?

Hello. I have a controller for VF Page. This code upload a profile's image (we can see it after that) and delete it. What kind of test can help me to check this file? I need to know, is it uploaded or not.

I work with ContentVersion  and ContentDocumentLink :

public class FileUploaderController {
    
    public ContentVersion conVer {get; set;}
    public ContentDocumentLink conDocLink {get; set;}
    public Candidate__c thisCandidate{get; set;}
   
    public FileUploaderController(ApexPages.StandardController controller){
        thisCandidate = (Candidate__c)controller.getRecord();
        conVer = new ContentVersion();
    }

    public PageReference uploadFile() {
        
        List<ContentVersion> conVerList = new List<ContentVersion>();
        List<ContentDocumentLink> conDocLinkList = new List<ContentDocumentLink>();
        
        if (conVer.VersionData == null) {
            conVer.VersionData = null;
            return null;
        }
        conVerList.add(conVer);
        insert conVerList;              
        thisCandidate.Photo__c = conVer.Id;
        update thisCandidate; 
        
        if (conVer.ContentDocumentId == null) {
            
            Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: conVer.Id].ContentDocumentId;
            conDocLink = new ContentDocumentLink(
                LinkedEntityId = thisCandidate.Id,
                ContentDocumentId = conDoc,
                ShareType = 'I'
            );
            conDocLinkList.add(conDocLink);
            insert conDocLinkList;
        } 
        conVer.VersionData = null;
        return null;
    }

    public PageReference deleteFile() {
        thisCandidate.Photo__c = null;
        update thisCandidate;
        conVer = new ContentVersion();
        return null;
    }
}
Best Answer chosen by Simon234
Raj VakatiRaj Vakati
Pls use this test class
 
@IsTest
public class FileUploaderControllerTest {
    
    static testMethod void validateHelloWorld()
    {
        System.Test.startTest() ;
        Candidate__c can = new Candidate__c(Name = 'Wholesale');
        insert can;
        
        
        ApexPages.StandardController sc = new ApexPages.StandardController(can);
        FileUploaderController conT = new FileUploaderController(sc);
        
        PageReference pageRef = Page.canpage;
        pageRef.getParameters().put('id', String.valueOf(can.Id));
        System.Test.setCurrentPage(pageRef);
        
        Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');

        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';           
        cv.VersionData =beforeblob;          
        insert cv;         

        ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];

        

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=can.id;
        contentlink.ShareType= 'V';
        contentlink.LinkedEntityId = can.Id; 
        contentlink.ContentDocumentId=testcontent.ContentDocumentId;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
        conT.uploadFile();
        conT.deleteFile();
        
        System.Test.stopTest();
    }
}