0

My test class sets up a Community User and some data prior to doing a runAs the new user. In the runAs section I call a controller which requires the use of the data I created earlier, but it doesn't seem to be available.

@IsTest
public class My_Test {

    static testmethod void test1(){
        //Create the Community User with an Account
        User testUser = TestData.createCommunityUser();
        insert testUser;
        testUser = [select Id, AccountId from User where Id =: testUser.Id limit 1];
        Account userAcct = [select Id, Name from Account where Id =: testUser.AccountId LIMIT 1];
        //Create a Unit for the Community User's Account
        Unit__c u = TestData.getUnit(); 
        Insert u;            
        units = [select id, Owner_Account__c, status__c from Unit__c];
        system.debug('@@@@My_Test units: ' + units);

        System.runAs(testUser){
            Test.startTest();
            MyTable controller = new MyTable();
            controller.account = userAcct;
            controller.user = testUser;
            system.debug('@@@@My_Test units: ' + units);

            controller.updateTableData();

            Test.stopTest();
        }
    }
}

The units created beofre the 'runAs' don't seem to be available in the controller, yet the debug statement shows they are there prior to running the controller.UpdateTableData() method. The Account and User are only available in the Controller because I explicitly set them, but I can't do this with the units as the controller code runs a query picking them up.

List<Unit__c> unitList = [select Id, Name, Owner_Account__c
     From Unit__c where (Owner_Account__c = :user.AccountId)];

What do I need to do for the units to be picked up in the controller query?

1

I believe the WHERE clause is not being fulfilled in the controller query:

List<Unit__c> unitList = [select Id, Name, Owner_Account__c
 From Unit__c where (Owner_Account__c = :user.AccountId)];

How is Owner_Account__c set on the unit? When you insert them using:

Unit__c u = TestData.getUnit(); 
Insert u;  

If it depends on the community user inserting them, why not insert the units as the testUser? This would work if they have create access:

System.runAs(testUser){    
    Unit__c u = TestData.getUnit(); 
    Insert u;            
    units = [select id, Owner_Account__c, status__c from Unit__c];
    system.debug('@@@@My_Test units: ' + units);

    Test.startTest();
    MyTable controller = new MyTable();
    controller.account = userAcct;
    controller.user = testUser;
    system.debug('@@@@My_Test units: ' + units);

    controller.updateTableData();
    Test.stopTest();
}

Otherwise I would assume sharing will need to be reconsidered for the unit records so the community users can obtain access (that or set the controller as without sharing).

  • The Owner account is set on the Unit - I checked that with the debug statements so I know the units are created and have also tested with a query/debug (outside the controller). The community user doesn't have create rights so I can't do the create as the user. I tested changing the controller to 'without sharing' and it then picks up the values. Not sure what the implications would be for this in a production environment though? – Irene Nov 14 at 0:42
  • without sharing would skip record visibility restrictions for all SOQL statements in the controller, returning every record as if it were executing in System Mode. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Glen De Marcos Nov 14 at 1:03
  • Okay thanks, that's all good then as the queries are all related to displaying data to the community user. – Irene Nov 14 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.