I have created a service which allows a portal user to query records based on some values set up on their own contact record.
In my test scenario, I want to set up something similar, where I first create a contact, create a portal user associated with that contact, runAs that user, then call that service.
However, upon executing the code, while the contact record for the portal user does indeed have an ID, no records are retrieved in a generic contact query. There's relatively little documentation on how to create a portal user, never mind interacting with the portal user's contact record.
Any insight on the reason why no contact records can be access would be greatly appreciated.
Code snippet below:
public testMethod static void testService()
{
User u;
System.runAs(getNonPortalUserWithRole())
{
setupTestData();
u = getPortalUser();
}
List<Contact> y = [
SELECT id, Offering_Project__c, Region__c
FROM Contact
WHERE id = :u.contactid
];
System.assertNotEquals(0, y.size(), 'No contact records found');
System.runAs(u)
{
System.assertEquals(u.Id, UserInfo.getUserId(), 'Running user is not the portal User.');
User x = [
SELECT id, contactid, contact.Offering_Project__c, contact.Region__c
FROM User
WHERE id = :UserInfo.getUserId()
];
System.assertNotEquals(null, x.contactid, 'Portal User contact is null');
List<Contact> c = [
SELECT id, Offering_Project__c, Region__c
FROM Contact
//WHERE id = :x.contactid
];
System.assertNotEquals(0, c.size(), 'No contact records found'); // Fails here.
}
}
static void setupTestData()
{
Account a = new Account();
a.name = 'TEST';
a.bkrAccount_Status__c = 'Active';
insert a;
a.isPartner = true;
update a;
a = [
SELECT id, recordTypeId, bkrAccount_Status__c
FROM Account
WHERE id = :a.Id
];
System.debug('!!! ' + a);
Contact c = new Contact();
c.AccountId = a.Id;
c.LastName = 'TEST';
c.Email = '[email protected]';
c.bkrContact_Status__c = 'Active';
c.Offering_Project__c = 'TEST OFFERING';
c.Region__c = 'TEST REGION';
insert c;
c = [
SELECT id, Offering_Project__c, Region__c, recordTypeId, bkrContact_Status__c
FROM Contact
WHERE id = :c.Id
];
System.debug('!!! ' + c);
}
private static User getNonPortalUserWithRole()
{
UserRole nonPortalRole = [
Select Id
FROM UserRole
Where PortalType = 'None'
Limit 1
];
System.debug('UserRole is ' + nonPortalRole);
Profile adminProfile = [
SELECT Id
FROM Profile
WHERE name = 'System Administrator'
];
User portalAccountOwner = new User();
portalAccountOwner.UserRoleId = nonPortalRole.Id;
portalAccountOwner.ProfileId = adminProfile.Id;
portalAccountOwner.Username = System.now().millisecond() + '[email protected]';
portalAccountOwner.Alias = 'batman';
portalAccountOwner.Email = '[email protected]';
portalAccountOwner.EmailEncodingKey = 'UTF-8';
portalAccountOwner.Firstname = 'Bruce';
portalAccountOwner.Lastname = 'Wayne';
portalAccountOwner.LanguageLocaleKey = 'en_US';
portalAccountOwner.LocaleSidKey = 'en_US';
portalAccountOwner.TimeZoneSidKey = 'America/Chicago';
insert portalAccountOwner;
return portalAccountOwner;
}
/*
* Returns portal user.
*/
private static User getPortalUser()
{
Profile p = [
SELECT Id
FROM Profile
WHERE Name = 'TEST PROFILE'
LIMIT 1
];
UserRole ur = [
SELECT id
FROM UserRole
WHERE Name = 'TEST USER ROLE'
LIMIT 1
];
Contact c = [
SELECT id
FROM Contact
WHERE LastName = 'TEST'
LIMIT 1
];
User u = new User();
u.FirstName = 'TEST';
u.LastName = 'TEST';
u.Alias = u.FirstName + '_' + u.LastName;
u.Email = u.FirstName + '.' + u.LastName + '@test.com';
u.Username = u.FirstName + '.' + u.LastName + '@test.com';
u.CommunityNickname = u.FirstName + '_' + u.LastName;
u.TimeZoneSidKey = 'America/Los_Angeles';
u.LocaleSidKey = 'en_US';
u.LanguageLocaleKey = 'en_US';
u.isActive = true;
u.EmailEncodingKey = 'UTF-8';
u.ProfileId = p.Id;
u.ContactId = c.Id;
u.userRole = ur;
insert u;
u = [
SELECT id, contactId, IsPortalEnabled, User.Profile.UserLicense.Name
FROM User
WHERE id = :u.id
];
System.debug('!!! ' + u);
return u;
}