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
Amidou CisseAmidou Cisse 

Hello everybody, Some baby could help me to test this trigger and class

//BlockContentLinkTrigger
trigger BlockContentLinkTrigger on ContentDocumentLink (before insert) {  
    for(ContentDocumentLink  contDocL : Trigger.new) {
      if (CDLHelper.isSharingAllowed(contDocL)) {  
          contDocL.addError('Pièce joint bloquée depuis objet ContentDocumentLink');
     } 
   }  
}

//CDLHelper
public class CDLHelper {
    public static boolean isSharingAllowed(ContentDocumentLink cdl) {
        //String docId = cdl.ContentDocumentId;        
        ContentVersion version = [select PublishStatus, FileExtension from ContentVersion
                                  where ContentDocumentId =: cdl.ContentDocumentId limit 5];              
        if (version.PublishStatus.equals('P') && (version.FileExtension != 'null')) {
            return false;
        }
        return true;
    }
}

 
Best Answer chosen by Amidou Cisse
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below Test class
 
@isTest
private class contentDocumentLinkTriggerTest
{
    @isTest
    static void validateDCL()
    {
        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';  
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');         
        cv.VersionData =bodyBlob;          
        insert cv; 
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
            
            contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: cv.id].contentdocumentid;
            contentlink.ShareType = 'V';
            test.starttest();
        insert contentlink;
test.stoptest();

    }
}

Hope this will help you. 

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use below Test class
 
@isTest
private class contentDocumentLinkTriggerTest
{
    @isTest
    static void validateDCL()
    {
        ContentVersion cv = new ContentVersion();
        cv.title = 'test content trigger';      
        cv.PathOnClient ='test';  
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body');         
        cv.VersionData =bodyBlob;          
        insert cv; 
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
            
            contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: cv.id].contentdocumentid;
            contentlink.ShareType = 'V';
            test.starttest();
        insert contentlink;
test.stoptest();

    }
}

Hope this will help you. 

Thanks
karthik

 
This was selected as the best answer
Amidou CisseAmidou Cisse
Thanks ! It's run 100%