I am attempting to test some code written for an inline visualforce page. This code operates under the assumption that the user viewing it has a contact associated to it. I am attempting to create the User and run my tests as that portal user. I am running into issues inserting the User object. The first error I receive is that the "portal account owner must have a role" at which point I add a role to the creation of the user object and receive the error "High Volume Portal Users cannot have a user role". Maybe I have a misconception or misunderstanding that is causing this problem. Here is the code that I have written to create the user for the first test:
Profile prof = [SELECT Id FROM Profile WHERE Name='CustomProfile With High Volume Web User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = prof.Id, ContactId = c1.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
and this code when I get the second error
UserRole r = new UserRole(name = 'TEST ROLE');
insert r;
Profile prof = [SELECT Id FROM Profile WHERE Name='CustomProfile With High Volume Web User'];
User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = prof.Id, ContactId = c1.Id, userroleid = r.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
Thanks for your help.
EDIT***
I tried implementing what @JimRae recommended but am getting a DML error when attempting to insert them the way he stated.
UserRole r = new UserRole(name = 'TEST ROLE');
insert r;
Profile prof1 = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u1 = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = prof1.Id, userroleid = r.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='stanuser@testorg.com');
insert u1;
Account a = new Account();
a.OwnerId = u1.Id;
insert a;
The error reads:
"13:26:10:192 EXCEPTION_THROWN [21]|System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []"