12

I Wrote a very simple VF Page and Controller to show the Accounts that voted on certain Ideas. Here is the controller. Just aggregates the Up and Down votes on the Idea excluding internal votes. Pretty straight forward...

public class IdeaVotesByAccountController {

    public Idea thisIdea                                            {get;set;}
    public list<AggregateResult> upResults                          {get;set;}
    public list<AggregateResult> downResults                        {get;set;}

    public IdeaVotesByAccountController() {

        thisIdea = [Select Id, Title 
                    From Idea 
                    Where Id =: system.currentPageReference().getParameters().get('ideaid')];

        upResults = [SELECT Count(Id) Votes, CreatedBy.Contact.Account.Name Name
                    From Vote Where ParentId =: thisIdea.Id And Type = 'up' And CreatedBy.Contact.Account.Name != 'My Company Name' And CreatedBy.Contact.Account.Name != null
                    Group By CreatedBy.Contact.Account.Name 
                    Order By Count(Id) desc, CreatedBy.Contact.Account.Name];

        downResults = [SELECT Count(Id) Votes, CreatedBy.Contact.Account.Name Name
                    From Vote Where ParentId =: thisIdea.Id And Type = 'down' And CreatedBy.Contact.Account.Name != 'My Company Name' And CreatedBy.Contact.Account.Name != null
                    Group By CreatedBy.Contact.Account.Name 
                    Order By Count(Id) desc, CreatedBy.Contact.Account.Name];

    }

    public PageReference returnToIdea() {

        PageReference pg = new PageReference('/' + thisIdea.Id);
        pg.setRedirect(true);
        return pg;

    }
}

My problem is coming when trying to write a unit test. When I say that we are excluding internal votes, basically, we are only counting portal user votes, from portal users that do not have a company name of my company name. So in order to test, I need to be able to test all 3 scenarios scenarios

  1. a vote from within the SF UI, not through the portal (No Issue)
  2. a vote by a portal user outside of my company (ISSUE)
  3. a vote by a portal user inside of my company (ISSUE)

The issue comes with trying to create a vote from a portal user. Here is my test code that I have. The first test method works fine, the second method is throwing an error

@isTest
private class IdeaVotesByAccountControllerTest {

    private static final Profile nonPortalProf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
    private static final Profile portalProf = [SELECT Id FROM Profile WHERE Name Like '%portal%' limit 1];
    private static final Account a = TestClassObjectUtility2.accountCreator('Test Account');
    private static final Contact con = TestClassObjectUtility2.contactCreator(a.Id, 'Joe', 'Schmoe');
    public static final Community zone = [Select Id From Community Limit 1];
    public static final PageReference pg = Page.IdeaVotesByAccount;
    public static final User nonPortalUser = TestClassObjectUtility2.userCreator(true, nonPortalProf);
    public static final User portalUser;
    public static IdeaVotesByAccountController controller;

    static {
        portalUser = TestClassObjectUtility2.userCreator(false, portalProf);
        portalUser.ContactId = con.Id;
        insert portalUser;
    }
    //WORKS FINE
    static testmethod void BaseTestNonPortalUser() {

        Idea myIdea = TestClassObjectUtility2.ideaCreator(true, 'Title', 'Body', zone.Id);
        myIdea = [Select Id From Idea Where Id =: myIdea.Id];

        pg.getParameters().put('ideaid', myIdea.id);
        Test.setCurrentPage(pg);

        controller = new IdeaVotesByAccountController();

        system.assertEquals(0, controller.upResults.size());
        system.assertEquals(0, controller.downResults.size());

        system.runAs(nonPortalUser){
            Vote v = TestClassObjectUtility2.voteCreator(true, myIdea.Id, 'up');
        }

        controller = new IdeaVotesByAccountController();
        system.assertEquals(0, controller.upResults.size());

    }
    //FAILS
    static testmethod void BaseTestPortalUser() {

        Idea myIdea = TestClassObjectUtility2.ideaCreator(true, 'Title', 'Body', zone.Id);

        pg.getParameters().put('ideaid', myIdea.id);
        Test.setCurrentPage(pg);

        controller = new IdeaVotesByAccountController();

        system.assertEquals(0, controller.upResults.size());
        system.assertEquals(0, controller.downResults.size());

        system.runAs(portalUser){
             //THIS LINE IS FAILING
             Vote v = TestClassObjectUtility2.voteCreator(true, myIdea.Id, 'up');
        }

        controller = new IdeaVotesByAccountController();
        system.assertEquals(1, controller.upResults.size());

    }
}

It throws the error on the line where I am trying to insert the Vote object. The error is shown below

enter image description here

System.DmlException: Insert failed. First exception on row 0; first error: COMMUNITY_NOT_ACCESSIBLE, You do not have permission to access the zone that this entity belongs to. You must be given permission to the zone before you can access this entity.: [ParentId]

So apparently the portal user needs permission to the community. The issue is I have searched around for documentation but I can't seem to find out how to do this. Has anyone seen this before. How can I overcome this error???

Any help is greatly appreciated.

7
  • I know you are probably already doing this but did you try wrapping the code that creates and inserts the Vote in a system.runAs block using the community user as the 'User'
    – Eric
    Commented Jul 27, 2014 at 17:35
  • Yes I am doing trying this currently and no dice :( Commented Jul 27, 2014 at 21:58
  • I am going to guess that communities use some form of criteria based sharing behind the scenes bypassing OWD sharing to provide access to the portal users. If that is the case then current test method infrastructure does not take these types of sharing rules into account. To test, refactor the code to not be a test method but to actually perform the steps in the environment. If it works that way then you will know it is a testing limitation. And old post from 2011 that may shed some light: developer.salesforce.com/forums/…
    – Eric
    Commented Jul 28, 2014 at 4:46
  • 1
    Have you made any headway on this? I see this issue possibly affecting me in the near future....
    – Eric
    Commented Aug 1, 2014 at 1:51
  • 1
    I haven't. I am guessing though by the lack of response that it might be a bug. Commented Aug 1, 2014 at 1:55

1 Answer 1

-2

Please refer following link. it may help https://help.salesforce.com/HTViewHelpDoc?id=ideas_new_comm.htm&language=en_US

1
  • 2
    Also, really has nothing to do with the question. Just a generic help doc on community zones. Commented Jan 14, 2016 at 12:35

You must log in to answer this question.

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