@isTest
private class CampaignMemberTriggerTest {

    public static Campaign createCampaignData( String inputName) {
        Campaign ret = new Campaign();
        ret.Name = inputName;
        ret.HostName__c = '会议主办方';
        ret.cooperatorCompany__c = '会议承办方';
        return ret;
    }

    public static Account createAccountData( String inputName) {
        Account ret = new Account();
        ret.Name = inputName;
        return ret;
    }

    public static Contact createContactData( String inputName, String inputEmail) {
        Contact ret = new Contact();
        ret.LastName = inputName;
        ret.Email = inputEmail;
        return ret;
    }

    public static Lead createLeadData( String inputName, String inputEmail, String inputCompany) {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GYN'];
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];

        
        Account hospital = new Account();
        hospital.recordtypeId = rectCo[0].Id;
        hospital.Name = 'test hospital';
        upsert hospital;
        
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name = '*';
        depart.Department_Name__c  = 'TestDepart';
        depart.Hospital__c         = hospital.Id;
        depart.Is_Active__c = '有効';
        upsert depart;
		
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[0].Id;
        depart2.Name = '*';
        depart2.Department_Name__c  = 'TestDepart2';
        depart2.Hospital__c         = hospital.Id;
        upsert depart2;
        
        Lead ret = new Lead();
        ret.FirstName = inputName;
        ret.Email = inputEmail;
        ret.Hospital_Name__c = depart.id;
        ret.company = inputCompany;
        return ret;
    }
	
    /**
    static testMethod void test01() {
        // キャンペーン作成
        Campaign targetCampaign = createCampaignData('test camapign');
        insert targetCampaign;
        
        // リード作成
        Lead targetLead = createLeadData( 'leadName', 'test@test.com.leadtest', 'tesetCompany');
        insert targetLead;
        
        // リードをキャンペーンメンバーに
        CampaignMember target = new CampaignMember();
        target.LeadId = targetLead.Id;
        target.CampaignId = targetCampaign.Id;
        insert target;
        
        // 何も起きていないことを確認
        
        // Contactは増えていない
        System.assertEquals( 0, [SELECT id FROM Contact].size());
        // Leadも、テストで作ったやつだけ
        System.assertEquals( 1, [SELECT id FROM Lead].size());
        // CampaignMemberも、テストで作ったやつだけ
        System.assertEquals( 1, [SELECT id FROM CampaignMember].size());
    }*/
    
    static testMethod void test02() {
        
        // レコードタイプ取得
        RecordType hospitalRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType sectionRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
        RecordType departmentRec = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        
        // 病院作成
        Account hospital = new Account();
        hospital.RecordTypeId = hospitalRec.Id;
        hospital.Name = 'TestHospital';
        insert hospital;
        
        // 病院を作ると戦略科室は、トリガーによって作られている
        // Account section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Parent.Id = :hospital.Id and RecordTypeId = :sectionRec.Id limit 1];
        Account section = [select Management_Code__c,  Name, Id from Account where Parent.Id = :hospital.Id and RecordTypeId = :sectionRec.Id limit 1];
        
        // 診療科1を作成
        Account depart1 = new Account();
        depart1.RecordTypeId = departmentRec.Id;
        depart1.Name = '*';
        depart1.Department_Name__c  = 'TestDepart';
        depart1.ParentId = section.Id;
        depart1.Department_Class__c = section.Id;
        depart1.Hospital__c = hospital.Id;
        insert depart1;
        
        // 診療科2を作成
        Account depart2 = new Account();
        depart2.RecordTypeId = departmentRec.Id;
        depart2.Name = '*';
        depart2.Department_Name__c  = 'TestDepart2';
        depart2.ParentId = section.Id;
        depart2.Department_Class__c = section.Id;
        depart2.Hospital__c = hospital.Id;
        insert depart2;
        
        // キャンペーン作成
        Campaign targetCampaign = createCampaignData('test camapign');
        insert targetCampaign;
        
        // コンタクト作成
        Contact targetContact = createContactData( 'test contact', 'daiske@oly.com.tesst');
        targetContact.accountId = depart1.id;
        targetContact.LastName ='ZZ';
        insert targetContact;
        
        // 事前の assert
        // Contactは、テストで作った1レコードのみ
        System.assertEquals( 1, [SELECT id FROM Contact].size());
        // Leadは、0レコード
        System.assertEquals( 0, [SELECT id FROM Lead].size());
        // CampaignMemberも、テストで作ったやつだけ
        System.assertEquals( 0, [SELECT id FROM CampaignMember].size());
        
        // コンタクトをキャンペーンメンバーに
        CampaignMember targetMember = new CampaignMember();
        targetMember.ContactId = targetContact.Id;
        targetMember.CampaignId = targetCampaign.Id;
        targetMember.Sales_lead__c = '创建购买意向';
        insert targetMember;
        
        // コンタクト配下にリードができている
        // そのリードは、キャンペーンメンバーにもなっている
        // Contactは、テストで作った1レコードのみ
        System.assertEquals( 1, [SELECT id FROM Contact].size());
        // Leadは、1レコード増えている
        List<Lead> insertedLead = [SELECT id, sendMailToOwner__c FROM Lead];
        System.assertEquals( 1, insertedLead.size());
        System.assertEquals( true, insertedLead[0].sendMailToOwner__c);
        // CampaignMemberも、増えている
        System.assertEquals( 2, [SELECT id FROM CampaignMember].size());
    }
   
}