Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Demonstrates problems with trying to create test customer community users.
- * How can we get the User ID of a customer community user created within an
- * Apex test method?
- *
- * These problems were last observed in API version 29.0
- *
- * @version 1.0
- * @author Marty Chang (Slalom Consulting)
- */
- @isTest
- private class CustomerCommunityUserTest {
- /*
- * First, we'll try to use the <code>Site.createPortalUser()</code>
- * method, as instructed in the <i>Getting Started With Communities</i>
- * guide for Winter '14.
- *
- * The problem here is that <code>Site.createPortalUser()</code> returns
- * a null value instead of the created user's ID.
- */
- public static testMethod void testThatSiteCreatePortalUserReturnsId() {
- // Create the Account to use in creating the user
- Account customerAccount = new Account(Name = 'ApexUnit Corporation');
- insert customerAccount;
- // Initialize the custommer community user record which we will
- // feed into the Site.createPortalUser() method
- User customerUser = new User(
- Username = '[email protected]',
- Email = '[email protected]',
- FirstName = 'John',
- LastName = 'Smith',
- Profile = new Profile(Name = 'Customer Community Login User'),
- Alias = 'jsmit',
- TimeZoneSidKey = 'America/Los_Angeles',
- LocaleSidKey = 'en_US',
- EmailEncodingKey = 'ISO-8859-1',
- LanguageLocaleKey = 'en_US');
- // Use Site.createPortalUser() to create the customer community user
- Id customerUserId = Site.createPortalUser(
- customerUser, customerAccount.Id, 'SimplePassword2014');
- // Validate the existence of a new customer community user
- // by virtue of having that user's ID
- System.assert(customerUserId != null,
- 'No Customer Community User ID');
- } // public static testMethod void testThatSiteCreatePortalUserReturnsId()
- /*
- * Next, we'll try using <code>Site.createPortalUser()</code> again, this
- * time running as a Site Guest User. According to the auto-generated
- * Salesforce test class when enabling Communities, we only get an ID
- * when the method is run in the context of a guest user.
- *
- * However, we will see that this is not the case, and even running
- * as a guest user fails to return a user ID.
- */
- public static testMethod void testThatGuestSiteCreatePortalUserReturnsId() {
- // Create the Account to use in creating the user
- Account customerAccount = new Account(Name = 'ApexUnit Corporation');
- insert customerAccount;
- // Initialize the custommer community user record which we will
- // feed into the Site.createPortalUser() method
- User customerUser = new User(
- Username = '[email protected]',
- Email = '[email protected]',
- FirstName = 'John',
- LastName = 'Smith',
- Profile = new Profile(Name = 'Customer Community Login User'),
- Alias = 'jsmit',
- TimeZoneSidKey = 'America/Los_Angeles',
- LocaleSidKey = 'en_US',
- EmailEncodingKey = 'ISO-8859-1',
- LanguageLocaleKey = 'en_US');
- // Locate a Site Guest User to use when creating
- // the customer community user
- List<User> siteGuestUsers = [
- SELECT Id
- FROM User
- WHERE Profile.UserLicense.Name = 'Guest User License'
- ];
- System.assert(!siteGuestUsers.isEmpty(),
- 'We need at least one Site Guest User.' +
- 'One should exist if we have at least one community');
- // Use Site.createPortalUser() to create the customer community user,
- // running as a Site Guest User
- System.runAs(siteGuestUsers.get(0)) {
- Id customerUserId = Site.createPortalUser(
- customerUser, customerAccount.Id, 'SimplePassword2014');
- // Validate the existence of a new customer community user
- // by virtue of having that user's ID
- System.assert(customerUserId != null,
- 'No Customer Community User ID');
- }
- } // public static testMethod void testThatGuestSiteCreatePortalUserReturnsId()
- /*
- * Lastly, we will try manually creating the user record for the
- * customer commmunity user. This appears to be the only method that works,
- * even though it is not how Salesforce recommends we create customer
- * community users.
- */
- public static testMethod void testThatAdminCanCreateCommunityLoginUserWithoutRole() {
- // Create a user role for the test internal user that will own
- // the customer account
- UserRole topOfTheHierarchy =
- new UserRole(Name = 'Top of the Hierarchy');
- insert topOfTheHierarchy;
- // Create the internal user that will own the customer account.
- User accountExecutive = new User(
- Username = '[email protected]',
- Email = '[email protected]',
- FirstName = 'Adam',
- LastName = 'Apple',
- UserRoleId = topOfTheHierarchy.Id,
- Profile = new Profile(Name = 'Standard User'),
- Alias = 'aappl',
- TimeZoneSidKey = 'America/Los_Angeles',
- LocaleSidKey = 'en_US',
- EmailEncodingKey = 'ISO-8859-1',
- LanguageLocaleKey = 'en_US');
- insert accountExecutive;
- // We need to use System.runAs() in order to avoid another catch-22
- // with Apex tests, where Salesforce throws a MIXED_DML_OPERATION
- // exception, "DML operation on setup object is not permitted
- // after you have updated a non-setup object (or vice versa)"
- System.runAs(new User(Id = UserInfo.getUserId())) {
- System.assert(accountExecutive.Id != null,
- 'We must have a User ID for our account executive');
- // Create the Account to use in creating the user
- Account customerAccount = new Account(
- Name = 'ApexUnit Corporation',
- OwnerId = accountExecutive.Id);
- insert customerAccount;
- // Create the customer's Contact to use when creating the user
- Contact customerContact = new Contact(
- FirstName = 'John',
- LastName = 'Smith',
- Email = '[email protected]',
- AccountId = customerAccount.Id);
- insert customerContact;
- // Initialize the custommer community user record which we will
- // feed into the Site.createPortalUser() method
- User customerUser = new User(
- Username = '[email protected]',
- Email = '[email protected]',
- FirstName = 'John',
- LastName = 'Smith',
- Profile = new Profile(Name = 'Customer Community Login User'),
- Alias = 'jsmit',
- TimeZoneSidKey = 'America/Los_Angeles',
- LocaleSidKey = 'en_US',
- EmailEncodingKey = 'ISO-8859-1',
- LanguageLocaleKey = 'en_US',
- ContactId = customerContact.Id);
- insert customerUser;
- // Validate the existence of a new customer community user
- // by virtue of having that user's ID
- System.assert(customerUser.Id != null,
- 'No Customer Community User ID');
- } // System.runAs(new User(Id = UserInfo.getUserId()))
- } // public static testMethod void testThatAdminCanCreateCommunityLoginUserWithoutRole()
- }
Advertisement
Add Comment
Please, Sign In to add comment