tylerzika
7/24/2017 - 10:11 PM

buttonTest.java

@testSetup 
static void setup() {

// used to determine if part of the lead trigger should run.
// default to true
	Run_Lead_Trigger__c setting = new Run_Lead_Trigger__c();   

insert setting;

// insert two users. one that will be the old owner of the contact, the other the new
User u1 = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
     LastName = 'Owner1',
     Email = 'test@1.com',
     Username = 'test@1.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);

User u2 = new User(
     ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
     LastName = 'Owner2',
     Email = 'test@2.com',
     Username = 'test@2.com' + System.currentTimeMillis(),
     CompanyName = 'TEST',
     Title = 'title',
     Alias = 'alias',
     TimeZoneSidKey = 'America/Los_Angeles',
     EmailEncodingKey = 'UTF-8',
     LanguageLocaleKey = 'en_US',
     LocaleSidKey = 'en_US'
);

List<User> users = new List<User>{u1, u2};
insert users;

// insert 1 account
Account a = new Account();
a.Name = 'Test Account';

RecordType accountRecordType = [SELECT Id 
					FROM RecordType
					WHERE Name ='Corp Both' LIMIT 1];

a.RecordTypeId = accountRecordType.Id;

insert a;
}


static testmethod void conversionButton() {

// make the lead trigger not run on this test, so button code can run.
// if you insert leads in the test code, they will be converted on insert,
// because part of the lead trigger is active.
Run_Lead_Trigger__c setting = [SELECT On__c FROM Run_Lead_Trigger__c];  

setting.On__c = false;

update setting;        

Account a = [SELECT Id FROM Account];

User owner1 = [SELECT Id FROM User WHERE LastName = 'Owner1'];
User owner2 = [SELECT Id FROM User WHERE LastName = 'Owner2'];        

Contact c1 = new Contact(AccountId = a.Id, LastName = 'LastName3', Email = 'test@email3.com', Marketo_Sync__c = true, OwnerId = owner1.Id);

insert c1;

Lead l1 = new Lead(LastName = 'LastName3', Email = 'test@email3.com', Marketo_Sync__c = true, OwnerId = owner2.Id, Status = 'Contacted');

insert l1;

// call code that used in the button.
LeadConversionButton.attemptConversion('Contact', c1.Id);

Contact c2 = new Contact(AccountId = a.Id, LastName = 'LastName4', Email = 'test@email4.com', Marketo_Sync__c = true, OwnerId = owner2.Id);

insert c2;

Lead l2 = new Lead(LastName = 'LastName4', Email = 'test@email4.com', Marketo_Sync__c = true, OwnerId = owner1.Id, Status = 'New – Not Contacted');

insert l2; 

// call code that used in the button.
LeadConversionButton.attemptConversion('Lead', l2.Id);

System.assertEquals(2, [SELECT count() FROM Lead WHERE IsConverted = true]);

// after conversion, the contact owner should always be the owner from  the lead that was converted.
System.assertEquals(owner2.Id, [SELECT OwnerId FROM Contact WHERE LastName = 'LastName3'].OwnerId);        
System.assertEquals(owner1.Id, [SELECT OwnerId FROM Contact WHERE LastName = 'LastName4'].OwnerId); 

System.assertEquals('Contacted', [SELECT Status__c FROM Contact WHERE LastName = 'LastName3'].Status__c);
System.assertEquals('New – Not Contacted', [SELECT Status__c FROM Contact WHERE LastName = 'LastName4'].Status__c);
}