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 on file

Hi All,
Please help me to write a test class for the below apex trigger.
Thanks in advance.
here is the code 

trigger PreventFileConDoc on ContentDocument (before delete) {
   // for(ContentDocumentLink conl : trigger.old){
   //     conl.adderror('ContentDocumentLink Cannot be deleted');
   // }
      Id profileId = userinfo.getProfileId();
    String profileName = [Select Id,name from Profile where Id =: profileId].Name;
   if(Trigger.isBefore && Trigger.isDelete){
        Map<Id, ContentDocument> mapContentDocument = new Map<Id, ContentDocument>([Select Id from ContentDocument where Id in: trigger.oldMap.keyset()]);
        for(ContentDocument cd : Trigger.Old){
             if (profileName != 'System Administrator') {
            if(mapContentDocument.containsKey(cd.Id)){
                cd.adderror('ContentDocument Cannot be deleted');
                system.debug('testing inside loop'+cd);
            }
        } 
        }

}



Thank you,
Bheem
Best Answer chosen by bheemudu neeli
sakhisakhi
Hi Bheem ,
 
@isTest
private class ContentDocumentTest {
  private static testMethod void testCreate() {
    ContentVersion contentVersion_1 = new ContentVersion(
      Title = 'Penguins',
      PathOnClient = 'Penguins.jpg',
      VersionData = Blob.valueOf('Test Content')
      IsMajorVersion = true
    );
    insert contentVersion_1;
    
    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

        System.runAs(u) {
			delete contentVersion_1;
		}
}
}

 

All Answers

sakhisakhi
Hi Bheem ,
 
@isTest
private class ContentDocumentTest {
  private static testMethod void testCreate() {
    ContentVersion contentVersion_1 = new ContentVersion(
      Title = 'Penguins',
      PathOnClient = 'Penguins.jpg',
      VersionData = Blob.valueOf('Test Content')
      IsMajorVersion = true
    );
    insert contentVersion_1;
    
    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

        System.runAs(u) {
			delete contentVersion_1;
		}
}
}

 
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

Hi neeli,

please run the below test.

@isTest
public class ContentDocumentTest{

public static testMethod void contentDocumentProfileTest(){
 profile pr = [select id,Name from profile where name = 'System Administrator'];
        user intgUser = new user(firstname = 'testFirstName',
                                 lastname = 'testLastName',
                                 profileid = pr.id,
                                 email = 'testuser@testclass.com',
                                 Username = 'testfirst.lastname@testclass.com',
                                 Alias = 'TfTl',
                                 CommunityNickname = 'test' ,
                                 TimeZoneSidKey = 'America/New_York', 
                                 LocaleSidKey = 'en_US', 
                                 EmailEncodingKey = 'UTF-8',
                                 LanguageLocaleKey = 'en_US'
                                );
        insert intgUser;
        system.runAs(intgUser){ 
            ContentVersion cv = new ContentVersion(
                Title = 'Test',
                PathOnClient = 'Test.jpg',
                VersionData = blob.valueof('Test Content Data'),
                IsMajorVersion = true,
             );
            insert cv;
}

}

Please mark it as the best Answer if it helps you

Thank You

bheemudu neelibheemudu neeli
Hi Sakhi,
Thank you for your response,
With your code I got the error 'dml operation delete not allowed on contentversion'
I changed code something like below and then covers code coverage as 75%.

@isTest 
public class Test_PreventFileDeletion {
  static testMethod void testMethod1() 
    {
      ContentVersion contentVersion = new ContentVersion(
      Title = 'Penguins',
      PathOnClient = 'Penguins.jpg',
      VersionData = Blob.valueOf('Test Content'),
      IsMajorVersion = true
    );
    insert contentVersion;

    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        try{
            delete documents;
        }
        catch(exception e){}
        
    }
}



Thank you,
Bheem
sakhisakhi
I am glad it could give you reference and you were able to proceed .Please mark it as the best Answer if it helped you.