0

This code is a controller for my VF Page. It inserts ContentVersion and ContentDocumentLink. After that a candidate field Photo__c take a ContentVersion.Id value. How to check it correctly?

Now Photo__c is null.

Controller:

public class FileUploaderController {

public ContentVersion conVer {get; set;}
public ContentDocumentLink conDocLink {get; set;}
public Candidate__c thisCandidate{get; set;}
public String errorMsg{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) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, Label.Error_No_File));
        conVer.VersionData = null;
        return null;
    }

    String path = conVer.PathOnClient;
    Set<String> acceptedExtensions = new Set<String> {'.jpeg', '.jpg', '.png', '.tiff', '.tif', '.bmp', '.jpe', '.jfif'};
    Boolean found = false;

    for(String ext : acceptedExtensions){
        if(found = path.endsWith(ext)){
            break;
        }   
    }
    if(!found){
        ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, Label.Error_Extension));
        conVer.VersionData = null;
        return null; 
    }

    conVerList.add(conVer);
    try {
        insert conVerList;              
    } catch(Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
    }

    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);
        try {
            insert conDocLinkList;
        } catch(Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
        }
    } 
    conVer.VersionData = null;
    return null;
}

public PageReference deleteFile() {

    thisCandidate.Photo__c = null;

    try {
        update thisCandidate;
    } catch(Exception e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, e.getMessage()));
    }
    conVer = new ContentVersion();
    return null;
}
}

TestClass:

@isTest
private class FileUploaderTest {


@isTest
static void insertFile(){

    Candidate__c cand = new Candidate__c(
        First_Name__c = '111'
    );
    insert cand;

    ContentVersion conVer = new ContentVersion(
        Title = 'Name',
        PathOnClient = 'test',
        VersionData = Blob.valueof('New Bitmap Image.bmp')
    );
    List<ContentVersion> conVerList = new List<ContentVersion>(); 
    conVerList.add(conVer); 
    insert conVerList;
    update cand;

    Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =: conVer.Id].ContentDocumentId;

    ContentDocumentLink conDocLink = new ContentDocumentLink(
        LinkedEntityId = cand.Id,
        ContentDocumentId = conDoc,
        ShareType = 'I'
    );
    List<ContentDocumentLink> conDocLinkList = new List<ContentDocumentLink>();
    conDocLinkList.add(conDocLink);
    insert conDocLink;

    Candidate__c cand2 = [SELECT Id, Photo__c FROM Candidate__c WHERE First_Name__c = '111' LIMIT 1];
    System.assertEquals(cand2.Photo__c, conVer.Id);
}
}
1

So your test class doesn't even use this "FileUploadController" at all from what i see in your post above.

What you will want to do is write test coverage that makes use of your Extension and will cover asserting the proper errors are given when you reach them. Something similar to below would work to generate proper coverage and assert that your errors are indicated in your test class.

@IsTest
private class FileUploaderTest {

    @TestSetup
    static void setupTestData(){
        Candidate__c c = new Candidate__c(
            First_Name__c = '111'
        );

        insert c;
    }

    static testMethod void TestFileUploadControllerFileUploadSuccess(){
        List<Candidate__c> candidates = [   SELECT Id, First_Name__c, Photo__c
                                            FROM Candidate__c
                                            WHERE First_Name__c =: '111' ];
        System.assertEquals( 1, candidates.size() );

        ApexPages.StandardController controller = new ApexPages.StandardController( candidates[ 0 ] );
        PageReference pg = Page.TestPage; // your page here 

        Test.setCurrentPageReference( pg );

        FileUploaderController ext = new FileUploaderController( controller );

        Blob vData = Blob.valueOf( 'Test Image File.bmp' );
        ext.conVer.VersionData = vData;
        ext.conVer.PathOnClient = 'Test Image File.bmp';

        PageReference uploadresult = ext.uploadFile();
        System.assertNotEquals( true, ApexPages.hasMessages() );    // assert all went smooth - no messages here

        List<Candidate__c> verify = [   SELECT Id, First_Name__c, Photo__c
                                        FROM Candidate__c
                                        WHERE First_Name__c =: '111' ];
        System.assertEquals( 1, verify.size() );
        // assert the value is on the record
        System.assertEquals( ext.conVer.Id, verify[ 0 ].Photo__c );

    }

