I know that ContentDocumentLink triggers are not bulkified by Salesforce. I was expecting that behavior and was writing test classes accordingly.
It looks like, ContentDocumentLinkTrigger doesn't respect the custom settings values inserted in the test context and uses the default value for that field.
Minimum Viable Code to Reproduce:
Hierarchical Custom Setting:
Name : OrgSettings__c
Field : Enable_CDL_Count__c type=checkbox default= false
Trigger:
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
switch on Trigger.operationType {
when AFTER_INSERT{
ContentDocumentLinkTriggerHandler.handleAfterInsert(Trigger.new);
}
}
Trigger Handler:
public class ContentDocumentLinkTriggerHandler {
@TestVisible
private static Integer count =0;
public static void handleAfterInsert(List<ContentDocumentLink> linkList) {
//
System.debug('Settings Value'+OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c);
if(OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c){
count ++;
}
}
}
}
And Finally Test Class:
@isTest
public class ContentDocumentLinkTriggerHandlerTest {
@istest
static void testBulkFileUploadWithCustomSetting(){
//Setup Dat
Account acc = new Account(Name='Dummy Account');
insert acc;
//creating 200 content version
List<ContentVersion> cvListToBeInserted = new List<ContentVersion>();
for(Integer i=0; i<200; i++){
ContentVersion contentVersionInsert = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = Blob.valueOf('Test Content Data'),
IsMajorVersion = true
);
cvListToBeInserted.add(contentVersionInsert);
}
insert cvListToBeInserted;
List<ContentVersion> insertedContentVersionList = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id IN : cvListToBeInserted ];
//Creating corresponding contentDocumentLink
List<ContentDocumentLink> cvlinkListToBeInserted = new List<ContentDocumentLink>();
for(ContentVersion contentVersionSelect : insertedContentVersionList){
ContentDocumentLink contentlink = new ContentDocumentLink();
contentlink.ShareType = 'V';
contentlink.LinkedEntityId = acc.Id;
contentlink.ContentDocumentId = contentVersionSelect.ContentDocumentId;
contentlink.Visibility = 'AllUsers';
cvlinkListToBeInserted.add(contentlink);
}
System.assertEquals(0, ContentDocumentLinkTriggerHandler.count);
OrgSettings__c orgs = OrgSettings__c.getOrgDefaults();
orgs.Enable_CDL_Count__c = true ;
upsert orgs;
test.startTest();
insert cvlinkListToBeInserted;
test.stopTest();
System.assertEquals(200, ContentDocumentLinkTriggerHandler.count);
//Assert Fails System.AssertException: Assertion Failed: Expected: 200, Actual: 1
}
}
As you can see,I am upserting the Org default for custom settings as true and I am incrementing count
variable to count how many time handler was called. Also, I added a switch to test that behavior using same Hierarchical custom settings(OrgSettings__c
).
When I ran the test class, it fails at assert. So apparently count value is 1 , which is just weird. On adding system.debug in my handler I found
OrgSettings__c.getOrgDefaults().Enable_CDL_Count__c
is false 199 times and is true 1(last record) time and hence the assert failed.
This is super weird, and I don't have any explanation for it. Can someone shed some light why this is happening?