-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserRoleControllerTest.cls
101 lines (86 loc) · 2.89 KB
/
UserRoleControllerTest.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @description - Test Class for UserRoleController
* Class constructed for the Organization Chart / Google Visualization API CloudSpokes Challenge
* @author - James Loghry ([email protected]) Twitter: dancinllama
*/
@isTest
private class UserRoleControllerTest {
static testMethod void testConstructor() {
Test.startTest();
insertUsers();
UserRoleController controller = new UserRoleController();
Test.stopTest();
System.assertNotEquals(null,controller.roles);
/*
Cannot delete existing users and add the new ones,
So add the ones we know of to a temp list and check the tests
against the temp list
*/
List<UserRole> roles = new List<UserRole>();
for(UserRole role : controller.roles){
if(role.Name != null &&
(role.Name.equals('TESTCTO') ||
role.Name.equals('TESTCFO') ||
role.Name.equals('TESTCEO') ||
role.Name.equals('TESTVP'))){
roles.add(role);
}
}
System.assertEquals(4,roles.size());
for(UserRole role : roles){
for(User user : role.Users){
System.assertEquals(role.Name + ' last',user.Name);
}
}
}
private static List<UserRole> insertRoles(){
List<UserRole> ans = new List<UserRole>();
/*
Usually, these inserts are done in lists.
However, the roles need 'ParentRoleId' to be populated,
so they must be inserted in a cascading fashion
*/
UserRole ceo = new UserRole(Name='TESTCEO');
insert ceo;
UserRole vp = new UserRole(Name='TESTVP',ParentRoleId=ceo.Id);
insert vp;
UserRole cfo = new UserRole(Name='TESTCFO',ParentRoleId=vp.Id);
UserRole cto = new UserRole(Name='TESTCTO',ParentRoleId=vp.Id);
List<UserRole> roles = new List<UserRole>{cfo,cto};
insert roles;
ans.add(ceo);
ans.add(vp);
ans.addAll(roles);
return ans;
}
/* Create a new user */
private static User getUser(String name,UserRole role,Profile profile){
User user = new User(
FirstName=name,
LastName='last',
Username=name+'@'+name+'.com',
Email=name+'@'+name+'.com',
CommunityNickname=name,
Alias=name,
LocaleSidKey='en_US',
LanguageLocaleKey='en_US',
TimeZoneSidKey='America/Chicago',
EmailEncodingKey='UTF-8',
ProfileId = profile.Id,
UserRoleId=(role!=null)? role.Id : null
);
return user;
}
/* DML to insert user and roles */
private static void insertUsers(){
Profile profile = [Select Id from Profile Limit 1];
List<UserRole> roles = insertRoles();
List<User> users = new List<User>();
for(UserRole role : roles){
users.add(getUser(role.Name,role,profile));
}
/* Add user with null role id to test specific condition in UserRoleController */
users.add(getUser('nullrole',null,profile));
insert users;
}
}