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
sfdcdudesfdcdude 

content document and content distribution

Hi Team,
Can anybody help me for the test class for teh below logic?

public without sharing class fileattachment {
    public User usr{get;set;}  
    
    public transient String base64{get;set;}
      public String fileName {get; set;}
    
    public fileattachment() {        
        Id userId = apexpages.currentpage().getparameters().get('id');
        usr = [SELECT Id,ContactId,MemberCardDownloadLink__c,MemberCardPreviewLink__c from User where Id=:userId];
    }
    
    public void savefile() {
        
        //Delete older Cards
        List<ContentDocument> docDeleteList = new List<ContentDocument>();
        for(ContentDocumentLink cdl : [select Id, ContentDocumentId 
                                       FROM ContentDocumentLink 
                                       WHERE LinkedEntityId =:usr.ContactId
                                       AND (ContentDocument.Title ='test.png' OR 
                                            ContentDocument.Title ='test1.png')]) 
        {
            docDeleteList.add(new ContentDocument(Id=cdl.ContentDocumentId));                            
        }
        if(!docDeleteList.isEmpty()) {
            delete docDeleteList;
        }
        
        //Insert Doc
        ContentVersion testContentInsert = new ContentVersion();
        testContentInsert.Title = filename;
        testContentInsert.VersionData = EncodingUtil.base64decode(base64);
        testContentInsert.PathOnClient = filename ;
        insert testContentInsert;
        
        List<ContentVersion> cvList = [select id, ContentDocumentId from ContentVersion WHERE Id =:testContentInsert.Id];
        
        ContentDocumentLink cl = new ContentDocumentLink();
        cl.ContentDocumentId = cvList[0].ContentDocumentId;
        cl.LinkedEntityId = usr.ContactId;
        cl.ShareType = 'V';
        cl.Visibility = 'AllUsers';        
        insert cl;        
        
        //Make it public
        ContentDistribution newDist = new ContentDistribution();
        newDist.ContentVersionId = cvList[0].Id;
        newDist.Name = 'External Link';
        newDist.PreferencesNotifyOnVisit = false;
        newDist.PreferencesAllowViewInBrowser = true;
        newDist.PreferencesAllowOriginalDownload=true;
        newDist.PreferencesAllowPDFDownload=true;
        insert newDist;
        
        //Store the link on User Record
        for(ContentDistribution distribution : [Select Id,ContentDownloadUrl,DistributionPublicUrl 
                                                FROM ContentDistribution 
                                                WHERE Id =: newDist.Id])
        {
            usr.MemberCardPreviewLink__c = distribution.DistributionPublicUrl;
            usr.MemberCardDownloadLink__c = distribution.ContentDownloadUrl;
        }
        update usr;
        
    }  
}
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Please refer the below link will help you to proceed further on your requirement.
https://developer.salesforce.com/forums/?id=9062I000000IDgYQAW
https://salesforce.stackexchange.com/questions/181963/test-class-for-contentdocumentlink
https://gist.github.com/xgeek-net/b96fa1794899f4c31428

Thanks!!