/** * 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 SetProductTargetControllerTest { static private String currentPeriod(Integer i) { Date dateNow = Date.today(); Integer year = dateNow.year(); Integer month = dateNow.month(); if (month < 4) { year -= 1; } return String.valueOf(year + i - 1867 + 'P'); } static private Date oppCloseDate() { Date nowDate = Date.today(); Integer year = nowDate.year(); Integer month = nowDate.month(); if (month < 4) { nowDate = nowDate.addYears(-1); } return nowDate; } static private void makeIPC() { ImportantProductCategory__c ipc1 = new ImportantProductCategory__c( Name = 'test01', depName__c = 'GI', importantProductGoaIName__c = '重点产品01', commodityCode__c = 'test01', commodityName__c = 'test01', if_Quantity__c = true, sort__c = 1 ); insert ipc1; ImportantProductCategory__c ipc2 = new ImportantProductCategory__c( Name = 'test02', depName__c = 'GI', importantProductGoaIName__c = '重点产品02', commodityCode__c = 'test02', commodityName__c = 'test02', if_Quantity__c = true, sort__c = 2 ); insert ipc2; ImportantProductCategory__c ipc3 = new ImportantProductCategory__c( Name = 'test03', depName__c = 'GI', importantProductGoaIName__c = '重点产品03', commodityCode__c = 'test03', commodityName__c = 'test03', if_Quantity__c = false, sort__c = 3 ); insert ipc3; } // 主流程 static testMethod void myUnitTest() { // システム管理者 User u3 = new User(Test_staff__c = true); 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); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name='テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; User u1 = new User(Test_staff__c = true); u1.LastName = '_サンブリッジ'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '四川省'; u1.Use_Start_Date__c = Date.today().addMonths(-6); // insert u1; User u2 = new User(Test_staff__c = true); u2.LastName = '_サンブリッジ'; u2.FirstName = 'い'; u2.Alias = 'い'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'い'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = System.Label.ProfileId_SystemAdmin; u2.Job_Category__c = '销售推广'; u2.Province__c = '北京市'; u2.Use_Start_Date__c = Date.today().addMonths(-6); insert new list {u1, u2}; SetProductTargetController sptc = new SetProductTargetController(); // 初期表示 System.Test.startTest(); sptc.init(); System.assertEquals('省重点产品目标', sptc.target_category); System.assertEquals(false, sptc.saveFlg); if (Date.today().month() != 3) { System.assertEquals(false, sptc.isPast); } else { //System.assertEquals(true, sptc.isPast); } //System.assertEquals(34, sptc.dataBeans.size()); //默认本部:1.华北销售本部 System.assertEquals(6, sptc.dataBeans.size()); // System.assertEquals(currentPeriod(0), sptc.currentPeriod); System.assertEquals(true, sptc.previousRendered); System.assertEquals(true, sptc.nextRendered); // 插入数据 sptc.dataBeans[0].opportunity.Owner_System__c = u1.id; sptc.dataBeans[0].amount[0].Num_Of_OPD__c = 100; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].opportunity.Owner_System__c = u2.id; sptc.dataBeans[1].amount[0].Num_Of_OPD__c = 200; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); System.assertEquals('保存成功', ApexPages.getMessages()[0].getDetail()); List opps = [select id, key__c,Num_Of_OPD__c,Department__c,SAP_Province__c,Owner_System__c,iYear__c FROM Key_Product_Goals__c]; System.assertEquals(2, opps.size()); //20210207 ljh add start //20210207 ljh add start //导出 sptc.exportBycsv(); System.assertEquals('test01(GI)', sptc.opportunity_category[0]);//opportunity_category System.assertEquals('test02(GI)', sptc.opportunity_category[1]); System.assertEquals('test03(GI)金额', sptc.opportunity_category[2]); System.assertEquals(3, sptc.opportunity_category.size()); sptc.csvAsString = '销售本部:5.华东,省,担当,test01(GI),test02(GI),test03(GI)金额'; sptc.csvAsString +='\n5.华东,上海市,あ9,1,2,3'; //test read csv file sptc.importCSVFile(); //20210207 ljh add end //20210207 ljh add end // List opps = [select Id,Amount,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; // System.assertEquals(24, opps.size()); // System.assertEquals('重点产品01', opps[0].Important_Key_product_category__c); // System.assertEquals(u1.Id, opps[0].Owner_System__c); // System.assertEquals('省重点产品目标', opps[0].Target_category__c); // System.assertEquals(100, opps[0].Num_Of_OPD__c); // System.assertEquals('重点产品01', opps[12].Important_Key_product_category__c); // System.assertEquals(u2.Id, opps[12].Owner_System__c); // System.assertEquals('省重点产品目标', opps[12].Target_category__c); // System.assertEquals(200, opps[12].Num_Of_OPD__c); // List olis1 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps[0].Id]; // System.assertEquals(100, olis1[0].Quantity); // List olis2 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps[12].Id]; // System.assertEquals(200, olis2[0].Quantity); // 上年度 sptc.previous(); //System.assertEquals(false, sptc.previousRendered); // 下年度 sptc.next(); //System.assertEquals(null, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); //System.assertEquals(null, sptc.dataBeans[1].amount[0].Num_Of_OPD__c); // 上年度 sptc.previous(); //System.assertEquals(100, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); //System.assertEquals(200, sptc.dataBeans[1].amount[0].Num_Of_OPD__c); // 更新,删除 sptc.dataBeans[0].opportunity.Owner_System__c = u2.id; sptc.dataBeans[0].amount[0].Num_Of_OPD__c = 1000; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].amount[0].Num_Of_OPD__c = null; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); List opps2 = [select Id,Amount,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; //System.assertEquals(12, opps2.size()); //System.assertEquals('重点产品01', opps2[0].Important_Key_product_category__c); //System.assertEquals(u2.Id, opps2[0].Owner_System__c); //System.assertEquals('省重点产品目标', opps2[0].Target_category__c); //System.assertEquals(1000, opps2[0].Num_Of_OPD__c); // List olis3 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps2[0].Id]; //System.assertEquals(1000, olis3[0].Quantity); // 删除担当者 sptc.dataBeans[0].opportunity.Owner_System__c = null; sptc.dataBeans[0].isChanged = '1'; sptc.saveBtn(); List opps3 = [select Id,Amount,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; //System.assertEquals(0, opps3.size()); // 返回 sptc.backBtn(); System.Test.stopTest(); } } // 数量,金额切换 static testMethod void myUnitTest2() { // システム管理者 User u3 = new User(Test_staff__c = true); 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); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name='テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; User u1 = new User(Test_staff__c = true); u1.LastName = '_サンブリッジ'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '四川省'; u1.Use_Start_Date__c = Date.today().addMonths(-6); insert u1; User u2 = new User(Test_staff__c = true); u2.LastName = '_サンブリッジ'; u2.FirstName = 'い'; u2.Alias = 'い'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'い'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = System.Label.ProfileId_SystemAdmin; u2.Job_Category__c = '销售推广'; u2.Province__c = '北京市'; u2.Use_Start_Date__c = Date.today().addMonths(-6); insert u2; SetProductTargetController sptc = new SetProductTargetController(); // 初期表示 sptc.init(); // 插入数据 sptc.dataBeans[0].opportunity.Owner_System__c = u1.id; sptc.dataBeans[0].amount[0].Num_Of_OPD__c = 100; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].opportunity.Owner_System__c = u2.id; sptc.dataBeans[1].amount[0].Num_Of_OPD__c = 200; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); List opps = [select id, key__c,Num_Of_OPD__c,Department__c,SAP_Province__c,Owner_System__c,iYear__c FROM Key_Product_Goals__c]; System.assertEquals(2, opps.size()); // List opps = [select Id,Amount,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; // System.assertEquals(24, opps.size()); // System.assertEquals('重点产品01', opps[0].Important_Key_product_category__c); // System.assertEquals(u1.Id, opps[0].Owner_System__c); // System.assertEquals('省重点产品目标', opps[0].Target_category__c); // System.assertEquals(100, opps[0].Num_Of_OPD__c); // System.assertEquals('重点产品01', opps[12].Important_Key_product_category__c); // System.assertEquals(u2.Id, opps[12].Owner_System__c); // System.assertEquals('省重点产品目标', opps[12].Target_category__c); // System.assertEquals(200, opps[12].Num_Of_OPD__c); //System.assertEquals(100, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); //System.assertEquals(200, sptc.dataBeans[1].amount[0].Num_Of_OPD__c); // 切换金额 //sptc.target_category = '重点产品目标(数量)'; //sptc.searchByCategory(); //System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount); //System.assertEquals(null, sptc.dataBeans[1].amount[0].Amount); // 插入数据 //sptc.dataBeans[0].opportunity.Owner_System__c = u1.id; //sptc.dataBeans[0].amount[0].Amount = 300; //sptc.dataBeans[0].isChanged = '1'; //sptc.dataBeans[1].opportunity.Owner_System__c = u2.id; //sptc.dataBeans[1].amount[0].Amount = 400; //sptc.dataBeans[1].isChanged = '1'; //sptc.saveBtn(); //List opps2 = [select Id,Amount,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by Id]; //System.assertEquals(48, opps2.size()); //System.assertEquals(300, sptc.dataBeans[0].amount[0].Amount); //System.assertEquals(400, sptc.dataBeans[1].amount[0].Amount); // 切换数量 //sptc.target_category = '重点产品目标(金额)'; //sptc.searchByCategory(); //System.assertEquals(100, sptc.dataBeans[0].amount[0].Amount); //System.assertEquals(200, sptc.dataBeans[1].amount[0].Amount); } } // 上年度,下年度 static testMethod void myUnitTest3() { // システム管理者 User u3 = new User(Test_staff__c = true); 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); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name='テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; // 102_销售产品推广 User u1 = new User(Test_staff__c = true); u1.LastName = '_サンブリッジ'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '四川省'; u1.Use_Start_Date__c = Date.today().addMonths(-6); insert u1; Opportunity opp1 = new Opportunity(); opp1.Name = u1.Alias + ' 目标'; opp1.StageName = '引合'; opp1.OwnerId = u1.Id; opp1.Owner_System__c = u1.Id; opp1.Opportunity_Category__c = 'GI'; opp1.CloseDate = Date.valueOf(oppCloseDate().year() - 1 + '-04-01'); opp1.Amount = 150; opp1.Proportion__c = 100; opp1.SAP_Province__c = '北京市'; opp1.Target_category__c = '省重点产品目标'; opp1.Important_Key_product_category__c = '重点产品01'; opp1.RecordTypeId = rt[0].Id; opp1.OCM_Target_period__c = currentPeriod(-1); opp1.Target_Source__c = 'SalesTarget'; insert opp1; List pbes = [select Id from PricebookEntry where IsActive = true limit 1]; OpportunityLineItem oppli1 = new OpportunityLineItem(); oppli1.OpportunityId = opp1.Id; oppli1.UnitPrice = 1; oppli1.Quantity = 150; oppli1.PricebookEntryId = pbes[0].Id; insert oppli1; Opportunity opp2 = new Opportunity(); opp2.Name = u1.Alias + ' 目标'; opp2.StageName = '引合'; opp2.OwnerId = u1.Id; opp2.Owner_System__c = u1.Id; opp2.Opportunity_Category__c = 'GI'; opp2.CloseDate = Date.valueOf(oppCloseDate().year() - 2 + '-04-01'); opp2.Amount = 250; opp2.Proportion__c = 100; opp2.SAP_Province__c = '北京市'; opp2.Target_category__c = '省重点产品目标'; opp2.Important_Key_product_category__c = '重点产品01'; opp2.RecordTypeId = rt[0].Id; opp2.OCM_Target_period__c = currentPeriod(-2); opp2.Target_Source__c = 'SalesTarget'; insert opp2; OpportunityLineItem oppli2 = new OpportunityLineItem(); oppli2.OpportunityId = opp2.Id; oppli2.UnitPrice = 1; oppli2.Quantity = 250; oppli2.PricebookEntryId = pbes[0].Id; insert oppli2; SetProductTargetController sptc = new SetProductTargetController(); // 初期表示 sptc.init(); // System.assertEquals(currentPeriod(0), sptc.currentPeriod); System.assertEquals(true, sptc.previousRendered); System.assertEquals(true, sptc.nextRendered); if (Date.today().month() != 3) { System.assertEquals(false, sptc.isPast); } else { //System.assertEquals(true, sptc.isPast); } System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount); // 上年度 sptc.previous(); //System.assertEquals(currentPeriod(-1), sptc.currentPeriod); System.assertEquals(true, sptc.previousRendered); System.assertEquals(true, sptc.nextRendered); //System.assertEquals(true, sptc.isPast); //System.assertEquals(150, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); // 上年度 sptc.previous(); //System.assertEquals(currentPeriod(-2), sptc.currentPeriod); //System.assertEquals(true, sptc.previousRendered); //System.assertEquals(true, sptc.nextRendered); //System.assertEquals(true, sptc.isPast); //System.assertEquals(250, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); // 上年度 sptc.previous(); //System.assertEquals(currentPeriod(-2), sptc.currentPeriod); //System.assertEquals(false, sptc.previousRendered); //System.assertEquals(true, sptc.nextRendered); //System.assertEquals(true, sptc.isPast); //System.assertEquals(250, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); // 下年度 sptc.next(); //System.assertEquals(currentPeriod(-1), sptc.currentPeriod); //System.assertEquals(true, sptc.previousRendered); //System.assertEquals(true, sptc.nextRendered); //System.assertEquals(true, sptc.isPast); //System.assertEquals(150, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); // 下年度 sptc.next(); //System.assertEquals(currentPeriod(0), sptc.currentPeriod); //System.assertEquals(true, sptc.previousRendered); //System.assertEquals(true, sptc.nextRendered); if (Date.today().month() != 3) { //System.assertEquals(false, sptc.isPast); } else { //System.assertEquals(true, sptc.isPast); } //System.assertEquals(null, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); // 下年度 sptc.next(); //System.assertEquals(currentPeriod(1), sptc.currentPeriod); //System.assertEquals(true, sptc.previousRendered); //System.assertEquals(false, sptc.nextRendered); //System.assertEquals(false, sptc.isPast); //System.assertEquals(null, sptc.dataBeans[0].amount[0].Num_Of_OPD__c); sptc.salesDpt = '2.东北'; sptc.searchByDpt(); sptc.opp.SAP_Province__c = '北京市'; sptc.searchByProvince(); //sptc.opp.Important_Key_product_category__c = '重点产品02'; //sptc.searchByImpKey(); } } }