function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Connor CainConnor Cain 

Apex Test Class for ContentDownloadHandler method

Ive created a Apex class that creates a record when a document is downloaded to have record that someone downloaded it, here is the code:
public class *method* implements Sfc.ContentDownloadHandlerFactory {

    public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
    	Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
        customObject__c um = new customObject__c();
  		um.User_Name__c = UserInfo.getName();
  		um.Access_Date__c = datetime.now();
  		um.Record_ID__c = ids.get(0);
    	        um.Type__c = 'File Download';
  		insert um;
        contentDownloadHandler.isDownloadAllowed = true;
        return contentDownloadHandler;
    }
}

how would I write a Test Class to simulate a User Downloading a File?
Best Answer chosen by Connor Cain
Raj VakatiRaj Vakati
try this  . you will get 100 %
 
@isTest
private class downloadmetodtest {
    private static testMethod void testCase_Upload() {
        Account testAccount = new Account(Name='TestAccount');
        insert testAccount;
        Attachment testAttachment = new Attachment(
            Name='test12345',
            Description='testDescription',
            ParentId=testAccount.Id);
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        testAttachment.body=bodyBlob;
        insert testAttachment;
        
        ContentVersion testContent = new ContentVersion(
            Title='test12345',
            Description='testDescription',
            ContentLocation = 'S',
            PathOnClient = 'test12345',
            VersionData = bodyBlob);
        insert testContent;
        
        ContentDocumentLink testLink = new ContentDocumentLink(
            ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:testContent.Id].ContentDocumentId,
            LinkedEntityId = testAccount.Id,
            ShareType = 'V');
        insert testLink;
        yourclassname f = new yourclassname() ;
        f.getContentDownloadHandler(new List<Id>{testLink.Id}, null);
    }
}

 

All Answers

Raj VakatiRaj Vakati
try this  . you will get 100 %
 
@isTest
private class downloadmetodtest {
    private static testMethod void testCase_Upload() {
        Account testAccount = new Account(Name='TestAccount');
        insert testAccount;
        Attachment testAttachment = new Attachment(
            Name='test12345',
            Description='testDescription',
            ParentId=testAccount.Id);
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        testAttachment.body=bodyBlob;
        insert testAttachment;
        
        ContentVersion testContent = new ContentVersion(
            Title='test12345',
            Description='testDescription',
            ContentLocation = 'S',
            PathOnClient = 'test12345',
            VersionData = bodyBlob);
        insert testContent;
        
        ContentDocumentLink testLink = new ContentDocumentLink(
            ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:testContent.Id].ContentDocumentId,
            LinkedEntityId = testAccount.Id,
            ShareType = 'V');
        insert testLink;
        yourclassname f = new yourclassname() ;
        f.getContentDownloadHandler(new List<Id>{testLink.Id}, null);
    }
}

 
This was selected as the best answer
Connor CainConnor Cain
thank you Raj, this is amazing, but when im changing "yourclassname" to the class that i have getContentDownloadHandler im getting "Invalid type: my class name".
Raj VakatiRaj Vakati
can u share the screenshot ??
Connor CainConnor Cain
@isTest
public class UserViewFilesTest {
    
    public static testMethod void testforFile(){
        Account testAccount = new Account(Name='TestAccount');
        insert testAccount;
        Attachment testAttachment = new Attachment(
            Name='test12345',
            Description='testDescription',
            ParentId=testAccount.Id);
        Blob bodyBlob = Blob.valueOf('Unit Test Attachment Body');
        testAttachment.body=bodyBlob;
        insert testAttachment;
        
        ContentVersion testContent = new ContentVersion(
            Title='test12345',
            Description='testDescription',
            ContentLocation = 'S',
            PathOnClient = 'test12345',
            VersionData = bodyBlob);
        insert testContent;
        
        ContentDocumentLink testLink = new ContentDocumentLink(
            ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =:testContent.Id].ContentDocumentId,
            LinkedEntityId = testAccount.Id,
            ShareType = 'V');
        insert testLink;
        UserViewFile f = new UserViewFile();
        f.getContentDownloadHandler(new List<Id>{testLink.Id}, null);
    }
}
Error:  Line 28 Invalid type: UserViewFile
 
Raj VakatiRaj Vakati
Give me UserViewFile class code also 
Connor CainConnor Cain
its in the original Question 
Raj VakatiRaj Vakati
I dnt see any think wrong in test class .. can you give me screen shot of the workspace and error message pls 
Connor CainConnor Cain
im sorry it was my bad. I forgot the 's' on UserviewFiles. Thank you for your help Raj!!