martyychang

CustomerCommunityUserTest

Feb 3rd, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.77 KB | None | 0 0
  1. /*
  2.  * Demonstrates problems with trying to create test customer community users.
  3.  * How can we get the User ID of a customer community user created within an
  4.  * Apex test method?
  5.  *
  6.  * These problems were last observed in API version 29.0
  7.  *
  8.  * @version 1.0
  9.  * @author  Marty Chang (Slalom Consulting)
  10.  */
  11. @isTest
  12. private class CustomerCommunityUserTest {
  13.    
  14.     /*
  15.      * First, we'll try to use the <code>Site.createPortalUser()</code>
  16.      * method, as instructed in the <i>Getting Started With Communities</i>
  17.      * guide for Winter '14.
  18.      *
  19.      * The problem here is that <code>Site.createPortalUser()</code> returns
  20.      * a null value instead of the created user's ID.
  21.      */
  22.     public static testMethod void testThatSiteCreatePortalUserReturnsId() {
  23.  
  24.         // Create the Account to use in creating the user
  25.  
  26.         Account customerAccount = new Account(Name = 'ApexUnit Corporation');
  27.         insert customerAccount;
  28.  
  29.         // Initialize the custommer community user record which we will
  30.         // feed into the Site.createPortalUser() method
  31.  
  32.         User customerUser = new User(
  33.             Username = '[email protected]',
  34.             Email = '[email protected]',
  35.             FirstName = 'John',
  36.             LastName = 'Smith',
  37.             Profile = new Profile(Name = 'Customer Community Login User'),
  38.             Alias = 'jsmit',
  39.             TimeZoneSidKey = 'America/Los_Angeles',
  40.             LocaleSidKey = 'en_US',
  41.             EmailEncodingKey = 'ISO-8859-1',
  42.             LanguageLocaleKey = 'en_US');
  43.  
  44.         // Use Site.createPortalUser() to create the customer community user
  45.  
  46.         Id customerUserId = Site.createPortalUser(
  47.             customerUser, customerAccount.Id, 'SimplePassword2014');
  48.  
  49.         // Validate the existence of a new customer community user
  50.         // by virtue of having that user's ID
  51.  
  52.         System.assert(customerUserId != null,
  53.             'No Customer Community User ID');
  54.     }   // public static testMethod void testThatSiteCreatePortalUserReturnsId()
  55.  
  56.     /*
  57.      * Next, we'll try using <code>Site.createPortalUser()</code> again, this
  58.      * time running as a Site Guest User. According to the auto-generated
  59.      * Salesforce test class when enabling Communities, we only get an ID
  60.      * when the method is run in the context of a guest user.
  61.      *
  62.      * However, we will see that this is not the case, and even running
  63.      * as a guest user fails to return a user ID.
  64.      */
  65.     public static testMethod void testThatGuestSiteCreatePortalUserReturnsId() {
  66.  
  67.         // Create the Account to use in creating the user
  68.  
  69.         Account customerAccount = new Account(Name = 'ApexUnit Corporation');
  70.         insert customerAccount;
  71.  
  72.         // Initialize the custommer community user record which we will
  73.         // feed into the Site.createPortalUser() method
  74.  
  75.         User customerUser = new User(
  76.             Username = '[email protected]',
  77.             Email = '[email protected]',
  78.             FirstName = 'John',
  79.             LastName = 'Smith',
  80.             Profile = new Profile(Name = 'Customer Community Login User'),
  81.             Alias = 'jsmit',
  82.             TimeZoneSidKey = 'America/Los_Angeles',
  83.             LocaleSidKey = 'en_US',
  84.             EmailEncodingKey = 'ISO-8859-1',
  85.             LanguageLocaleKey = 'en_US');
  86.  
  87.         // Locate a Site Guest User to use when creating
  88.         // the customer community user
  89.  
  90.         List<User> siteGuestUsers = [
  91.             SELECT Id
  92.             FROM User
  93.             WHERE Profile.UserLicense.Name = 'Guest User License'
  94.         ];
  95.  
  96.         System.assert(!siteGuestUsers.isEmpty(),
  97.             'We need at least one Site Guest User.' +
  98.                 'One should exist if we have at least one community');
  99.  
  100.         // Use Site.createPortalUser() to create the customer community user,
  101.         // running as a Site Guest User
  102.  
  103.         System.runAs(siteGuestUsers.get(0)) {
  104.             Id customerUserId = Site.createPortalUser(
  105.                 customerUser, customerAccount.Id, 'SimplePassword2014');
  106.  
  107.             // Validate the existence of a new customer community user
  108.             // by virtue of having that user's ID
  109.  
  110.             System.assert(customerUserId != null,
  111.                 'No Customer Community User ID');
  112.         }
  113.     }   // public static testMethod void testThatGuestSiteCreatePortalUserReturnsId()
  114.  
  115.     /*
  116.      * Lastly, we will try manually creating the user record for the
  117.      * customer commmunity user. This appears to be the only method that works,
  118.      * even though it is not how Salesforce recommends we create customer
  119.      * community users.
  120.      */
  121.     public static testMethod void testThatAdminCanCreateCommunityLoginUserWithoutRole() {
  122.  
  123.         // Create a user role for the test internal user that will own
  124.         // the customer account
  125.  
  126.         UserRole topOfTheHierarchy =
  127.             new UserRole(Name = 'Top of the Hierarchy');
  128.  
  129.         insert topOfTheHierarchy;
  130.  
  131.         // Create the internal user that will own the customer account.
  132.  
  133.         User accountExecutive = new User(
  134.             Username = '[email protected]',
  135.             Email = '[email protected]',
  136.             FirstName = 'Adam',
  137.             LastName = 'Apple',
  138.             UserRoleId = topOfTheHierarchy.Id,
  139.             Profile = new Profile(Name = 'Standard User'),
  140.             Alias = 'aappl',
  141.             TimeZoneSidKey = 'America/Los_Angeles',
  142.             LocaleSidKey = 'en_US',
  143.             EmailEncodingKey = 'ISO-8859-1',
  144.             LanguageLocaleKey = 'en_US');
  145.  
  146.         insert accountExecutive;
  147.  
  148.         // We need to use System.runAs() in order to avoid another catch-22
  149.         // with Apex tests, where Salesforce throws a MIXED_DML_OPERATION
  150.         // exception, "DML operation on setup object is not permitted
  151.         // after you have updated a non-setup object (or vice versa)"
  152.  
  153.         System.runAs(new User(Id = UserInfo.getUserId())) {
  154.             System.assert(accountExecutive.Id != null,
  155.                 'We must have a User ID for our account executive');
  156.  
  157.             // Create the Account to use in creating the user
  158.  
  159.             Account customerAccount = new Account(
  160.                 Name = 'ApexUnit Corporation',
  161.                 OwnerId = accountExecutive.Id);
  162.            
  163.                 insert customerAccount;
  164.  
  165.             // Create the customer's Contact to use when creating the user
  166.  
  167.             Contact customerContact = new Contact(
  168.                 FirstName = 'John',
  169.                 LastName = 'Smith',
  170.                 Email = '[email protected]',
  171.                 AccountId = customerAccount.Id);
  172.  
  173.             insert customerContact;
  174.  
  175.             // Initialize the custommer community user record which we will
  176.             // feed into the Site.createPortalUser() method
  177.  
  178.             User customerUser = new User(
  179.                 Username = '[email protected]',
  180.                 Email = '[email protected]',
  181.                 FirstName = 'John',
  182.                 LastName = 'Smith',
  183.                 Profile = new Profile(Name = 'Customer Community Login User'),
  184.                 Alias = 'jsmit',
  185.                 TimeZoneSidKey = 'America/Los_Angeles',
  186.                 LocaleSidKey = 'en_US',
  187.                 EmailEncodingKey = 'ISO-8859-1',
  188.                 LanguageLocaleKey = 'en_US',
  189.                 ContactId = customerContact.Id);
  190.  
  191.             insert customerUser;
  192.  
  193.             // Validate the existence of a new customer community user
  194.             // by virtue of having that user's ID
  195.  
  196.             System.assert(customerUser.Id != null,
  197.                 'No Customer Community User ID');
  198.         }   // System.runAs(new User(Id = UserInfo.getUserId()))
  199.     }   // public static testMethod void testThatAdminCanCreateCommunityLoginUserWithoutRole()
  200. }
Advertisement
Add Comment
Please, Sign In to add comment