+ Start a Discussion
Luke Higgins 22Luke Higgins 22 

Need help with a test class

Hi, 

I've been trying to write a test class for this Apex class that is the controller for a lightning:fileUploader that uploads files and shows a list of the uploaded files. I've worked on it for a while now and nothing has worked so far. Can someone give me some assistance? 

Apex class:
public class fileUploaderClass {  
   @AuraEnabled  
   public static List<ContentDocument> getFiles(string recordId){  
     List<ContentDocument> DocumentList = new List<ContentDocument>();  
     Set<Id> documentIds = new Set<Id>();  //store file ids
     List<ContentDocumentLink> cdl=[select id,LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId=:recordId];  
     for(ContentDocumentLink cdLink:cdl){  
       documentIds.add(cdLink.ContentDocumentId);  // Document ids
     }      
     DocumentList = [select Id,Title,FileType,ContentSize,Description from ContentDocument where id IN: documentIds];  
     return DocumentList;  
   }  
   @AuraEnabled  
   public static List<ContentDocument> UpdateFiles(string documentId,string title,string recordId){  
     system.debug('title: ' +title);  
     ContentDocument cd = [select id,title from ContentDocument where Id=:documentId]; // Getting files from Parent record 
     cd.Title = title;  // Changing file Title with user entered title
     try{  
       update cd;  // Update ContentDocument (File)
     }  
     catch(DMLException e){  
       system.debug('Exception has occurred! ' +e.getMessage());  
     }  
      List<ContentDocument> DocumentList = new List<ContentDocument>();  
     Set<Id> documentIds = new Set<Id>();  
     List<ContentDocumentLink> cdl=[select id,LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId=:recordId];  
     for(ContentDocumentLink cdLink:cdl){  
       documentIds.add(cdLink.ContentDocumentId);  
     }      
     DocumentList = [select Id,Title,FileType,ContentSize,Description from ContentDocument where id IN: documentIds];  
     return DocumentList;  // Return list of files on parent record
   }  
 }

 
Best Answer chosen by Luke Higgins 22
Uttpal chandraUttpal chandra
Hi Luke,

Just copy and paste below code in your org.
 
@isTest
public class fileUploaderClassTest {
		
      static testMethod void testMethod1() 
 	{
        Account testAccount = new Account();
		testAccount.Name='Test Account' ;
		insert testAccount;

		ContentVersion contentVersionInsert = new ContentVersion(
            Title = 'Test',
            PathOnClient = 'Test.jpg',
            VersionData = Blob.valueOf('Test Content Data'),
            IsMajorVersion = true
        );
        insert contentVersionInsert;
        
        ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
        ContentDocument documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument Limit 1];
       
        
        fileUploaderClass .getFiles(testAccount.id);
       fileUploaderClass .UpdateFiles(documents.id,documents.Title,testAccount.Id);
 	}
}


Kindly mark it as solved if it  solve your query. 

Thanks and Regards,
Uttpal Chandra