You need to sign in to do that
Don't have an account?

Test class for a simple trigger on ContentDocumentLink
I have a simple trigger on ContentDocumentLink. The trigger finds the linked record's field and stores it's value in a ContentVersion field Title.
So we have four objects that are linked like this: ContentVersion -> ContentDocument -> ContentDocumentLink -> Some object
I take field from Some object and populate ContentVersion Title with the value. There is the code:
Question is how do I write the test class? I have 4 linked objects I need to use. Which one do I insert first?
So we have four objects that are linked like this: ContentVersion -> ContentDocument -> ContentDocumentLink -> Some object
I take field from Some object and populate ContentVersion Title with the value. There is the code:
trigger ContentDocumentLink on ContentDocumentLink (before insert) { Set<Id> aids = new Set<Id>(); Set<Id> bids = new Set<Id>(); Set<Id> cids = new Set<Id>(); Set<Id> dids = new Set<Id>(); for(ContentDocumentLink a :Trigger.New) { aids.add(a.Id); bids.add(a.ContentDocumentId); cids.add(a.LinkedEntityId); } ContentDocument[] b = [Select Id, LatestPublishedVersionId, Title from ContentDocument Where Id in :bids]; for(ContentDocument c :b) { dids.add(c.LatestPublishedVersionId); ContentVersion[] d = [Select Id, Title from ContentVersion where Id in :dids]; for(ContentVersion e :d) { e.Title = 'letitgo'; update e; } } }
Question is how do I write the test class? I have 4 linked objects I need to use. Which one do I insert first?
By the below approach you can create test data for once and you can use it for all methods. In this way, the execution speed can be controlled for test class while deploying the code to production.
@isTest
public class ContentDocumentLinkTest {
@TestSetup static void prepareTestData() {
// Create a test data for ContentVersion
ContentVersion testContentVer = new ContentVersion();
// Field assignment will go here
insert testContentVer;
// Create a test data for ContentDocument
ContentDocument testContentDoc = new ContentDocument();
// Field assignment will go here
testContentDoc.LatestPublishedVersionId = testContentVer.Id; // Main field
inset testContentDoc;
// Create a test data for ContentDocumentLink
ContentDocumentLink testDocLink = new ContentDocumentLink();
// Field assignment will go here
testDocLink.ContentDocumentId = testContentDoc.Id; // Main value
insert testDocLink;
//Some system asserts to check for the correct test data
System.assert(testContentVer != null);
System.assertEquals(testContentVer.Id, testContentDoc.LatestPublishedVersionId);
System.assertEquals(testContentDoc.Id, testDocLink.ContentDocumentId);
}
// Actual test method
@isTest static void testMethod1() {
ContentVersion testContentVersion = [SELECT ID, Title from ContentVersion LIMIT 1];
testContentVersion.Title = 'letitgo';
update testContentVersion;
}
}
The trigger works fine now, I tested it all manually, it' really great.
I'm writing the test class the way you mentioned. I found out that it is impossible to insert a ContentDocument record, becuase it is craeted automaticly with content version. My test class now looks like this:
I got an error:
That is a big problem, help me with this please, how do I fix this?