@isTest
private class AgencyReportHeaderTriggerTest{
    
    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static User user = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    public static Agency_Contact__c agency_contact = new Agency_Contact__c();
    public static ProductTypes__c product_types = new ProductTypes__c();
    public static Agency_Opportunity__c agency_opportunity = new Agency_Opportunity__c();
    public static OlympusCalendar__c olympus_calendar = new OlympusCalendar__c();
    
    public static String report_id;
    public static String report_header_id;
    
    /* 病院。コンタクト、ユーザー、カレンダーなどの初期データ作る
     */
    static testMethod void test_beforeExecute(){
        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;
        
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
        
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;
        
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        // ユーザー
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com';
        user.Alias = 'テユ';
        user.CommunityNickname = 'テストユーザー';
        insert user;
        
        olympus_calendar.Date__c = Date.valueOf('2017-04-10');
        olympus_calendar.OwnerId = user.Id;
        insert olympus_calendar;
    
        System.runAs(user) {
            // 週報ヘッダー作成
            String name = 'テスト ユーザー';
            String s_date = '2017-04-10';
            String s_agency = contact2.Id;
            String head_key = contact2.Id + ':20170410';
            report_header_id = WeeklyReportCmp.createReportHeader(name, s_date, s_agency, head_key);
            
            Agency_Report_Header__c data = [select Id, Name, Agency__c,Owner_Agency_ID__c,Agency_ID__c,Week__c, Agency_Person2__c, HeaderInputKey__c, OlympusDate__r.Date__c from Agency_Report_Header__c where HeaderInputKey__c = :head_key];
   
            System.assertEquals(data.Agency__c , data.Owner_Agency_ID__c);
            System.assertEquals(data.Agency_ID__c , String.valueOf(data.Owner_Agency_ID__c).substring(0,15));
        }
    }

    @isTest static void test_shareAgency_Report_Header_ToRole() {
        MergeAgencyActivityBatchTest.makeNormalData(true);

        // assert
        List<Agency_Report_Header__c> tList = [SELECT Name, Agency__c
                FROM Agency_Report_Header__c ORDER BY Id LIMIT 2];
        System.assertEquals(2, tList.size());
        System.assertEquals(MergeAgencyActivityBatchTest.agency1.Id, tList[0].Agency__c);
        System.assertEquals(MergeAgencyActivityBatchTest.agency1.Id, tList[1].Agency__c);
        List<Agency_Report_Header__share> tsList = [SELECT Id
                FROM Agency_Report_Header__share WHERE ParentId = :tList AND RowCause = 'Manual'];
        System.assertEquals(2, tsList.size());

        Test.startTest();
        Delete tList[0];
        Test.stopTest();
        tsList = [SELECT Id
                FROM Agency_Report_Header__share WHERE ParentId = :tList AND RowCause = 'Manual'];
        System.assertEquals(1, tsList.size());

        UnDelete tList[0];
        tsList = [SELECT Id
                FROM Agency_Report_Header__share WHERE ParentId = :tList AND RowCause = 'Manual'];
        System.assertEquals(2, tsList.size());
    }
}