Created
February 23, 2022 21:01
-
-
Save snugsfbay/1f020eded09e04a402c1be73e8768947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IsTest | |
public with sharing class UserTest { | |
// from Trailblazer Community https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000FSHP0SAP | |
public static String getAppName() { | |
UserAppInfo userAppInfo = [SELECT Id, AppDefinitionId FROM UserAppInfo WHERE UserId = :UserInfo.getUserId() LIMIT 1]; | |
AppDefinition appDefinition = [SELECT DurableId, Label FROM AppDefinition Where DurableId = :userAppInfo.AppDefinitionId LIMIT 1]; | |
return appDefinition.Label; | |
} | |
// failing test to illustrate the problem | |
@IsTest | |
static void queryTest(){ | |
User appUser = new User(); | |
try{ | |
appUser = [SELECT id FROM User WHERE Id IN (SELECT userid FROM UserAppInfo) LIMIT 1]; | |
} catch (Exception e){ | |
System.assert(false, e.getMessage()); | |
} | |
System.assert(appUser.Id != null, 'User should be found if that user opened an app in the test context.'); | |
} | |
// passing test to illustrate the solution | |
@IsTest | |
static void newUser(){ | |
User standardUser = createUser('Standard User'); | |
String appLabel = 'Sales'; | |
insert standardUser; | |
getLabelAs(standardUser, appLabel); | |
} | |
// reuseable code in case there is a need to run the same test for multiple users/profiles | |
private static void getLabelAs(User u, String appLabel){ | |
String label = ''; | |
UserAppInfo app = new UserAppInfo(); | |
System.runAs(u){ | |
app = insertUserAppInfo(u, appLabel); | |
label = getAppName(); | |
} | |
System.assertEquals(label,appLabel, 'User app info record should be for the ' + appLabel +' record rather than ' +label); | |
} | |
// if you can't insert a user with this method, does the org have a user record you can query for instead? | |
private static User createUser(String profileName) { | |
Id profileID = [SELECT id FROM Profile WHERE Name = :profileName LIMIT 1]?.id; | |
User newUser = New User( | |
FirstName = 'Test', | |
LastName = 'User', | |
Email = UserInfo.getUserEmail(), | |
Alias = 'test', | |
Username = 'test' + profileID + '@test.com', | |
LocaleSidKey = 'en_US', | |
TimeZoneSidKey = 'GMT', | |
ProfileId = profileId, | |
LanguageLocaleKey = 'en_US', | |
EmailEncodingKey = 'UTF-8', | |
IsActive = true | |
); | |
return newUser; | |
} | |
// setup a record for the running user to indicate their UserAppInfo with a nonspecific AppDefinition | |
private static UserAppInfo insertUserAppInfo(User u, String name){ | |
AppDefinition appDefinition = [SELECT DurableId, Label FROM AppDefinition WHERE Label = :name LIMIT 1]; | |
UserAppInfo app = new UserAppInfo(AppDefinitionId = appDefinition.DurableId, UserId = u.id, FormFactor = 'Small'); | |
insert app; | |
return app; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment