高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
@isTest
private class ReceivingNoteSummarySelectControllerTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @param typeStringListString 格式:'备品类型,管理种类;备品类型,管理种类...'
    @description 造出指定类型的清单数据,并插入数据库
    */
    static void setupTestData(String typeStringListString){
        factory = new ReceivingNoteTestDataFactory();
        factory.setupMasterData();
        if(String.isBlank(typeStringListString)){
            typeStringListString = '固定资产,个体管理;附属品,个体管理;附属品,数量管理;耗材,数量管理;低值易耗品,数量管理';
        }
        List<String> typeStringList = typeStringListString.split(';');
        factory.setupReceivingNoteData(typeStringList);
    }
    /**
    @description 测试searchOpp()方法
    */
    static testMethod void unitTestSearchOpp(){
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Fixture_Set__c = null;
        upsert rns Internal_Asset_number__c;
 
        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
 
        Test.setCurrentPage(ref);
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
 
        Test.startTest();
        controller.init();
        // 初始时一览中的配套字段没有被赋值
        rns = [select Fixture_Set__c
                                        from ReceivingNoteSummary__c
                                        WHERE Id=:rnsId];
        System.assertEquals(null, rns.Fixture_Set__c);
 
        // 查找配套
        controller.keyword = 'SB-FS-01';
        controller.searchOpp(); // 查找方法中包括保存配套字段的操作
        System.assertEquals(1, controller.viewList.size());
 
        Test.stopTest();
    }
 
    /**
    @description 给一览对象的配套字段赋值
    */
    static testMethod void unitTestSave(){
 
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Fixture_Set__c = null;
        upsert rns Internal_Asset_number__c;
 
        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
 
        Test.setCurrentPage(ref);
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
 
        Test.startTest();
        controller.init();
 
        // 初始时一览中的配套字段没有被赋值
        rns = [SELECT Fixture_Set__c
                FROM ReceivingNoteSummary__c
                WHERE Id=:rnsId];
        System.assertEquals(null, rns.Fixture_Set__c);
 
        // 不加检索关键词会显示所有可选配套
        controller.keyword = '';
        controller.searchOpp();
        System.assertEquals(1, controller.viewList.size());
 
        // 勾选配套
        controller.viewList[0].check = true;
 
        // 保存
        controller.save();
 
        // 查看是否被赋上值
        ReceivingNoteSummary__c newRns = [select Fixture_Set__r.Fixture_Set_Body_Model_No__c
                                        from ReceivingNoteSummary__c
                                        WHERE Id=:rnsId];
        System.assertEquals('SB-FS-MODEL-01', newRns.Fixture_Set__r.Fixture_Set_Body_Model_No__c);
        Test.stopTest();
    }
 
    /**
    @description 发送邮件
    */
    static testMethod void unitTestSendEmail() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
 
        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        ReceivingNoteSummary__c rns = [select Name, No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals(null, rns.No_Matching_Reminder_Email_Text__c);
 
        Test.startTest();
        // 初始化
        controller.init();
 
        String emailMsg = rns.Name;
        String emailBody = 'ARE YOU OK?';
        ref.getParameters().put('emailBody', emailBody);
        controller.sendEmail();
 
        ReceivingNoteSummary__c newRns = [select No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals('【自由填写内容】\nARE YOU OK?\n备品配套:\nSB-FS-01', newRns.No_Matching_Reminder_Email_Text__c);
 
        Test.stopTest();
    }
    static testMethod void unitTestCleanFixtureSet(){
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
        ReceivingNoteDetail__c newRnd = new ReceivingNoteDetail__c();
        newRnd.Fixture_Set_Detail__c = factory.fsdList[1].Id;
        newRnd.ReceivingNoteSummary__c = rnsId;
        insert newRnd;
 
        List <ReceivingNoteDetail__c> rndList = [
                SELECT Id, Fixture_Set_Detail__c
                FROM ReceivingNoteDetail__c
                WHERE ReceivingNoteSummary__c=:rnsId];
        System.assertEquals(2, rndList.size());
        System.assertNotEquals(null, rndList[0].Fixture_Set_Detail__c);
 
        List<ReceivingNoteSummary__c> rnsList = [SELECT Id, Fixture_Set__c FROM ReceivingNoteSummary__c WHERE Id=:rnsId];
        System.assertNotEquals(null, rnsList[0].Fixture_Set__c);
 
        PageReference ref = new PageReference('ReceivingNoteSummarySelect?pt_recid='+rnId+'&raesid='+rnsId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteSummarySelectController controller = new ReceivingNoteSummarySelectController();
        ReceivingNoteSummary__c rns = [select Name, No_Matching_Reminder_Email_Text__c from ReceivingNoteSummary__c WHERE Id=:rnsId ];
        System.assertEquals(null, rns.No_Matching_Reminder_Email_Text__c);
 
        Test.startTest();
        // 初始化
        controller.init();
        controller.cleanFixtureSet();
        Test.stopTest();
        rndList = [SELECT Id, Fixture_Set_Detail__c FROM ReceivingNoteDetail__c];
        System.assertEquals(1, rndList.size());
        System.assertEquals(null, rndList[0].Fixture_Set_Detail__c);
        rnsList = [SELECT Id, Fixture_Set__c FROM ReceivingNoteSummary__c];
        System.assertEquals(null, rnsList[0].Fixture_Set__c);
    }
}