I have a setup where a platform event is being created by a flow. I am using an apex trigger to subscribe to the platform event. The action being taken is using the ConnectAPIHelper to create a chatter notification that at mentions the triggering user.
I am using platform events so we can apply a Platform Event Subscriber Config to force the automation to run as system user. We need the chatter notification created as another user so that the @mentioned user receives an alert that they've been tagged (users don't receive alerts for tagging themselves).
This set up works as expected when I manually test it in the browser. However, when I am unit testing, the Platform Event Subscriber Config is not applying. I've tried downgrading the API on the trigger (v55 to v54), rebuilding the whole thing, deleting the PESC and recreating it. I'm still getting the same results. I also tried System.runAs, but in each scenario, the Automated Process user is executing the trigger and not my intended user. This is an issue because the autoproc user is not allowed to use the ConnectAPI, which causes me to be unable to test this functionality.
I did see this post Is there a way to force a Platform Event Trigger to honor the PlatformEventSubscriberConfig in unit tests? , but looks like poster took a different approach to get around the issue.
What am I missing?
Trigger
trigger ChatterNotificationTrigger on Chatter_Notification__e (after insert) {
system.debug('*** current running user: ' + UserInfo.getUserId());
for(Chatter_Notification__e chatterNotification : Trigger.new) {
ConnectApi.FeedItem fi = (ConnectApi.FeedItem) ConnectApiHelper.postFeedItemWithMentions(null, chatterNotification.Target_ID__c, chatterNotification.Message__c);
}
}
Unit Test
@isTest(SeeAllData=true)
public class ChatterNotificationTriggerTest {
@isTest(SeeAllData=true)
static void verifyPostPublished() {
Account account = [SELECT Id, OwnerId FROM Account LIMIT 1];
List<AccountFeed> feedItems = [SELECT Id FROM AccountFeed WHERE ParentId = :account.Id];
Integer startingCount = feedItems.size();
system.debug('Starting count: ' + startingCount + ' | accountId: ' + account.Id);
Test.startTest();
List<Chatter_Notification__e> chatterNotifications = new List<Chatter_Notification__e>();
chatterNotifications.add(new Chatter_Notification__e(Target_ID__c = account.Id, Message__c = '<b>This</b> is a test to tag {' + account.OwnerId + '}.'));
chatterNotifications.add(new Chatter_Notification__e(Target_ID__c = account.Id, Message__c = '{' + account.OwnerId + '} Please follow up with this test'));
chatterNotifications.add(new Chatter_Notification__e(Target_ID__c = account.Id, Message__c = '{' + account.OwnerId + '} Please follow up with this test'));
List<Database.SaveResult> saveResults = Eventbus.publish(chatterNotifications);
Test.getEventBus().deliver();
feedItems = [SELECT Id FROM AccountFeed WHERE ParentId = :account.Id];
system.debug('Ending count: ' + feedItems.size());
system.assertEquals(3, startingCount - feedItems.size());
Test.stopTest();
}
}
Platform Event Subscriber Config
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventSubscriberConfig xmlns="http://soap.sforce.com/2006/04/metadata">
<platformEventConsumer>ChatterNotificationTrigger</platformEventConsumer>
<batchSize>2000</batchSize>
<masterLabel>ChatterNotificationTriggerConfig</masterLabel>
<user>[email protected]</user>
<isProtected>false</isProtected>
</PlatformEventSubscriberConfig>