Skip to main content
added 342 characters in body
Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72
  1. Create an account or any record. 2. Create ContentVersion record 3. Create ContentDocumentLink record using Account as Parent. 4. Lock the Account record. 5. Try to delete ContentDocument 6. System will throw the exception. 7. You can also verify the error message by Apexpages.Message

Definitely you will get 100% coverage.

  1. Create an account or any record. 2. Create ContentVersion record 3. Create ContentDocumentLink record using Account as Parent. 4. Lock the Account record. 5. Try to delete ContentDocument 6. System will throw the exception. 7. You can also verify the error message by Apexpages.Message

Definitely you will get 100% coverage.

Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

This is following way you could write the class and scenario.

To apply locking from apex code, choose the checkbox Enable record locking and unlocking in Apex as true, upon navigating Setup->Create-> Workflows and Approvals -> Process Automation Settings

Enable locking from Apex

@istest
public class TestNoteOnContentDocuments {
  static testmethod void NoteOnContentDoc()
  {
    List<Apexpages.Message> msgs;
    
    //create testUser 
    
    System.runAs(testUser)
    {
        //create an account 
        Account acct = new Account(Name='TEST_ACCT');
        insert acct;
        
        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];
        
        //create ContentDocumentLink  record for that account
        ContentDocumentLink cdl = New ContentDocumentLink(
                    LinkedEntityId = acct.id, ContentDocumentId = documents[0].Id, shareType = 'V');
        insert cdl;
    
        List<Account> accts = [SELECT Id from Account WHERE Name = 'TEST_ACCT'];
        
        //lock the account record.
        Approval.LockResult[] lrList = Approval.lock(accts, false);
        
        Test.startTest();
        try { delete documents; }
        catch (Exception dmx) 
        { 
           Boolean expectedExceptionThrown = dmx.getMessage().contains('Approval pending. You do not have the permission to edit/delete this note/attachment, please contact your administrator.') ? true : false; 
           System.assertEquals(expectedExceptionThrown, true);  
        }
        
        //Also it can tested like this through Apexpages.Message
        
        List<Apexpages.Message> msgs;
        msgs = ApexPages.getMessages();
            boolean isErrorMessage = false;
            for(ApexPages.Message msg : msgs)
            {
                if (msg.getSummary().contains('Approval pending.') )
                {
                    isErrorMessage  = true;
                }
            }
        System.debug('testing messages');
        System.assert(isErrorMessage);

        Test.stopTest();
    }
    
}