高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@isTest
private class CampaignMemberTriggerTest {
 
    public static Campaign createCampaignData( String inputName) {
        Campaign ret = new Campaign();
        ret.Name = inputName;
        ret.HostName__c = '会议主办方';
        ret.cooperatorCompany__c = '会议承办方';
        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());
    }
   
}