1

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;
}
}

1 Answer 1

2

You can actually test this code in a way that's pretty similar to how you would test any other code that performs DML - that is, you synthesize its inputs in your test class, you execute the code, and then you query against the database to find the changes you expect to have taken place and write assertions against them.

The chief difficulty with Content-related objects here is that you can't write a query against ContentDocumentLink that doesn't have filters on it, but that won't hold you back because you have both its ContentDocumentId and LinkedEntityId available for querying.

In loose pseudocode, your test class should look something like

@isTest
public class myTest {
    @isTest
    public static void controller_uploads_and_links_file() {
        // Insert a new Candidate__c record.
        Candidate__c c = new Candidate__c();
        // populate its values...
        insert c;

        FileUploaderController f = new FileUploaderController(new ApexPages.StandardController(c));

        f.conVer.VersionData = Blob.valueOf('Test Data');

        Test.startTest();
        f.uploadFile();
        Test.stopTest();

        // Now, query for the resulting files and validate them.
        List<ContentDocumentLink> cdls = [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :c.Id];
        System.assertEquals(1, cdls.size(), 'exactly 1 linked file');
        // Write other validations here...
    }
}

Note that that covers the "happy path" of your code. You've also got some error pathways and another method to cover, and you could construct tests to cover those pathways using similar techniques.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .