+ Start a Discussion
Muhammad Jawwad 16Muhammad Jawwad 16 

How to write a test class for helper class(Content Document Link?

Please find my trigger  in code sample and suggest me how to write a test class for helper class.

Trigger :

trigger ContentDocument on ContentDocument (after insert) 
{
 if(Trigger.isAfter && Trigger.isInsert)
   {
   
  //1. SELECT THE OPPORTUNITY TEAM
  SET<ID> SetOfContentDocumentIDs = new SET<ID>();
  for(ContentDocument cdoc: Trigger.New)
  {
    SetOfContentDocumentIDs.add(cdoc.Id);
  } 
 
  //2. Sharing Files With Opportunity Team
  ContentDocumentHelper.shareOpportunityPrivateFileWithOppTeam(SetOfContentDocumentIDs);
  }
  
}

Helper Class:

public class ContentDocumentHelper{

 @future
 public static void shareOpportunityPrivateFileWithOppTeam(SET<ID> SetOfContentDocumentIDs)
 {
    
    System.Debug('AM: SetOfContentDocumentIDs :' + SetOfContentDocumentIDs);
    List<ContentDocumentLink> ListOfContentDocumentLink = new List<ContentDocumentLink>();
    
    integer i = 0;
    while(i <=80 && ListOfContentDocumentLink.size()<=1 )
    {
      ListOfContentDocumentLink = [ SELECT LinkedEntityId,ContentDocumentId 
                                    FROM ContentDocumentLink 
                                    WHERE ContentDocumentId IN : SetOfContentDocumentIDs];
      i++;
    }
                                                             
  //2.Get OpportunityTeamMember  
  SET<ID> SetOfOpportunityIDs = new SET<ID>();
  for(ContentDocumentLink cdl : ListOfContentDocumentLink )
  {
    SetOfOpportunityIDs.add(cdl.LinkedEntityId);
  } 
  
  System.Debug('AM: SetOfOpportunityIDs : ' + SetOfOpportunityIDs );
  
   
  List<OpportunityTeamMember> ListofOpportunityTeamMember =  [SELECT UserId,OpportunityId 
                                                              FROM OpportunityTeamMember 
                                                              WHERE OpportunityId IN : SetOfOpportunityIDs];
  
  
  System.Debug('AM: ListofOpportunityTeamMember :' + ListofOpportunityTeamMember );
  
  MAP<ID,SET<ID>> MapOfOppIDSetUsersID = new MAP<ID,SET<ID>>();
  
  for(OpportunityTeamMember oppteam :ListofOpportunityTeamMember )
  {
    if(MapOfOppIDSetUsersID.get(oppteam.OpportunityId) == null)
    {
      SET<ID> SetofUserID = new SET<ID>();
      SetofUserID.add(oppteam.UserId);
      MapOfOppIDSetUsersID.put(oppteam.OpportunityId,SetofUserID);
    }
    else
    {
      SET<ID> SetofUserID = MapOfOppIDSetUsersID.get(oppteam.OpportunityId);
      SetofUserID.add(oppteam.UserId);
      MapOfOppIDSetUsersID.put(oppteam.OpportunityId,SetofUserID);
      
    }
  }
  
  System.Debug('AM: MapOfOppIDSetUsersID: ' + MapOfOppIDSetUsersID);
 
  //3.Process the documents and share with users
  List<ContentDocumentLink > ListofUserShare = new List<ContentDocumentLink >();
  
  for(ContentDocumentLink cdl: ListOfContentDocumentLink)
  {
    
   SET<ID> SetofUserIDs = new SET<ID>();
   SetofUserIDs = MapOfOppIDSetUsersID.get(cdl.LinkedEntityId);
   if(SetofUserIDs!=null)
   { 
    if(SetofUserIDs.size()>0)
    {
     for(ID userID: SetofUserIDs)
     {
       if(!SetOfOpportunityIDs.contains(userID))
       {
         ListofUserShare.add(
            new ContentDocumentLink (
                  ContentDocumentId = cdl.ContentDocumentId ,
                  LinkedEntityId = userID,
                  ShareType = 'C'
                )
         );
       }
    }
   }
  }  
  } 
  
  System.Debug('AM: ListofUserShare : ' + ListofUserShare);
  
  if(ListofUserShare.size()>0)
  {
    insert ListofUserShare;
  }
}

}


Test Class:

@isTest
public class ContentDocumentTest {
@istest    
 public static  void test() {
            
           
        
                    
           Opportunity o = New Opportunity ();
           o.StageName ='Open';
           
           o.CloseDate=System.today() + 5;
           o.Name='Test NL Opportunity';
           
           insert o;             
            
            
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=o.Id;
        contentlink.ShareType= 'v';
        contentlink.LinkedEntityId = o.id;
        contentlink.ContentDocumentId=documents[0].Id;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;          
            
               
             
           
        } 
}

Please help​​​​​​​
Best Answer chosen by Muhammad Jawwad 16
Sfdc CoupleSfdc Couple
Hi Muhammad,

Please find the test class and let me know if you have any queries
 
@isTest
public class ContentDocumentTest {
@istest    
 public static  void test() {
            
           
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
    User u = new User(Alias = 'standt', Email='standard12@testorg.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='standard12@testorg.com');
     insert u;
            
           Opportunity o = New Opportunity ();
           o.StageName ='Open';
           
           o.CloseDate=System.today() + 5;
           o.Name='Test NL Opportunity';
           
           insert o;             
            
 OpportunityTeamMember otm = new OpportunityTeamMember (OpportunityId = o.id,UserId = u.id,TeamMemberRole = 'Account Manager');
 insert otm;     
     system.debug('hi'+otm);
        ContentVersion cv = new ContentVersion(title='foo', contentUrl='http:///www.foor.bar');
insert cv;
ContentVersion cvRes = [select id, ContentDocument.id, ContentDocument.title 
                         from ContentVersion where id= :cv.id];
ContentDocument cd = cv.ContentDocument;
     Id cdid = [select id, ContentDocument.id, ContentDocument.title 
                         from ContentVersion where id= :cv.id].ContentDocument.id;

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=o.Id;
        contentlink.ShareType= 'v';
        contentlink.LinkedEntityId = o.id;
        contentlink.ContentDocumentId=cdid;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;          
            
              
             
           
        } 
}

Thanks,
SfdcCouple​​​​​​​

All Answers

Sfdc CoupleSfdc Couple
Hi Muhammad,

Please find the test class and let me know if you have any queries
 
@isTest
public class ContentDocumentTest {
@istest    
 public static  void test() {
            
           
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
    User u = new User(Alias = 'standt', Email='standard12@testorg.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='standard12@testorg.com');
     insert u;
            
           Opportunity o = New Opportunity ();
           o.StageName ='Open';
           
           o.CloseDate=System.today() + 5;
           o.Name='Test NL Opportunity';
           
           insert o;             
            
 OpportunityTeamMember otm = new OpportunityTeamMember (OpportunityId = o.id,UserId = u.id,TeamMemberRole = 'Account Manager');
 insert otm;     
     system.debug('hi'+otm);
        ContentVersion cv = new ContentVersion(title='foo', contentUrl='http:///www.foor.bar');
insert cv;
ContentVersion cvRes = [select id, ContentDocument.id, ContentDocument.title 
                         from ContentVersion where id= :cv.id];
ContentDocument cd = cv.ContentDocument;
     Id cdid = [select id, ContentDocument.id, ContentDocument.title 
                         from ContentVersion where id= :cv.id].ContentDocument.id;

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=o.Id;
        contentlink.ShareType= 'v';
        contentlink.LinkedEntityId = o.id;
        contentlink.ContentDocumentId=cdid;
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;          
            
              
             
           
        } 
}

Thanks,
SfdcCouple​​​​​​​
This was selected as the best answer
Muhammad Jawwad 16Muhammad Jawwad 16
Thank you so much :)