@isTest
private class ReceivingNoteWaitingReceiptCtrlTest {
    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 测试检索功能
    */
    static testMethod void unitTestSearch() {
        setupTestData('');

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + factory.rnMap.get('固定资产,个体管理').Id);
        Test.setCurrentPage(ref);

        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();

        Test.startTest();
        controller.init();
        controller.getStatusOps();
        controller.getSalesdepartmentOps();
        controller.getSalesProvinceOps();
        controller.getProductCategoryOps();
        controller.getEquipmentTypeOps();
        controller.getManageTypeOps();
        controller.getUnknowSerialNOProductOps();
        controller.getLoanerAccsessaryOps();
        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());

        Test.stopTest();

    }

    /**
    *@description 测试保存功能
    */
    static testMethod void unitTestSaveRecord() {
        setupTestData('固定资产,个体管理');

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + factory.rnMap.get('固定资产,个体管理').Id);
        Test.setCurrentPage(ref);

        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();

        Test.startTest();
        // 初始化
        controller.init();
        controller.search();
        // 改为可编辑
        controller.changeFixModel();
        // 取出第一条数据
        ReceivingNoteWaitingReceiptController.RowBean rowB = controller.pageB.rowBList[0];
        Id rndId = rowB.rnd.Id;
        // 修改字段值
        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 = 'FALSE';
        rowB.rnd.Loaner_accsessary__c = 'TRUE';
        rowB.rnd.DataConfirmation__c = true;
        // 运行saveRecord()
        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
                     , DataConfirmation__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('FALSE', newRnd.unknow_serial_NO_product__c);
        // System.assertEquals('TRUE', newRnd.Loaner_accsessary__c);
        // System.assertEquals(true, newRnd.DataConfirmation__c);

        Test.stopTest();
    }

    /**
    *@description 测试取消功能
    */
    static testMethod void unitTestCancelRecord() {

    }

    /**
    @description 测试【选择配套】按键
    */
    static testMethod void unitTestSelectRNS() {

        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + rnId);
        Test.setCurrentPage(ref);

        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();

        Test.startTest();
        controller.init();
        controller.search();
        controller.pageB.rowBList[0].checked = true;

        //   跳转页面
        PageReference pg = controller.selectRNS();
        Map<String,String> params = pg.getParameters();

        // System.assertEquals(params.get('pt_recid'), rnId);
        // System.assertEquals(params.get('raesid'), rnsId);

        Test.stopTest();
    }
    /**
    @description 显示错误信息
    */
    static testMethod void unitTestShowApplyMsg() {

        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + rnId);
        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();
        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();
    }
    /**
    @description 发邮件测试
    */
    static testMethod void unitTestSendEmail() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + factory.rnMap.get('固定资产,个体管理').Id);
        Test.setCurrentPage(ref);

        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();

        Test.startTest();
        // 初始化
        controller.init();
        controller.search();

        controller.pageB.rowBList[0].checked = true;
        String emailMsg = controller.pageB.rowBList[0].rnd.Name+'<BR>';
        String emailBody = 'ARE YOU OK?';
        ref.getParameters().put('emailBody', emailBody);
        controller.sendEmail();

        ReceivingNote__c rn = [select Model_Reminder_Email_Text__c from ReceivingNote__c WHERE Id=:rnId ];
        // System.assertNotEquals(null, rn.Model_Reminder_Email_Text__c);

        Test.stopTest();
    }
    /**
    @description 编辑备品入库处理、备品入库产品
    */
    static testMethod void unitTestRules() {
        setupTestData('固定资产,个体管理');

        // 选择变体时，备品入库产品不能为空
        ReceivingNoteDetail__c rndObj = factory.rndMap.get('固定资产,个体管理');
        rndObj.Fixture_Arrival_Process__c = '变体';

        String msg;

        Test.startTest();
        try {
            update rndObj;
        } catch (Exception e) {
            msg = e.getMessage();
        }

        // System.assertEquals(true, msg.contains('请选择备品入库产品。'));

        rndObj.Fixture_Arrival_Product__c = factory.proList[2].Id;
        rndObj.Fixture_Arrival_Process__c = '变体';
        update rndObj;
        ReceivingNoteDetail__c rndObjUpdated = [SELECT Id, Fixture_Arrival_Product__c FROM ReceivingNoteDetail__c WHERE Id=:rndObj.Id];
        // System.assertNotEquals(null, rndObjUpdated.Fixture_Arrival_Product__c);

        Test.stopTest();

    }

    //add by rentx 20210824 start
    static testMethod void testMethod01() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteWaitingReceipt?parId=' + factory.rnMap.get('固定资产,个体管理').Id);
        Test.setCurrentPage(ref);

        ReceivingNoteWaitingReceiptController controller = new ReceivingNoteWaitingReceiptController();

        Test.startTest();
        // 初始化
        controller.init();
        controller.search();

        controller.pageB.rowBList[0].checked = true;
        controller.pageB.rowBList[0].rnd.Loaner_accsessary__c = 'FALSE';
        controller.saveRecord();
        Test.stopTest();
    }
    //add by rentx 20210824 end

}