Guest User

Untitled

a guest
Dec 7th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. public SiteRegisterController () {
  2. }
  3.  
  4. public String username {get; set;}
  5. public String email {get; set;}
  6. public String password {get; set {password = value == null ? value : value.trim(); } }
  7. public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
  8. public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
  9.  
  10. private boolean isValidPassword() {
  11. return password == confirmPassword;
  12. }
  13.  
  14. public PageReference registerUser() {
  15. // it's okay if password is null - we'll send the user a random password in that case
  16. if (!isValidPassword()) {
  17. ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
  18. ApexPages.addMessage(msg);
  19. return null;
  20. }
  21. User u = new User();
  22. u.Username = username;
  23. u.Email = email;
  24. u.CommunityNickname = communityNickname;
  25.  
  26. String accountId = PORTAL_ACCOUNT_ID;
  27.  
  28. // lastName is a required field on user, but if it isn't specified, we'll default it to the username
  29. String userId = Site.createPortalUser(u, accountId, password);
  30. if (userId != null) {
  31. if (password != null && password.length() > 1) {
  32. return Site.login(username, password, null);
  33. }
  34. else {
  35. PageReference page = System.Page.SiteRegisterConfirm;
  36. page.setRedirect(true);
  37. return page;
  38. }
  39. }
  40. return null;
  41. }
  42.  
  43. // Test method to bring this class's test coverage over the required 75%
  44. static testMethod void testRegistration() {
  45. SiteRegisterController controller = new SiteRegisterController();
  46. controller.username = 'test@force.com';
  47. controller.email = 'test@force.com';
  48. controller.communityNickname = 'test';
  49. // registerUser will always return null when the page isn't accessed as a guest user
  50. System.assert(controller.registerUser() == null);
  51.  
  52. controller.password = 'abcd1234';
  53. controller.confirmPassword = 'abcd123';
  54. System.assert(controller.registerUser() == null);
  55. }
  56. static testmethod void testuserid(){
  57. Account ac = new Account(name ='testAccount') ;
  58. insert ac;
  59.  
  60. Contact con = new Contact(FirstName='testName',LastName ='testCon',AccountId = ac.Id,email='test123@test.com');
  61. insert con;
  62. profile p=[select id,name from profile where id='00e20000001VEG9'];
  63.  
  64. //user u= testUtil.buildAUPartnerUser(con);
  65. //insert u;
  66. User newUser = new User();
  67.  
  68. newUser.ContactId = con.Id;
  69. newUser.Alias = (con.FirstName != null ? con.FirstName.substring(1,1) : '') + (con.LastName.length() > 6 ? con.LastName.substring(1,6) : con.LastName);
  70. newUser.Username = Utils.getSandboxMaskedPortalUsername(con.Email);
  71. newUser.FirstName = con.FirstName;
  72. newUser.LastName = con.LastName;
  73. newUser.ProfileId = p.Id;
  74. newUser.ContactId = con.Id;
  75. newUser.Email = Utils.getSandboxMaskedEmail(con.Email);
  76. newUser.EmailEncodingKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_EMAIL_ENCODING_KEY;
  77. newUser.LanguageLocaleKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_LANGUAGE_LOCALE_KEY;
  78. newUser.LocaleSidKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_LOCALE_SID_KEY;
  79. newUser.TimezoneSidKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_TIMEZONE_SID_KEY;
  80. newUser.IsActive = true;
  81.  
  82. insert newUser;
  83. // user u=[select name,email,communityNickname from user where id=:newUser.Id];
  84. SiteRegisterController controller = new SiteRegisterController();
  85. string username = newUser.Name;
  86. //system.assert(newUser.Name!=null);
  87. system.debug('User name:'+username);
  88. controller.email = newUser.Email;
  89. controller.communityNickname = newUser.communityNickname;
  90. // registerUser will always return null when the page isn't accessed as a guest user
  91. System.assert(controller.registerUser() == null);
  92. //controller.setpassword('abcd1234');
  93. controller.password = 'abcd123';
  94. controller.confirmPassword = 'abcd123';
  95. System.assert(controller.registerUser() == null);
  96. string accountId='001x000xxx35tPN';
  97. string userid=newUser.id;
  98. string pwd=controller.password;
  99. //string password='abcdqwe';
  100. system.assert(userid!=null);
  101. if (pwd != null && pwd.length() > 1) {
  102. Site.login(username, pwd, null);
  103. }
Add Comment
Please, Sign In to add comment