/** * 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 CheckEventTimeChangeTest { static testMethod void test01() { Date t = Date.today(); Date t1 = t.addMonths(6); Date t2 = t.addMonths(-6); Event e = new Event( 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 e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ①新規する時、ActivityDateModifiedDate__cは今日 System.assertEquals(t, e.ActivityDateModifiedDate__c); // ②未来日なので、計画です System.assertEquals(true, e.IsScheduled__c); e.StartDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 10, 0, 0); e.EndDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 11, 0, 0); update e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ③過去日なので、計画ではない System.assertEquals(false, e.IsScheduled__c); e.StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 8, 0, 0); e.EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 9, 0, 0); update e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ④未来日なので、計画です System.assertEquals(true, e.IsScheduled__c); } static testMethod void test02() { Date t = Date.today(); Date t1 = t.addMonths(6); Date t2 = t.addMonths(-6); Date t3 = t2.addDays(10); Event e = new Event( StartDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 9, 0, 0), EndDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 10, 0, 0) ); insert e; // 過去の計画Eventの更新を模擬するため、項目値を強制変更 e.ActivityDateModifiedDate__c = Date.newInstance(t2.year(), t2.month(), t2.day()); e.isScheduled__c = true; update e; e.StartDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 10, 0, 0); e.EndDateTime = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 11, 0, 0); update e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ①更新(活動日変わらない)の時、ActivityDateModifiedDate__cそのまま System.assertEquals(Date.newInstance(t2.year(), t2.month(), t2.day()), e.ActivityDateModifiedDate__c); // ②過去の計画でも、活動日変わらないので、IsScheduled__cそのまま System.assertEquals(true, e.IsScheduled__c); e.StartDateTime = Datetime.newInstance(t3.year(), t3.month(), t3.day(), 10, 0, 0); e.EndDateTime = Datetime.newInstance(t3.year(), t3.month(), t3.day(), 11, 0, 0); update e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ③更新(活動日変わる)の時、ActivityDateModifiedDate__cは今日 System.assertEquals(Date.today(), e.ActivityDateModifiedDate__c); // ②活動日変わって、フラグ再設定、過去なので計画ではない System.assertEquals(false, e.IsScheduled__c); e.StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0); e.EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 11, 0, 0); update e; e = [select IsScheduled__c, ActivityDateModifiedDate__c from Event where Id = :e.Id]; // ③更新(活動日変わる)の時、ActivityDateModifiedDate__cは今日 System.assertEquals(Date.today(), e.ActivityDateModifiedDate__c); // ②活動日変わって、フラグ再設定、未来なので計画です System.assertEquals(true, e.IsScheduled__c); } static testMethod void test03() { Date t = Date.today(); Date t1 = t.addMonths(6); Date t2 = t.addMonths(-6); Date t3 = t.addYears(1); Daily_Report__c dr1 = new Daily_Report__c( Reported_Date__c = t1, Status__c = '作成中', Reporter__c = Userinfo.getUserId() ); Daily_Report__c dr2 = new Daily_Report__c( Reported_Date__c = t2, Status__c = '作成中', Reporter__c = Userinfo.getUserId() ); insert new Daily_Report__c[] {dr1, dr2}; Event__c ec1 = new Event__c( Daily_Report__c = dr1.Id, StartDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0), EndDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 11, 0, 0), ActivityDate__c = dr1.Reported_Date__c, Subject__c = 'eventc1' ); insert ec1; Event__c ec2 = new Event__c( Daily_Report__c = dr2.Id, StartDateTime__c = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 10, 0, 0), EndDateTime__c = Datetime.newInstance(t2.year(), t2.month(), t2.day(), 11, 0, 0), ActivityDate__c = dr2.Reported_Date__c, Subject__c = 'eventc2' ); insert ec2; // 未来EventCなので、計画立つ ec1 = [select IsScheduled__c, StartDateTime__c, IsScheduled_StartDateTime__c, EndDateTime__c, IsScheduled_EndDateTime__c, Location__c, IsScheduled_Location__c, Subject__c, IsScheduled_Subject__c from Event__c where Id = :ec1.Id]; System.assertEquals(true, ec1.IsScheduled__c); System.assertEquals(ec1.StartDateTime__c, ec1.IsScheduled_StartDateTime__c); System.assertEquals(ec1.EndDateTime__c, ec1.IsScheduled_EndDateTime__c); System.assertEquals(ec1.Location__c, ec1.IsScheduled_Location__c); System.assertEquals(ec1.Subject__c, ec1.IsScheduled_Subject__c); // 過去EventCなので、計画なし ec2 = [select IsScheduled__c, StartDateTime__c, IsScheduled_StartDateTime__c, EndDateTime__c, IsScheduled_EndDateTime__c, Location__c, IsScheduled_Location__c, Subject__c, IsScheduled_Subject__c from Event__c where Id = :ec2.Id]; System.assertEquals(false, ec2.IsScheduled__c); System.assertEquals(null, ec2.IsScheduled_StartDateTime__c); System.assertEquals(null, ec2.IsScheduled_EndDateTime__c); System.assertEquals(null, ec2.IsScheduled_Location__c); System.assertEquals(null, ec2.IsScheduled_Subject__c); // --------------------Event__cより作成したEventの更新-------------------- StaticParameter.NotUpdEventCFlg = false; Event e2 = [select Id from Event where EventC_ID__c = :ec2.Id]; e2.ActivityDateModifiedDate__c = Date.newInstance(t2.year(), t2.month(), t2.day()); e2.StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 15, 0, 0); e2.EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 18, 0, 0); update e2; // Eventを未来日に変更、計画立つ ec2 = [select IsScheduled__c, StartDateTime__c, IsScheduled_StartDateTime__c, EndDateTime__c, IsScheduled_EndDateTime__c, Location__c, IsScheduled_Location__c, Subject__c, IsScheduled_Subject__c, Daily_Report__c from Event__c where Id = :ec2.Id]; System.assertEquals(true, ec2.IsScheduled__c); System.assertEquals(ec2.StartDateTime__c, ec2.IsScheduled_StartDateTime__c); System.assertEquals(ec2.EndDateTime__c, ec2.IsScheduled_EndDateTime__c); System.assertEquals(ec2.Location__c, ec2.IsScheduled_Location__c); System.assertEquals(ec2.Subject__c, ec2.IsScheduled_Subject__c); System.assertEquals(dr1.Id, ec2.Daily_Report__c); e2 = [select ActivityDateModifiedDate__c from Event where EventC_ID__c = :ec2.Id]; System.assertEquals(Date.today(), e2.ActivityDateModifiedDate__c); // 変更不可のエラーをキャッチ e2.StartDateTime = Datetime.newInstance(t3.year(), t3.month(), t3.day(), 15, 0, 0); e2.EndDateTime = Datetime.newInstance(t3.year(), t3.month(), t3.day(), 16, 0, 0); try { update e2; } catch (Exception exp) { System.assertEquals(true, true); } } static testMethod void test04() { Date t = Date.today(); Date t1 = t.addMonths(6); Daily_Report__c dr1 = new Daily_Report__c( Reported_Date__c = t1, Status__c = '作成中', Reporter__c = Userinfo.getUserId() ); insert dr1; Event__c ec1 = new Event__c( Daily_Report__c = dr1.Id, StartDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0), EndDateTime__c = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 11, 0, 0), ActivityDate__c = dr1.Reported_Date__c, Subject__c = 'eventc1' ); insert ec1; StaticParameter.NotUpdEventCFlg = false; Event e1 = [select Id from Event where EventC_ID__c = :ec1.Id]; e1.StartDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 9, 0, 0); e1.EndDateTime = Datetime.newInstance(t1.year(), t1.month(), t1.day(), 18, 0, 0); update e1; ec1 = [select IsScheduled__c, StartDateTime__c, EndDateTime__c, IsScheduled_StartDateTime__c, IsScheduled_EndDateTime__c from Event__c where Id = :ec1.Id]; System.assertEquals(true, ec1.IsScheduled__c); System.assertEquals(Datetime.newInstance(t1.year(), t1.month(), t1.day(), 10, 0, 0), ec1.IsScheduled_StartDateTime__c); System.assertEquals(Datetime.newInstance(t1.year(), t1.month(), t1.day(), 11, 0, 0), ec1.IsScheduled_EndDateTime__c); System.assertNotEquals(ec1.IsScheduled_StartDateTime__c, ec1.StartDateTime__c); System.assertNotEquals(ec1.IsScheduled_EndDateTime__c, ec1.EndDateTime__c); } }