@isTest
private class ApprovalHistoryUtilTest {
    static User setNewUser(String firstName, String lastName, String aName, String email) {
        User user = new User(Test_staff__c = true);
        user.LastName = ' ' + lastName;
        user.FirstName = firstName;
        user.Alias = aName;
        user.Email = email;
        user.Username = 'Olympus' + email;
        user.CommunityNickname = aName;
        user.IsActive = 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.Use_Start_Date__c = Date.today().addMonths(-6);
        insert user;

        return user;
    }
    @testSetup static void methodName() {
        User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
         Account newAccount = new Account();
        newAccount.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        newAccount.Name = 'test hospital';
        newAccount.Is_Active__c = '有効';
        newAccount.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert newAccount;
                }
        }
    }
    @isTest static void isCurrentUserIdTest_SystemAdmin(){
        // List<User> users = [select Id, FirstName, LastName FROM User WHERE Name = '黄 千龙' LIMIT 1];
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        // System.runAs(users.get(0)){
            boolean isAdmin = ApprovalHistoryUtil.isCurrentUserSystemAdmin(accounts.get(0).Id);
            // System.assertEquals(true, isAdmin, 'The user was supposed to be found admin');
        // }
       
    }

    @isTest static void isCurrentUserIdTest_NOT_SystemAdmin(){
        List<User> users = [select Id, FirstName, LastName FROM User WHERE Profile.Name != 'System Administrator' LIMIT 1];
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        System.runAs(users.get(0)){
            boolean isAdmin = ApprovalHistoryUtil.isCurrentUserSystemAdmin(accounts.get(0).Id);
            System.assertEquals(false, isAdmin, 'The user was not supposed to be found admin');
        }
       
    }

    @isTest static void processStepTest_EmptyWorkItems(){
        List<User> users = [select Id, FirstName, LastName FROM User WHERE Profile.Name != 'System Administrator' LIMIT 1];
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        List<ProcessInstanceWorkitem> workItems = new List<ProcessInstanceWorkitem>();
        String retVal = ApprovalHistoryUtil.processStep( workItems, 'comments',users.get(0).Id, accounts.get(0).Id);

    }
    
    @isTest static void processStepTest_NotEmptyWorkItems(){
        List<User> users = [select Id, FirstName, LastName FROM User WHERE Profile.Name != 'System Administrator' LIMIT 1];
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        List<ProcessInstanceWorkitem> workItems = new List<ProcessInstanceWorkitem>();
        workItems.add(new ProcessInstanceWorkitem(Id = '04i3h000001L5fGAAS'));
        try{
            String retVal = ApprovalHistoryUtil.processStep( workItems, 'comments',users.get(0).Id, accounts.get(0).Id);
        }catch(Exception e){

        }
    }

    @isTest static void reassignStepTest(){
        List<User> users = [SELECT Id, FirstName, LastName FROM User LIMIT 1];
        List<ProcessInstanceWorkitem> workItems = new List<ProcessInstanceWorkitem>();
        workItems.add(new ProcessInstanceWorkitem(Id = '04i3h000001L5fGAAS'));
        try{
            ApprovalHistoryUtil.reassignStep(workItems, users.get(0).Id);
        }catch(Exception e){
        }
    }

    @isTest static void populateApprovalHistoryTest(){

         RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;
             Account acc2= new Account();
            acc2.RecordTypeId = rectCo.Id;
            acc2.Name = 'HP test22';
            acc2.Is_Active__c = '有效';
            acc2.Is_upload_file__c = true;
            acc2.HospitalType__c = '企业集团';
            acc2.State_Master__c = al.id;
            acc2.InstitutionalType__c = '非医疗机构';
            insert acc2;
            Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(acc1.Id);
            Approval.process(r);
            List<ProcessInstance> processInstances = [SELECT Id,SubmittedById, ProcessDefinition.Name , (SELECT ID, ProcessNodeId,  
                                                            StepStatus,Comments,TargetObjectId,ActorId,CreatedById,IsDeleted,IsPending  
                                                            ,OriginalActorId,ProcessInstanceId,RemindersSent,CreatedDate, Actor.Name,  
                                                            OriginalActor.Name , ProcessNode.Name FROM StepsAndWorkitems order by IsPending DESC, CreatedDate DESC )   
                                                    FROM ProcessInstance where TargetObjectId =:acc1.Id order by CreatedDate DESC];    
        ApprovalHistoryController.ApprovalHistory retVal = ApprovalHistoryUtil.populateApprovalHistorySteps(processInstances, acc1.Id);

    }

    @isTest static void populateApprovalHistoryRecord(){
        List<User> users = [SELECT Id, FirstName, LastName FROM User LIMIT 1];
        List<ApprovalHistoryController.ApprovalHistoryStep> approvalSteps = new List<ApprovalHistoryController.ApprovalHistoryStep>();
        approvalSteps.add(new ApprovalHistoryController.ApprovalHistoryStep( 'stepName','stepId', DateTime.now(),'stepStatus', 'assignedTo', 'assignedToId', 'Comments'));
        ApprovalHistoryController.ApprovalHistory approvalHistoryToReturn = new ApprovalHistoryController.ApprovalHistory( approvalSteps,  false, true, true);
        ProcessInstanceHistory step = new ProcessInstanceHistory();
        ApprovalHistoryUtil.populateApprovalHistoryRecord(approvalHistoryToReturn, step,  true, users.get(0).Id );
        System.assertEquals(true, approvalHistoryToReturn.showRecall, 'The recall attribute is supposed to be true');
        System.assertEquals(true, approvalHistoryToReturn.isCurrentUserApprover, 'The isCurrentUserApprover attribute is supposed to be true');
        System.assertEquals(false, approvalHistoryToReturn.showSubmitForApproval, 'The showSubmitForApproval attribute is supposed to be false');
        
    }


}