I have written a Trigger for ContentDocumentLink and I am trying to write a test class but using Test Class Trigger is not getting covered.
Trigger :
trigger contentDocumentLinkTrigger on ContentDocumentLink (after insert) {
if(contentDocumentLinkTriggerHandler.isExecute)
contentDocumentLinkTriggerHandler.processOnInsert(trigger.new);
}
Handler :
public with sharing class contentDocumentLinkTriggerHandler {
public static boolean isExecute = true;
public static void processOnInsert(list<ContentDocumentLink> newList) {
list<ContentDocumentLink> contentList = new list<ContentDocumentLink>();
set<id> accId = new set<id>();
map<id,list<ContentDocumentLink>> parentContentMap = new map<id,list<ContentDocumentLink>>();
set<id> contId = new set<id>();
set<id> opptyId = new set<id>();
for(ContentDocumentLink nt : newList){
string pId = string.valueOf(nt.linkedentityid).substring(0,3);
if(pid == '001'){
accId.add(nt.linkedentityid);
}
if(pid == '003'){
contId.add(nt.linkedentityid);
}
if(pid == '006'){
opptyId.add(nt.linkedentityid);
}
if(parentContentMap.containsKey(nt.linkedentityid))
parentContentMap.get(nt.linkedentityid).add(nt);
else
parentContentMap.put(nt.linkedentityid,new list<ContentDocumentLink>{nt});
}
if(!accId.isEmpty()){
for(Account acc : [select id,(select id from contacts),(select id from opportunities) from Account where id in : accId]){
if(parentContentMap.containsKey(acc.id)){
for(ContentDocumentLink nt : parentContentMap.get(acc.id)){
for(Contact ct : acc.contacts){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = ct.id;
contentList.add(nt1);
}
for(Opportunity opp : acc.opportunities){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = opp.id;
contentList.add(nt1);
}
}
}
}
}
if(!contId.isEmpty()){
map<id,id> conAccMap = new map<id,id>();
for(Contact cont : [select accountId from Contact where id in : contId]){
conAccMap.put(cont.accountId,cont.id);
}
for(Account acc : [select id,(select id from contacts),(select id from opportunities) from Account where id in : conAccMap.keySet()]){
if(parentContentMap.containsKey(conAccMap.get(acc.id))){
for(ContentDocumentLink nt : parentContentMap.get(conAccMap.get(acc.id))){
ContentDocumentLink nt2 = nt.clone(false);
nt2.linkedentityid = acc.id;
contentList.add(nt2);
for(Contact ct : acc.contacts){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = ct.id;
if(conAccMap.get(acc.id) != ct.id)
contentList.add(nt1);
}
for(Opportunity opp : acc.opportunities){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = opp.id;
contentList.add(nt1);
}
}
}
}
}
if(!opptyId.isEmpty()){
map<id,id> optyAccMap = new map<id,id>();
for(Opportunity oppty : [select accountId from Opportunity where id in : opptyId]){
optyAccMap.put(oppty.accountId,oppty.id);
}
for(Account acc : [select id,(select id from contacts),(select id from opportunities) from Account where id in : optyAccMap.keySet()]){
if(parentContentMap.containsKey(optyAccMap.get(acc.id))){
for(ContentDocumentLink nt : parentContentMap.get(optyAccMap.get(acc.id))){
ContentDocumentLink nt2 = nt.clone(false);
nt2.linkedentityid = acc.id;
contentList.add(nt2);
for(Contact ct : acc.contacts){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = ct.id;
contentList.add(nt1);
}
for(Opportunity opp : acc.opportunities){
ContentDocumentLink nt1 = nt.clone(false);
nt1.linkedentityid = opp.id;
if(optyAccMap.get(acc.id) != opp.id)
contentList.add(nt1);
}
}
}
}
}
if(contentList.size() > 0){
isExecute = false;
insert contentList;
}
}
}
Test Class :
@isTest
private class contentDocumentLinkTriggerTest
{
@isTest
static void itShould()
{
Account acc = new Account(name='test acc');
insert acc;
Contact con = new Contact(lastname='test cont',accountid=acc.id);
insert con;
Opportunity opp = new Opportunity(name='testoppty',AccountId=acc.id,stageName='Qualification',closedate=system.today());
insert opp;
ContentVersion content=new ContentVersion();
content.Title='Header_Picture1';
content.PathOnClient='/' + content.Title + '.jpg';
Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');
content.VersionData=bodyBlob;
//content.LinkedEntityId=sub.id;
content.origin = 'H';
insert content;
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=acc.id;
contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
contentlink.ShareType = 'V';
test.starttest();
insert contentlink;
test.stoptest();
}
}
if(pid == '001')
, useif (nt.linkedentityid.getSobjectType() == Account.SObjectType)
- future-proofed and self-documenting.