    static testMethod void TestFileUploadControllerFileUploadNoData(){
        List<Candidate__c> candidates = [   SELECT Id, First_Name__c, Photo__c
        FROM Candidate__c
        WHERE First_Name__c =: '111' ];
        System.assertEquals( 1, candidates.size() );

        ApexPages.StandardController controller = new ApexPages.StandardController( candidates[ 0 ] );
        PageReference pg = Page.TestPage;

        Test.setCurrentPageReference( pg );

        FileUploaderController ext = new FileUploaderController( controller );

        Blob vData = Blob.valueOf( 'Test Image File.bmp' );
        ext.conVer.VersionData = null;

        PageReference uploadresult = ext.uploadFile();
        System.assertEquals( true, ApexPages.hasMessages() );    // assert errors were given
        List<ApexPages.Message> messages = ApexPages.getMessages();
        System.assertNotEquals( null, messages );
        System.assertEquals( 1, messages.size() );
        System.assertEquals( true, messages[ 0 ].getDetail().containsIgnoreCase( Label.Error_No_File ));

    }

    static testMethod void TestFileUploadControllerFileUploadBadExtension(){
        List<Candidate__c> candidates = [   SELECT Id, First_Name__c, Photo__c
        FROM Candidate__c
        WHERE First_Name__c =: '111' ];
        System.assertEquals( 1, candidates.size() );

        ApexPages.StandardController controller = new ApexPages.StandardController( candidates[ 0 ] );
        PageReference pg = Page.TestPage;

        Test.setCurrentPageReference( pg );

        FileUploaderController ext = new FileUploaderController( controller );

        Blob vData = Blob.valueOf( 'Test Image File.doc' );
        ext.conVer.VersionData = vData;
        ext.conVer.PathOnClient = 'Test Image File.doc';

        PageReference uploadresult = ext.uploadFile();
        System.assertEquals( true, ApexPages.hasMessages() );    // assert errors were given
        List<ApexPages.Message> messages = ApexPages.getMessages();
        System.assertNotEquals( null, messages );
        System.assertEquals( 1, messages.size() );
        System.assertEquals( true, messages[ 0 ].getDetail().containsIgnoreCase( Label.Error_Extension )); // your error is here 

    }

}

Here you will see this will make use of your controller and test your upload method appropriately.

NOTE I also took notice to your deleteFile method. all it appears you are doing is removing the value on the Candidate__c record. I assume that you are triggering to remove the file as well from the system? seemed a little confusing given it's method name that the result wasn't really doing much along the lines there. You would need to build some coverage in this case to test that as well if you are. I would recommend that your test cases also test the negative test cases appropriately so that you can ensure that you are actually removing ALL of your intended items / on error that you are ensuring that your output is at least handled properly.

  • 1
    Thank you so much! I just can't inderstand how to make bulkification test – MichaelLev19 Oct 1 '18 at 8:36
  • ask another question and let's see what you've tried and maybe we can help you figure that out. – Ronnie Oct 2 '18 at 3:11
1

You have not called the method uploadFile() in your test class, that is the reason behind the below code is not getting covered and you are getting the null value for Photo__c.

thisCandidate.Photo__c = conVer.Id;
    update thisCandidate; 

You need to add below code to your test class:-

    PageReference pageRef = Page.your_page_name;
            Test.startTest();
            Test.setCurrentPage(pageRef);
            pageRef.getParameters().put('id',cand.Id);
            ApexPages.StandardController sc = new ApexPages.standardController(cand);
            FileUploaderController controller= new FileUploaderController (sc);
            controller.uploadFile();
            Test.stopTest();

What I am doing here is creating a page reference for your page and setting the record and creating standard Controller and then Create one Instance of Your Class and then called the method.

  • Thank you. But Static method cannot be referenced from a non static context: System.PageReference FileUploaderController.uploadFile() – MichaelLev19 Sep 28 '18 at 12:07
  • CAn you update your question with the test class you tried with my piece of code? – sanket kumar Sep 28 '18 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.