3

Im trying to setup a simple test to show the "sharing set" is working properly.

I a communities sharign set that says

Account | User:Account = Account:Id | Read/Write

and I know this works cause I can verify it in my app, but when i tried to write a simple test to check not having any luck?

Theres a utility class that i wont post here but I can verify from the debug logs that they two users do share an account and yet one cant see the others customers?

Is it an known issue that sharing sets arent evaluated in apex tests?

@isTest static void sharingInsideOrg() {

        list<Customer__c> allCustomers = new list<Customer__c>();

        Contact contact;

        User        u = TestUtility.setupTestCoach();
        System.runAs(u) {

            contact = [Select Id, AccountId from Contact where ID =:u.ContactId ];
            System.Debug('*** User 1:'+contact.AccountId);


            Customer__c c = TestUtility.setupTestCustomer();
            c.Name = 'Customer1';
            c.Coach__c    = u.ContactId;
            insert c;

            Meeting__c m    = TestUtility.createMeeting();
            m.Customer__c   = c.Id;
            insert m;

            allCustomers = [Select Id from Customer__c ];

            System.assertEquals(1, allCustomers.size());
        }

        // create a second user with the same organization
        User        u2 = TestUtility.setupTestCoachSameOrg(u.ContactId);

        System.runAs(u2) {

            contact = [Select Id, AccountId from Contact where ID =:u2.ContactId ];


            System.Debug('*** User 2:'+contact.AccountId);

            allCustomers = [Select Id from Customer__c ];

            System.assertEquals(1, allCustomers.size());

        }


    }
4
  • You created a trigger correct, or is this Based sharing?
    – EricSSH
    Commented Jul 14, 2014 at 22:13
  • This is using Communities Sharing sets. Commented Jul 14, 2014 at 22:14
  • Did you create a trigger?
    – EricSSH
    Commented Jul 14, 2014 at 22:23
  • no no trigger, its all just config settings via communities. See help.salesforce.com/… Commented Jul 14, 2014 at 22:25

2 Answers 2

1

Test methods never actually commit data, so there are a slew of ancillary tables that don't get created in tests, the most common example are field history tables. There is not anything in the docs that say for certain whether sharing sets can be affected by this, but I wouldn't be surprised.

You might consider writing a test using @SeeAllData(true) and hard code the IDs of the data you built out manually and see if you can verify that way. It won't be a deployable tests, but at least you can validate the behavior. In any event, it's usually advisable not to write tests that make assumptions about how an administrator has configured the org (i.e. profile security and sharing sets).

2
  • Thanks Ralph, my issue is the client is looking to have a unit that validates that the sharing set is working. I guess i'll have to look into @seeAllData(true) Commented Jul 14, 2014 at 22:05
  • @KeithMancuso I hear ya, outside of this question being interesting in it's own right, if I were in your shoes I'd ask the client if they'd like to need a developer to make changes to their unit tests if that changed down the line. Usually admins want the freedom to change things without blocking all future deployments until an invalidated test method can be fixed. Commented Jul 14, 2014 at 22:13
1

I cannot find any documentation on it, but there is no way to unit test Criteria Based Sharing, so I'm pretty sure this will fall under the same umbrella as Sharing Sets. I've also never heard of writing a test class for something that isn't a trigger/class, someone feel free to correct me If they have an example.. So with that being said "Is it a known issue"? It seems obvious that you can't/don't have to write a test class

https://help.salesforce.com/HTViewHelpDoc?id=security_sharing_cbs_about.htm&language=en_US

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .