@IsTest
private class PortalRunAsTests {
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='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);
}
/* 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;
}
}
Please mark this as solution if this will help you. So some one has same issue this post will help
Thanks a lot for the information.Tried all methods to see to create a site.portaluser via these methods but none of them making the site.creteportaluser and getting null values. Can you help me how to check basically a test calss to site.createportaluser
Hi Amit, I am trying to run as Partner community user by accessing data from org (seeAllData = true) . I do not want to create dummy user in test class because I want to do some update operation on case by logging as real partner community user and commit update operation. Any help is greatly appreciated. Thanks!
Example 1:-
Example 2:-
http://www.mindfiresolutions.com/Create-A-Customer-Portal-User-In-Test-Class-2462.php
Example 3:-
Please try below code:-
Please mark this as solution if this will help you. So some one has same issue this post will help
Thanks,
Amit Chaudhary
Thanks a lot for the information.Tried all methods to see to create a site.portaluser via these methods but none of them making the site.creteportaluser and getting null values. Can you help me how to check basically a test calss to site.createportaluser
static testMethod void passwordresettest()
{
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
system.debug('portalRole is ' + portalRole);
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
UserRoleId = portalRole.Id,
ProfileId = profile1.Id,
Username = System.now().millisecond() + 'test2@test.com',
Alias = 'batman',
Email='bruce.wayne@wayneenterprises.com',
EmailEncodingKey='UTF-8',
Firstname='Bruce',
Lastname='Wayne',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);
User portalUser ;
System.runAs ( portalAccountOwner1 )
{
//Create account
Account portalAccount1 = new Account(
Name = 'TestAccount',
OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);
//Create contact
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'McTesty',
AccountId = portalAccount1.Id,
Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);
//Create user
Profile portalProfile = [SELECT Id FROM Profile Limit 1];
portalUser = new User(
Username = System.now().millisecond() + 'test12345@test.com',
ContactId = contact1.Id,
ProfileId = portalProfile.Id,
Alias = 'test123',
Email = 'test12345@test.com',
EmailEncodingKey = 'UTF-8',
LastName = 'McTesty',
CommunityNickname = 'test12345',
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US'
);
Database.insert(portalUser);
}
System.runAs ( portalUser )
{
// Code Under Portal User
}
}
I am trying to run as Partner community user by accessing data from org (seeAllData = true) . I do not want to create dummy user in test class because I want to do some update operation on case by logging as real partner community user and commit update operation. Any help is greatly appreciated. Thanks!