Skip to content

Instantly share code, notes, and snippets.

@xgeek-net
Last active August 20, 2021 11:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xgeek-net/b96fa1794899f4c31428 to your computer and use it in GitHub Desktop.
Save xgeek-net/b96fa1794899f4c31428 to your computer and use it in GitHub Desktop.
ContentDocumentTest Apex TestClass
@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;
ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
System.assertEquals(documents.size(), 1);
System.assertEquals(documents[0].Id, contentVersion_2.ContentDocumentId);
System.assertEquals(documents[0].LatestPublishedVersionId, contentVersion_2.Id);
System.assertEquals(documents[0].Title, contentVersion_2.Title);
}
}
@sfdc-SupportDevelopment
Copy link

sfdc-SupportDevelopment commented Dec 9, 2016

       #  this is working code for custome object linked with Contentversion files(Salesforce files)

        Matter__c thisMatter = [select id,Request__r.Requestor__r.Id from matter__c  limit 1];
       Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        ContentVersion contentVersion_1 = new ContentVersion(
            Title='Header_Picture1', 
            PathOnClient ='/Header_Picture1.jpg',
            VersionData = bodyBlob, 
            origin = 'H'
        );
        insert contentVersion_1;
       
        ContentVersion contentVersion_2 = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersion_1.Id LIMIT 1];
        List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
        
        ContentDocumentLink contentlink = new ContentDocumentLink();
        contentlink.LinkedEntityId = thisMatter.id;
        contentlink.contentdocumentid = contentVersion_2.contentdocumentid;
        contentlink.ShareType = 'V';
        insert contentlink; 
        
        System.assertEquals(documents.size(), 1);
        System.assertEquals(documents[0].Id, contentVersion_2.ContentDocumentId);
        System.assertEquals(documents[0].LatestPublishedVersionId, contentVersion_2.Id);
        System.assertEquals(documents[0].Title, contentVersion_2.Title);
        System.assertEquals(contentlink.ContentDocumentId,contentVersion_2.ContentDocumentId );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment