+ Start a Discussion
pupilstuffpupilstuff 

test class for the portal user

Hello I am just trying to write a test class for the portal users

 

  profileName=[select IsPortalEnabled,Profile.Name from User]; 
                 if(profileName[0].IsPortalEnabled==true){

  // some code
                        }       

               else{

// some code

             }

 

my test code is.................................................................................

 

        enum PortalType { CSPLiteUser, PowerPartner, PowerCustomerSuccess, CustomerSuccess }
        
        static testmethod void usertest() {
            User pu = getPortalUser(PortalType.PowerCustomerSuccess, null, true);
            System.assert([select isPortalEnabled
                             from user
                            where id = :pu.id].isPortalEnabled,
                          'User was not flagged as portal enabled.');     
            
            System.RunAs(pu) {
                System.assert([select isPortalEnabled
                                 from user
                                where id = :UserInfo.getUserId()].isPortalEnabled,
                              'User wasnt portal enabled within the runas block. ');
            }
        }
        
        public static User getPortalUser(PortalType portalType, User userWithRole, Boolean doInsert) {
        
            /* Make sure the running user has a role otherwise an exception
               will be thrown. */
            if(userWithRole == null) { 
                
                if(UserInfo.getUserRoleId() == null) {
    
                    UserRole r = new UserRole(name = 'TEST ROLE');
                    Database.insert(r);
                    
                    userWithRole = new User(alias = 'hasrole', email='userwithrole@roletest1.com', userroleid = r.id,
                                        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                                        localesidkey='en_US', profileid = UserInfo.getProfileId(),
                                        timezonesidkey='America/Los_Angeles', username='userwithrole@testorg.com');
                } else {
                    userWithRole = new User(Id = UserInfo.getUserId(), UserRoleId = UserInfo.getUserRoleId());
                }
                
                System.assert(userWithRole.userRoleId != null,
                              'This test requires the target org to have at least one UserRole created. Please create a user role in this organization and try again.');
            }
    
            Account a;
            Contact c;
            System.runAs(userWithRole) {
    
                a = new Account(name = 'TEST ACCOUNT');
                Database.insert(a);
                
                c = new Contact(AccountId = a.id, lastname = 'lastname');
                Database.insert(c);
 
       
        mainSSID= new Sold_Service__c(
            CRM_Account__c=a.id,
           
            SSID__c='22224',
            Name='Test_mayank_IPA',
            CRM_Product_Category__c='VPN & Ethernet',
            CRM_Product_Name__c='Ethernet over SDH'
        );
            insert mainSSID;
       
        mainCase = new Case(
            Parent_Account__c=a.id,
            SSID__c=mainSSID.id,
            accountId = a.id,
            contactId = c.id,
            Type='Incident',
            OPC__c='2222'
        );
            insert mainCase;


            }
            
            /* Get any profile for the given type.*/
            Profile p = [select id
                          from profile
                         where usertype = :portalType.name()
                         limit 1]; 
            
            String testemail = 'puser000@amamama.com';
            User pu = new User(profileId = p.id, username = testemail, email = testemail,
                               emailencodingkey = 'UTF-8', localesidkey = 'en_US',
                               languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles',
                               alias='cspu', lastname='lastname', contactId = c.id);
          
          
          
          
          
            
            if(doInsert) {
                Database.insert(pu);
            }
            return pu;
        }
  
        private static testmethod void incidentSsidPortalTest(){
        Test.startTest();
        usertest();
        controller = new TT_Case_NewButtonVFC_Ext(new ApexPages.StandardController(mainCase));
        PageReference pr = controller.goToCaseEditPage();
        Test.stopTest();
    }
  

 

But still its checking only else loop..means profile is not getting enabled for postal user

 

 

Plz help for this