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
Michael MMichael M 

Help with test code for deletion prevention trigger

Hello, we just wrote a trigger to prevent one of our users from deleting files that were not created by him. This is the trigger, below. I tried writing test code, but it is covering 0% of the trigger. Any idea why, or how I can get the test code to work? 

Trigger and test below.


Trigger:
trigger FileDeletionPrevention on ContentDocument (before delete) {
    if (trigger.isBefore){
    for (ContentDocument cd : trigger.old){
            Id deleter = UserInfo.getUserId();
            Id uploader = cd.CreatedById;
        if (deleter == '0051N000006BSeyQAG'){
            if (deleter != uploader){
                cd.adderror('File cannot be deleted');
            }
        }
    }
}
}


Test:
@isTest(seealldata = true)
public class FileDeletionPreventionTest {

   @isTest
    public static void fileDeleter(){

        
        user u = [select id, name from user where name = 'Integration User'];
        
        Test.startTest();
        
      ContentVersion cv=new Contentversion();
        cv.title='ABC';
        cv.PathOnClient ='test';
        Blob b=Blob.valueOf('Unit Test Attachment Body');
        cv.versiondata=EncodingUtil.base64Decode('Unit Test Attachment Body');
        insert cv;
        
        System.runas(u){
            id cvDocId = cv.ContentDocumentId;
          list<contentDocument> cd = [select id from contentdocument where id =:cvDocId];
            delete cd;
        }
        Test.stopTest();
        
    }
}
AbhishekAbhishek (Salesforce Developers) 
Micheal,

The below blog's mentioned similar issues, Try the suggestions as mentioned in it,

https://salesforce.stackexchange.com/questions/157413/wrote-a-trigger-to-prevent-record-deletion-now-need-help-with-test-class

https://developer.salesforce.com/forums/?id=906F0000000947PIAQ


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.