Skip to content

Instantly share code, notes, and snippets.

@Walletau
Last active April 1, 2017 06:22
Show Gist options
  • Save Walletau/f3266a9bd841d5d95e0ce43db3eba1e8 to your computer and use it in GitHub Desktop.
Save Walletau/f3266a9bd841d5d95e0ce43db3eba1e8 to your computer and use it in GitHub Desktop.
Test Factory Example
@isTest
private class TestUserUtil {
@isTest static void updateRelatedContactAndAccountRecordsTest() {
// Create a User, Contact and Account
Account newAccount = Build.anAccount()
.withRecordType(Build.AccountRecordType.Organisation)
.build();
insert newAccount;
Contact contactForUser = Build.aContact()
.withName('Test1', 'Test2')
.withAccount(newAccount)
.build();
insert contactForUser;
User adminUser = Build.aAdminUser()
.withUserName('adminTestUser')
.build();
insert adminUser;
User communityUser = Build.aUser()
.withProfile('Member User')
.withContact(contactForUser)
.build();
insert communityUser;
System.runAs(adminUser) {
Test.startTest();
// Change the User's FirstName
communityUser.FirstName = 'SomethingElse';
update communityUser;
Test.stopTest();
}
// Ensure the Contact and Account are updated too
System.assertEquals([SELECT FirstName FROM Contact LIMIT 1].FirstName, communityUser.FirstName);
System.assertEquals([SELECT Name FROM Account LIMIT 1].Name, communityUser.FirstName + ' Test1');
}
}
public class Build {
public static final String DEFAULT_CURRENCY_ISO_CODE = 'AUD';
//
public enum AccountRecordType {
RecordType1,
Organisation
}
//
public enum ContactRecordType {
RecordType1,
OrganisationContact
}
//
private static Map<String, ContactRecordType> contactDevNamesToRecType = new Map<String, ContactRecordType>{
'RecordType1' => ContactRecordType.RecordType1,
'OrganisationContact' => ContactRecordType.OrganisationContact
};
//
private static Map<String, AccountRecordType> accountDevNamesToRecType = new Map<String, AccountRecordType>{
'RecordType1' => AccountRecordType.RecordType1,
'Organisation' => AccountRecordType.Organisation
};
/**
* Map from `Build.OpportunityRecordType` enums to their corresponding
* RecordType records
*/
public static Map<ContactRecordType, RecordType> contactRecTypeSObjects {
get {
if (contactRecTypeSObjects == null) {
contactRecTypeSObjects = new Map<ContactRecordType, RecordType>();
List<RecordType> activeContactRecordTypes = [
SELECT
ID,
DeveloperName
FROM
RecordType
WHERE
SObjectType = 'Contact'
AND IsActive = true
];
for (RecordType recordType : activeContactRecordTypes) {
ContactRecordType recTypeEnum = contactDevNamesToRecType.get(recordType.DeveloperName);
if (recTypeEnum != null) {
contactRecTypeSObjects.put(recTypeEnum, recordType);
}
}
}
return contactRecTypeSObjects;
}
private set;
}
/**
* Map from `Build.OpportunityRecordType` enums to their corresponding
* RecordType records
*/
public static Map<AccountRecordType, RecordType> accountRecordTypeSObjects {
get {
if (accountRecordTypeSObjects == null) {
accountRecordTypeSObjects = new Map<AccountRecordType, RecordType>();
List<RecordType> activeAccountRecordTypes = [
SELECT
ID,
DeveloperName
FROM
RecordType
WHERE
SObjectType = 'Account'
AND IsActive = true
];
for (RecordType recordType : activeAccountRecordTypes) {
AccountRecordType recTypeEnum = accountDevNamesToRecType.get(recordType.DeveloperName);
if (recTypeEnum != null) {
accountRecordTypeSObjects.put(recTypeEnum, recordType);
}
}
}
return accountRecordTypeSObjects;
}
private set;
}
//------------------------------------------------------------------------------
//Account
public class AccountBuilder {
public AccountBuilder withName(String name) {
this.name = name;
return this;
}
String name;
public AccountBuilder withIndustry(String ind) {
industry = ind;
return this;
}
String industry;
public AccountBuilder withRecordType(Build.AccountRecordType recType) {
this.recordTypeId = accountRecordTypeSObjects.get(recType).Id;
return this;
}
String recordTypeId;
public AccountBuilder withParentAccount(Account parentAccount) {
this.parentAccount = parentAccount;
return this;
}
Account parentAccount;
// object to build
public Account build() {
Account builtAccount = (Account) SmartFactory.createSObject('Account', false);
builtAccount.Name = name != null ? name : builtAccount.Name;
builtAccount.Industry = industry != null ? industry : builtAccount.Industry;
builtAccount.ParentId = parentAccount != null ? parentAccount.Id : builtAccount.ParentId;
builtAccount.RecordTypeId = recordTypeId != null ? recordTypeId : builtAccount.RecordTypeId;
return builtAccount;
}
}
public static AccountBuilder anAccount() {
return new AccountBuilder();
}
//------------------------------------------------------------------------------
//Contact
public class ContactBuilder {
public ContactBuilder withName(String first, String last) {
fName = first;
lName = last;
return this;
}
String fName;
String lName;
public ContactBuilder withAccount(Account acct) {
this.contactAccount = acct;
return this;
}
Account contactAccount;
public ContactBuilder withMember(Member__c member) {
this.contactMemberId = member.Id;
return this;
}
String contactMemberId;
public ContactBuilder withRecordType(Build.ContactRecordType recordType) {
this.recordTypeId = contactRecTypeSObjects.get(recordType).Id;
return this;
}
String recordTypeId;
public ContactBuilder withBirthdate(Date birthDate) {
this.birthDate = birthDate;
return this;
}
Date birthDate;
public ContactBuilder withEmail(String email) {
this.email = email;
return this;
}
String email;
public ContactBuilder withCascade() {
SmartFactory.FillAllFields = true;
cascadingContact = (Contact) SmartFactory.createSObject('Contact', true);
return this;
}
Contact cascadingContact;
// object to build
public Contact build() {
Contact builtContact = cascadingContact != null ? cascadingContact : (Contact) SmartFactory.createSObject('Contact', false);
builtContact.FirstName = fName != null ? fname : builtContact.FirstName;
builtContact.LastName = lName != null ? lName : builtContact.LastName;
builtContact.Email = email != null ? email : builtContact.Email;
builtContact.Birthdate = birthDate != null ? birthDate : builtContact.Birthdate;
builtContact.OrgResp_RelatedMember__c = contactMemberId != null ? contactMemberId : builtContact.OrgResp_RelatedMember__c;
builtContact.RecordTypeId = recordTypeId != null ? recordTypeId : builtContact.RecordTypeId;
builtContact.AccountId = contactAccount.Id != null ? contactAccount.Id : builtContact.AccountId;
return builtContact;
}
}
public static ContactBuilder aContact() {
return new ContactBuilder();
}
//---------------------------------------------------------------------------------------------------------------------------
// User
public class UserBuilder {
public UserBuilder withLastName(String lastName) {
this.lastName = lastName;
return this;
}
String lastName;
public UserBuilder withUserName(String usName) {
this.userName = usName + '@test.com';
return this;
}
String userName;
public UserBuilder withProfile(String profileName) {
Profile profile = [
SELECT Id
FROM Profile
WHERE Name = :profileName
];
this.profileId = profile.Id;
return this;
}
String profileId;
public UserBuilder withContact(Contact contact) {
this.userContact = contact;
return this;
}
Contact userContact;
public User build() {
User builtUser = (User) SmartFactory.createSObject('User', false);
builtUser.LastName = lastName != null ? lastName : builtUser.LastName;
builtUser.UserName = userName != null ? userName : builtUser.UserName + '.qwetest';
builtUser.ProfileId = profileId != null ? profileId : builtUser.ProfileId;
if (userContact != null) {
builtUser.ContactId = userContact.Id;
builtUser.FirstName = userContact.FirstName;
builtUser.LastName = userContact.LastName;
builtUser.Email = userContact.Email;
builtUser.Username = userContact.Email + '.qwetest';
}
return builtUser;
}
}
public static UserBuilder aUser() {
return new UserBuilder();
}
public static UserBuilder aAdminUser() {
UserBuilder testAdmin = new UserBuilder().
withProfile('System Administrator');
return testAdmin;
}
}
/*
Copyright 2011 Mavens Consulting, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
public with sharing class SmartFactory {
public static boolean FillAllFields = false;
// Key : SobjectAPIName For ex. Account
// Value : Map<String, Schema.SObjectField>, field map (k:fieldname, v:Schema.Sobjectfield)
public static Profile userProfile = [Select Id from Profile where Name = 'System Administrator'];
private static final Map<String, Map<String, Schema.SObjectField>> FieldMapCache = new Map<String, Map<String, Schema.SObjectField>>();
private static final Set<String> allowCascade = new Set<String>{
'OpportunityLineItem', 'PricebookEntry', 'Product2'
};
private static Pricebook2 stdPriceBook;
private static final Map<String, Schema.SObjectType> GlobalDescribe = Schema.getGlobalDescribe();
// Key: sobject.field
// Value: first picklist value
private static final Map<String, String> DefaultPicklistValue = new Map<String, String>();
// can't map by Schema.sObjectType, use object name String instead
public static map<String, set<String>> ExcludedFields = new map<String, set<String>>{
'All' => new set<String>{
'OwnerId', 'LastModifiedById', 'CreatedById', 'LastModifiedDate', 'CreatedDate'
},
'Account' => new set<String>{
'FirstName', 'LastName'
},
'User' => new set<String>{
'IsActive', 'DelegatedApproverId', 'CallCenterId', 'ContactId', 'DelegatedApproverId', 'ManagerId', 'UserRoleId', 'FederationIdentifier'
}
};
// include nillable fields
public static map<String, set<String>> IncludedFields = new map<String, set<String>>();
public static SObject createSObject(String objectType) {
return createSObject(objectType, false);
}
public static List<SObject> createSObjectList(String objectType, boolean cascade, Integer numberOfObjects) {
List<SObject> sos = new List<SObject>();
for (Integer i = 0; i < numberOfObjects; i++)
sos.add(createSObject(objectType, cascade, i));
return sos;
}
public static SObject createSObject(String objectType, boolean cascade, Integer counter) {
System.debug('Creating ' + objectType);
Schema.sObjectType token = GlobalDescribe.get(objectType);
if (token == null) {
throw new UnsupportedObjectTypeException('Unsupported ObjectType ' + objectType);
}
SObject obj = token.newSObject();
for (Schema.SObjectField field : fieldMapFor(objectType).values()) {
setFieldValue(obj, field, cascade, counter);
}
return obj;
}
public static SObject createSObject(String objectType, boolean cascade) {
return createSObject(objectType, cascade, 1);
}
/**
Returns a field map for a given sobject.
Note : this method is kept public for Test cases to share the same field map info, without requiring a field desribe.
@param objectType sobject api name for ex. Account
@returns FieldMap [Key:FieldName,Value:Schema.SObjectField]
*/
public static Map<String, Schema.SObjectField> fieldMapFor(String objectType) {
Map<String, Schema.SObjectField> fieldMap = null;
String normalizedObjectType = objectType.toLowerCase();
if (FieldMapCache.containsKey(normalizedObjectType)) {
fieldMap = FieldMapCache.get(normalizedObjectType);
} else {
fieldMap = GlobalDescribe.get(objectType).getDescribe().fields.getMap();
// cache it for next use
FieldMapCache.put(normalizedObjectType, fieldMap);
}
return fieldMap;
}
static String getDefaultPicklistValue(SObject obj, Schema.DescribeFieldResult fieldDescribe) {
String key = obj.getSObjectType() + '.' + fieldDescribe.getName();
if (!DefaultPicklistValue.containsKey(key)) {
List<Schema.PicklistEntry> entries = fieldDescribe.getPicklistValues();
String value = entries.size() > 0 ? entries[0].getValue() : null;
DefaultPicklistValue.put(key, value);
}
return DefaultPicklistValue.get(key);
}
static boolean isExcludedField(Schema.DescribeFieldResult fieldDescribe) {
return ExcludedFields.get('All').contains(fieldDescribe.getName());
}
static boolean isExcludedField(SObject obj, Schema.DescribeFieldResult fieldDescribe) {
set<String> fields = ExcludedFields.get(obj.getSObjectType().getDescribe().getName());
return fields == null ? false : fields.contains(fieldDescribe.getName());
}
static boolean isIncludedField(SObject obj, Schema.DescribeFieldResult fieldDescribe) {
set<String> fields = includedFields.get(obj.getSObjectType().getDescribe().getName());
return fields == null ? false : fields.contains(fieldDescribe.getName());
}
static boolean isPersonAccountField(Schema.DescribeFieldResult fieldDescribe) {
Boolean isPersonAccountEnabled = fieldMapFor('Account').get('IsPersonAccount') != null;
set<string> skipPersonAccountFields = new set<string>{
'Salutation'
};
Boolean CustomPerson = fieldDescribe.isCustom() && fieldDescribe.getName().endsWith('pc');
Boolean StandardPerson = !fieldDescribe.isCustom() && fieldDescribe.getName().startsWith('Person');
return CustomPerson || StandardPerson ||
(isPersonAccountEnabled && skipPersonAccountFields.contains(fieldDescribe.getName()));
}
static void setFieldValue(SObject obj, Schema.SObjectField field, boolean cascade) {
setFieldValue(obj, field, cascade, 0);
}
static void setFieldValue(SObject obj, Schema.SObjectField field, boolean cascade, Integer counter) {
Schema.DescribeFieldResult fieldDescribe = field.getDescribe();
if (fieldDescribe.isCreateable() &&
(
isIncludedField(obj, fieldDescribe) ||
(
(
(!fieldDescribe.isNillable() ||
FillAllFields) ||
(fieldDescribe.getType() == Schema.DisplayType.Reference && cascade) // always fill references with cascade
) &&
!isExcludedField(fieldDescribe) &&
!isExcludedField(obj, fieldDescribe) &&
!isPersonAccountField(fieldDescribe)
)
)
) {
if (fieldDescribe.getType() == Schema.DisplayType.base64) {
obj.put(field, blob.valueOf(counter.format()));
} else if (fieldDescribe.getType() == Schema.DisplayType.Boolean) {
obj.put(field, false);
} else if (fieldDescribe.getType() == Schema.DisplayType.Combobox) {
obj.put(field, counter.format());
} else if (fieldDescribe.getType() == Schema.DisplayType.Currency) {
obj.put(field, counter);
} else if (fieldDescribe.getType() == Schema.DisplayType.Date) {
obj.put(field, Date.today());
} else if (fieldDescribe.getType() == Schema.DisplayType.DateTime) {
obj.put(field, DateTime.now());
} else if (fieldDescribe.getType() == Schema.DisplayType.Double) {
obj.put(field, counter);
} else if (fieldDescribe.getType() == Schema.DisplayType.Email) {
obj.put(field, 'test' + counter.format() + '@test.com');
} else if (fieldDescribe.getType() == Schema.DisplayType.EncryptedString) {
obj.put(field, 's');
} else if (fieldDescribe.getType() == Schema.DisplayType.Id) {
//System.debug('Id field ' + fieldDescribe.getName());
} else if (fieldDescribe.getType() == Schema.DisplayType.Integer) {
obj.put(field, counter);
} else if (fieldDescribe.getType() == Schema.DisplayType.MultiPicklist) {
obj.put(field, getDefaultPicklistValue(obj, fieldDescribe));
} else if (fieldDescribe.getType() == Schema.DisplayType.Percent) {
obj.put(field, counter);
} else if (fieldDescribe.getType() == Schema.DisplayType.Phone) {
obj.put(field, '123-456-7890');
} else if (fieldDescribe.getType() == Schema.DisplayType.Picklist) {
obj.put(field, getDefaultPicklistValue(obj, fieldDescribe));
} else if (fieldDescribe.getName() == 'CommunityNickname' && fieldDescribe.getType() == Schema.DisplayType.String) {
obj.put(field, 'test' + string.valueof(math.roundtolong(math.random() * 1000000)));
} else if (fieldDescribe.getName() == 'UserName' && fieldDescribe.getType() == Schema.DisplayType.String) {
obj.put(field, 'test' + string.valueof(Userinfo.getOrganizationId()) + string.valueof(math.roundtolong(math.random() * 1000000)) + string.valueof(Datetime.now()).replace('-', '').replace(':', '').replace(' ', '') + '@test.com'); // was a@a.com
} else if (fieldDescribe.getType() == Schema.DisplayType.String) {
obj.put(field, counter.format());
} else if (fieldDescribe.getType() == Schema.DisplayType.Reference) {
String referenceObjectType = fieldDescribe.getReferenceTo()[0].getDescribe().getName();
if (referenceObjectType == 'RecordType') {
setRecordType(obj);
} else if (cascade && referenceObjectType != obj.getSObjectType().getDescribe().getName()) {
// TODO avoid infinite loop for same-type references
System.debug('Creating reference to ' + referenceObjectType + ' for field ' + obj.getSObjectType().getDescribe().getName() + '.' + fieldDescribe.getName());
SObject reference;
if (referenceObjectType == 'Product2') {
reference = createSObject('Product2', false);
insert reference;
PricebookEntry standardPricebookEntry = (PricebookEntry) createSObject('PricebookEntry', false);
standardPricebookEntry.Pricebook2Id = getStdPricebookId();
standardPricebookEntry.Product2Id = reference.Id;
insert standardPricebookEntry;
} else {
reference = createSObject(referenceObjectType, allowCascade.contains(referenceObjectType));
insert reference;
}
System.debug('Inserting ' + reference);
obj.put(field, reference.Id);
} else if (referenceObjectType == 'Profile') {
obj.put(field, userProfile.Id);
}
} else if (fieldDescribe.getType() == Schema.DisplayType.TextArea) {
obj.put(field, counter.format());
} else if (fieldDescribe.getType() == Schema.DisplayType.Time) {
obj.put(field, Time.newInstance(0, 0, 0, 0));
} else if (fieldDescribe.getType() == Schema.DisplayType.URL) {
obj.put(field, 'http://test' + counter + '.com');
} else {
System.debug('Unhandled field type ' + fieldDescribe.getType());
}
}
}
static void setRecordType(SObject obj) {
List<Schema.RecordTypeInfo> recordTypes = obj.getSObjectType().getDescribe().getRecordTypeInfos();
if (recordTypes.size() > 1) { // all objects have default Master type
//System.debug('RecordTypes ' + recordTypes);
for (Schema.RecordTypeInfo recordType : recordTypes) {
if (recordType.isAvailable() && recordType.isDefaultRecordTypeMapping()) {
obj.put('RecordTypeId', recordType.getRecordTypeId());
}
}
}
}
public static ID getStdPricebookId() {
if (Test.isRunningTest())
return Test.getStandardPricebookId();
return getStdPricebook().id;
}
public static Pricebook2 getStdPriceBook() {
if (stdPriceBook == null)
stdPriceBook = [select id, name from Pricebook2 where isStandard = true limit 1];
return stdPriceBook;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment