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
Siddharth LakhotiaSiddharth Lakhotia 

Need Help in writting Test Class when Permission set is assigned via Role??

Hi,

I have written a trigger, where-in I am assigning Permission set to a user based on his roles.

if a user role name contains Sales Manager or Director, Permission set gets assigned to him at the time of Update and Insert.

When the userrole contains Executive , Permission Set gets Deleted automatically at the time of Update and Insert.

Can anyone help me in writting Test Class for the same.

 
Best Answer chosen by Siddharth Lakhotia
Raj VakatiRaj Vakati
Use this code .. its 100 % code coverage 
 
@isTest
private class UserTriggerTestClass{
    static testmethod void setupTestData(){
        
        System.Test.startTest() ;
        
        UserRole ur = new UserRole(Name = 'Sales Manager');
        insert ur;
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         UserRoleId = ur.Id,
                         Sg_Region__c = 'China',
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.Test.stopTest();
        
    }
    
     static testmethod void setupTestData2(){
        
        System.Test.startTest() ;
        
        UserRole ur = new UserRole(Name = 'Exectuive');
        insert ur;
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         UserRoleId = ur.Id,
                         Sg_Region__c = 'China',
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.Test.stopTest();
        
    }
    
}

 

All Answers

gopinath perumalgopinath perumal
Hi Siddharth,

You could create a test user with required role ('Sales Manager' or 'Director') and check if the permission Set gets assigned to the inserted user.
 
UserRole role = new UserRole(DeveloperName = 'Sales Manager', Name = 'Sales Manager');
insert role;

User u = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'YOUR PROFILE'].Id,
     LastName = 'last',
     Email = 'testtt@tessst.com',
     Username = 'testtt@tessst.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US',
     UserRoleId = role.Id
);
Insert u;

PermissionSet perSet = [SELECT Id FROM PermissionSet WHERE Name='PERMISSION SET NAME'];

PermissionSetAssignment permAssnment = [SELECT Id, AssigneeId FROM PermissionSetAssignment WHERE AssigneeId= :u.Id LIMIT 1];

System.assertEquals(perSet.Id,permAssnment.PermissionSetId);

Hope it helps.

Thanks.
Gopi​
Siddharth LakhotiaSiddharth Lakhotia
Hi Gopi, 

I copied your Test class code . It still gives me 0% coverage

@isTest public class PermSetAssignTest 
{

static TestMethod void RoleTest(){
   UserRole role = new UserRole(DeveloperName = 'Sales Manager', Name = 'Sales Manager');
   insert role;

User u = new User(

     ProfileId = [SELECT Id FROM Profile WHERE Name = 'YOUR PROFILE'].Id,

     LastName = 'last',

     Email = 'testtt@tessst.com',

     Username = 'testtt@tessst.com' + System.currentTimeMillis(),

     CompanyName = 'TEST',

     Title = 'title',

     Alias = 'alias',

     TimeZoneSidKey = 'America/Los_Angeles',

     EmailEncodingKey = 'UTF-8',

     LanguageLocaleKey = 'en_US',

     LocaleSidKey = 'en_US',

     UserRoleId = role.Id

);

Insert u;
PermissionSet perSet = [SELECT Id FROM PermissionSet WHERE Name='China_Allow_Report_Access'];

PermissionSetAssignment permAssnment = [SELECT Id, AssigneeId FROM PermissionSetAssignment WHERE AssigneeId= :u.Id LIMIT 1];

System.assertEquals(perSet.Id,permAssnment.PermissionSetId);   
       
  
  
     System.runAs(u)
        {
            PermSetAssignTest  pr = new PermSetAssignTest ();
        }
        }
       
    }
gopinath perumalgopinath perumal
Hi Siddarth,

Could you please post your trigger code here.?

Thanks.
Gopi.
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

Please try the below code:
 
static testMethod void insertUser1()
{
Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];

UserRole ur = new UserRole(Name = 'Sales Manager');
insert ur;

User u = new User(
ProfileId = p.Id,
LastName = 'testt',
Email = 'testtt@test.com',
Username = 'testtt@test.com',
UserRoleId = ur.Id,
Sg_Region__c = 'China');
try
{
insert usr;
}
Catch(DMLException e)
{

}

}
 static testMethod void insertUser2()
{
	Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
   
	UserRole ur = new UserRole(Name = 'Marketing Manager');
	insert ur;
   
	User u = new User(
 ProfileId = p.Id,
 LastName = 'testt',
 Email = 'testtt@tessst.com',
 Username = 'testtt@tessst.com',
 UserRoleId = ur.Id,
 Sg_Region__c = 'China');
	try
	{
		insert usr;
	 }
	 Catch(DMLException e)
	 {
	 
	 }
   
}

Hope this will help.

Thanks
Bhargavi.​
Raj VakatiRaj Vakati
Use this code .. its 100 % code coverage 
 
@isTest
private class UserTriggerTestClass{
    static testmethod void setupTestData(){
        
        System.Test.startTest() ;
        
        UserRole ur = new UserRole(Name = 'Sales Manager');
        insert ur;
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         UserRoleId = ur.Id,
                         Sg_Region__c = 'China',
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.Test.stopTest();
        
    }
    
     static testmethod void setupTestData2(){
        
        System.Test.startTest() ;
        
        UserRole ur = new UserRole(Name = 'Exectuive');
        insert ur;
        
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         UserRoleId = ur.Id,
                         Sg_Region__c = 'China',
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.Test.stopTest();
        
    }
    
}

 
This was selected as the best answer
Siddharth LakhotiaSiddharth Lakhotia
Thanks a lot.. !! It indeed was a big big help. I am deleting trigger from this page due to security issues..
Thanks once again