高章伟
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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
@isTest
private class ReceivingNoteStockInRequestCtrlTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @description 安装master数据
    */
    @testSetup static void setupMasterData() {
        factory = new ReceivingNoteTestDataFactory();
        factory.setupMasterData();
    }
    /**
    @param typeStringListString 格式:'备品类型,管理种类;备品类型,管理种类...'
    @description 造出指定类型的清单数据,并插入数据库
    */
    static void setupTestData(String typeStringListString) {
        factory = new ReceivingNoteTestDataFactory();
        if(String.isBlank(typeStringListString)) {
            typeStringListString = '固定资产,个体管理;附属品,个体管理;附属品,数量管理;耗材,数量管理;低值易耗品,数量管理';
        }
        List<String> typeStringList = typeStringListString.split(';');
        factory.setupReceivingNoteData(typeStringList);
    }
    /**
    *@description 测试检索功能
    */
    static testMethod void unitTestSearch() {
 
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
 
        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
 
        Test.startTest();
        controller.init();
        controller.getStatusOps();
        controller.getcategoryOps();
        // 不加检索条件则得到全部明细
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
 
        // 单条件
        controller.pageB.rn.Status__c = null;
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = null;
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);
 
        // 两条件
        controller.pageB.rn.Status__c = '待数据确认';
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = null;
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);
 
        // 三条件
        controller.pageB.rn.Status__c = '待数据确认';
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = 'SB-MODEL-01';
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);
 
        Test.stopTest();
 
    }
    /**
    @description 测试保存功能
    */
    static testMethod void unitTestSaveRecord() {
        // 状态调整为已验收
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'FALSE';
        rnd.DataConfirmation__c = true;
        upsert rnd Product_Serial_No__c;
        // 收货
        ReceivingNoteSummary__c  rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Received_Confirm_Time__c = System.Now();
        upsert rns Internal_Asset_number__c;
        // 清点
        rnd.AcceptanceTime__c = System.Now();
        rnd.AcceptanceResult__c = 'OK';
        // 验收
        rnd.Inspection_result_after__c = 'OK';
        upsert rnd Product_Serial_No__c;
 
        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
 
        Test.startTest();
        // 初始化
        controller.init();
        controller.search();
        // 改为可编辑
        controller.changeFixModel();
        // 取出第一条数据
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];
        Id rndId = rowB.rnd.Id;
        // 确认修改前各字段值
        System.assertEquals(null, rowB.rnd.Salesdepartment__c);
        System.assertEquals(null, rowB.rnd.SalesProvince__c);
        System.assertEquals(null, rowB.rnd.Product_category__c);
        System.assertEquals(null, rowB.rnd.Equipment_Type__c);
        System.assertEquals('个体管理', rowB.rnd.Manage_type__c);
        System.assertEquals('FALSE', rowB.rnd.unknow_serial_NO_product__c);
        System.assertEquals('FALSE', rowB.rnd.Loaner_accsessary__c);
        System.assertEquals(false, rowB.rnd.DataConfirmationAgain__c);
        // 修改字段值
        rowB.checked = true;
        rowB.rnd.Salesdepartment__c = '0.备品中心';
        rowB.rnd.SalesProvince__c = '大连';
        rowB.rnd.Product_category__c = 'GI';
        rowB.rnd.Equipment_Type__c = '产品试用';
        rowB.rnd.Manage_type__c = '个体管理';
        rowB.rnd.unknow_serial_NO_product__c = 'TRUE';
        rowB.rnd.Loaner_accsessary__c = 'TRUE';
        rowB.rnd.DataConfirmationAgain__c = true;
 
        controller.saveRecord();
        // 确认修改后结果
        ReceivingNoteDetail__c newRnd = [
                SELECT Salesdepartment__c
                     , SalesProvince__c
                     , Product_category__c
                     , Equipment_Type__c
                     , Manage_type__c
                     , unknow_serial_NO_product__c
                     , Loaner_accsessary__c
                     , DataConfirmationAgain__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id = :rndId
                ];
        System.assertEquals('0.备品中心', newRnd.Salesdepartment__c);
        System.assertEquals('大连', newRnd.SalesProvince__c);
        System.assertEquals('GI', newRnd.Product_category__c);
        System.assertEquals('产品试用', newRnd.Equipment_Type__c);
        System.assertEquals('个体管理', newRnd.Manage_type__c);
        System.assertEquals('TRUE', newRnd.unknow_serial_NO_product__c);
        System.assertEquals('TRUE', newRnd.Loaner_accsessary__c);
        System.assertEquals(true, newRnd.DataConfirmationAgain__c);
 
        Test.stopTest();
    }
    /**
    @description 测试驳回功能
    */
    static testMethod void unitTestReject() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rndId = factory.rndMap.get('固定资产,个体管理').Id;
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.Reject_Time__c = null;
        Datetime now = Datetime.now();
        rnd.After_Inspection_time__c = now;
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.Inspection_staff_After__c = Userinfo.getUserId();
        rnd.Inspection_Comment__c = 'INSPECTION_COMMENT';
        rnd.Inspection_result_after__c = 'OK';
        rnd.AcceptanceComments__c = 'ACCEPTANCECOMMENTS';
        rnd.AcceptanceResult__c = 'OK';
        rnd.Reject_Reason__c = '驳回理由';
        rnd.Received_Quantity__c = 1;
        rnd.Inspection_Cnt__c = 1;
        upsert rnd Product_Serial_No__c;
 
        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
 
        Test.startTest();
        controller.init();
        controller.search();
        // 取出第一条数据
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];
 
        // 确认修改前各字段值
        rnd = [SELECT Reject_Time__c
                    , After_Inspection_time__c
                    , Inspection_staff_After__c
                    , Inspection_Comment__c
                    , Inspection_result_after__c
                    , AcceptanceComments__c
                    , AcceptanceResult__c
                    , Received_Quantity__c
                    , Inspection_Cnt__c
                 FROM ReceivingNoteDetail__c
                WHERE Id = :rndId];
 
        System.assertEquals(null, rnd.Reject_Time__c);
        System.assertEquals(now, rnd.After_Inspection_time__c);
        System.assertEquals( Userinfo.getUserId(), rnd.Inspection_staff_After__c);
        System.assertEquals('INSPECTION_COMMENT', rnd.Inspection_Comment__c);
        System.assertEquals('OK', rnd.Inspection_result_after__c);
        System.assertEquals('ACCEPTANCECOMMENTS', rnd.AcceptanceComments__c);
        System.assertEquals('OK', rnd.AcceptanceResult__c);
        System.assertEquals(1, rnd.Received_Quantity__c);
        System.assertEquals(1, rnd.Inspection_Cnt__c);
 
        // 执行驳回
        rowB.checked = true;
        controller.reject();
 
        // 确认修改后结果
        rnd = [
                SELECT Reject_Time__c
                     , After_Inspection_time__c
                     , Inspection_staff_After__c
                     , Inspection_Comment__c
                     , Inspection_result_after__c
                     , AcceptanceComments__c
                     , AcceptanceResult__c
                     , Reject_Reason__c
                     , Received_Quantity__c
                     , Inspection_Cnt__c
 
                  FROM ReceivingNoteDetail__c
                 WHERE Id=:rndId
                ];
 
        // 确认修改后各字段值
        System.assertNotEquals(null, rnd.Reject_Time__c);
        System.assertEquals(null, rnd.After_Inspection_time__c);
        System.assertEquals(null, rnd.Inspection_staff_After__c);
        System.assertEquals(null, rnd.Inspection_Comment__c);
        System.assertEquals(null, rnd.Inspection_result_after__c);
        System.assertEquals(0, rnd.Received_Quantity__c);
        System.assertEquals(0, rnd.Inspection_Cnt__c);
        System.assertEquals('驳回理由', rnd.Reject_Reason__c);
 
        Test.stopTest();
 
    }
    /**
    @description 测试入库指示
    */
    static testMethod void unitTestInStockRequest() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rndId = factory.rndMap.get('固定资产,个体管理').Id;
 
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;
        rnd.AcceptanceTime__c = null;
        rnd.Inspection_result_after__c = 'OK';
        rnd.DataConfirmationAgain__c = true;
        upsert rnd Product_Serial_No__c;
 
        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);
 
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
 
        Test.startTest();
        controller.init();
        controller.search();
 
        // 查看入库指示前
        rnd = [
                SELECT Arrival_wh_Request_time__c
                     , DataConfirmation__c
                     , AcceptanceTime__c
                     , Inspection_result_after__c
                     , DataConfirmationAgain__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id = :rndId
        ];
        System.assertEquals(null, rnd.Arrival_wh_Request_time__c);
        // 取出第一条数据,【再数据确认】字段打勾
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];
        rowB.checked = true;
        // 入库指示
        controller.inStockRequest();
        // 查看入库指示后
        rnd = [
                SELECT Arrival_wh_Request_time__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id=:rndId
            ];
        System.assertNotEquals(null, rnd.Arrival_wh_Request_time__c.format('MM/dd/yyyy'));
 
        Test.stopTest();
    }
    /**
    @description 测试取消明细功能
    */
    //static testMethod void unitTestCancelRecord() {
    //    setupTestData('固定资产,个体管理');
    //    Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
    //    Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
    //    addDetails(rnsId);
 
    //    PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
    //    Test.setCurrentPage(ref);
 
    //    ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
 
    //    Test.startTest();
    //    controller.init();
    //    controller.search();
    //    // 取出不带资产的一条明细
    //    List<Id> rndIdList = new List<Id>();
    //    for(ReceivingNoteStockInRequestController.RowBean rowB : controller.pageB.rowBList) {
    //        if(rowB.rnd.RNDAssert__c==null) {
    //            rndIdList.add(rowB.rnd.Id);
    //            rowB.checked = true;
    //            break;
    //        }
    //    }
    //    // 取消前明细的Delete_Flag__c为false
    //    List<ReceivingNoteDetail__c> rndList = [
    //            SELECT Delete_Flag__c
    //              FROM ReceivingNoteDetail__c
    //             WHERE Id in: rndIdList
    //        ];
    //    for(ReceivingNoteDetail__c rnd : rndList) {
    //        System.assertEquals(false, rnd.Delete_Flag__c);
    //    }
    //    // 取消
    //    controller.cancelRecord();
    //    // 取消后该条明细的Delete_Flag__c为true
    //    rndList = [
    //            SELECT Id, Delete_Flag__c
    //              FROM ReceivingNoteDetail__c
    //             WHERE Id in:rndIdList
    //            ];
    //    for(ReceivingNoteDetail__c rnd : rndList) {
    //        System.assertEquals(true, rnd.Delete_Flag__c);
    //    }
    //    Test.stopTest();
 
    //}
    /**
    @param rnsId 一览Id
    @description 给一个一览分配明细
    */
    private static void addDetails(Id rnsId) {
 
        PageReference ref = new PageReference('/apex/ReceivingNoteDetailSelect?sids=' + rnsId);
        Test.setCurrentPage(ref);
        ReceivingNoteDetailSelectController controller = new ReceivingNoteDetailSelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Id,Fixture_Set_Detail__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c =: rnsId
            ];
        // 本来有一条一览明细
        System.assertEquals(1,rndList.size());
 
        controller.init();
        controller.searchOpp();
        // 本来有两条配套明细
        System.assertEquals(2, controller.viewList.size());
        ReceivingNoteDetail__c rnd = (ReceivingNoteDetail__c) controller.viewList[1].sobj;
 
        // 第二条配套明细数量调为3
        rnd.Rental_Num__c = 3;
        controller.viewList[0].check = true;
        controller.viewList[1].check = true;
        controller.save();
 
        rndList = [
                SELECT Id,Fixture_Set_Detail__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c =: rnsId
            ];
        // 原有的1条加上后加的3条,一共有4条一览明细
        System.assertEquals(4,rndList.size());
    }
 
    /**
    @description 显示错误信息
    */
    static testMethod void unitTestShowApplyMsg() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
 
        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
        Test.setCurrentPage(ref);
        Test.startTest();
 
        // 设定错误信息
        ref.getParameters().put('applyMsg', 'TEST MESSAGE');
        ref.getParameters().put('applyMsgType', 'Fail');
 
        // 执行之前,页面上没有报错误信息
        List<Apexpages.Message> msgs = ApexPages.getMessages();
        Boolean b = false;
        for(Apexpages.Message msg:msgs) {
            if (msg.getDetail().contains('TEST MESSAGE')) {
                b = true;
            }
        }
        System.assertEquals(false, b);
 
        // 执行
        controller.showApplyMsg();
 
        // 错误信息被报出
        msgs = ApexPages.getMessages();
        b = false;
        for(Apexpages.Message msg:msgs) {
            if (msg.getDetail().contains('TEST MESSAGE')) {
                b = true;
            }
        }
        System.assertEquals(true, b);
        Test.stopTest();
    }
}