I have a trigger on UserChangeEvent that upon Email change event, performs certain operations with the new email address.
Upon email change, Salesforce sends a confirmation email to the user, who then has to verify the change in order for the event to fire. I have successfully manually tested this logic and it works perfect, however, I could not find a way to write a script to test this.
My script successfully creates the user but the email change update never fires (possibly because that cannot be automated)
How can I solve this problem?
@isTest
public class UserEmailChangeTriggerTest {
@isTest static void updateUserTest() {
List<User> user_list = new List<User>();
Id profileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id;
for(Integer i = 1; i <= 2; i++) {
String email = UserInfo.getUserEmail().substringBefore('@') + '+test' + i + '@' + UserInfo.getUserEmail().substringAfter('@');
user_list.add(new User(
FirstName = 'Test',
LastName = 'User ' + i,
Email = email,
Username = email + '.test',
ProfileId = profileId,
Alias = 'tUser' + i,
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
));
}
System.debug('Users created -');
System.debug(user_list);
Test.startTest();
insert user_list;
Test.enableChangeDataCapture();
user_list[1].Email = UserInfo.getUserEmail().substringBefore('@') + '+test1a@' + UserInfo.getUserEmail().substringAfter('@');
update user_list;
Test.getEventBus().deliver();
Test.stopTest();
}
}