fork download
  1. @isTest
  2. private class LeadConvertPageTest {
  3.  
  4. static testMethod void myUnitTest() {
  5. User current = [
  6. SELECT Id, Alias, Email, ProfileId, TimeZoneSidKey, LocaleSidKey,
  7. LanguageLocaleKey, EmailEncodingKey
  8. FROM User
  9. WHERE Id = :UserInfo.getUserId()
  10. LIMIT 1
  11. ];
  12. Account newAccount = new Account(
  13. name = 'Test Account'
  14. );
  15. Insert newAccount;
  16.  
  17. E_B__c eb = new E_B__c(
  18. Use_Case_Short__c = 'short',
  19. Integrations_Short__c = 'short',
  20. Technical_Evaluator_Email__c = 'asd@asd.com',
  21. Technical_Evaluator_Name__c = 'test'
  22. );
  23. insert eb;
  24.  
  25. Lead newLead = new Lead(
  26. Email = 'newlead1@example.com',
  27. Company = 'Test Account',
  28. LastName= 'Test Lead',
  29. Status = 'Attempted',
  30. LeadSource = 'Web',
  31. CountryCode = 'UA',
  32. Lifecycle_Stage__c ='MQL',
  33. Ownerid = Current.Id,
  34. E_B__c = eb.Id
  35. );
  36.  
  37. // Prepare DML options to bypass duplicate rules
  38. Database.DMLOptions dmlOpts = new Database.DMLOptions();
  39. dmlOpts.duplicateRuleHeader.allowSave = true; // allow the save even if a duplicate is detected
  40. dmlOpts.duplicateRuleHeader.runAsCurrentUser = true;
  41. // (optional) you can also set dmlOpts.duplicateRuleHeader.duplicateRuleContext
  42. // to e.g. 'TEST' or 'BYPASS' for reporting or rule‐context filtering
  43.  
  44. // Assign the options to your record
  45. newLead.setOptions(dmlOpts);
  46.  
  47. // Perform the insert
  48. Database.SaveResult sr = Database.insert(newLead, dmlOpts);
  49. System.assert(sr.isSuccess(), 'Lead inserted despite duplicate rule');
  50.  
  51. ApexPages.StandardController stdController = new ApexPages.StandardController(newLead);
  52. leadConvertController leadController = new leadConvertController(stdController);
  53.  
  54. leadcontroller.leadToConvert = newLead;
  55.  
  56. leadController.getMyComponentController();
  57. leadController.getmyDescriptionComponentController();
  58. leadController.getmyTaskComponentController();
  59. leadController.getThis();
  60.  
  61. PageControllerBase pgBase = new PageControllerBase();
  62. pgBase.getMyComponentController();
  63. pgBase.getmyDescriptionComponentController();
  64. pgBase.getmyTaskComponentController();
  65. pgBase.getThis();
  66. pgBase.getmyReminderComponentController();
  67.  
  68. ComponentControllerBase compBase = new ComponentControllerBase();
  69. compBase.pageController = pgBase;
  70. compBase.pageControllerDescription = pgBase;
  71. compBase.pageControllerReminder = pgBase;
  72. compBase.pageControllerTask = pgBase;
  73.  
  74.  
  75. leadController.setComponentController(new leadConvertCoreComponentController());
  76. leadController.setDescriptionComponentController(new leadConvertTaskDescComponentController());
  77. leadController.setTaskComponentController(new leadConvertTaskInfoComponentController() );
  78.  
  79. system.assert(leadController.myTaskComponentController != null);
  80. leadController.myTaskComponentController.taskID.Subject = 'TEST TASK';
  81. leadController.myTaskComponentController.taskID.Priority = 'High';
  82. leadController.myTaskComponentController.taskID.Status = 'Not Started';
  83. leadController.myComponentController.selectedAccount = newAccount.Id;
  84. leadController.myComponentController.leadConvert = newLead;
  85.  
  86.  
  87.  
  88. Contact contactID = leadController.myComponentController.contactID;
  89. leadController.myComponentController.doNotCreateOppty = true;
  90. leadController.myComponentController.opportunityID.Name = 'Test';
  91. List<SelectOption> leadStatuses = leadController.myComponentController.LeadStatusOption;
  92.  
  93.  
  94. Opportunity opportunityID = leadController.myComponentController.opportunityID;
  95. //leadController.reminder = true;
  96. String reminderTime = leadController.myTaskComponentController.remCon.reminderTime;
  97. List<SelectOption> timeOptions = leadController.myTaskComponentController.remCon.ReminderTimeOption;
  98. leadController.myDescriptionComponentController.sendNotificationEmail = false;
  99. leadController.myComponentController.sendOwnerEmail = false;
  100.  
  101.  
  102. List<SelectOption> priorityOptions = leadController.myTaskComponentController.TaskPriorityOption;
  103. List<SelectOption> statusOptions = leadController.myTaskComponentController.TaskStatusOption;
  104.  
  105.  
  106. leadController.LeadToConvert.OwnerId=Current.Id;
  107. leadController.leadToConvert.Status = 'Qualified';
  108. leadController.leadToConvert.Lifecycle_Stage__c ='SQL';
  109. leadController.myComponentController.doNotCreateOppty = true;
  110.  
  111. leadController.PrintErrors(new List<Database.Error>());
  112. leadController.PrintError('Test');
  113.  
  114. // Act
  115. Test.startTest();
  116. PageReference pr = leadController.convertLead();
  117. Test.stopTest();
  118.  
  119. System.assert(leadController.leadConvertResult.success);
  120.  
  121. //see if the new account was created
  122. Account [] checkAccount = [SELECT Id FROM Account WHERE Name ='Test Account' ];
  123. system.debug(checkAccount);
  124. system.assertEquals(1, checkAccount.size(), 'There was a problem converting lead to an account');
  125.  
  126. //see if the new contact was created
  127. Contact [] checkContact = [SELECT Id FROM Contact WHERE Name ='Test Lead' ];
  128. system.debug(checkContact);
  129. system.assertEquals(1, checkContact.size(), 'There was a problem converting lead to a contact');
  130.  
  131. //
  132. string reminderDate = leadController.myTaskComponentController.remCon.disabledActivityDate;
  133. leadController.myComponentController.accountChanged();
  134.  
  135. leadController.myComponentController.selectedAccount = 'NEW';
  136.  
  137. leadController.myComponentController.accountChanged();
  138.  
  139. // test the reminder time as a French user to test the 24 hour clock
  140. Profile p = [select id from profile where name='Standard User'];
  141. //List<UserRole> roles = [SELECT Id, DeveloperName FROM UserRole WHERE DeveloperName = 'Director_Inside_Sales'];
  142.  
  143. User u = new User(alias = 'standt', email='standarduser@testorg.com',
  144.  
  145. emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='fr',
  146.  
  147. localesidkey='fr', profileid = p.Id,
  148.  
  149. timezonesidkey='America/Los_Angeles',
  150. username='testUsera1@testleadconvert.com'//,
  151. //UserRole = roles.get(0)
  152. );
  153. System.runAs(u) {
  154. timeOptions = leadController.myTaskComponentController.remCon.ReminderTimeOption;
  155.  
  156.  
  157. leadController.myComponentController.selectedAccount = 'NONE';
  158. leadController.myDescriptionComponentController.sendNotificationEmail = false;
  159.  
  160. //test the situation where there is a due date but no subject
  161. leadController.myTaskComponentController.taskID.ActivityDate = system.today();
  162. leadController.myTaskComponentController.taskID.Subject = '';
  163. leadController.myComponentController.leadConvert.Status = 'NONE';
  164.  
  165. //convert the lead
  166. leadController.myComponentController.doNotCreateOppty = true;
  167. leadController.saveLead();
  168. leadController.convertLead();
  169. }
  170. leadController.myComponentController.accountLookedUp();
  171. leadController.myTaskComponentController.DueDateChanged();
  172.  
  173. }
  174. @IsTest
  175. static void failConvertTest() {
  176. // Given
  177. Account newAccount = new Account(Name = 'Test Account');
  178. insert newAccount;
  179.  
  180. Lead newLead = new Lead(
  181. Email = 'newlead@example.com',
  182. Company = 'Test Account',
  183. LastName = 'Test Lead',
  184. Status = 'Attempted',
  185. LeadSource = 'Web',
  186. Lifecycle_Stage__c = 'MQL'
  187. );
  188. insert newLead;
  189.  
  190. ApexPages.StandardController stdController = new ApexPages.StandardController(newLead);
  191. LeadConvertController leadController = new LeadConvertController(stdController);
  192.  
  193. // wire in your component controllers
  194. leadController.setComponentController(new leadConvertCoreComponentController());
  195. // ── add this line to avoid NPE ──
  196. leadController.myComponentController.leadConvert = leadController.leadToConvert;
  197. leadController.setDescriptionComponentController(new leadConvertTaskDescComponentController());
  198. leadController.setTaskComponentController(new leadConvertTaskInfoComponentController());
  199. leadController.myComponentController.doNotCreateOppty = false;
  200.  
  201. // When
  202. Test.startTest();
  203. leadController.convertLead();
  204. Test.stopTest();
  205.  
  206. // Then: no Contact created
  207. List<Contact> checkContact = [SELECT Id FROM Contact WHERE Name = 'Test Lead'];
  208. System.assertEquals(0, checkContact.size());
  209.  
  210. // — lines to cover contactID owner logic —
  211. Contact autoContact = leadController.myComponentController.contactID;
  212. Id dbOwner = [SELECT OwnerId FROM Lead WHERE Id = :newLead.Id].OwnerId;
  213. System.assertEquals(
  214. dbOwner,
  215. autoContact.OwnerId,
  216. 'When leadConvert.ownerId starts with 005, contactID.OwnerId must match the Lead’s owner'
  217. );
  218. }
  219.  
  220.  
  221.  
  222. @IsTest
  223. static void testControllerSettersAndGetters() {
  224. // — Arrange: create & insert dummy Lead
  225. Lead dummyLead = new Lead(LastName='Tester', Company='Acme');
  226. insert dummyLead;
  227.  
  228. // — Instantiate your main controller —
  229. ApexPages.StandardController stdCtrl = new ApexPages.StandardController(dummyLead);
  230. LeadConvertController controller = new LeadConvertController(stdCtrl);
  231.  
  232. // — Instantiate each sub-controller
  233. LeadConvertCoreComponentController coreCtrl = new LeadConvertCoreComponentController();
  234. LeadConvertTaskInfoComponentController taskInfoCtrl = new LeadConvertTaskInfoComponentController();
  235. LeadConvertTaskDescComponentController taskDescCtrl = new LeadConvertTaskDescComponentController();
  236.  
  237. // — Wire them in via your overrides —
  238. controller.setComponentController(coreCtrl);
  239. controller.setTaskComponentController(taskInfoCtrl);
  240. controller.setDescriptionComponentController(taskDescCtrl);
  241.  
  242. // — Assert: getters return exactly what we set —
  243. ComponentControllerBase gotCore = controller.getMyComponentController();
  244. ComponentControllerBase gotTaskInfo = controller.getmyTaskComponentController();
  245. ComponentControllerBase gotTaskDesc = controller.getmyDescriptionComponentController();
  246.  
  247. System.assertEquals(coreCtrl, (LeadConvertCoreComponentController)gotCore,
  248. 'getMyComponentController() should return the core controller instance');
  249. System.assertEquals(taskInfoCtrl, (LeadConvertTaskInfoComponentController)gotTaskInfo,
  250. 'getmyTaskComponentController() should return the task-info controller instance');
  251. System.assertEquals(taskDescCtrl, (LeadConvertTaskDescComponentController)gotTaskDesc,
  252. 'getmyDescriptionComponentController() should return the desc controller instance');
  253.  
  254. // — Now exercise saveLead() for coverage —
  255.  
  256. // Capture original values
  257. String origStatus = controller.leadToConvert.Status;
  258. String origStage = controller.leadToConvert.Lifecycle_Stage__c;
  259.  
  260. // Overwrite in-memory to the static QSO values
  261. controller.leadToConvert.Status = LeadConvertController.QSO_LEAD_STATUS;
  262. controller.leadToConvert.Lifecycle_Stage__c = LeadConvertController.QSO_LEAD_STAGE;
  263.  
  264. Test.startTest();
  265. controller.saveLead();
  266. Test.stopTest();
  267.  
  268. // Verify that the DB record rolled back to original
  269. Lead fresh = [SELECT Status, Lifecycle_Stage__c
  270. FROM Lead
  271. WHERE Id = :dummyLead.Id];
  272. System.assertEquals(origStatus, fresh.Status,
  273. 'saveLead() should revert the DB Status to its original value');
  274. System.assertEquals(origStage, fresh.Lifecycle_Stage__c,
  275. 'saveLead() should revert the DB Stage to its original value');
  276.  
  277. // And the controller instance still holds the “new” values
  278. System.assertEquals(LeadConvertController.QSO_LEAD_STATUS,
  279. controller.leadToConvert.Status,
  280. 'Controller instance should retain the new Status');
  281. System.assertEquals(LeadConvertController.QSO_LEAD_STAGE,
  282. controller.leadToConvert.Lifecycle_Stage__c,
  283. 'Controller instance should retain the new Stage');
  284. }
  285.  
  286.  
  287. @IsTest
  288. /**
  289. * Helper to create & insert a Lead under a fresh Account,
  290. * then re-query it with its picklist and OwnerId populated.
  291. */
  292. private static Lead makeLead() {
  293. Account acct = new Account(Name='Test Account');
  294. insert acct;
  295. // grab first picklist values
  296. String st = Lead.Status.getDescribe().getPicklistValues()[0].getValue();
  297. String ls = Lead.Lifecycle_Stage__c.getDescribe().getPicklistValues()[0].getValue();
  298. Lead L = new Lead(
  299. LastName = 'AutoTest',
  300. Company = acct.Name,
  301. Status = st,
  302. Lifecycle_Stage__c = ls
  303. );
  304. insert L;
  305. // re-query to get OwnerId, etc.
  306. return [SELECT Id, Company, Status, Lifecycle_Stage__c, OwnerId
  307. FROM Lead WHERE Id = :L.Id];
  308. }
  309.  
  310. static void testConvertLead_missingStatusShowsError() {
  311. // Arrange
  312. Lead L = makeLead(); // assumes you have a helper that creates & inserts a Lead
  313. LeadConvertController ctl = new LeadConvertController(
  314. new ApexPages.StandardController(L)
  315. );
  316.  
  317. // Wire in a core controller that has no status selected
  318. LeadConvertCoreComponentController core = new LeadConvertCoreComponentController();
  319. core.leadConvert = ctl.leadToConvert;
  320. core.leadConvert.Status = 'NONE';
  321. ctl.setComponentController(core);
  322.  
  323. // Act
  324. Test.startTest();
  325. PageReference pr = ctl.convertLead();
  326. Test.stopTest();
  327.  
  328. // Assert: we should short-circuit and throw a message
  329. System.assertEquals(null, pr);
  330. Boolean found = false;
  331. for (ApexPages.Message m : ApexPages.getMessages()) {
  332. if (m.getDetail().contains('Please select a Lead Status')) {
  333. found = true;
  334. break;
  335. }
  336. }
  337. System.assert(found, 'Expected “Please select a Lead Status.” error');
  338. }
  339.  
  340. @IsTest
  341. static void testConvertLead_missingStageShowsError() {
  342. // Arrange
  343. Lead L = makeLead();
  344. LeadConvertController ctl = new LeadConvertController(
  345. new ApexPages.StandardController(L)
  346. );
  347.  
  348. // Wire in a core controller that has no stage selected
  349. LeadConvertCoreComponentController core = new LeadConvertCoreComponentController();
  350. core.leadConvert = ctl.leadToConvert;
  351. core.leadConvert.Lifecycle_Stage__c = 'NONE';
  352. core.selectedAccount = 'NEW'; // skip account error
  353. ctl.setComponentController(core);
  354.  
  355. // Act
  356. Test.startTest();
  357. PageReference pr = ctl.convertLead();
  358. Test.stopTest();
  359.  
  360. // Assert
  361. System.assertEquals(null, pr);
  362. Boolean found = false;
  363. for (ApexPages.Message m : ApexPages.getMessages()) {
  364. if (m.getDetail().contains('Please select a Lifecycle Stage')) {
  365. found = true;
  366. break;
  367. }
  368. }
  369. System.assert(found, 'Expected “Please select a Lifecycle Stage.” error');
  370. }
  371.  
  372. @IsTest
  373. static void testPrintErrorAddsPageMessage() {
  374. // Arrange
  375. Lead L = makeLead();
  376. LeadConvertController ctl = new LeadConvertController(
  377. new ApexPages.StandardController(L)
  378. );
  379. // clear any prior messages
  380. // (ApexPages.getMessages() appends; we’ll just look for our new text)
  381.  
  382. // Act
  383. ctl.PrintError('UnitTestError123');
  384.  
  385. // Assert
  386. Boolean found = false;
  387. for (ApexPages.Message m : ApexPages.getMessages()) {
  388. if (m.getDetail() == 'UnitTestError123') {
  389. found = true;
  390. break;
  391. }
  392. }
  393. System.assert(found, 'PrintError should add a page message with our text');
  394. }
  395. }
