function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SalesforceAddictSalesforceAddict 

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;
    }
Raj VakatiRaj Vakati
Try this code  and create a one more test method for site type salesforce .this logic is only for site user testing 

 
@isTest
private class LoginControllerTest {
 static testMethod void testLoginCon() {
	 String     AccountRecordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Professional').getRecordTypeId();

  
    Account portalAccount = new Account(name = 'portalAccount' ,recordtypeid =AccountRecordTypeId , User_Name__c='youcomanyusername@comapnay.com',
	Password__c='demo123',Portal_User_Type__c='Site');//create a portal account first
  insert portalAccount;
  Contact portalContact = new contact(LastName = 'portalContact', AccountId = portalAccount.Id); //create a portal contact
  insert portalContact;
  
  PageReference myVfPage = Page.YOURVFPAGE;
Test.setCurrentPage(myVfPage);

  
  ApexPages.currentPage().getCookies().set('Username','youcomanyusername@comapnay.com');
  ApexPages.currentPage().getCookies().set('PassWord','demo123');
  ApexPages.currentPage().getCookies().set('Type','Site');
  
  
     
        Profile pf= [Select Id from profile where Name='System Administrator']; 
  
  User u1 = new User( email='youcomanyusername@comapnay.com',
                profileid = pf.id, 
                UserName='youcomanyusername@comapnay.com', 
                Alias = 'GDS',
                TimeZoneSidKey='America/New_York',
                EmailEncodingKey='ISO-8859-1',
                LocaleSidKey='en_US', 
                LanguageLocaleKey='en_US',
                ContactId = portalContact.Id,
                PortalRole = 'Manager',
                FirstName = 'Genelia',
                LastName = 'Dsouza');
				
				insert u1 ;
				
		
		LoginController lo = new LoginController() ;
		lo.SubmitPage();
		lo.VerifyPassword();
  
 }
}

 
fgwarb_devfgwarb_dev
ApexPages.currentPage().getCookies().set('Username','youcomanyusername@comapnay.com');
Trying to save that gives this error
Method does not exist or incorrect signature: void set(String, String) from the type Map<String,System.Cookie>


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...
Returns a map of cookie names and cookie objects, where the key is a String of the cookie name and the the value contains the cookie object with that name.

So, instead of passing a string into the set method, we have to pass a cookie object.  Something like (this is pseudo code)
Cookie testCookie = new Cookie('cookieName', 'cookieValue', '', 5000, true);
ApexPages.currentPage().setCookies(new Cookie[]{testCookie});