@isTest
private class SendEmailControllerTest {
    static testMethod void Add() {

        User_FaultInfo__c ca = new User_FaultInfo__c();
        ca.ORIGIN__c = 'Web';
        ca.SUBJECT__c = 'test';
        ca.OFFICE_OWNER__c = 'Beijing';
        ca.Product_Type__c = 'NDT';
        ca.TYPE__c = 'Service';
        ca.LOGISTICS_SHIPMENT_TYPE__c = 'Domestic';
        ca.STATUS__c = 'Not Started';
        ca.PRIORITY__c = 'High';
        insert ca;

        SWO__c swo = new SWO__c();
        swo.Name = 'Test';
        swo.QUANTITY__c = 22;
        swo.ESTIMATED_LABOUR_HOURS__c = 22;
        swo.LABOUR_RATE__c = 22;
        swo.StatusMark__c = 2;
        insert swo;

        Quotes__c quotes = new Quotes__c();
        quotes.SWO__c = swo.Id;
        quotes.QuotesType__c = '零件报价单';
        insert quotes;

        Mail_Merge__c  mailMerge = new Mail_Merge__c(); 
        mailMerge.RECORD__c = swo.Id;
        mailMerge.ALL_MEMBER__c = '1234Test@olympus.inv;1234Test1@olympus.inv;1234Test2@olympus.inv;';
        mailMerge.ALL_MEMBER_NAME__c = 'Test01;Test02;Test03;';
        mailMerge.ALL_MEMBER_TYPE__c = 'to;cc;bcc;';
        mailMerge.RECORD_TYPE__c = 'SWO';
        mailMerge.SWO__c = swo.Id;
        mailMerge.CaseF__c = ca.Id;
        mailMerge.Quotes__c = quotes.Id;
        mailMerge.SUBJECT__c = 'TEST';
        mailMerge.MESSAGE__c = 'TEST';
        mailMerge.EMAIL_SENT__c = 'NO';
        mailMerge.TYPE__c = 'reply';
        mailMerge.DATE__c = Datetime.now();
        insert mailMerge;

        Attachment att = new Attachment(
            Name = 'test.pdf',
            parentId = mailMerge.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att;
        Attachment att1 = new Attachment(
            Name = 'test1.pdf',
            parentId = mailMerge.Id,
            Body = EncodingUtil.base64Decode('test')
        );
        insert att1;

        PageReference page = new PageReference('/apex/SendEmail?type=SWO&typeid='+swo.Id);
        System.Test.setCurrentPage(page);
        SendEmailController controller = new SendEmailController(new ApexPages.StandardController(mailMerge));
        controller.init();
        controller.SendEmail();

        page = new PageReference('/apex/SendEmail?id='+mailMerge.Id+'&type=SWO&typeid='+swo.Id+'&openType=Reply');
        System.Test.setCurrentPage(page);
        controller = new SendEmailController(new ApexPages.StandardController(mailMerge));
        controller.init();
        controller.SendEmail();

        PageReference page1 = new PageReference('/apex/SendEmail?id='+mailMerge.Id+'&type=SWO&typeid='+swo.Id);
        System.Test.setCurrentPage(page1);
        SendEmailController controller1 = new SendEmailController(new ApexPages.StandardController(mailMerge));
        controller1.init();
        controller1.addLine();
        controller1.lineNum = 0;
        controller1.deLine();
        controller1.deLine();
        controller1.deLine();
        controller1.deLine();
        controller1.uploadFile();
        controller1.fileId = att1.Id;
        controller1.seeFile();
        controller1.deleteFile();
        controller1.handleType = 'send';
        controller1.SendEmail();
        controller1.lineNum = 0;
        controller1.memberName = 'test';
        controller1.memberEmail = '1234Test@olympus.inv';
        controller1.memberId = 'test';
        controller1.addMemberData();
        mailMerge.SUBJECT__c = '';
        update mailMerge;
        controller1.SendEmail();
        mailMerge.SUBJECT__c = 'Test';
        update mailMerge;
        controller1.SendEmail();

        mailMerge.RECORD__c = ca.Id;
        mailMerge.RECORD_TYPE__c = 'Case';
        update mailMerge;
        PageReference page2 = new PageReference('/apex/SendEmail?id='+mailMerge.Id+'&type=Case&typeid='+ca.Id);
        System.Test.setCurrentPage(page2);
        SendEmailController controller2 = new SendEmailController(new ApexPages.StandardController(mailMerge));
        controller2.init();
        controller2.handleType = 'send';
        controller2.SendEmail();
    }

    static testMethod void quoteSendEmail() {
        User_FaultInfo__c ca = new User_FaultInfo__c();
        ca.ORIGIN__c = 'Web';
        ca.SUBJECT__c = 'test';
        ca.OFFICE_OWNER__c = 'Beijing';
        ca.Product_Type__c = 'NDT';
        ca.TYPE__c = 'Service';
        ca.LOGISTICS_SHIPMENT_TYPE__c = 'Domestic';
        ca.STATUS__c = 'Not Started';
        ca.PRIORITY__c = 'High';
        insert ca;

        SWO__c swo = new SWO__c();
        swo.Name = 'Test';
        swo.QUANTITY__c = 22;
        swo.ESTIMATED_LABOUR_HOURS__c = 22;
        swo.LABOUR_RATE__c = 22;
        swo.StatusMark__c = 2;
        insert swo;

        Quotes__c quotes = new Quotes__c();
        quotes.SWO__c = swo.Id;
        quotes.QuotesType__c = '零件报价单';
        insert quotes;

        Mail_Merge__c  mailMerge = new Mail_Merge__c(); 
        mailMerge.RECORD__c = quotes.Id;
        mailMerge.ALL_MEMBER__c = '1234Test@olympus.inv;1234Test1@olympus.inv;1234Test2@olympus.inv;';
        mailMerge.ALL_MEMBER_NAME__c = 'Test01;Test02;Test03;';
        mailMerge.ALL_MEMBER_TYPE__c = 'to;cc;bcc;';
        mailMerge.RECORD_TYPE__c = 'Quotes';
        mailMerge.SWO__c = swo.Id;
        mailMerge.CaseF__c = ca.Id;
        mailMerge.Quotes__c = quotes.Id;
        mailMerge.SUBJECT__c = 'TEST';
        mailMerge.MESSAGE__c = 'TEST';
        mailMerge.EMAIL_SENT__c = 'YES';
        mailMerge.TYPE__c = 'reply';
        mailMerge.DATE__c = Datetime.now();
        insert mailMerge;

        PageReference page = new PageReference('/apex/SendEmail?type=Quotes&typeid='+quotes.Id);
        System.Test.setCurrentPage(page);
        SendEmailController controller = new SendEmailController(new ApexPages.StandardController(mailMerge));
        controller.init();
        controller.uploadFileS();
        controller.RefreshPage();
        controller.SendEmail();
        // controller.addPDF();
        PageReference page2 = new PageReference('apex/SendEmail?id='+mailMerge.Id+'&type=Quotes&typeid='+quotes.Id+'&openType=Reply');
        System.Test.setCurrentPage(page2);
        SendEmailController controller2 = new SendEmailController();
        controller2.init();
        controller2.SendEmail();

    }

}