Success #stdin #stdout #stderr 0.02s 7776KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
./prog:1: expected expression
./prog:42: Invalid character 0xe2
./prog:42: Invalid character 0x80
./prog:42: Invalid character 0x90
./prog:79: expected expression
./prog:195: Invalid character 0xe2
./prog:195: Invalid character 0x94
./prog:195: Invalid character 0x80
./prog:195: Invalid character 0xe2
./prog:195: Invalid character 0x94
./prog:195: Invalid character 0x80
./prog:195: Invalid character 0xe2
./prog:195: Invalid character 0x94
./prog:195: Invalid character 0x80
./prog:195: Invalid character 0xe2
./prog:195: Invalid character 0x94
./prog:195: Invalid character 0x80
./prog:210: Invalid character 0xe2
./prog:210: Invalid character 0x80
./prog:210: Invalid character 0x94
./prog:210: Invalid character 0xe2
./prog:210: Invalid character 0x80
./prog:210: Invalid character 0x94
./prog:224: Invalid character 0xe2
./prog:224: Invalid character 0x80
./prog:224: Invalid character 0x94
./prog:224: Invalid character 0xe2
./prog:224: Invalid character 0x80
./prog:224: Invalid character 0x94
./prog:228: Invalid character 0xe2
./prog:228: Invalid character 0x80
./prog:228: Invalid character 0x94
./prog:228: Invalid character 0xe2
./prog:228: Invalid character 0x80
./prog:228: Invalid character 0x94
./prog:232: Invalid character 0xe2
./prog:232: Invalid character 0x80
./prog:232: Invalid character 0x94
./prog:232: Invalid character 0xe2
./prog:232: Invalid character 0x80
./prog:232: Invalid character 0x94
./prog:237: Invalid character 0xe2
./prog:237: Invalid character 0x80
./prog:237: Invalid character 0x94
./prog:237: Invalid character 0xe2
./prog:237: Invalid character 0x80
./prog:237: Invalid character 0x94
./prog:242: Invalid character 0xe2
./prog:242: Invalid character 0x80
./prog:242: Invalid character 0x94
./prog:242: Invalid character 0xe2
./prog:242: Invalid character 0x80
./prog:242: Invalid character 0x94
./prog:254: Invalid character 0xe2
./prog:254: Invalid character 0x80
./prog:254: Invalid character 0x94
./prog:254: Invalid character 0xe2
./prog:254: Invalid character 0x80
./prog:254: Invalid character 0x94
./prog:277: Invalid character 0xe2
./prog:277: Invalid character 0x80
./prog:277: Invalid character 0x9c
./prog:277: Invalid character 0xe2
./prog:277: Invalid character 0x80
./prog:277: Invalid character 0x9d
./prog:380: Invalid character 0xe2
./prog:380: Invalid character 0x80
./prog:380: Invalid character 0x99