Skip to content

Instantly share code, notes, and snippets.

@svierk
Last active January 23, 2024 22:36
Show Gist options
  • Save svierk/7f38d03838ae77bcd5ca27192b72e398 to your computer and use it in GitHub Desktop.
Save svierk/7f38d03838ae77bcd5ca27192b72e398 to your computer and use it in GitHub Desktop.
Apex Test Class for Content Document Table Component | Content Setup
private static Id createContentSetup(Id recordId) {
ContentWorkspace cw = getContentWorkspace('Test Documents');
insert cw;
ContentFolderLink cfl = [
SELECT Id, ContentFolderId, ParentEntityId
FROM ContentFolderLink
WHERE ParentEntityId = :cw.Id
];
ContentFolder cf = getContentFolder(recordId.getSObjectType().getDescribe().getName(), cfl.ContentFolderId);
insert cf;
ContentVersion cv = getContentVersion(1);
insert cv;
ContentDocumentLink cdl = getContentDocumentLink(cv, cw.Id);
insert cdl;
ContentFolderMember cfm = getContentFolderMember(cdl.ContentDocumentId, cfl.ContentFolderId, cf.Id);
update cfm;
return cdl.ContentDocumentId;
}
private static ContentWorkspace getContentWorkspace(String name) {
ContentWorkspace cw = new ContentWorkspace();
cw.Name = name;
return cw;
}
private static ContentFolder getContentFolder(String name, Id id) {
ContentFolder cf = new ContentFolder();
cf.Name = name;
cf.ParentContentFolderId = id;
return cf;
}
private static ContentVersion getContentVersion(Integer counter) {
ContentVersion cv = new ContentVersion();
cv.Title = 'Content_' + counter;
cv.PathOnClient = '/' + cv.Title + '.jpg';
Blob bodyBlob = Blob.valueOf('ContentVersion Body');
cv.VersionData = bodyBlob;
cv.Origin = 'H';
return cv;
}
private static ContentDocumentLink getContentDocumentLink(ContentVersion cv, Id id) {
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = id;
cdl.ContentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id][0].ContentDocumentId;
cdl.ShareType = 'I';
cdl.Visibility = 'AllUsers';
return cdl;
}
private static ContentFolderMember getContentFolderMember(Id fileId, Id parentId, Id childId) {
ContentFolderMember cfm = [
SELECT Id, ChildRecordId, ParentContentFolderId
FROM ContentFolderMember
WHERE ChildRecordId = :fileId AND ParentContentFolderId = :parentId
];
cfm.ParentContentFolderId = childId;
return cfm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment