Below is the test class which creates Account and Contact,Community User as Sys Admin and test class runs as Community User.
And issue comes when am trying to access Account and Contact from Custom Controller as Account and Contact is set to private under Sharing Settings
Account a = new Account(Name='Test Account Name');
insert a;
Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
insert c;
User user = new User();
user.ProfileID = [Select Id From Profile Where Name='CustomerCommunity User Profile'].id;
user.EmailEncodingKey = 'ISO-8859-1';
user.LanguageLocaleKey = 'en_US';
user.TimeZoneSidKey = 'America/New_York';
user.LocaleSidKey = 'en_US';
user.FirstName = 'first';
user.LastName = 'last';
user.Username = '[email protected]';
user.CommunityNickname = 'testUser123';
user.Alias = 't1';
user.Email = '[email protected]';
user.IsActive = true;
user.ContactId = c.Id;
insert user;
System.RunAs(user) {
// do all of my tests as
}
conList and accList are getting as null as Community user doesn't have access to account and contact.
public void getContactDetails(){
conList = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,Contact_Type__c,AccountId
FROM Contact where AccountID=:currentUser[0].AccountId];
}
public void getAccountDetails(){
accList = [SELECT id, name, BillingStreet, BillingCity, BillingState, BillingPostalCode,
website,phone FROM Account where id=: currentUser[0].AccountId];
}
Updated: I tried to query Contact and Account and I can access it in my test method whereas In my Custom Controller am not able to access Contact or Account.
@isTest
public class CommunityUser_Test
{
/*
* This method prepares the test data of this class.
*/
@testSetup static void prepareSetupData()
{
Account a = new Account(Name='Test Account Name');
insert a;
Contact c = new Contact(LastName = 'Contact Last Name', AccountId = a.id);
insert c;
User user = new User();
user.ProfileID = [Select Id From Profile Where Name='Customer Community User'].id;
user.EmailEncodingKey = 'ISO-8859-1';
user.LanguageLocaleKey = 'en_US';
user.TimeZoneSidKey = 'America/New_York';
user.LocaleSidKey = 'en_US';
user.FirstName = 'first';
user.LastName = 'last';
user.Username = '[email protected]';
user.CommunityNickname = 'testUser123';
user.Alias = 't1';
user.Email = '[email protected]';
user.IsActive = true;
user.ContactId = c.Id;
insert user;
}
static testMethod void testforActualScenarios()
{
User userObj = [SELECT Id,ContactId, AccountId FROM User WHERE Username='[email protected]'];
System.RunAs(userObj) {
//retrieve the contact.
Contact contactObj = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
FROM Contact where Id=: userObj.ContactId];
System.assertEquals(contactObj.LastName,'Contact Last Name');
List<Contact> conList = [SELECT Name,FirstName,LastName,Account.Name, Title, email,Phone,AccountId
FROM Contact where AccountID=:userObj.AccountId];
System.assertEquals(conList[0].LastName,'Contact Last Name');
//retrieve the account
Account acct = [SELECT Id, Name FROM Account WHERE Id=:userObj.AccountId];
System.assertEquals(acct.Name,'Test Account Name');
}
}
}