liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
@isTest
private class TrainingTestResultTriggerTest {
 
    static testMethod void testExecute() {
        User loginUser = [select Id from User where Id = :UserInfo.getUserId()];
        List<RecordType> rt2 = [select Id from RecordType where SobjectType = 'Training_test_Result__c' and DeveloperName in ('BTC', 'ETC') order by DeveloperName];
        Id rtId = '01210000000Qtky';
        User user = new User();
        user.LastName = 'LastName';
        user.FirstName = 'FirstName';
        user.Alias = 'Alias';
        user.Email = 'olympustest03@sunbridge.com';
        user.Username = 'olympustest03@sunbridge.com';
        user.CommunityNickname = 'CommunityNickname';
        user.IsActive = true;               // trueの場合ライセンス数にカウントされない
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Post__c = '经理';
        user.MobilePhone = '54321';
        user.Mobile_Phone__c = '12345';
        user.Employee_No__c = '112233';
        user.Work_Location__c = 'Location';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        insert user;
        system.runAs(loginUser) {
            user.IsActive = false;
            update user;
        }
        Campaign c = new Campaign(Name = 'test', RecordTypeId = '01210000000R91f');       // Internal_training
        insert c;
        Contact e1 = new Contact(LastName = 'loginUser', User__c = UserInfo.getUserId());
        Contact e2 = new Contact(LastName = 'loginUser', User__c = user.Id);
        insert new List<Contact>{e1, e2};
        
        List<Contact> eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        System.assertEquals(null, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(null, eList[0].Number_of_participant_for_ETC__c);
        
        Training_test_Result__c ttr1 = new Training_test_Result__c(RecordTypeId = rt2[0].Id, employee__c = e1.Id, campaign__c = c.Id);
        insert ttr1;
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        system.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        system.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
 
        // 2件同時 insert
        delete ttr1;
        ttr1 = new Training_test_Result__c(RecordTypeId = rt2[0].Id, employee__c = e1.Id, campaign__c = c.Id);
        Training_test_Result__c ttr2 = new Training_test_Result__c(RecordTypeId = rt2[1].Id, employee__c = e1.Id, campaign__c = c.Id);
        insert new List<Training_test_Result__c>{ttr1, ttr2};
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(1, eList[0].Number_of_participant_for_ETC__c);
        
        // User inactive insert
        Training_test_Result__c ttr3 = new Training_test_Result__c(RecordTypeId = rt2[1].Id, employee__c = e2.Id, campaign__c = c.Id);
        insert ttr3;
        Training_test_Result__c ttr = [select Id,OwnerId from Training_test_Result__c where Id = :ttr3.Id];
        System.assertEquals('00510000000fSYI', ttr.OwnerId); // batch用户ID
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e2.Id];
        System.assertEquals(0, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(1, eList[0].Number_of_participant_for_ETC__c);
        
        delete ttr3;
 
        // レコードタイプ変更
        ttr2.RecordTypeId = rt2[0].Id;
        update ttr2;
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        System.assertEquals(2, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
 
        // e2に変更
        ttr2.employee__c = e2.id;
        update ttr2;
        ttr = [select Id,OwnerId from Training_test_Result__c where Id = :ttr2.Id];
        System.assertEquals('00510000000fSYI', ttr.OwnerId); // batch用户ID
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e2.Id];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
        
        ttr2.employee__c = e1.id;
        update ttr2;
 
        // TODO 本身也是loginUser再跑,为什么用runAs跑就不会报错 MIXED_DML_OPERATION
        system.runAs(loginUser) {
            user.IsActive = true;
            update user;
        }
        // User Active insert
        ttr3 = new Training_test_Result__c(RecordTypeId = rt2[1].Id, employee__c = e2.Id, campaign__c = c.Id);
        insert ttr3;
        ttr3 = [select Id, OwnerId from Training_test_Result__c where Id = :ttr3.Id];
        System.assertEquals(user.Id, ttr3.OwnerId);
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e2.Id];
        System.assertEquals(0, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(1, eList[0].Number_of_participant_for_ETC__c);
 
        // update to e2
        ttr2.employee__c = e2.id;
        update ttr2;
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e1.Id];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id = :e2.Id];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(1, eList[0].Number_of_participant_for_ETC__c);
        ttr2 = [select Id, OwnerId from Training_test_Result__c where Id = :ttr2.Id];
        System.assertEquals(user.Id, ttr2.OwnerId);
 
        // 削除
        delete ttr2;
        eList = [select Id, Number_of_participant_for_BTC__c, Number_of_participant_for_ETC__c from Contact where Id IN: new List<Id>{e1.Id, e2.id}];
        System.assertEquals(1, eList[0].Number_of_participant_for_BTC__c);
        System.assertEquals(0, eList[0].Number_of_participant_for_ETC__c);
    }
}