You need to sign in to do that
Don't have an account?

Hi Developers . can you help me in tes class?
Can you help me how to test this controller :( plsss i need your help.
public with sharing class GHI_Portal_Login {
public Boolean hasInputUsername { get; set; }
public Boolean hasInputPassword { get; set; }
private List<User> userLogin;
public String username { get; set; }
public String password { get; set; }
public String loginMessage { get; set; }
public GHI_Portal_Login(){
this.hasInputUsername = true;
this.hasInputPassword = true;
String aMsg = ApexPages.currentPage().getParameters().get('A');
aMsg = (aMsg != null && aMsg != '') ? aMsg : '';
this.loginMessage = '';
if (aMsg == '1') {
this.loginMessage = 'Please log in.';
} else if (aMsg == '2') {
this.loginMessage = 'Your session has expired. Please log in.';
}
}
public PageReference login() {
// where startUrl is the landing page after logging in
String startUrl = '/apex/GHI_Portal_Home';
if (username.length() == 0) {
this.hasInputUsername = false;
} else {
this.hasInputUsername = true;
}
if (password.length() == 0) {
this.hasInputPassword = false;
} else {
this.hasInputPassword = true;
}
userLogin = new List<User>([SELECT Username, GHI_Portal_Number_of_Attempts__c FROM User WHERE Username = :username LIMIT 1]);
PageReference prSiteLogin = Site.login(username, password, startUrl);
if (userLogin.size() == 0) {
this.loginMessage = 'Username is not valid';
} else {
if(userLogin[0].GHI_Portal_Number_of_Attempts__c < 4 || userLogin[0].GHI_Portal_Number_of_Attempts__c == null){
if (prSiteLogin == null && this.hasInputPassword) {
updateUserLoginAttempt();
Integer numberOfAttempts = 5 - Integer.valueOf(userLogin[0].GHI_Portal_Number_of_Attempts__c);
this.loginMessage = 'Login attempt unsuccessful. You have '+numberOfAttempts+' remaining.' ;
}
else {
this.loginMessage = '';
}
}
else{
this.loginMessage = 'Account locked. To reset password select the Forgot Password link below. For additional assistance, contact Customer Service.';
}
}
if(hasInputUsername && hasInputPassword){
List <LoginHistory> loginHistory= [SELECT LoginTime,Status,UserId FROM LoginHistory where UserId=:userLogin[0].Id order by LoginTime DESC limit 1];
if(loginHistory.size() > 0){
if(loginHistory[0].Status=='Success'){
userLogin[0].GHI_Portal_Number_of_Attempts__c = 0;
update userLogin;
}
}
return prSiteLogin;
}
else{
return ApexPages.currentPage();
}
}
public void updateUserLoginAttempt() {
if(userLogin.size()>0){
if(userLogin[0].GHI_Portal_Number_of_Attempts__c!=null){
userLogin[0].GHI_Portal_Number_of_Attempts__c +=1;
}
else{
userLogin[0].GHI_Portal_Number_of_Attempts__c = 0;
userLogin[0].GHI_Portal_Number_of_Attempts__c +=1;
}
update userLogin;
}
}
/* PAGE REDIRECTIONS */
public PageReference goToHome() {
PageReference pr = Page.GHI_Portal_Home;
pr.setRedirect(true);
return pr;
}
public PageReference goToPasswordResetStep1() {
PageReference pr = Page.GHI_Portal_PasswordReset_Step1;
pr.setRedirect(true);
return pr;
}
public PageReference goToUserNameResetStep1() {
PageReference pr = Page.GHI_Portal_ForgottenUserName_Step1 ;
pr.setRedirect(true);
return pr;
}
}
public with sharing class GHI_Portal_Login {
public Boolean hasInputUsername { get; set; }
public Boolean hasInputPassword { get; set; }
private List<User> userLogin;
public String username { get; set; }
public String password { get; set; }
public String loginMessage { get; set; }
public GHI_Portal_Login(){
this.hasInputUsername = true;
this.hasInputPassword = true;
String aMsg = ApexPages.currentPage().getParameters().get('A');
aMsg = (aMsg != null && aMsg != '') ? aMsg : '';
this.loginMessage = '';
if (aMsg == '1') {
this.loginMessage = 'Please log in.';
} else if (aMsg == '2') {
this.loginMessage = 'Your session has expired. Please log in.';
}
}
public PageReference login() {
// where startUrl is the landing page after logging in
String startUrl = '/apex/GHI_Portal_Home';
if (username.length() == 0) {
this.hasInputUsername = false;
} else {
this.hasInputUsername = true;
}
if (password.length() == 0) {
this.hasInputPassword = false;
} else {
this.hasInputPassword = true;
}
userLogin = new List<User>([SELECT Username, GHI_Portal_Number_of_Attempts__c FROM User WHERE Username = :username LIMIT 1]);
PageReference prSiteLogin = Site.login(username, password, startUrl);
if (userLogin.size() == 0) {
this.loginMessage = 'Username is not valid';
} else {
if(userLogin[0].GHI_Portal_Number_of_Attempts__c < 4 || userLogin[0].GHI_Portal_Number_of_Attempts__c == null){
if (prSiteLogin == null && this.hasInputPassword) {
updateUserLoginAttempt();
Integer numberOfAttempts = 5 - Integer.valueOf(userLogin[0].GHI_Portal_Number_of_Attempts__c);
this.loginMessage = 'Login attempt unsuccessful. You have '+numberOfAttempts+' remaining.' ;
}
else {
this.loginMessage = '';
}
}
else{
this.loginMessage = 'Account locked. To reset password select the Forgot Password link below. For additional assistance, contact Customer Service.';
}
}
if(hasInputUsername && hasInputPassword){
List <LoginHistory> loginHistory= [SELECT LoginTime,Status,UserId FROM LoginHistory where UserId=:userLogin[0].Id order by LoginTime DESC limit 1];
if(loginHistory.size() > 0){
if(loginHistory[0].Status=='Success'){
userLogin[0].GHI_Portal_Number_of_Attempts__c = 0;
update userLogin;
}
}
return prSiteLogin;
}
else{
return ApexPages.currentPage();
}
}
public void updateUserLoginAttempt() {
if(userLogin.size()>0){
if(userLogin[0].GHI_Portal_Number_of_Attempts__c!=null){
userLogin[0].GHI_Portal_Number_of_Attempts__c +=1;
}
else{
userLogin[0].GHI_Portal_Number_of_Attempts__c = 0;
userLogin[0].GHI_Portal_Number_of_Attempts__c +=1;
}
update userLogin;
}
}
/* PAGE REDIRECTIONS */
public PageReference goToHome() {
PageReference pr = Page.GHI_Portal_Home;
pr.setRedirect(true);
return pr;
}
public PageReference goToPasswordResetStep1() {
PageReference pr = Page.GHI_Portal_PasswordReset_Step1;
pr.setRedirect(true);
return pr;
}
public PageReference goToUserNameResetStep1() {
PageReference pr = Page.GHI_Portal_ForgottenUserName_Step1 ;
pr.setRedirect(true);
return pr;
}
}
Read this, it will help you - https://developer.salesforce.com/trailhead/module/apex_testing