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
bheemudu neelibheemudu neeli 

Need a help on code coverage for before delete apex trigger

Hi All,
Need a help on code coverage for before delete apex trigger 
Thanks in Advance, can any one please help me to write a test class for the below trigger... I tried my known ways but failed.
here is my code....
trigger PreventFileDeleion on ContentDocumentLink (before delete) {
      Id profileId = userinfo.getProfileId();
   String profileName = [Select Id,name from Profile where Id =: profileId].Name;
    
   if(Trigger.isBefore && Trigger.isDelete){
        Map<Id, ContentDocumentLink> mapContentDocument = new Map<Id, ContentDocumentLink>([Select Id from ContentDocumentLink where Id in: trigger.oldMap.keyset()]);
        for(ContentDocumentLink cd : Trigger.Old){
            if (profileName != 'System Administrator') {
            if(mapContentDocument.containsKey(cd.Id)){
                cd.adderror('ContentDocumentLink Cannot be deleted');
                system.debug('testing inside loop'+cd);
            }
        } 
        }
}
}
 
Best Answer chosen by bheemudu neeli
CharuDuttCharuDutt
HII Bheemudu Neeli
Try Below Code
@isTest
public class PreventFileDeleionTest{
@isTest
    public Static Void unitTest(){
        
         
        string base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
        Account Acc = new Account();
        Acc.Name='Test';
        insert Acc;
        ContentVersion cv = new ContentVersion();
        cv.Title = 'fileName';
        cv.PathOnClient = 'fileName.pdf';
        cv.VersionData =   EncodingUtil.base64Decode(base64Data); 
        cv.IsMajorVersion = true; 
        
        insert cv;
        system.debug('cv  : ' + cv );
        
        Id conDocId = [SELECT id,ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
        system.debug('conDocId : ' + conDocId);
        
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = Acc.Id;
        cdl.ContentDocumentId = conDocId;
        cdl.shareType = 'V';
        
        insert cdl; 
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Office Profile'];
     	User usr = new User(LastName = 'sample',
                           FirstName='sam',
                           Alias = 'simplSam',
                           Email = 'ssamsimple@asdf.com',
                           Username = 'ssamsimple@asdf.com',
                           ProfileId = p.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US'
                           );
        insert usr;
        delete cdl;
    }
}

Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
HII Bheemudu Neeli
Try Below Code
@isTest
public class PreventFileDeleionTest{
@isTest
    public Static Void unitTest(){
        
         
        string base64Data = EncodingUtil.urlDecode('Test base64Data For testing', 'UTF-8');
        Account Acc = new Account();
        Acc.Name='Test';
        insert Acc;
        ContentVersion cv = new ContentVersion();
        cv.Title = 'fileName';
        cv.PathOnClient = 'fileName.pdf';
        cv.VersionData =   EncodingUtil.base64Decode(base64Data); 
        cv.IsMajorVersion = true; 
        
        insert cv;
        system.debug('cv  : ' + cv );
        
        Id conDocId = [SELECT id,ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
        system.debug('conDocId : ' + conDocId);
        
        
        ContentDocumentLink cdl = New ContentDocumentLink();
        cdl.LinkedEntityId = Acc.Id;
        cdl.ContentDocumentId = conDocId;
        cdl.shareType = 'V';
        
        insert cdl; 
        
        Profile p = [SELECT Id FROM Profile WHERE Name='Office Profile'];
     	User usr = new User(LastName = 'sample',
                           FirstName='sam',
                           Alias = 'simplSam',
                           Email = 'ssamsimple@asdf.com',
                           Username = 'ssamsimple@asdf.com',
                           ProfileId = p.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US'
                           );
        insert usr;
        delete cdl;
    }
}

Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
bheemudu neelibheemudu neeli
Thank you CharuDutt,
I got 75% code coverage, I have some lines those are not covered.
 if (profileName != 'System Administrator') {
            if(mapContentDocument.containsKey(cd.Id)){
                cd.adderror('ContentDocumentLink Cannot be deleted');

Thank you,
Bheem


 
CharuDuttCharuDutt
Hii Bheemudu Neeli
Try Below Made Some Change Are In Bold
trigger PreventFileDeleion on ContentDocumentLink (before delete) {
      Id profileId = userinfo.getProfileId();
    system.debug(profileId);
    
   String profileName = '';
    if(test.isRunningTest()){ 
    profileName = [select id,name from profile where name != 'System Administrator' limit 1].Name;
    }else{
        profileName = [Select Id,name from Profile where Id =: profileId].Name;}
   if(Trigger.isBefore && Trigger.isDelete){
        Map<Id, ContentDocumentLink> mapContentDocument = new Map<Id, ContentDocumentLink>( [Select Id from ContentDocumentLink where Id in: trigger.oldMap.keyset()]);
       system.debug('one===> '+mapContentDocument.keyset());
       for(ContentDocumentLink cd : Trigger.Old){
           system.debug('Two===> '+mapContentDocument.containsKey(cd.Id)); 
            system.debug('Id==> '+cd.Id);
          
            if (profileName != 'System Administrator') {
           if(mapContentDocument.containsKey(cd.Id) == true){
                cd.adderror('ContentDocumentLink Cannot be deleted');
                system.debug('testing inside loop'+cd);
            }
        } 
        }
}
}
Please Mark It As Best Answer If It Helps
Thank You!