/**
 * 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 PersonalCalendarControllerTest {

    static testMethod void myUnitTest() {
        UserRole ur3 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Department'];
        // システム管理者
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sb.com';
        u3.Username = 'olympusTest03@sb.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        u3.UserRoleId = ur3.id;
        insert u3;
        
        System.runAs(u3) {
            // 5.华南销售本部に変更
            List<User> us = [select Id from User where IsActive = true and Id != :u3.Id];
            for (User u : us) {
                u.Province__c = '湖北省';
            }
            update us;

            PersonalCalendarController tc = new PersonalCalendarController();
            // 初期表示
            tc.init();
            // assert default
            System.assertEquals(true, tc.getIsWeek());
            System.assertEquals(1, tc.userIds.size());
            System.assertEquals('上海市', tc.loginUser.Province__c);
            System.assertEquals('', tc.salesDpt);
            Date mdDay = Date.today();
            mdDay = mdDay.toStartofWeek();
            Datetime dt = DateTime.newInstance(mdDay.year(), mdDay.month(), mdDay.day());
            String dayOfWeek = dt.format('EEEE'); //returns Sunday or Monday or ..
            if  (dayOfWeek == 'Monday') {
                mdDay = mdDay.addDays(-1);
            }
            System.assertEquals(mdDay, tc.mdDay);
        }
    }

    // init mk指定
    static testMethod void testInit_mk() {
        Apexpages.currentPage().getParameters().put('mk', '2014/10/01');
        PersonalCalendarController tc = new PersonalCalendarController();
        tc.init();
        // assert default
        System.assertEquals(true, tc.getIsWeek());
        System.assertEquals(Date.newInstance(2014, 9, 28), tc.mdDay);
    }

    // init md指定
    static testMethod void testInit_md() {
        Apexpages.currentPage().getParameters().put('md', '2014/10/01');
        PersonalCalendarController tc = new PersonalCalendarController();
        tc.init();
        // assert default
        System.assertEquals(false, tc.getIsWeek());
        System.assertEquals(Date.newInstance(2014, 10, 1), tc.mdDay);
    }

    // getUrl、assert省略、VFにて確認
    static testMethod void testGetUrl() {
        Apexpages.currentPage().getParameters().put('mk', '2014/10/01');
        PersonalCalendarController tc = new PersonalCalendarController();
        tc.init();
        tc.getSearchJsUrl();
        tc.getShowDayAllUrl();
        tc.getProvinceUrl();
        tc.getDayViewUrl();
        tc.getWeekViewUrl();
        tc.getChangeDayUrl();
        tc.getPrevUrl();
        tc.getNextUrl();
        Apexpages.currentPage().getParameters().put('md', '2014/10/01');
        tc = new PersonalCalendarController();
        tc.init();
        tc.getSearchJsUrl();
        tc.getShowDayAllUrl();
        tc.getProvinceUrl();
        tc.getDayViewUrl();
        tc.getWeekViewUrl();
        tc.getChangeDayUrl();
        tc.getPrevUrl();
        tc.getNextUrl();
    }

    // event取得、assert省略、VFにて確認
    static testMethod void testAddEvent() {
        UserRole ur3 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Department'];
        // システム管理者
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        u3.UserRoleId = ur3.id;
        insert u3;
        
        System.runAs(u3) {
            Date t1 = Date.newInstance(2014, 10, 1);
        
            Event e1 = new Event(
                Subject = 'S1',
                StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 9, 0, 0),
                EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0)
            );
            insert e1;
            Campaign c1 = new Campaign(
                Name = 'testnewCampaign',
                StartDate = t1,
                EndDate = t1,
                IsActive = true,
                Status = 'planed',
                cooperatorCompany__c = 'test1',
                HostName__c = 'test'
            );
            insert c1;
        
            Apexpages.currentPage().getParameters().put('mk', '2014/10/01');
            PersonalCalendarController tc = new PersonalCalendarController();
            tc.init();
            System.assertEquals(1, tc.getCampaignMaxTr());
            List<List<Map<String, String>>> trList = tc.userCalendarInfoMap.get(u3.Id).getRowInfoList();
            System.assertEquals(1, trList.size());
            System.assertEquals(7, trList[0].size());
            System.assertEquals(1, tc.userCalendarInfoMap.get(u3.Id).maxTr);
            
            Apexpages.currentPage().getParameters().put('md', '2014/10/01');
            tc = new PersonalCalendarController();
            tc.init();
            tc.userCalendarInfoMap.get(u3.Id).getMdKeyList3();
            List<List<Map<String, String>>> tdList = tc.userCalendarInfoMap.get(u3.Id).getDayEventInfoList();
            System.assertEquals(14, tdList.size());

            Apexpages.currentPage().getParameters().put('md', '2014/10/01');
            Apexpages.currentPage().getParameters().put('t2', 'all');
            tc = new PersonalCalendarController();
            tc.init();
            tc.userCalendarInfoMap.get(u3.Id).getMdKeyList3();
            tdList = tc.userCalendarInfoMap.get(u3.Id).getDayEventInfoList();
            System.assertEquals(24, tdList.size());
        }
    }
    
    // search確認
    static testMethod void testSearch_1() {
        UserRole ur3 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Department'];
        // システム管理者
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        u3.UserRoleId = ur3.id;
        insert u3;
        
        System.runAs(u3) {
            Date t1 = Date.newInstance(2014, 10, 1);
        
            Event e1 = new Event(
                Subject = 'S1',
                StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 9, 0, 0),
                EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0)
            );
            insert e1;
            // 等于 单值
            Apexpages.currentPage().getParameters().put('text', 'S:Alias');
            Apexpages.currentPage().getParameters().put('cond', 'equals');
            Apexpages.currentPage().getParameters().put('val', 'う');
            Apexpages.currentPage().getParameters().put('mk', '2014/10/01');
            PersonalCalendarController tc1 = new PersonalCalendarController();
            tc1.init();
            tc1.getSearchJsUrl();
            
            List<List<Map<String, String>>> trList = tc1.userCalendarInfoMap.get(u3.Id).getRowInfoList();
            System.assertEquals(1, trList.size());
            System.assertEquals(7, trList[0].size());
            
            // 等于 空
            Apexpages.currentPage().getParameters().put('text', 'S:Alias');
            Apexpages.currentPage().getParameters().put('cond', 'equals');
            Apexpages.currentPage().getParameters().put('val', '');
            PersonalCalendarController tc2 = new PersonalCalendarController();
            tc2.init();
            // 不等于 单值
            Apexpages.currentPage().getParameters().put('text', 'S:Alias');
            Apexpages.currentPage().getParameters().put('cond', 'notequals');
            Apexpages.currentPage().getParameters().put('val', 'う');
            PersonalCalendarController tc3 = new PersonalCalendarController();
            tc3.init();
            // 包含 单值
            Apexpages.currentPage().getParameters().put('text', 'S:Alias');
            Apexpages.currentPage().getParameters().put('cond', 'contains');
            Apexpages.currentPage().getParameters().put('val', 'う');
            PersonalCalendarController tc4 = new PersonalCalendarController();
            tc4.init();
            // 不包含 单值
            Apexpages.currentPage().getParameters().put('text', 'S:Alias');
            Apexpages.currentPage().getParameters().put('cond', 'notcontains');
            Apexpages.currentPage().getParameters().put('val', 'う');
            PersonalCalendarController tc5 = new PersonalCalendarController();
            tc5.init();
        }
    }
    
    static testMethod void testSearch_2() {
        UserRole ur3 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Department'];
        // システム管理者
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        u3.UserRoleId = ur3.id;
        insert u3;
        
        System.runAs(u3) {
            Date t1 = Date.newInstance(2014, 10, 1);
        
            Event e1 = new Event(
                Subject = 'S1',
                StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 9, 0, 0),
                EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0)
            );
            insert e1;
            
            // 等于 多值
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'equals');
            Apexpages.currentPage().getParameters().put('val', 'S1,S2');
            PersonalCalendarController tc6 = new PersonalCalendarController();
            tc6.init();
            // 不等于 多值
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'notequals');
            Apexpages.currentPage().getParameters().put('val', 'S1,S2');
            PersonalCalendarController tc7 = new PersonalCalendarController();
            tc7.init();
            // 不等于 空
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'notequals');
            Apexpages.currentPage().getParameters().put('val', '');
            PersonalCalendarController tc8 = new PersonalCalendarController();
            tc8.init();
            // 包含 多值
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'contains');
            Apexpages.currentPage().getParameters().put('val', 'S 1');
            PersonalCalendarController tc9 = new PersonalCalendarController();
            tc9.init();
            // 不包含 多值
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'notcontains');
            Apexpages.currentPage().getParameters().put('val', 'S 1');
            PersonalCalendarController tc10 = new PersonalCalendarController();
            tc10.init();
            // 不包含 空
            Apexpages.currentPage().getParameters().put('text', 'S:Subject');
            Apexpages.currentPage().getParameters().put('cond', 'notcontains');
            Apexpages.currentPage().getParameters().put('val', '');
            PersonalCalendarController tc11 = new PersonalCalendarController();
            tc11.init();
        }
    }
}