@isTest
|
private class CampaignMemberTriggerTest {
|
|
public static Campaign createCampaignData( String inputName) {
|
Campaign ret = new Campaign();
|
ret.Name = inputName;
|
return ret;
|
}
|
|
public static Account createAccountData( String inputName) {
|
Account ret = new Account();
|
ret.Name = inputName;
|
return ret;
|
}
|
|
public static Contact createContactData( String inputName, String inputEmail) {
|
Contact ret = new Contact();
|
ret.LastName = inputName;
|
ret.Email = inputEmail;
|
return ret;
|
}
|
|
public static Lead createLeadData( String inputName, String inputEmail, String inputCompany) {
|
Lead ret = new Lead();
|
ret.LastName = inputName;
|
ret.Email = inputEmail;
|
ret.company = inputCompany;
|
return ret;
|
}
|
|
static testMethod void test01() {
|
// キャンペーン作成
|
Campaign targetCampaign = createCampaignData('test camapign');
|
insert targetCampaign;
|
|
// リード作成
|
Lead targetLead = createLeadData( 'leadName', 'test@test.com.leadtest', 'tesetCompany');
|
insert targetLead;
|
|
// リードをキャンペーンメンバーに
|
CampaignMember target = new CampaignMember();
|
target.LeadId = targetLead.Id;
|
target.CampaignId = targetCampaign.Id;
|
insert target;
|
|
// 何も起きていないことを確認
|
|
// Contactは増えていない
|
System.assertEquals( 0, [SELECT id FROM Contact].size());
|
// Leadも、テストで作ったやつだけ
|
System.assertEquals( 1, [SELECT id FROM Lead].size());
|
// CampaignMemberも、テストで作ったやつだけ
|
System.assertEquals( 1, [SELECT id FROM CampaignMember].size());
|
}
|
|
static testMethod void test02() {
|
|
// レコードタイプ取得
|
RecordType hospitalRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
|
RecordType sectionRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
|
RecordType departmentRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
|
|
// 病院作成
|
Account hospital = new Account();
|
hospital.RecordTypeId = hospitalRec.Id;
|
hospital.Name = 'TestHospital';
|
insert hospital;
|
|
// 病院を作ると戦略科室は、トリガーによって作られている
|
Account section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Parent.Id = :hospital.Id and RecordTypeId = :sectionRec.Id limit 1];
|
|
// 診療科1を作成
|
Account depart1 = new Account();
|
depart1.RecordTypeId = departmentRec.Id;
|
depart1.Name = '*';
|
depart1.Department_Name__c = 'TestDepart';
|
depart1.ParentId = section.Id;
|
depart1.Department_Class__c = section.Id;
|
depart1.Hospital__c = hospital.Id;
|
insert depart1;
|
|
// 診療科2を作成
|
Account depart2 = new Account();
|
depart2.RecordTypeId = departmentRec.Id;
|
depart2.Name = '*';
|
depart2.Department_Name__c = 'TestDepart2';
|
depart2.ParentId = section.Id;
|
depart2.Department_Class__c = section.Id;
|
depart2.Hospital__c = hospital.Id;
|
insert depart2;
|
|
// キャンペーン作成
|
Campaign targetCampaign = createCampaignData('test camapign');
|
insert targetCampaign;
|
|
// コンタクト作成
|
Contact targetContact = createContactData( 'test contact', 'daiske@oly.com.tesst');
|
targetContact.accountId = depart1.id;
|
targetContact.FirstName ='ZZ';
|
insert targetContact;
|
|
// 事前の assert
|
// Contactは、テストで作った1レコードのみ
|
System.assertEquals( 1, [SELECT id FROM Contact].size());
|
// Leadは、0レコード
|
System.assertEquals( 0, [SELECT id FROM Lead].size());
|
// CampaignMemberも、テストで作ったやつだけ
|
System.assertEquals( 0, [SELECT id FROM CampaignMember].size());
|
|
// コンタクトをキャンペーンメンバーに
|
CampaignMember targetMember = new CampaignMember();
|
targetMember.ContactId = targetContact.Id;
|
targetMember.CampaignId = targetCampaign.Id;
|
targetMember.Sales_lead__c = '创建购买意向';
|
insert targetMember;
|
|
// コンタクト配下にリードができている
|
// そのリードは、キャンペーンメンバーにもなっている
|
// Contactは、テストで作った1レコードのみ
|
System.assertEquals( 1, [SELECT id FROM Contact].size());
|
// Leadは、1レコード増えている
|
List<Lead> insertedLead = [SELECT id, sendMailToOwner__c FROM Lead];
|
System.assertEquals( 1, insertedLead.size());
|
System.assertEquals( true, insertedLead[0].sendMailToOwner__c);
|
// CampaignMemberも、増えている
|
System.assertEquals( 2, [SELECT id FROM CampaignMember].size());
|
}
|
|
}
|