We are an ISV developing a Managed Package.
We wrote some Unit tests that do things like (this is an Example)
Custom_Object__c a = new Custom_Object__c(Name = 'Test');
insert a;
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User usr = new User(Alias = 'standt', Email= 'somerandomEmail@here.com',
EmailEncodingKey='UTF-8', LastName='Testing',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName= 'somerandomUsername@here.com');
insert usr;
System.runAs(usr) {
a.Name = 'testxxx';
update a;
}
In our dev org, the sharing model for Custom_Object__c
is Public Read/Write
. However, some customers in some of their orgs run with Private
Sharing model.
When we run our tests in their orgs, they fail.
However, if we switch our dev org to Private
and amend the tests like so
Custom_Object__c a = new Custom_Object__c(Name = 'Test');
insert a;
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User usr = new User(Alias = 'standt', Email= 'somerandomEmail@here.com',
EmailEncodingKey='UTF-8', LastName='Testing',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles',
UserName= 'somerandomUsername@here.com');
insert usr;
UserRecordAccess ura = [SELECT HasEditAccess, RecordId FROM UserRecordAccess WHERE UserId = :usr.Id AND RecordId = :a.Id];
if (!ura.HasEditAccess) {
Custom_Object__Share share = new Custom_Object__Share (ParentId = t.Id, UserOrGroupId = usr.Id, AccessLevel = 'Edit');
insert share;
}
System.runAs(usr) {
a.Name = 'testxxx';
update a;
}
Then switching back to Public Read/Write
fails because
Error: Unable to change sharing model because sharing objects are referenced by the following components:
How are we meant to support both sharing models as an ISV?
Ideally we want to baseline our package on Public Read/Write
but then we also want our tests not fail if the customer has changed to Private
or Public Read Only
.