mardi 3 novembre 2015

Test Class for Apex Answers Authorisation Provider Chatter Registration

Hello I recently started my Internship as a salesforce developer and I'm having a very difficult time with test classes. But there is a perticular case that has really got me banging my head against a wall.

When you enable Chatter, a couple of classes get added to your Account. I've managed to create basic test classes for most of them but this one really got me stuck. Here is the piece of code that was generated.
global class ChatterAnswersAuthProviderRegistration implements Auth.RegistrationHandler{
global User createUser(Id portalId, Auth.UserData data){
Savepoint sp = Database.setSavepoint();
String accountId = null;
try { 
    accountId = new ChatterAnswers().createAccount(data.firstname, data.lastname, Site.getAdminId());
} catch (Exception e) {
    Database.rollback(sp);
    return null;
}
User u = new User();
u.FirstName = data.firstname;
u.LastName = data.lastname;
u.Username = accountId + '@facebook.com';
u.Email = data.email;
u.CommunityNickname = data.email.substring(0,  data.email.indexOf('@')) + System.currentTimeMillis();
u.UserPermissionsChatterAnswersUser = true;
//If using this registration handler with Salesforce Community registration then specify the profileid to associated with the new user.
//This profile is used only for Salesforce Community site registration and not for standalone Force.com site registration.
//u.profileId = '';
Site.setPortalUserAsAuthProvider(u, accountId);
return u;
}

global void updateUser(Id userId, Id portalId, Auth.UserData data){
Savepoint sp = Database.setSavepoint();
User u = [SELECT Contact.email, Contact.firstName, Contact.lastName FROM User WHERE id=:userId];
u.Contact.email = data.email;
u.Contact.firstName = data.firstName;
u.Contact.lastName = data.lastName;
u.firstName = data.firstName;
u.lastName = data.lastName;
u.email = data.email;
try {
    update u;
    update u.Contact;
 } catch (Exception e) {
    Database.rollback(sp);
}
}
}

And here is the test class that I've got online from Salesforce developer website.

@isTest
   private class ChatterAnswersAuthProviderRegTest {
   static testMethod void validateCreateUpdateUser() {
   User thisUser = [ select Id from User where Id =    :UserInfo.getUserId() ];
    System.runAs ( thisUser ) {
    Auth.UserData userData = new Auth.UserData('testId', 'testFirst', 'testLast',
   'testFirs

t testLast', 'no-reply@salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
null, new Map<String, String>{'language' => 'en_US'});
      ChatterAnswersAuthProviderRegistration reg = new ChatterAnswersAuthProviderRegistration();
User newUser = reg.createUser(null, userData);
System.assert(newUser != null, 'A new user should have been created');
System.assertEquals(newUser.Firstname, 'testFirst', 'First name should have been same');
System.assertEquals(newUser.Lastname, 'testLast', 'Last name should have been same');
System.assertEquals(newUser.Email, 'no-reply@salesforce.com', 'Email should have been same');

Contact c = new Contact();
c.AccountId = (newUser.Username.split('@'))[0];
c.LastName = 'contactLast';
insert(c);

newUser.Alias = 'firstusr';
newUser.TimeZoneSidKey = 'America/Los_Angeles';
newUser.LocaleSidKey = 'en_US';
newUser.EmailEncodingKey = 'UTF-8';
newUser.LanguageLocaleKey = 'en_US';
newUser.ContactId = c.Id;

insert(newUser);


Auth.UserData updateUserData = new Auth.UserData('testId', 'updatedFirst', 'updatedLast',
'updatedFirst updatedLast', 'no-reply@new.salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
null, new Map<String, String>{'language' => 'en_US'});
reg.updateUser(newUser.Id, null, updateUserData);

User dbUser = [SELECT Id, Firstname, Lastname, Email FROM User WHERE Id = :newUser.Id];
System.assertEquals(dbUser.Firstname, 'updatedFirst', 'First name should have been updated');
System.assertEquals(dbUser.Lastname, 'updatedLast', 'Last name should have been updated');
System.assertEquals(dbUser.Email, 'no-reply@new.salesforce.com', 'Email should have been updated');
       }
       }
       }

The problem with this is that it comes up with two errors. One of them is

Failed to create createContainerMember for containerId=1dc250000009zMVAAY: duplicate value found: duplicates value on record with id:

and the other one is

expecting a right parentheses, found ','

On the website it says

Make sure you put the right profile id (i.e the one that you use for actual Chatter answers user registration) in the ChatterAnswersAuthProviderRegistration.createUser() (line #28) method before you run the test.

but I have no idea where I can get the Profile ID from and how to make this code work. Any help will be appriciated

Aucun commentaire:

Enregistrer un commentaire