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
Travis16Travis16 

Test class on ContentDocumentLink

Trying to write a test class for the following trigger that gives access to community members for documents under the quotes object. I am just not sure how to test/check in the test class. 

Trigger
trigger shareFilesWithCommunityUsers on ContentDocumentLink(before insert){

   Schema.DescribeSObjectResult r = Quote.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();

      for(ContentDocumentLink cdl:trigger.new){
        if((String.valueOf(cdl.LinkedEntityId)).startsWith(keyPrefix)){
          cdl.ShareType = 'I';
          cdl.Visibility = 'AllUsers';
          } 
       }
    }

Test Class
@IsTest
Public class TestShareFilesWithCommunityUsers {
    
     static testmethod void testmethod1(){
        

        
           //Create 2 accounts
        List<Account> accounts = new List<Account>();
        Account accA = bg_Account_Test_Utils.createAccount('Influencer', 'Account A');
        accounts.add(accA);
        insert accounts;

        //Create 2 contacts, linked to the accounts
        List<Contact> contacts = new List<Contact>();
        Contact conA = bg_Contact_Test_Utils.createContact('Test', 'Contact A', accA.Id);
        contacts.add(conA);
        insert contacts;

        //Create a project
        Project__c project = bg_Project_Test_Utils.createProject('Test Project 1', 'Education', 'Tender Phase', 'Leeds', 'LS61QF', accA.Id, conA.Id, 'New build project');
        insert project;

        List<Opportunity> opps = new List<Opportunity>();
        Opportunity wonOpp = bg_Opportunity_Test_Utils.createOpportunity('Controlled Environments', 'Opportunity 1', accA.Id, 'Outline Specification', project.Id, Date.Today() + 30);
        opps.add(wonOpp);
        insert opps;
        
        Quote Q = New Quote(Name='Test Quote', OpportunityId=WonOpp.Id);
        insert Q;
        
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=q.id;
        contentlink.ShareType= 'I';
        contentlink.LinkedEntityId = q.Id; 
        contentlink.Visibility = 'AllUsers'; 
        insert contentlink;
         
   }
}

Sorry Still Trying to Wrap my head around apex. 
Best Answer chosen by Travis16
Raj VakatiRaj Vakati
I used this code which is giving 100 % 

 
@IsTest
Public class TestShareFilesWithCommunityUsers {
    
    static testmethod void testmethod1(){
        Test.startTest();
        Account accA =new Account(Name='Demo');
        insert accA ; 
        Opportunity opp = new Opportunity(Name='test opp',StageName='stage', AccountId = accA.Id ,Probability = 95, CloseDate=system.today());
        insert opp;
        
        Quote Q = New Quote(Name='Test Quote', OpportunityId=opp.Id);
        insert Q;
        
        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=q.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
        Test.stopTest();
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
I used this code which is giving 100 % 

 
@IsTest
Public class TestShareFilesWithCommunityUsers {
    
    static testmethod void testmethod1(){
        Test.startTest();
        Account accA =new Account(Name='Demo');
        insert accA ; 
        Opportunity opp = new Opportunity(Name='test opp',StageName='stage', AccountId = accA.Id ,Probability = 95, CloseDate=system.today());
        insert opp;
        
        Quote Q = New Quote(Name='Test Quote', OpportunityId=opp.Id);
        insert Q;
        
        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=q.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'I';
        contentlink.Visibility = 'AllUsers'; 
        
        
        insert contentlink;
        Test.stopTest();
        
    }
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
Is its working ?
Travis16Travis16
Raj, This is working after adding in the requirements for the org. Can you let me know what is checking the ContentDocumentLink in the test class?
shubham r 9shubham r 9
Hello Raj, Thanks for the share. I have a trigger on contentdocumentlink, with your help the test coverage is meet.  Thankyou!