+ Start a Discussion
BobPBobP 

Test class to check user profile and page reference

I am working on a apex class for the opportunity object to display a visualforce page if the current user has a specific profile i.e. System Administrator. I'm not sure how to write a test class to check user profiles and page references.  Any help on this would be greatly appreciated. 

Apex Class:
 
public class overrideOpp {
   String recordId;
   
public overrideOpp(ApexPages.StandardController 
       controller) {recordId = controller.getId();}

public PageReference redirect() {
  Profile p = [select name from Profile where id = 
               :UserInfo.getProfileId()];
  if ('System Administrator'.equals(p.name) 
      || 'Inside Standard User'.equals(p.name)) 
      {
       PageReference customPage =  Page.OppTabDetailinsidesales2;
       customPage.setRedirect(true);
       customPage.getParameters().put('id', recordId);
       return customPage;
      } else {
          return null; //otherwise stay on the same page  
    
      }
   }
}

 
Best Answer chosen by BobP
Steven NsubugaSteven Nsubuga
Improved test with 100% coverage
@isTest
private class overrideOppTest {
   
	@isTest static void testSystemAdministrator() {
    
		Profile pf= [Select Id from profile where Name='System Administrator']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
					 lastName = 'XYZ', 
					 email =  'uniqueName@test' + orgId + '.org', 
					 Username = 'uniqueName@test' + orgId + '.org', 
					 EmailEncodingKey = 'ISO-8859-1', 
					 Alias = 'ABC', 
					 TimeZoneSidKey = 'America/Los_Angeles', 
					 LocaleSidKey = 'en_US', 
					 LanguageLocaleKey = 'en_US', 
					 ProfileId = pf.Id
					); 

		insert uu;
		
		system.runAs(uu) {
			Account obj = new Account(Name ='Test');
			insert obj;
		
			ApexPages.StandardController sc = new ApexPages.StandardController(obj);
			overrideOpp testOpp = new overrideOpp(sc);
			
			testOpp.redirect();
		}
}

	@isTest static void testStandardUser() {
		
		
		Profile pf= [Select Id from profile where Name='Standard User']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
					 lastName = 'XYZ', 
					 email =  'uniqueName@test' + orgId + '.org', 
					 Username = 'uniqueName@test' + orgId + '.org', 
					 EmailEncodingKey = 'ISO-8859-1', 
					 Alias = 'ABC', 
					 TimeZoneSidKey = 'America/Los_Angeles', 
					 LocaleSidKey = 'en_US', 
					 LanguageLocaleKey = 'en_US', 
					 ProfileId = pf.Id
					); 

		insert uu;
		
		system.runAs(uu) {
			Account obj = new Account(Name ='Test');
			insert obj;
		
			ApexPages.StandardController sc = new ApexPages.StandardController(obj);
			overrideOpp testOpp = new overrideOpp(sc);
			
			testOpp.redirect();
		}
	}

}

 

All Answers

Steven NsubugaSteven Nsubuga
Improved test with 100% coverage
@isTest
private class overrideOppTest {
   
	@isTest static void testSystemAdministrator() {
    
		Profile pf= [Select Id from profile where Name='System Administrator']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
					 lastName = 'XYZ', 
					 email =  'uniqueName@test' + orgId + '.org', 
					 Username = 'uniqueName@test' + orgId + '.org', 
					 EmailEncodingKey = 'ISO-8859-1', 
					 Alias = 'ABC', 
					 TimeZoneSidKey = 'America/Los_Angeles', 
					 LocaleSidKey = 'en_US', 
					 LanguageLocaleKey = 'en_US', 
					 ProfileId = pf.Id
					); 

		insert uu;
		
		system.runAs(uu) {
			Account obj = new Account(Name ='Test');
			insert obj;
		
			ApexPages.StandardController sc = new ApexPages.StandardController(obj);
			overrideOpp testOpp = new overrideOpp(sc);
			
			testOpp.redirect();
		}
}

	@isTest static void testStandardUser() {
		
		
		Profile pf= [Select Id from profile where Name='Standard User']; 
		String orgId=UserInfo.getOrganizationId(); 
		User uu=new User(firstname = 'ABC', 
					 lastName = 'XYZ', 
					 email =  'uniqueName@test' + orgId + '.org', 
					 Username = 'uniqueName@test' + orgId + '.org', 
					 EmailEncodingKey = 'ISO-8859-1', 
					 Alias = 'ABC', 
					 TimeZoneSidKey = 'America/Los_Angeles', 
					 LocaleSidKey = 'en_US', 
					 LanguageLocaleKey = 'en_US', 
					 ProfileId = pf.Id
					); 

		insert uu;
		
		system.runAs(uu) {
			Account obj = new Account(Name ='Test');
			insert obj;
		
			ApexPages.StandardController sc = new ApexPages.StandardController(obj);
			overrideOpp testOpp = new overrideOpp(sc);
			
			testOpp.redirect();
		}
	}

}

 
This was selected as the best answer
BobPBobP
Thank you Steven. That did the trick