You need to sign in to do that
Don't have an account?
SalesforceAddict
i want to write test class and cover cookies code help?
Apex Class===
public class LoginController {
public static String FinalString;
public static String userEmail;
Public String UserName{get;set;}
Public String Password{get;set;}
Public Boolean IsLoggedIn{get;set;}
Public String PortalOwnerId{get;set;}
Public String LogInPlace{get;set;}
Public Boolean UserNamePasswordMatched{get;set;}
public String AccountRecordTypeId{get;set;}
public Account acc{get;set;}
public String accId{get;set;}
public Account acc1{get;set;}
public Account accConfirmPassword{get;set;}
public Account accIdToChangePassword{get;set;}
public list<account> accList{get;set;}
public static list<String> setToAddresses{get;set;}
public String PortalUserType{get;set;}
public Boolean isBlankPassword{get;set;}
public String accIdToGetCurrentPage{get;set;}
public Boolean isActive{get;set;}
public LoginController(){
acc=new Account();
acc1=new Account();
accConfirmPassword=new Account();
accList = new list<account>();
isLoggedIn=true;
isBlankPassword = true;
UserName='';
AccountRecordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Professional').getRecordTypeId();
if(userinfo.getUserId()==System.Label.SiteUserId){
LogInPlace='Site';
}else{
LogInPlace='Salesforce';
}
if(ApexPages.currentPage().getCookies().get('Username') != null && ApexPages.currentPage().getCookies().get('PassWord') != null && ApexPages.currentPage().getCookies().get('Type') != null){
String CookieUserName=ApexPages.currentPage().getCookies().get('Username').getValue();
String CookiePassWord=ApexPages.currentPage().getCookies().get('PassWord').getValue();
Boolean IsVerifiedUser=false;
for(Account acc:[SELECT Id,Name,User_Name__c,Password__c,Portal_User_Type__c FROM Account WHERE User_Name__c=: CookieUserName AND Password__c=: CookiePassWord AND RecordTypeId=:AccountRecordTypeId]){
PortalOwnerId=acc.id;
IsVerifiedUser=true;
isLoggedIn=true;
PortalUserType=acc.Portal_User_Type__c ;
if(String.IsNotBlank(PortalUserType)){
if(PortalUserType.contains('Pioneer') && !PortalUserType.contains('PathFinder')){
Cookie portaltypeStore = new Cookie('Type','Pioneer',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
else{
Cookie portaltypeStore = new Cookie('Type','PathFinder',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
}
Cookie UserStore = new Cookie('Username',acc.User_Name__c,null,900,true);
Cookie passwordStore = new Cookie('PassWord',acc.Password__c,null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{UserStore});
ApexPages.currentPage().setCookies(new Cookie[]{passwordStore});
}
if(!IsVerifiedUser){
isLoggedIn=false;
}
}else{
isLoggedIn=false;
}
}
public Pagereference SubmitPage(){
if(String.IsNotBlank(UserName) && String.IsNotBlank(PassWord)){
UserNamePasswordMatched=false;
for(Account acc:[SELECT Id,User_Name__c,Password__c,Portal_User_Type__c,isActivate__c FROM Account WHERE User_Name__c=: UserName AND Password__c=: PassWord AND RecordTypeId=:AccountRecordTypeId]){
if(acc.Portal_User_Type__c != null && acc.Password__c !=null){
PortalOwnerId=acc.id;
UserNamePasswordMatched = true;
isActive = true;
Cookie UserStore = new Cookie('Username',UserName,null,900,true);
Cookie passwordStore = new Cookie('PassWord',PassWord,null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{UserStore});
ApexPages.currentPage().setCookies(new Cookie[]{passwordStore});
PortalUserType=acc.Portal_User_Type__c ;
if(String.IsNotBlank(PortalUserType)){
if(PortalUserType.contains('Pioneer') && !PortalUserType.contains('PathFinder')){
Cookie portaltypeStore = new Cookie('Type','Pioneer',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
else{
Cookie portaltypeStore = new Cookie('Type','PathFinder',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
}
if(acc.isActivate__c){
//isActive = true;
if(LogInPlace=='Site'){
PageReference pg = new PageReference('/Portal/PathFinderLinksVf');
if(String.isNotBlank(ApexPages.CurrentPage().getParameters().get('retUrl')))
pg = new PageReference(ApexPages.CurrentPage().getParameters().get('retUrl'));
pg.setRedirect(false);
return pg;
}else{
PageReference pg = new PageReference('/apex/PathFinderLinksVf');
if(String.isNotBlank(ApexPages.CurrentPage().getParameters().get('retUrl')))
pg = new PageReference(ApexPages.CurrentPage().getParameters().get('retUrl'));
pg.setRedirect(false);
return pg;
}
}else{
return null;
}
}
}
}
return null;
}
public PageReference VerifyPassword() {
System.debug('isLoggedIn:- '+isLoggedIn);
if(isLoggedIn){
if(LogInPlace=='Site'){
PageReference pg = new PageReference('/Portal/PathFinderLinksVf');
pg.setRedirect(false);
return pg;
}else{
PageReference pg = new PageReference('/apex/PathFinderLinksVf');
pg.setRedirect(false);
return pg;
}
}
return null;
}
public class LoginController {
public static String FinalString;
public static String userEmail;
Public String UserName{get;set;}
Public String Password{get;set;}
Public Boolean IsLoggedIn{get;set;}
Public String PortalOwnerId{get;set;}
Public String LogInPlace{get;set;}
Public Boolean UserNamePasswordMatched{get;set;}
public String AccountRecordTypeId{get;set;}
public Account acc{get;set;}
public String accId{get;set;}
public Account acc1{get;set;}
public Account accConfirmPassword{get;set;}
public Account accIdToChangePassword{get;set;}
public list<account> accList{get;set;}
public static list<String> setToAddresses{get;set;}
public String PortalUserType{get;set;}
public Boolean isBlankPassword{get;set;}
public String accIdToGetCurrentPage{get;set;}
public Boolean isActive{get;set;}
public LoginController(){
acc=new Account();
acc1=new Account();
accConfirmPassword=new Account();
accList = new list<account>();
isLoggedIn=true;
isBlankPassword = true;
UserName='';
AccountRecordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Professional').getRecordTypeId();
if(userinfo.getUserId()==System.Label.SiteUserId){
LogInPlace='Site';
}else{
LogInPlace='Salesforce';
}
if(ApexPages.currentPage().getCookies().get('Username') != null && ApexPages.currentPage().getCookies().get('PassWord') != null && ApexPages.currentPage().getCookies().get('Type') != null){
String CookieUserName=ApexPages.currentPage().getCookies().get('Username').getValue();
String CookiePassWord=ApexPages.currentPage().getCookies().get('PassWord').getValue();
Boolean IsVerifiedUser=false;
for(Account acc:[SELECT Id,Name,User_Name__c,Password__c,Portal_User_Type__c FROM Account WHERE User_Name__c=: CookieUserName AND Password__c=: CookiePassWord AND RecordTypeId=:AccountRecordTypeId]){
PortalOwnerId=acc.id;
IsVerifiedUser=true;
isLoggedIn=true;
PortalUserType=acc.Portal_User_Type__c ;
if(String.IsNotBlank(PortalUserType)){
if(PortalUserType.contains('Pioneer') && !PortalUserType.contains('PathFinder')){
Cookie portaltypeStore = new Cookie('Type','Pioneer',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
else{
Cookie portaltypeStore = new Cookie('Type','PathFinder',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
}
Cookie UserStore = new Cookie('Username',acc.User_Name__c,null,900,true);
Cookie passwordStore = new Cookie('PassWord',acc.Password__c,null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{UserStore});
ApexPages.currentPage().setCookies(new Cookie[]{passwordStore});
}
if(!IsVerifiedUser){
isLoggedIn=false;
}
}else{
isLoggedIn=false;
}
}
public Pagereference SubmitPage(){
if(String.IsNotBlank(UserName) && String.IsNotBlank(PassWord)){
UserNamePasswordMatched=false;
for(Account acc:[SELECT Id,User_Name__c,Password__c,Portal_User_Type__c,isActivate__c FROM Account WHERE User_Name__c=: UserName AND Password__c=: PassWord AND RecordTypeId=:AccountRecordTypeId]){
if(acc.Portal_User_Type__c != null && acc.Password__c !=null){
PortalOwnerId=acc.id;
UserNamePasswordMatched = true;
isActive = true;
Cookie UserStore = new Cookie('Username',UserName,null,900,true);
Cookie passwordStore = new Cookie('PassWord',PassWord,null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{UserStore});
ApexPages.currentPage().setCookies(new Cookie[]{passwordStore});
PortalUserType=acc.Portal_User_Type__c ;
if(String.IsNotBlank(PortalUserType)){
if(PortalUserType.contains('Pioneer') && !PortalUserType.contains('PathFinder')){
Cookie portaltypeStore = new Cookie('Type','Pioneer',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
else{
Cookie portaltypeStore = new Cookie('Type','PathFinder',null,900,true);
ApexPages.currentPage().setCookies(new Cookie[]{portaltypeStore});
}
}
if(acc.isActivate__c){
//isActive = true;
if(LogInPlace=='Site'){
PageReference pg = new PageReference('/Portal/PathFinderLinksVf');
if(String.isNotBlank(ApexPages.CurrentPage().getParameters().get('retUrl')))
pg = new PageReference(ApexPages.CurrentPage().getParameters().get('retUrl'));
pg.setRedirect(false);
return pg;
}else{
PageReference pg = new PageReference('/apex/PathFinderLinksVf');
if(String.isNotBlank(ApexPages.CurrentPage().getParameters().get('retUrl')))
pg = new PageReference(ApexPages.CurrentPage().getParameters().get('retUrl'));
pg.setRedirect(false);
return pg;
}
}else{
return null;
}
}
}
}
return null;
}
public PageReference VerifyPassword() {
System.debug('isLoggedIn:- '+isLoggedIn);
if(isLoggedIn){
if(LogInPlace=='Site'){
PageReference pg = new PageReference('/Portal/PathFinderLinksVf');
pg.setRedirect(false);
return pg;
}else{
PageReference pg = new PageReference('/apex/PathFinderLinksVf');
pg.setRedirect(false);
return pg;
}
}
return null;
}
Per the docs at https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_System_PageReference_getCookies.htm it says the "getCookies()" method...
So, instead of passing a string into the set method, we have to pass a cookie object. Something like (this is pseudo code)