+ Start a Discussion
Haseeb Ahmad 9Haseeb Ahmad 9 

Salesforce lightning component controller apex test class

Hi Everyone,

Having some issues creating a test class for the controller.
public without sharing class DeleteFilesCtrl {
    public static List<FilesWrap> getFiles(string objectId){
        List<FilesWrap> wrapList = new List<FilesWrap>();
    	List<ContentDocumentLink> cdList = [SELECT ContentDocument.Title,ContentDocument.FileType,ContentDocument.LastModifiedDate , ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =:objectId];
        for(ContentDocumentLink cd : cdList){
            if(cd.ContentDocument.FileType != 'SNOTE'){
        		wrapList.add(new FilesWrap(cd));
        return wrapList;
    public static string deleteFiles(string objectId,string contentDocumentId){
    	List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId=:contentDocumentId];
        	if(cdlList.size() <= 2){
               delete [SELECT Id FROM ContentDocument WHERE Id = :contentDocumentId];
                delete [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId=:objectId AND ContentDocumentId=:contentDocumentId];    
        }catch(Exception ex){
            return ex.getMessage();
        return 'Success';
    public class FilesWrap{
        @AuraEnabled public String title;
        @AuraEnabled public String fileType;
        @AuraEnabled public String id;
        @AuraEnabled public DateTime lastModifiedDate;
        public FilesWrap(ContentDocumentLink cd){
            title = cd.ContentDocument.Title;
            id = cd.ContentDocumentId;
            fileType = cd.ContentDocument.FileType;
            lastModifiedDate= cd.ContentDocument.LastModifiedDate;

I am have tried many ways but either I am not getting any coverages or getting errors. Would love some helpful insight on how to create a test for this. thank you. 
Best Answer chosen by Haseeb Ahmad 9
Haseeb Ahmad 9Haseeb Ahmad 9
This is resolve by following test class:
public class DeleteFilesCtrlTest  {
    static void DeleteFilesCtrl()
        Account acct = new Account(Name = 'TEST_ACCT');
        insert acct;

        Account acct1 = new Account(Name = 'TEST_ACCT1');
        insert acct1;
        acct = [select name from account where id = :acct.id];
        system.assertEquals('TEST_ACCT', acct.name);
        ContentVersion contentVersion = new ContentVersion(
            Title = 'Penguins',
            PathOnClient = 'Penguins.jpg',
            VersionData = Blob.valueOf('Test Content'),
            IsMajorVersion = true
        insert contentVersion;  
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

        //create ContentDocumentLink  record
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.LinkedEntityId = acct.Id;
        cdl.ContentDocumentId = documents[0].Id;
        cdl.ShareType = 'V';
        insert cdl;

        ContentDocumentLink cd2 = new ContentDocumentLink();
        cd2.LinkedEntityId = acct1.Id;
        cd2.ContentDocumentId = documents[0].Id;
        cd2.ShareType = 'V';
        insert cd2;

        // CAlling second time to cover the if and else part.
        DeleteFilesCtrl.deleteFiles(acct.Id, documents[0].Id);
        DeleteFilesCtrl.deleteFiles(acct.Id, documents[0].Id);