buli
2022-04-08 f1c525740c6a45b875d8ed96fb0ddb68c97ef3df
force-app/main/default/classes/AccountWebServiceTest.cls
@@ -1,68 +1,166 @@
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 *
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class AccountWebServiceTest {
    static testMethod void myUnitTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        Account hp = new Account(
            RecordTypeId = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'].Id,
            Name = 'AccountCaseHistoryTestHp1',
            OwnerId = hpOwner.Id
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            AccountStatus__c = 'Cancel',
            PostCode__c = '123456'
        );
        insert hp;
        Account acc2 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            PostCode__c = '123456'
        );
        Account acc3 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            CancelReason__c = 'test',
            PostCode__c = '123456'
        );
        insert new List<Account> {acc1,acc2,acc3};
        
        RecordType rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        Account section = [Select Name, Department_Class_Label__c, OwnerId, ParentId, Hospital_Department_Class__c from Account where ParentId = :hp.Id and RecordTypeId = :rectSct.Id];
        String rs = '';
        rs = AccountWebService.AccountCancel('12345678');
        System.assertEquals('无效的客户', rs);
        
        Account depart = new Account();
        depart.RecordTypeId = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'dept1';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hp.Id;
        insert depart;
        rs = AccountWebService.AccountCancel(acc1.Id);
        System.assertEquals('客户已经取消', rs);
        
        // 診療科テスト
        depart.Is_Active__c = '無効';
        update depart;
        rs = AccountWebService.AccountCancel(acc2.Id);
        System.assertEquals('必须输入取消理由', rs);
        
        AccountWebService.toBatchOwner(hp.Id);
        depart = [select OwnerId from Account where Id = :depart.Id];
        System.assertEquals('00510000000fSYI', depart.OwnerId);
        // 病院テスト
        hp.Is_Active__c = '無効';
        update hp;
        AccountWebService.toBatchOwner(hp.Id);
        hp = [select OwnerId from Account where Id = :hp.Id];
        System.assertEquals('00510000000fSYI', hp.OwnerId);
        section = [select OwnerId from Account where Id = :section.Id];
        System.assertEquals('00510000000fSYI', section.OwnerId);
        rs = AccountWebService.AccountCancel(acc3.Id);
        System.assertEquals('1', rs);
        List<Account> accList = [select Id,AccountStatus__c from Account where Id = :acc3.Id];
        System.assertEquals('Cancel', accList[0].AccountStatus__c);
    }
    static testMethod void myUnitTest2() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        String localid = UserInfo.getUserId();
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            AccountStatus__c = 'Cancel',
            PostCode__c = '123456'
        );
        insert acc1;
        User user = new User();
        user.LastName = 'test20201014';
        user.FirstName = 'test';
        user.Alias = 'test';
        user.Email = 'test@test.com';
        user.Username = 'test111@test222.com';
        user.CommunityNickname = 'test20201014';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.SystemAdmin;
        insert user;
        AccountTeamMember atm = new AccountTeamMember(
            AccountId = acc1.Id,
            userId = localid,
            teamMemberRole = 'Sales Manager'
        );
        AccountTeamMember atm2 = new AccountTeamMember(
            AccountId = acc1.Id,
            userId = user.Id,
            teamMemberRole = 'Sales Manager'
        );
        insert new AccountTeamMember[] {atm,atm2};
        String rs = '';
        rs = AccountWebService.UpdateShare(acc1.Id, localid, user.Id);
        System.assertEquals('1', rs);
        List<AccountTeamMember> atmList = [select Id, AccountAccessLevel from AccountTeamMember where Id = :atm.Id];
        System.assertEquals('Edit', atmList[0].AccountAccessLevel);
    }
    // 技术服务部客户与联系人申请提交时字段验证
    static testMethod void TechnicalServiceCreated() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer BS'];
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            AccountStatus__c = 'Cancel',
            PostCode__c = '123456',
            serviceCreate__c = true
        );
        Account acc2 = new Account(
            name = '*',
            RecordTypeId = rectBS[0].Id,
            OwnerId = UserInfo.getUserId(),
            //DivisionName__c = 'Customer BS',
            FacilityName__c = 'abc'
        );
        Account acc3 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            DivisionName_D__c = 'Customer IE',
            FacilityNameD__c = 'abc',
            CancelReason__c = 'test',
            PostCode__c = '123456'
        );
        User hpOwner = new User( LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = System.Label.SystemAdmin2,MarketingStrategy__c =true);
        insert hpOwner;
        System.runAs(hpOwner){
            insert new List<Account> {acc1,acc2,acc3};
            // insert dealer;
            Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=acc1.id);
            insert core;
            String rs = '';
            // rs = AccountWebService.AccountCancel('12345678');
            // System.assertEquals('无效的客户', rs);
            rs = AccountWebService.RequiredField(acc1.Id);
            // System.assertEquals('客户已经取消', rs);
            rs = AccountWebService.RequiredField(acc2.Id);
            // System.assertEquals('必须输入取消理由', rs);
            rs = AccountWebService.ContactRequiredField(core.Id);
            // rs = AccountWebService.AccountCancel(acc3.Id);
            // System.assertEquals('1', rs);
            // List<Account> accList = [select Id,AccountStatus__c from Account where Id = :acc3.Id];
            // System.assertEquals('Cancel', accList[0].AccountStatus__c);
        }
    }
}