黄千龙
2022-04-08 01f207d979d6be17c8cdec293feab48828c0ec3e
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class StartTradingControllerTest {
    
    static final String NONE = StartTradingController.NONE;
    static final String RC_HOSPITAL = '病院';
    static final String RC_OPP = '引合';
    static final String RC_SENRYAKUKASHITSUBUNRUI = '戦略科室分類 消化科';
    static final String RC_BYOUIN = '病院';
    static final String RC_SHINRYOUKA = '診療科 消化科';
    @TestSetup
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration('Contact');
    }
    /** 初期処理 */
    static testMethod void testInit() {
        StartTradingController st = new StartTradingController(null);
        
        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        // 戦略課室を取得
        List<Account> dcList = [select Id from Account where ParentId = :hospital.Id and RecordType.Name = :RC_SENRYAKUKASHITSUBUNRUI];
        Lead l = new Lead();
        l.Hospital_Name__c = hospital.Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.LeadSource = 'その他';
        l.Other_Society__c = 'その他学会テキスト';
        l.Company = '会社名';
        insert l;
        
        // リードID
        st.leadId = l.Id;
        
        RecordType dept_rect = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name =:RC_SHINRYOUKA];
        // 診療科選択リストテストデータ
        Account[] acts = new Account[]{
            new Account(Name='*', Department_Name__c = '診療科01', Hospital__c = l.Hospital_Name__c, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id),
            new Account(Name='*', Department_Name__c = '診療科02', Hospital__c = l.Hospital_Name__c, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id),
            new Account(Name='*', Department_Name__c = '診療科03', Hospital__c = l.Hospital_Name__c, Department_Class__c = dcList[0].Id, ParentId = dcList[0].Id, RecordTypeId = dept_rect.Id)
        };
        insert acts;
        
        // 期待値:診療科選択リスト
        List<SelectOption> expectDepList = new List<SelectOption>();
        expectDepList.add(new SelectOption(NONE, NONE));
        expectDepList.add(new SelectOption(acts[0].Id, '中国病院 消化科 診療科01'));
        expectDepList.add(new SelectOption(acts[1].Id, '中国病院 消化科 診療科02'));
        expectDepList.add(new SelectOption(acts[2].Id, '中国病院 消化科 診療科03'));
        
        // 担当者選択リストテストデータ
        Contact[] cts = new Contact[]{
            new Contact(LastName = '担当者1',FirstName= 'ZZ1', AccountId = acts[0].Id),
            new Contact(LastName = '担当者1',FirstName= 'ZZ2', AccountId = acts[1].Id),
            new Contact(LastName = '担当者1',FirstName= 'ZZ3',AccountId = acts[2].Id),
            new Contact(LastName = '担当者2',FirstName= 'ZZ4', AccountId = acts[2].Id),
            new Contact(LastName = '担当者3',FirstName= 'ZZ5', AccountId = acts[2].Id)
        };
        Database.SaveResult[] ctsRet = Database.insert(cts, false);
        
        // 期待値:担当者選択リストMap
        Map<String,List<SelectOption>> expectConMap = new Map<String,List<SelectOption>>();
        // なしの担当者選択リスト
        List<SelectOption> expectConList0 = new List<SelectOption>();
        expectConList0.add(new SelectOption(NONE, NONE));
        expectConMap.put(NONE, expectConList0);
        // 診療科01の担当者選択リスト
        List<SelectOption> expectConList1 = new List<SelectOption>();
        expectConList1.add(new SelectOption(NONE, NONE));
        expectConList1.add(new SelectOption(ctsRet[0].getId(), '担当者1 ZZ1'));
        expectConMap.put(acts[0].Id, expectConList1);
        // 診療科02の担当者選択リスト
        List<SelectOption> expectConList2 = new List<SelectOption>();
        expectConList2.add(new SelectOption(NONE, NONE));
        expectConList2.add(new SelectOption(ctsRet[1].getId(), '担当者1 ZZ2'));
        expectConMap.put(acts[1].Id, expectConList2);
        // 診療科03の担当者選択リスト
        List<SelectOption> expectConList3 = new List<SelectOption>();
        expectConList3.add(new SelectOption(NONE, NONE));
        expectConList3.add(new SelectOption(ctsRet[4].getId(), '担当者3 ZZ5'));
        expectConList3.add(new SelectOption(ctsRet[2].getId(), '担当者1 ZZ3'));
        expectConList3.add(new SelectOption(ctsRet[3].getId(), '担当者2 ZZ4'));
        
        expectConMap.put(acts[2].Id, expectConList3);
        
        // 初期処理テスト
        st.init();
        
        // 診療科選択リストチェック 
        system.assertEquals(expectDepList, st.depList);
        
        // 担当者選択リストMapチェック
        //system.assertEquals(expectConMap, st.conMap);
    }
    
    /** 診療科選択リスト変更イベント */
    static testMethod void testChange() {
        StartTradingController st = new StartTradingController(null);
        
        // Map設定
        st.conMap = new Map<String,List<SelectOption>>();
        
        // 診療科未設定
        List<SelectOption> sltOptNasi = new List<SelectOption>();
        sltOptNasi.add(new SelectOption(NONE, NONE));
        st.conMap.put(NONE, sltOptNasi);
        
        // 診療科TEST1
        List<SelectOption> sltOpt1 = new List<SelectOption>();
        sltOpt1.add(new SelectOption(NONE, NONE));
        sltOpt1.add(new SelectOption('TANTO1', '担当者1'));
        sltOpt1.add(new SelectOption('TANTO2', '担当者2'));
        st.conMap.put('TEST1', sltOpt1);
        
        // 診療科TEST2
        List<SelectOption> sltOpt2 = new List<SelectOption>();
        sltOpt2.add(new SelectOption(NONE, NONE));
        sltOpt2.add(new SelectOption('TANTO2', '担当者2'));
        sltOpt2.add(new SelectOption('TANTO3', '担当者3'));
        sltOpt2.add(new SelectOption('TANTO4', '担当者4'));
        st.conMap.put('TEST2', sltOpt2);
        
        // 診療科未設定テスト
        st.sltDep = NONE;
        st.depChange();
        system.assertEquals(sltOptNasi, st.conList);
        
        // 診療科TEST1選択テスト
        st.sltDep = 'TEST1';
        st.depChange();
        system.assertEquals(sltOpt1, st.conList);
        
        // 診療科TEST2選択テスト
        st.sltDep = 'TEST2';
        st.depChange();
        system.assertEquals(sltOpt2, st.conList);
    }
    
    /** キャンセル */
    static testMethod void testCancel() {
        StartTradingController st = new StartTradingController(null);
        // リードID
        st.leadId = 'test_leadid';
        // 期待値
        String expectUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/test_leadid';
        // キャンセルテスト
        PageReference pr = st.cancel();
        system.assertEquals(expectUrl, pr.getUrl());
        
    }
    
    /** 戦略科室コード取得 */
    static testMethod void testGetDepartment() {
        StartTradingController st = new StartTradingController(null);
 
        // 戦略科室・診療科選択テストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        Account a = new Account(Name = '診療科01');
        a.ParentId = deptA.Id;
        a.Hospital__c = hospital.Id;
        a.Department_Class__c = deptA.Id;
        a.RecordTypeId = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name =:RC_SHINRYOUKA].Id;
        insert a;
 
        // 診療科選択値
        st.sltDep = a.Id;
        
        // 戦略科室コード取得テスト
        Account dept = st.getDepartment();
        // 戻り値チェック
        system.assertEquals(deptA.Id, dept.Department_Class__c);
    }
    
    /** 取引の開始(診療科未選択) */
    static testMethod void testStart01() {
        StartTradingController st = new StartTradingController(null);
        // 診療科未選択
        st.sltDep = NONE;
        // 取引の開始
        PageReference pr = st.start();
        // 戻り値NULL
        system.assertEquals(pr, null);
    }
    
    /** 取引の開始(担当者未選択→担当者登録をチェック) */
    static testMethod void testStart02() {
        StartTradingController st = new StartTradingController(null);
 
        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        Lead l = new Lead();
        l.Hospital_Name__c = hospital.Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.LeadSource = 'その他';
        l.Other_Society__c = 'その他学会テキスト';
        l.Company = '会社名';
        insert l;
 
        // トレーニング
        //2021-07-06  mzy  SWAG-C4H99E 询价中的来源更改 start
        RecordType rectCam =
            [select Id from RecordType
             where IsActive = true and SobjectType = 'Campaign'
                              and DeveloperName = 'ServiceEngineerTraining'];
        Campaign c = new Campaign(Name = 'トレーニング1');
        c.Name2__c = '1234';
        c.RecordTypeId = rectCam.Id;
        c.StartDate = Date.today().addDays(-15);
        c.EndDate = Date.today().addDays(18);
        c.Mailflg_after45__c = true;
        c.Mailflg_cancel__c = true;
        c.Mailflg_before15__c = true;
        c.Mailflg_before7__c = true;
        c.Mailflg_after3__c = true;
        c.BusinessPurpose__c = '10000000000<img';
        c.BusinessResult__c = '10000000000<img';
        c.Society_Hold_Place__c = '10000000000<img';
        insert c;
        CampaignMember[] children = new CampaignMember[] {
            new CampaignMember(LeadId = l.Id, CampaignId = c.Id)
        };
        insert children;
        //2021-07-06  mzy  SWAG-C4H99E 询价中的来源更改 end
 
        // 戦略科室・診療科選択テストデータ
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        RecordType recSinryouka = [select id from RecordType where IsActive = true and DeveloperName=:'Department_GI' limit 1];
        Account a = new Account();
        a.Name = '診療科01';
        a.Department_Name__c = '診療科01';
        a.RecordTypeId = recSinryouka.id;
        a.Hospital__c = hospital.Id;
        a.Department_Class__c = deptA.Id;
        a.ParentId = deptA.Id;
        insert a;
 
        // リード
        st.leadId = l.Id;
        st.lead = l;
        // 診療科選択あり
        st.sltDep = a.Id;
        // 担当者未選択
        st.sltCon = NONE;
        
        System.Test.startTest();
 
        // 取引の開始
        PageReference pr = st.start();
        
        // 担当者が登録されていることをチェック
        List<Contact> contList = [select LastName, FirstName, Strategic_dept_Class__c from Contact where AccountId =: a.Id order by CreatedDate desc];
        Contact cont = contList[0];
        system.assertEquals(cont.LastName, l.LastName);
        system.assertEquals(cont.FirstName, l.FirstName);
        system.assertEquals(cont.Strategic_dept_Class__c, deptA.Id);
 
        System.Test.stopTest();
    }
    
    /** 取引の開始(診療科・担当者選択あり→URLをチェック) */
    static testMethod void testStart03() {
        StartTradingController st = new StartTradingController(null);
 
        // リードテストデータ
        RecordType rect = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name =: RC_HOSPITAL limit 1];
        Account hospital = new Account(name = '中国病院');
        hospital.RecordTypeId = rect.Id;
        insert hospital;
        Lead l = new Lead();
        l.Hospital_Name__c = hospital.Id;
        l.LastName = '毛';
        l.FirstName = '沢東';
        l.Company = '会社名';
        insert l;
 
        // 戦略科室・診療科選択テストデータ
        RecordType recSenryakukashitsu = [select id from RecordType where IsActive = true and SobjectType=: 'Account' and Name=:RC_SENRYAKUKASHITSUBUNRUI limit 1];
        Account deptA = new Account(Name = '戦略科室テスト');
        deptA.RecordTypeId = recSenryakukashitsu.id;
        deptA.Department_Class_Label__c = '消化科';
        deptA.parentId = hospital.Id;
        insert deptA;
        RecordType recSinryouka = [select id from RecordType where IsActive = true and DeveloperName=:'Department_GI' limit 1];
        Account a = new Account();
        a.Name = '診療科01';
        a.ParentId = deptA.Id;
        a.Department_Name__c = '診療科01';
        a.RecordTypeId = recSinryouka.id;
        a.Hospital__c = l.Hospital_Name__c;
        a.Department_Class__c = deptA.Id;
        insert a;
 
        // 担当者選択テストデータ
        Contact c = new Contact(LastName = '担当者1',FirstName='ZZ6', AccountId = a.Id);
        insert c;
            
        // リードID
        st.leadId = l.Id;
        st.lead = l;
        // 診療科選択あり
        st.sltDep = a.Id;
        // 担当者未選択
        st.sltCon = c.Id;
        
        System.Test.startTest();
 
        // 取引の開始
        PageReference pr = st.start();
        
        // 期待値
        /*
        String uri = 'retURL=%2F' + l.Id;
        uri += '&ent=Opportunity';
        rect = [select id from RecordType where IsActive = true and SobjectType =: 'Opportunity' and Name =: RC_OPP limit 1];
        uri += '&RecordType=' + rect.Id;
        uri += '&' + system.label.StartTrading_P_Hospital + '_lkid=' + hospital.Id;
        uri += '&' + system.label.StartTrading_P_Hospital + '=' + hospital.name;
        uri += '&' + system.label.StartTrading_P_Dept + '_lkid=' + deptA.Id;
        uri += '&' + system.label.StartTrading_P_Dept + '=' + deptA.name;
        uri += '&' + system.label.StartTrading_P_Name + '=' + a.Id;
        */
        Opportunity opp = [select Id from Opportunity where AccountId = :a.Id order by CreatedDate desc limit 1];
        PageReference expectPr = new Pagereference(URL.getSalesforceBaseUrl().toExternalForm() + '/' + opp.Id + '/e?ent=Opportunity&retURL=%2F' + opp.Id);
        system.assertEquals(expectPr.getUrl(), pr.getUrl());
 
        System.Test.stopTest();
    }    
}