I am migrating and Apex Visualforce page and controller from a Sandbox to Production for the first time. The page is an update of a community registration with a few minor changes (removing a field, changing some the password requirements, and updating the error messages.) I have updated the page and the controller but getting the test class to 75% code coverage is way beyond my very limited coding stills. Can anyone suggest ways to get this to 75%. Thank you!!
Here is the controller page:
public without sharing class VolunteerRegPage2019 {
public String firstName {get; set;}
public String lastName {get; set;}
public String email {get; set;}
public String chapterAffiliationName {get;set;}
public String chapterAffiliationId {get;set;}
public String password {get; set; }
public String confirmPassword {get; set; }
public String accKeySearch {get;set;}
public Boolean openPopUp{get;set;}
public Boolean openLookup {get;set;}
public Boolean err{get;set;}
public Boolean redir{get;set;}
public String errMsg {get;set;}
public String pHeaderMsg {get;set;}
public List<Account> accountList {get;set;}
private String userId {get; set;}
public VolunteerRegPage2019(){
openPopUp = false;
openLookup = false;
err = false;
redir = false;
chapterAffiliationId = '';
accountList = [Select Id, Name, Owner.Name, Type, RecordTypeId from Account
where RecordTypeId =: '01236000000x7F4AAI' Limit 5];
}
private boolean isValidPass(){
if(password == confirmPassword){
if(password.length() == 8 || password.length() > 8){
System.debug('password.toUpperCase(): ' + password.toUpperCase());
// if(Pattern.matches('^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]+$', password.toUpperCase())){
// ^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])
// ^(?=.[A-Za-z])(?=.\d)(?=.[@$!%#?&])[A-Za-z\d@$!%*#?&]{8,}$
if(Pattern.matches('^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!#%*?&]{8,20}$', password)){
System.debug('match pass ok.');
return true;
}else{
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Warning';
errMsg = 'Your password must be 8-20 characters and contain:' +
'<br />' +
'one uppercase letter' +
'<br />' +
'one lowercase letter' +
'<br />' +
'one number' +
'<br />' +
'one symbol';
return false;
}
}else{
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Warning';
errMsg = 'Your password must be 8-20 characters and contain:' +
'<br />' +
'one uppercase letter' +
'<br />' +
'one lowercase letter' +
'<br />' +
'one number' +
'<br />' +
'one symbol';
return false;
}
}else {
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Warning';
errMsg = 'Please check. Passwords do not match!';
return false;
}
}
public void initialSelect(){
System.debug('call initialSelect()');
openPopUp = true;
openLookup = true;
err = false;
pHeaderMsg = 'Info';
System.debug('openPopUp: ' + openPopUp);
}
public void searchAccount(){
String search = '%' + accKeySearch + '%';
accountList = [Select Id, Name, RecordTypeId from Account where Name like: search
and RecordTypeId =: '01236000000x7F4AAI' Limit 5];
}
public void selectAccount(){
System.debug('chapterAffiliationId: ' + chapterAffiliationId);
if(chapterAffiliationId != null && chapterAffiliationId.length() > 0){
Account a = [Select Id, Name from Account where id =: chapterAffiliationId Limit 1];
chapterAffiliationName = String.valueOf(a.Name);
openLookup = false;
openPopUp = false;
err = false;
}
}
public void saveVolunteer() {
//check if password is valid
if (!isValidPass()) return;
List<Contact> contacts;
List<Account> accounts;
contacts = [
SELECT Id, FirstName, LastName, Email, EWB_USA_Membership_Status1__c
FROM Contact
WHERE Email = :email
AND LastName = :lastName
AND FirstName = :firstName
AND EWB_USA_Membership_Status1__c = 'Expired'
];
if (contacts.size() == 1) {
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Error';
errMsg = 'Only active EWB-USA members can access Volunteer Village.';
return;
}
contacts = [
SELECT Id, FirstName, LastName, Email, EWB_USA_Membership_Status1__c
FROM Contact
WHERE Email != :email
AND LastName = :lastName
AND FirstName = :firstName
AND EWB_USA_Membership_Status1__c = 'Active'
];
if (contacts.size() == 1) {
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Error';
errMsg = 'Please enter the same name and email address as you used to register';
}
contacts = [
SELECT Id, FirstName, LastName, Email, EWB_USA_Membership_Status1__c
FROM Contact
WHERE Email = :email
AND LastName = :lastName
AND FirstName = :firstName
AND EWB_USA_Membership_Status1__c = 'Active'
];
if (contacts.size() != 1) {
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Error';
errMsg = 'Your entry does not match our volunteer record.;
return;
}
Contact cont = contacts[0];
Account acc;
if (chapterAffiliationId != null && chapterAffiliationId.length() > 0) {
accounts = [SELECT Id, Name FROM Account WHERE Id =: chapterAffiliationId LIMIT 1];
if (accounts.size() == 1) {
acc = accounts[0];
} else {
// eroare
}
}
/*else {
accounts = [SELECT Id, Name FROM Account WHERE Name = 'Volunteer Account' LIMIT 1];
if (accounts.size() == 1) {
acc = accounts[0];
} else {
acc = new Account();
acc.Name = 'Volunteer Account';
insert acc;
}
} */
//update contact
// cont.AccountId = acc.Id;
update cont;
contact ct = [select id, accountId from contact where id=:cont.id];
Account ac = [select id, name from account where Id =: ct.accountId limit 1];
System.debug('account id' +ac.id);
System.debug('cont : ' + cont );
List<User> existingUsers = [
SELECT Id, IsActive
FROM User
WHERE Email = :email AND Profile.UserLicense.Name = 'Customer Community Plus Login'
AND Contact.EWB_USA_Membership_Status1__c = 'Active'
];
if (existingUsers.size() == 1) {
if (existingUsers[0].IsActive == false) {
existingUsers[0].IsActive = true;
update existingUsers[0];
}
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Error';
errMsg = 'You already have an account, click OK to sign in!';
userId = existingUsers[0].Id;
} else {
// registerUser(acc, cont);
registerUser(ac, cont);
}
}
public PageReference closePopup() {
if(userId != null){
if (password != null && password.length() > 1) {
return Site.login(email, password, ApexPages.currentPage().getParameters().get('startURL'));
}else {
PageReference page = new PageReference('https://ewb-usa.force.com/VolunteerVillage/_ui/core/chatter/ui/ChatterPage');
page.setRedirect(true);
return page;
}
}
openPopUp = false;
openLookup = false;
err = false;
return null;
}
public void registerUser(Account acc, Contact cont) {
//create user
Profile prof = [Select Id, Name from Profile where Name = 'EWB Volunteer' limit 1];
Integer rand = Math.round(Math.random()*1000);
String userName = email;
String nickName = firstName + '_' + lastName + '' + String.valueOf(rand);
User u = new User();
u.Username = userName;
u.Email = email;
u.FirstName = firstName;
u.LastName = lastName;
u.CommunityNickname = nickName;
u.ProfileId = prof.Id;
try {
userId = Site.createExternalUser(u, String.valueOf(acc.Id), password);
//redirect to login page
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Thank you!';
if (password != null && password.length() > 1) {
errMsg = 'Your Account was created succesfully.';
}else {
errMsg = 'Your Account was created succesfully.An email has been sent to you with your temporary password.';
}
redir = true;
//redirectPage(userId, userName, password);
} catch(Site.ExternalUserCreateException ex) {
openPopUp = true;
openLookup = false;
err = true;
pHeaderMsg = 'Error';
errMsg = '';
List<String> errors = ex.getDisplayMessages();
for (String error : errors) {
errMsg += error + ' \n';
}
System.debug(ex.getMessage());
}
}
}
And the Test Class
@isTest(SeeAllData=true)
public class VolunteerReg_TestClass {
public static testMethod void testMethods(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
List<Account> acc1List = [Select Id, Name, Owner.Name, Type, RecordTypeId from Account
where RecordTypeId =: '01236000000x7F4AAI' Limit 5];
System.assertEquals(false, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(false, controller.err);
System.assertEquals(false, controller.redir);
controller.initialSelect();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(true, controller.openLookup);
System.assertEquals(false, controller.err);
System.assertEquals('Info', controller.pHeaderMsg);
controller.accKeySearch = 'acc';
controller.searchAccount();
RecordType rec = [Select Id, Name From RecordType where id=: '01236000000x7F4AAI' Limit 1];
Account acc = new Account();
acc.Name = 'Volunteer';
acc.RecordTypeId = rec.Id;
insert acc;
List<Account> acc2List = [Select Id, Name, RecordTypeId from Account where Name like: '%Vol%'
and RecordTypeId =: rec.Id Limit 5];
//System.assertEquals(acc2List, controller.accountList);
controller.chapterAffiliationId = String.valueOf(acc2List[0].Id);
controller.selectAccount();
System.assertEquals(acc2List[0].Name, controller.chapterAffiliationName);
controller.closePopup();
System.assertEquals(false, controller.openLookup);
System.assertEquals(false, controller.err);
System.assertEquals(false, controller.openPopUp);
}
public static testMethod void testUserReg(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Test1234';
controller.confirmPassword = 'Test1234';
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
System.assertEquals(false, controller.redir);
System.assertEquals('Warning', controller.pHeaderMsg);
}
public static testMethod void testPAssNotEqual(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Test1234!';
controller.confirmPassword = 'Test12356u7658';
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
}
public static testMethod void testcreateContact(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Qwerty123454534252';
controller.confirmPassword = 'Qwerty123454534252';
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
}
public static testMethod void testContact(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Qwerty123454534252';
controller.confirmPassword = 'Qwerty123454534252';
RecordType rec = [Select Id, Name from RecordType where DeveloperName = 'Chapter_Member_Record_Type'];
Contact con = new Contact();
con.RecordTypeId = rec.Id;
con.FirstName = 'testFrst';
con.lastName = 'testLast';
con.email = '[email protected]';
insert con;
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
}
public static testMethod void testShortPass(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Test';
controller.confirmPassword = 'Test';
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
System.assertEquals('Warning', controller.pHeaderMsg);
}
public static testMethod void testExistingContact(){
VolunteerRegPage2019 controller = new VolunteerRegPage2019();
controller.firstName = 'testFrst';
controller.lastName = 'testLast';
controller.email = '[email protected]';
controller.password = 'Qwerty123454534252!';
controller.confirmPassword = 'Qwerty123454534252!';
RecordType rec = [Select Id, Name from RecordType where DeveloperName = 'Chapter_Member_Record_Type'];
Contact con = new Contact();
con.RecordTypeId = rec.Id;
con.FirstName = 'testFrst';
con.lastName = 'testLast';
con.email = '[email protected]';
insert con;
controller.saveVolunteer();
System.assertEquals(true, controller.openPopUp);
System.assertEquals(false, controller.openLookup);
System.assertEquals(true, controller.err);
}
}
seeAllData=true
test classes is often an exercise in futility because their behavior changes based on the data in the org. I'd suggest working through the linked resources and reconstructing this test class towards best practices, which is likely to produce more coverage.