I received the result from security review from Salesforce team and it failed. One of the vulnerability shown in the report was CRUD/FLS Enforcement. The report suggest I use the isAccessible() check before accessing data.
I have a controller that does look up on Contact, Account, and Task object. I added the isAccessible check before selecting from the object. Here is one of the method from the controller (after I added isAccessible check)
public String getAllContacts(){
// Check if the user has read access on the Contact fields
if (!Schema.sObjectType.Contact.fields.id.isAccessible() &&
!Schema.sObjectType.Contact.fields.phone.isAccessible() &&
!Schema.sObjectType.Contact.fields.firstname.isAccessible() &&
!Schema.sObjectType.Contact.fields.lastname.isAccessible())
{
return '';
}
allContactList = [SELECT id, phone, firstname, lastname FROM Contact LIMIT 200];
return JSON.serialize(allContactList);
}
After adding the check, my code coverage went down to 63%. And I think it is because of the if statement.
How can I include the check for isAccessible in my test class to increase my code coverage?
Here is my test class:
@isTest
public class SoftphoneSearchController_test {
public static testMethod void validateSoftphoneController() {
Test.startTest();
SoftphoneSearchController objSoftphone = new SoftphoneSearchController();
list<SoftphoneSearchController> SoftList = new list<SoftphoneSearchController>();
String name = 'Jon';
objSoftPhone.contactName = 'Snow';
objSoftPhone.accountName = 'United';
objSoftPhone.phoneNumber='5556667778';
list<Contact> ct=new list<Contact>();
objSoftphone.contactList=ct;
list<Account> Ac=new list<Account>();
objSoftphone.accountList=Ac;
list<task> T=new list<task>();
objSoftphone.commentList=T;
list<task> Ta=new list<task>();
objSoftphone.allCommentList=Ta;
objSoftphone.getAllComments();
objSoftphone.getAllContacts();
objSoftphone.getCurrentUserID();
//objSoftPhone.searchAccounts();
objSoftPhone.getContactByName(name);
objSoftPhone.getContact('1112223333');
objSoftPhone.getCommentByParam('12345');
SoftList.add(objSoftPhone);
//insert SoftList;
System.assert(SoftList.size() > 0);
Test.stopTest();
}
}
Currently my code coverage is at 63%. Please advise how can I increase my code coverage.
I have looked at
- https://developer.salesforce.com/page/Enforcing_CRUD_and_FLS
- https://developer.salesforce.com/page/Testing_CRUD_and_FLS_Enforcement
These article doesn't have what I am looking for.
Thank you