Last active
August 29, 2015 14:04
-
-
Save pcon/ca95d121d71ead05dedf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Gets an admin user | |
* | |
* @return An admin user | |
*/ | |
public static User getAdminUser() { | |
Profile adminProfile = [ | |
select id | |
from profile | |
where name = 'System Administrator' | |
]; | |
User admin = [ | |
select Name | |
from User | |
where ProfileId = :adminProfile.Id and | |
isActive = true and | |
LocaleSidKey = 'en_US' | |
limit 1 | |
]; | |
return admin; | |
} | |
/** | |
* Gets a user with the given profile and role | |
* | |
* @param username The username to use (must be in the form of an email) | |
* @param profile The name of the profile to use | |
* @param role The name of the role to use | |
* @return A test user | |
*/ | |
public static User getTestUser(String username, String profile, String role) { | |
Profile currentProfile = [ | |
select id | |
from profile | |
where name = :profile | |
]; | |
Userrole currentRole = [ | |
select id | |
from UserRole | |
where Name = :role | |
]; | |
return new User( | |
alias = 'admintes', | |
email = '[email protected]', | |
emailencodingkey = 'UTF-8', | |
lastname = 'Testing', | |
languagelocalekey = 'en_US', | |
localesidkey = 'en_US', | |
profileid = currentProfile.Id, | |
timezonesidkey = 'America/New_York', | |
username = username, | |
userroleid = currentRole.Id, | |
); | |
} | |
/** | |
* Creates a test user | |
* | |
* (NOTE: inserts user record) | |
* @param username The username to use (must be in the form of an email) | |
* @param profile The name of the profile to use | |
* @param role The name of the role to use | |
* @return A test user | |
*/ | |
public static User createTestUser(String username, String profile, String role) { | |
User admin = getAdminUser(); | |
User testUser; | |
System.runAs(admin) { | |
testUser = getTestUser(username, profile, role); | |
insert testUser; | |
} | |
return testUser; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment