Profile profile1 = [Select Id from Profile where name = 'GHI Portal User'];
User portalAccountOwner1 = new User(
ProfileId = profile1.Id,
Username ='[email protected]',
Alias = 'batman',
Email='[email protected]',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago'
);
insert portalAccountOwner1;
System.runAs ( portalAccountOwner1 ) {
//Create account
Account portalAccount1 = new Account(
Name = 'TestAccount',
OwnerId = portalAccountOwner1.Id
);
insert portalAccount1;
//Create contact
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'McTesty',
AccountId = portalAccount1.Id,
Email = '[email protected]'
);
insert contact1;
//Create user
Profile portalProfile = [SELECT Id FROM Profile Limit 1];
User user1 = new User(
Username = '[email protected]',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
Alias = 'test123',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
insert user1;
update user1;
}
}
2 Answers
You can follow the detailed tutorial here. The key points to note are
- The user that creates the portal user MUST have a Role.
getPortalUser()
returns a reference to portal user- You then use this reference with
runAs()
to check whether Portal permissions are enabled or not
`
@IsTest enum PortalType { CSPLiteUser, PowerPartner, PowerCustomerSuccess, CustomerSuccess }
static testmethod void usertest() {
User pu = getPortalUser(PortalType.PowerPartner, 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='[email protected]', userroleid = r.id,
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = UserInfo.getProfileId(),
timezonesidkey='America/Los_Angeles', 36
} 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);
}
/* Get any profile for the given type.*/
Profile p = [select id
from profile
where usertype = :portalType.name()
limit 1];
String testemail = '[email protected]';
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;
}
}
Use following codes
Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess', 'CustomerSuccess'};
Account acc1 = new Account (
Name = 'newAcc1'
);
insert acc1;
Contact conCase = new Contact (
AccountId = acc1.id,
LastName = 'portalTestUserv1'
);
insert conCase;
//Create user
Profile prfile = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
User newUser1 = new User(
profileId = prfile.id,
username = '[email protected]',
email = '[email protected]',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey = 'America/Los_Angeles',
alias='nuser',
lastname='lastname',
contactId = conCase.id
);
insert newUser1;