@isTest
private class PersonalProcessInstanceControllerTest {

	@isTest static void test_method_one() {
        Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
        Approval.ProcessResult submitResult = null;

        List<RecordType> rectRVI = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer RVI'];
        Account accRVI = new Account(
            Name = '*',
            RecordTypeId = rectRVI[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer RVI',
            FacilityName__c='abc',
            PostCode__c='000000',
            stautesD__c = 'Completed',
            IsNew__c = true
        );
        insert accRVI;

        psr = new Approval.ProcessSubmitRequest();
        psr.setObjectId(accRVI.Id);
        submitResult = Approval.process(psr);

        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accRVI.Id;
        con.MobilePhone = 'MobilePhoneD__c';
        con.OtherPhone = 'OtherPhoneD__c';
        con.Fax = 'FaxD__c';
        con.Email = 'EmailD__c@test.com';
        con.Phone = 'PhoneD__c';
        con.Title = 'TitleD__c';
        con.Address1__c = 'Address1D__c';
        con.Address2__c = 'Address2D__c';
        con.Address3__c = 'Address3D__c';
        con.Postcode__c = '100111';
        con.ContactStatus__c = 'ContactStatusD__c';
        con.CancelReason__c = 'CancelReasonD__c';
        con.StatusD__c = 'Completed';
        con.IsNew__c = true;
        con.DealerSelectOwner__c = UserInfo.getUserId();

        insert con;

        psr = new Approval.ProcessSubmitRequest();
        psr.setObjectId(con.Id);
        submitResult = Approval.process(psr);

        Id pricebookId = Test.getStandardPricebookId();
        PriceBook2 pricebook =new PriceBook2(
            Name = 'RVI',
            ProductSegment__c = 'RVI',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true
        );
        insert pricebook;

        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.ProductCode = 'product1';
        product1.Product_ECCode__c = 'product1';
        product1.IsActive = true;
        insert product1;

        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true
        );
        insert standardPrice1;

        PricebookEntry entry1 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY'
        );
        insert entry1;

        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        // 有预定下单日
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accRVI.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Prospect Created',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'RVI',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation',
            Machine_Parts_D__c = 'Machine',
            ApprovalStatus_D__c = 'Completed',
            IsNew__c = true
        );
        insert opp;

        OpportunityLineItem oli1 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oli1;

        opp.ApprovalStatus_D__c = 'Completed';
        update opp;

        psr = new Approval.ProcessSubmitRequest();
        psr.setObjectId(opp.Id);
        submitResult = Approval.process(psr);

        List<RecordType> rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'RVISalesContract'];
        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accRVI.Id,
            ApproveStatus__c = 'Completed2',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            RecordTypeId = rectOdr[0].Id,
            SpecialDeliveryAccount__c = accRVI.Id,
            SpecialDeliveryAccount_D__c = accRVI.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            IsCopy__c = true
        );
        insert odr;

        psr = new Approval.ProcessSubmitRequest();
        psr.setObjectId(odr.Id);
        submitResult = Approval.process(psr);

        ProcessInstanceWorkitem piw = [select id, ActorId, ProcessInstance.TargetobjectId from ProcessInstanceWorkitem where ProcessInstance.TargetobjectId = :odr.Id];
        piw.ActorId = UserInfo.getUserId();
        update piw;

        PageReference page = new PageReference('/apex/PersonalProcessInstance');
        System.Test.setCurrentPage(page);

        PersonalProcessInstanceController controller = new PersonalProcessInstanceController();

        //初始化
        controller.init();

        controller.getAccPIListSize();
        controller.getConPIListSize();
        controller.getOppPIListSize();
        controller.getOdrPiListSize();
        controller.getOthPIListSize();
	}

}