@isTest
private class LeadConvertPageTest {
static testMethod void myUnitTest() {
User current = [
SELECT Id, Alias, Email, ProfileId, TimeZoneSidKey, LocaleSidKey,
LanguageLocaleKey, EmailEncodingKey
FROM User
WHERE Id = :UserInfo.getUserId()
LIMIT 1
];
Account newAccount = new Account(
name = 'Test Account'
);
Insert newAccount;
E_B__c eb = new E_B__c(
Use_Case_Short__c = 'short',
Integrations_Short__c = 'short',
Technical_Evaluator_Email__c = 'asd@asd.com',
Technical_Evaluator_Name__c = 'test'
);
insert eb;
Lead newLead = new Lead(
Email = 'newlead1@example.com',
Company = 'Test Account',
LastName= 'Test Lead',
Status = 'Attempted',
LeadSource = 'Web',
CountryCode = 'UA',
Lifecycle_Stage__c ='MQL',
Ownerid = Current.Id,
E_B__c = eb.Id
);
// Prepare DML options to bypass duplicate rules
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.duplicateRuleHeader.allowSave = true; // allow the save even if a duplicate is detected
dmlOpts.duplicateRuleHeader.runAsCurrentUser = true;
// (optional) you can also set dmlOpts.duplicateRuleHeader.duplicateRuleContext
// to e.g. 'TEST' or 'BYPASS' for reporting or rule‐context filtering
// Assign the options to your record
newLead.setOptions(dmlOpts);
// Perform the insert
Database.SaveResult sr = Database.insert(newLead, dmlOpts);
System.assert(sr.isSuccess(), 'Lead inserted despite duplicate rule');
ApexPages.StandardController stdController = new ApexPages.StandardController(newLead);
leadConvertController leadController = new leadConvertController(stdController);
leadcontroller.leadToConvert = newLead;
leadController.getMyComponentController();
leadController.getmyDescriptionComponentController();
leadController.getmyTaskComponentController();
leadController.getThis();
PageControllerBase pgBase = new PageControllerBase();
pgBase.getMyComponentController();
pgBase.getmyDescriptionComponentController();
pgBase.getmyTaskComponentController();
pgBase.getThis();
pgBase.getmyReminderComponentController();
ComponentControllerBase compBase = new ComponentControllerBase();
compBase.pageController = pgBase;
compBase.pageControllerDescription = pgBase;
compBase.pageControllerReminder = pgBase;
compBase.pageControllerTask = pgBase;
leadController.setComponentController(new leadConvertCoreComponentController());
leadController.setDescriptionComponentController(new leadConvertTaskDescComponentController());
leadController.setTaskComponentController(new leadConvertTaskInfoComponentController() );
system.assert(leadController.myTaskComponentController != null);
leadController.myTaskComponentController.taskID.Subject = 'TEST TASK';
leadController.myTaskComponentController.taskID.Priority = 'High';
leadController.myTaskComponentController.taskID.Status = 'Not Started';
leadController.myComponentController.selectedAccount = newAccount.Id;
leadController.myComponentController.leadConvert = newLead;
Contact contactID = leadController.myComponentController.contactID;
leadController.myComponentController.doNotCreateOppty = true;
leadController.myComponentController.opportunityID.Name = 'Test';
List<SelectOption> leadStatuses = leadController.myComponentController.LeadStatusOption;
Opportunity opportunityID = leadController.myComponentController.opportunityID;
//leadController.reminder = true;
String reminderTime = leadController.myTaskComponentController.remCon.reminderTime;
List<SelectOption> timeOptions = leadController.myTaskComponentController.remCon.ReminderTimeOption;
leadController.myDescriptionComponentController.sendNotificationEmail = false;
leadController.myComponentController.sendOwnerEmail = false;
List<SelectOption> priorityOptions = leadController.myTaskComponentController.TaskPriorityOption;
List<SelectOption> statusOptions = leadController.myTaskComponentController.TaskStatusOption;
leadController.LeadToConvert.OwnerId=Current.Id;
leadController.leadToConvert.Status = 'Qualified';
leadController.leadToConvert.Lifecycle_Stage__c ='SQL';
leadController.myComponentController.doNotCreateOppty = true;
leadController.PrintErrors(new List<Database.Error>());
leadController.PrintError('Test');
// Act
Test.startTest();
PageReference pr = leadController.convertLead();
Test.stopTest();
System.assert(leadController.leadConvertResult.success);
//see if the new account was created
Account [] checkAccount = [SELECT Id FROM Account WHERE Name ='Test Account' ];
system.debug(checkAccount);
system.assertEquals(1, checkAccount.size(), 'There was a problem converting lead to an account');
//see if the new contact was created
Contact [] checkContact = [SELECT Id FROM Contact WHERE Name ='Test Lead' ];
system.debug(checkContact);
system.assertEquals(1, checkContact.size(), 'There was a problem converting lead to a contact');
//
string reminderDate = leadController.myTaskComponentController.remCon.disabledActivityDate;
leadController.myComponentController.accountChanged();
leadController.myComponentController.selectedAccount = 'NEW';
leadController.myComponentController.accountChanged();
// test the reminder time as a French user to test the 24 hour clock
Profile p = [select id from profile where name='Standard User'];
//List<UserRole> roles = [SELECT Id, DeveloperName FROM UserRole WHERE DeveloperName = 'Director_Inside_Sales'];
User u = new User(alias = 'standt', email='standarduser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='fr',
localesidkey='fr', profileid = p.Id,
timezonesidkey='America/Los_Angeles',
username='testUsera1@testleadconvert.com'//,
//UserRole = roles.get(0)
);
System.runAs(u) {
timeOptions = leadController.myTaskComponentController.remCon.ReminderTimeOption;
leadController.myComponentController.selectedAccount = 'NONE';
leadController.myDescriptionComponentController.sendNotificationEmail = false;
//test the situation where there is a due date but no subject
leadController.myTaskComponentController.taskID.ActivityDate = system.today();
leadController.myTaskComponentController.taskID.Subject = '';
leadController.myComponentController.leadConvert.Status = 'NONE';
//convert the lead
leadController.myComponentController.doNotCreateOppty = true;
leadController.saveLead();
leadController.convertLead();
}
leadController.myComponentController.accountLookedUp();
leadController.myTaskComponentController.DueDateChanged();
}
@IsTest
static void failConvertTest() {
// Given
Account newAccount = new Account(Name = 'Test Account');
insert newAccount;
Lead newLead = new Lead(
Email = 'newlead@example.com',
Company = 'Test Account',
LastName = 'Test Lead',
Status = 'Attempted',
LeadSource = 'Web',
Lifecycle_Stage__c = 'MQL'
);
insert newLead;
ApexPages.StandardController stdController = new ApexPages.StandardController(newLead);
LeadConvertController leadController = new LeadConvertController(stdController);
// wire in your component controllers
leadController.setComponentController(new leadConvertCoreComponentController());
// ── add this line to avoid NPE ──
leadController.myComponentController.leadConvert = leadController.leadToConvert;
leadController.setDescriptionComponentController(new leadConvertTaskDescComponentController());
leadController.setTaskComponentController(new leadConvertTaskInfoComponentController());
leadController.myComponentController.doNotCreateOppty = false;
// When
Test.startTest();
leadController.convertLead();
Test.stopTest();
// Then: no Contact created
List<Contact> checkContact = [SELECT Id FROM Contact WHERE Name = 'Test Lead'];
System.assertEquals(0, checkContact.size());
// — lines to cover contactID owner logic —
Contact autoContact = leadController.myComponentController.contactID;
Id dbOwner = [SELECT OwnerId FROM Lead WHERE Id = :newLead.Id].OwnerId;
System.assertEquals(
dbOwner,
autoContact.OwnerId,
'When leadConvert.ownerId starts with 005, contactID.OwnerId must match the Lead’s owner'
);
}
@IsTest
static void testControllerSettersAndGetters() {
// — Arrange: create & insert dummy Lead —
Lead dummyLead = new Lead(LastName='Tester', Company='Acme');
insert dummyLead;
// — Instantiate your main controller —
ApexPages.StandardController stdCtrl = new ApexPages.StandardController(dummyLead);
LeadConvertController controller = new LeadConvertController(stdCtrl);
// — Instantiate each sub-controller —
LeadConvertCoreComponentController coreCtrl = new LeadConvertCoreComponentController();
LeadConvertTaskInfoComponentController taskInfoCtrl = new LeadConvertTaskInfoComponentController();
LeadConvertTaskDescComponentController taskDescCtrl = new LeadConvertTaskDescComponentController();
// — Wire them in via your overrides —
controller.setComponentController(coreCtrl);
controller.setTaskComponentController(taskInfoCtrl);
controller.setDescriptionComponentController(taskDescCtrl);
// — Assert: getters return exactly what we set —
ComponentControllerBase gotCore = controller.getMyComponentController();
ComponentControllerBase gotTaskInfo = controller.getmyTaskComponentController();
ComponentControllerBase gotTaskDesc = controller.getmyDescriptionComponentController();
System.assertEquals(coreCtrl, (LeadConvertCoreComponentController)gotCore,
'getMyComponentController() should return the core controller instance');
System.assertEquals(taskInfoCtrl, (LeadConvertTaskInfoComponentController)gotTaskInfo,
'getmyTaskComponentController() should return the task-info controller instance');
System.assertEquals(taskDescCtrl, (LeadConvertTaskDescComponentController)gotTaskDesc,
'getmyDescriptionComponentController() should return the desc controller instance');
// — Now exercise saveLead() for coverage —
// Capture original values
String origStatus = controller.leadToConvert.Status;
String origStage = controller.leadToConvert.Lifecycle_Stage__c;
// Overwrite in-memory to the static QSO values
controller.leadToConvert.Status = LeadConvertController.QSO_LEAD_STATUS;
controller.leadToConvert.Lifecycle_Stage__c = LeadConvertController.QSO_LEAD_STAGE;
Test.startTest();
controller.saveLead();
Test.stopTest();
// Verify that the DB record rolled back to original
Lead fresh = [SELECT Status, Lifecycle_Stage__c
FROM Lead
WHERE Id = :dummyLead.Id];
System.assertEquals(origStatus, fresh.Status,
'saveLead() should revert the DB Status to its original value');
System.assertEquals(origStage, fresh.Lifecycle_Stage__c,
'saveLead() should revert the DB Stage to its original value');
// And the controller instance still holds the “new” values
System.assertEquals(LeadConvertController.QSO_LEAD_STATUS,
controller.leadToConvert.Status,
'Controller instance should retain the new Status');
System.assertEquals(LeadConvertController.QSO_LEAD_STAGE,
controller.leadToConvert.Lifecycle_Stage__c,
'Controller instance should retain the new Stage');
}
@IsTest
/**
* Helper to create & insert a Lead under a fresh Account,
* then re-query it with its picklist and OwnerId populated.
*/
private static Lead makeLead() {
Account acct = new Account(Name='Test Account');
insert acct;
// grab first picklist values
String st = Lead.Status.getDescribe().getPicklistValues()[0].getValue();
String ls = Lead.Lifecycle_Stage__c.getDescribe().getPicklistValues()[0].getValue();
Lead L = new Lead(
LastName = 'AutoTest',
Company = acct.Name,
Status = st,
Lifecycle_Stage__c = ls
);
insert L;
// re-query to get OwnerId, etc.
return [SELECT Id, Company, Status, Lifecycle_Stage__c, OwnerId
FROM Lead WHERE Id = :L.Id];
}
static void testConvertLead_missingStatusShowsError() {
// Arrange
Lead L = makeLead(); // assumes you have a helper that creates & inserts a Lead
LeadConvertController ctl = new LeadConvertController(
new ApexPages.StandardController(L)
);
// Wire in a core controller that has no status selected
LeadConvertCoreComponentController core = new LeadConvertCoreComponentController();
core.leadConvert = ctl.leadToConvert;
core.leadConvert.Status = 'NONE';
ctl.setComponentController(core);
// Act
Test.startTest();
PageReference pr = ctl.convertLead();
Test.stopTest();
// Assert: we should short-circuit and throw a message
System.assertEquals(null, pr);
Boolean found = false;
for (ApexPages.Message m : ApexPages.getMessages()) {
if (m.getDetail().contains('Please select a Lead Status')) {
found = true;
break;
}
}
System.assert(found, 'Expected “Please select a Lead Status.” error');
}
@IsTest
static void testConvertLead_missingStageShowsError() {
// Arrange
Lead L = makeLead();
LeadConvertController ctl = new LeadConvertController(
new ApexPages.StandardController(L)
);
// Wire in a core controller that has no stage selected
LeadConvertCoreComponentController core = new LeadConvertCoreComponentController();
core.leadConvert = ctl.leadToConvert;
core.leadConvert.Lifecycle_Stage__c = 'NONE';
core.selectedAccount = 'NEW'; // skip account error
ctl.setComponentController(core);
// Act
Test.startTest();
PageReference pr = ctl.convertLead();
Test.stopTest();
// Assert
System.assertEquals(null, pr);
Boolean found = false;
for (ApexPages.Message m : ApexPages.getMessages()) {
if (m.getDetail().contains('Please select a Lifecycle Stage')) {
found = true;
break;
}
}
System.assert(found, 'Expected “Please select a Lifecycle Stage.” error');
}
@IsTest
static void testPrintErrorAddsPageMessage() {
// Arrange
Lead L = makeLead();
LeadConvertController ctl = new LeadConvertController(
new ApexPages.StandardController(L)
);
// clear any prior messages
// (ApexPages.getMessages() appends; we’ll just look for our new text)
// Act
ctl.PrintError('UnitTestError123');
// Assert
Boolean found = false;
for (ApexPages.Message m : ApexPages.getMessages()) {
if (m.getDetail() == 'UnitTestError123') {
found = true;
break;
}
}
System.assert(found, 'PrintError should add a page message with our text');
}
}