I have to create a test class that checks for this "if" condition, and I don't know how to set user.Profile.name for test data.
//System Admin User Id
string userId = UserInfo.getUserId();
//Get user profile
User user = [select id, ProfileId, Profile.Name from user where id = :userId];
Config__c Config = [select System_Admin_Id__c from Config__c];
if(user.Profile.Name != 'System Administrator'){
...
}
I have tried this:
String uniqueUserName = 'user' + DateTime.now().getTime() + '@org.com';
User user = new User(Username = uniqueUserName,
Email = '[email protected]',
LastName = 'Admin',
ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' limit 1].Id,
Alias = 'testUser',
TimeZoneSidKey = 'Asia/Bangkok',
LocaleSidKey = 'th_TH',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert adminUser;
Config__c config = new Config__c(
System_Admin_Id__c = user1.Id
);
insert config;
but turned out that the user.Profile.name is null. Please help me.