Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public SiteRegisterController () {
- }
- public String username {get; set;}
- public String email {get; set;}
- public String password {get; set {password = value == null ? value : value.trim(); } }
- public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
- public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
- private boolean isValidPassword() {
- return password == confirmPassword;
- }
- public PageReference registerUser() {
- // it's okay if password is null - we'll send the user a random password in that case
- if (!isValidPassword()) {
- ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
- ApexPages.addMessage(msg);
- return null;
- }
- User u = new User();
- u.Username = username;
- u.Email = email;
- u.CommunityNickname = communityNickname;
- String accountId = PORTAL_ACCOUNT_ID;
- // lastName is a required field on user, but if it isn't specified, we'll default it to the username
- String userId = Site.createPortalUser(u, accountId, password);
- if (userId != null) {
- if (password != null && password.length() > 1) {
- return Site.login(username, password, null);
- }
- else {
- PageReference page = System.Page.SiteRegisterConfirm;
- page.setRedirect(true);
- return page;
- }
- }
- return null;
- }
- // Test method to bring this class's test coverage over the required 75%
- static testMethod void testRegistration() {
- SiteRegisterController controller = new SiteRegisterController();
- controller.username = 'test@force.com';
- controller.email = 'test@force.com';
- controller.communityNickname = 'test';
- // registerUser will always return null when the page isn't accessed as a guest user
- System.assert(controller.registerUser() == null);
- controller.password = 'abcd1234';
- controller.confirmPassword = 'abcd123';
- System.assert(controller.registerUser() == null);
- }
- static testmethod void testuserid(){
- Account ac = new Account(name ='testAccount') ;
- insert ac;
- Contact con = new Contact(FirstName='testName',LastName ='testCon',AccountId = ac.Id,email='test123@test.com');
- insert con;
- profile p=[select id,name from profile where id='00e20000001VEG9'];
- //user u= testUtil.buildAUPartnerUser(con);
- //insert u;
- User newUser = new User();
- newUser.ContactId = con.Id;
- newUser.Alias = (con.FirstName != null ? con.FirstName.substring(1,1) : '') + (con.LastName.length() > 6 ? con.LastName.substring(1,6) : con.LastName);
- newUser.Username = Utils.getSandboxMaskedPortalUsername(con.Email);
- newUser.FirstName = con.FirstName;
- newUser.LastName = con.LastName;
- newUser.ProfileId = p.Id;
- newUser.ContactId = con.Id;
- newUser.Email = Utils.getSandboxMaskedEmail(con.Email);
- newUser.EmailEncodingKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_EMAIL_ENCODING_KEY;
- newUser.LanguageLocaleKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_LANGUAGE_LOCALE_KEY;
- newUser.LocaleSidKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_LOCALE_SID_KEY;
- newUser.TimezoneSidKey = PartnerUserAccessProvisioningUtil.PARTNER_USER_DEFAULT_TIMEZONE_SID_KEY;
- newUser.IsActive = true;
- insert newUser;
- // user u=[select name,email,communityNickname from user where id=:newUser.Id];
- SiteRegisterController controller = new SiteRegisterController();
- string username = newUser.Name;
- //system.assert(newUser.Name!=null);
- system.debug('User name:'+username);
- controller.email = newUser.Email;
- controller.communityNickname = newUser.communityNickname;
- // registerUser will always return null when the page isn't accessed as a guest user
- System.assert(controller.registerUser() == null);
- //controller.setpassword('abcd1234');
- controller.password = 'abcd123';
- controller.confirmPassword = 'abcd123';
- System.assert(controller.registerUser() == null);
- string accountId='001x000xxx35tPN';
- string userid=newUser.id;
- string pwd=controller.password;
- //string password='abcdqwe';
- system.assert(userid!=null);
- if (pwd != null && pwd.length() > 1) {
- Site.login(username, pwd, null);
- }
Add Comment
Please, Sign In to add comment