@isTest private class OrderPdfControllerTest { @TestSetup static void setup(){ TestDataUtility.CreatePIPolicyConfigurations(new string[]{'Opportunity','Contact','Order','Document'}); } @isTest static void test_init() { List rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; List rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE']; Account buyer = new Account( Name = '*', FacilityName__c = 'buyer', PostCode__c = '123456', RecordTypeId = rectDealer[0].Id, ProductSegment__c = 'IE' ); Account delivery = new Account( Name = '*', FacilityName__c = 'delivery', PostCode__c = '123456' ); Account user = new Account( Name = '*', FacilityName__c = 'user', PostCode__c = '123456', RecordTypeId = rectIE[0].Id, ProductSegment__c = 'IE' ); insert new List {buyer, delivery, user}; Contact delivery_contact = new Contact( LastName = 'delivery_contact', AccountId = delivery.Id ); Contact contact = new Contact( LastName = 'contact', AccountId = user.Id ); insert new List {delivery_contact, contact}; Id pricebookId = Test.getStandardPricebookId(); Pricebook2 pricebook = new Pricebook2( Name = 'IE', ProductSegment__c = 'IE', TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', MachineParts__c = 'Machine', isActive = true, CurrencyIsoCode = 'USD' ); insert pricebook; Product2 product1 = new Product2(); product1.Name = 'product1'; product1.ProductCode = 'product1'; product1.Product_ECCode__c = 'product1'; insert product1; PricebookEntry standardPrice1 = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = product1.Id, UnitPrice = 0, IsActive = true, CurrencyIsoCode = 'USD' ); insert standardPrice1; PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id); entry1.UnitPrice = 0; entry1.IsActive = true; entry1.UseStandardPrice = false; entry1.CurrencyIsoCode = 'USD'; insert entry1; Opportunity opp = new Opportunity( Name = 'test opp', AccountId = user.Id, StageName = 'Phase3', CurrencyIsoCode = 'USD', ProductSegment__c = 'IE', CloseDate = Date.today(), NewInquiryDate__c = Date.today().addDays(-2), ExpectedOrderDate__c = Date.today().addDays(2), TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', Machine_Parts__c = 'Machine', Dealer__c = buyer.Id, SpecialDeliveryAddress__c = delivery.Id, SpecialDeliveryContact__c = delivery_contact.Id, Pricebook2Id = pricebook.Id ); insert opp; OpportunityLineItem oli = new OpportunityLineItem( OpportunityId = opp.Id, PricebookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); insert oli; OpportunityContactRole ocr = new OpportunityContactRole( Role = 'End user', OpportunityId = opp.Id, ContactId = contact.Id, IsPrimary = true ); insert ocr; List rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract']; Order order = new Order( Name = 'order', CurrencyIsoCode = 'USD', AccountId = user.Id, SpecialDeliveryAccount__c = user.Id, SpecialDeliveryAccount_D__c = user.Id, SpecialDeliveryContact2_D__c = contact.Id, OpportunityId = opp.Id, EffectiveDate = Date.today(), ProductListApproveStatus__c = 'Draft',RecordTypeId=rectOdr[0].Id, Status = 'Draft', Pricebook2Id = pricebook.Id, EndUser__c = contact.Id, EndUserD__c = contact.Id ); insert order; List oiList = new List(); for (Integer i = 0; i < 1; i++) { OrderItem oi = new OrderItem( OrderId = order.Id, PriceBookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); oiList.add(oi); } insert oiList; Test.startTest(); PageReference page = new PageReference('/apex/OrderContract?id=' + order.Id); System.Test.setCurrentPage(page); OrderPdfController controller = new OrderPdfController(); controller.init(); controller.saveBtn(); controller.printBtn(); Test.stopTest(); } @isTest static void test_init2() { List rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; Account parent = new Account( Name = '*', FacilityName__c = 'parent', PostCode__c = '123456' ); insert parent; Account buyer = new Account( Name = '*', FacilityName__c = 'buyer', PostCode__c = '123456', RecordTypeId = rectDealer[0].Id, ParentId = parent.Id ); Account delivery = new Account( Name = '*', FacilityName__c = 'delivery', PostCode__c = '123456' ); Account user = new Account( Name = '*', FacilityName__c = 'user', PostCode__c = '123456' ); insert new List {buyer, delivery, user}; Contact delivery_contact = new Contact( LastName = 'delivery_contact', AccountId = delivery.Id ); Contact contact = new Contact( LastName = 'contact', AccountId = user.Id ); insert new List {delivery_contact, contact}; Id pricebookId = Test.getStandardPricebookId(); Pricebook2 pricebook = new Pricebook2( Name = 'BS', ProductSegment__c = 'BS', TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', MachineParts__c = 'Machine', isActive = true, CurrencyIsoCode = 'USD' ); insert pricebook; Product2 product1 = new Product2(); product1.Name = 'product1'; product1.ProductCode = 'product1'; product1.Product_ECCode__c = 'product1'; product1.IsActive = true; insert product1; PricebookEntry standardPrice1 = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = product1.Id, UnitPrice = 0, IsActive = true, CurrencyIsoCode = 'USD' ); insert standardPrice1; PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id); entry1.UnitPrice = 0; entry1.IsActive = true; entry1.UseStandardPrice = false; entry1.CurrencyIsoCode = 'USD'; insert entry1; Opportunity opp = new Opportunity( Name = 'test opp', AccountId = user.Id, StageName = 'Phase3', CurrencyIsoCode = 'USD', ProductSegment__c = 'BS', CloseDate = Date.today(), NewInquiryDate__c = Date.today().addDays(-2), ExpectedOrderDate__c = Date.today().addDays(2), TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', Machine_Parts__c = 'Machine', Dealer__c = buyer.Id, SpecialDeliveryAddress__c = delivery.Id, SpecialDeliveryContact__c = delivery_contact.Id, Pricebook2Id = pricebook.Id ); insert opp; Quote quo = new Quote( Name = 'quo', OpportunityId = opp.Id, Pricebook2Id = pricebook.Id, SetName1__c = 'setname01', SetQty1__c = 1 ); insert quo; QuoteLineItem qli = new QuoteLineItem( QuoteId = quo.Id, PricebookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10, Set__c = 'set01' ); insert qli; opp.SyncedQuoteId = quo.Id; update opp; OpportunityContactRole ocr = new OpportunityContactRole( Role = 'End user', OpportunityId = opp.Id, ContactId = contact.Id, IsPrimary = true ); insert ocr; List rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract']; Order order = new Order( Name = 'order', CurrencyIsoCode = 'USD', AccountId = user.Id, SpecialDeliveryAccount__c = user.Id, SpecialDeliveryAccount_D__c = user.Id, SpecialDeliveryContact2_D__c = contact.Id, OpportunityId = opp.Id, EffectiveDate = Date.today(), ProductListApproveStatus__c = 'Draft',RecordTypeId=rectOdr[0].Id, Status = 'Draft', Pricebook2Id = pricebook.Id, EndUser__c = contact.Id, EndUserD__c = contact.Id ); insert order; List oiList = new List(); for (Integer i = 0; i < 1; i++) { OrderItem oi = new OrderItem( OrderId = order.Id, PriceBookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); oiList.add(oi); } insert oiList; Test.startTest(); PageReference page = new PageReference('/apex/OrderPdf?id=' + order.Id); System.Test.setCurrentPage(page); OrderPdfController controller = new OrderPdfController(); //controller.init(); controller.init2(); //controller.init(); Test.stopTest(); } @isTest static void test_init2_2() { List rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; Account parent = new Account( Name = '*', FacilityName__c = 'parent', PostCode__c = '123456' ); insert parent; Account buyer = new Account( Name = '*', FacilityName__c = 'buyer', PostCode__c = '123456', RecordTypeId = rectDealer[0].Id, ParentId = parent.Id ); Account delivery = new Account( Name = '*', FacilityName__c = 'delivery', PostCode__c = '123456' ); Account user = new Account( Name = '*', FacilityName__c = 'user', PostCode__c = '123456' ); insert new List {buyer, delivery, user}; Contact delivery_contact = new Contact( LastName = 'delivery_contact', AccountId = delivery.Id ); Contact contact = new Contact( LastName = 'contact', AccountId = user.Id ); insert new List {delivery_contact, contact}; Id pricebookId = Test.getStandardPricebookId(); Pricebook2 pricebook = new Pricebook2( Name = 'NDT', ProductSegment__c = 'NDT', TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', MachineParts__c = 'Machine', isActive = true, CurrencyIsoCode = 'USD' ); insert pricebook; Product2 product1 = new Product2(); product1.Name = 'product1'; product1.ProductCode = 'product1'; product1.Product_ECCode__c = 'product1'; product1.IsActive = true; insert product1; PricebookEntry standardPrice1 = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = product1.Id, UnitPrice = 0, IsActive = true, CurrencyIsoCode = 'USD' ); insert standardPrice1; PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id); entry1.UnitPrice = 0; entry1.IsActive = true; entry1.UseStandardPrice = false; entry1.CurrencyIsoCode = 'USD'; insert entry1; Opportunity opp = new Opportunity( Name = 'test opp', AccountId = user.Id, StageName = 'Phase3', CurrencyIsoCode = 'USD', ProductSegment__c = 'NDT', CloseDate = Date.today(), NewInquiryDate__c = Date.today().addDays(-2), ExpectedOrderDate__c = Date.today().addDays(2), TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', Machine_Parts__c = 'Machine', Dealer__c = buyer.Id, SpecialDeliveryAddress__c = delivery.Id, SpecialDeliveryContact__c = delivery_contact.Id, Pricebook2Id = pricebook.Id ); insert opp; OpportunityLineItem oli = new OpportunityLineItem( OpportunityId = opp.Id, PricebookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); insert oli; OpportunityContactRole ocr = new OpportunityContactRole( Role = 'End user', OpportunityId = opp.Id, ContactId = contact.Id, IsPrimary = true ); insert ocr; List rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract']; Order order = new Order( Name = 'order', CurrencyIsoCode = 'USD', AccountId = user.Id, SpecialDeliveryAccount__c = user.Id, SpecialDeliveryAccount_D__c = user.Id, SpecialDeliveryContact2_D__c = contact.Id, OpportunityId = opp.Id, EffectiveDate = Date.today(), ProductListApproveStatus__c = 'Draft',RecordTypeId=rectOdr[0].Id, Status = 'Draft', Pricebook2Id = pricebook.Id, EndUser__c = contact.Id, EndUserD__c = contact.Id ); insert order; List oiList = new List(); for (Integer i = 0; i < 1; i++) { OrderItem oi = new OrderItem( OrderId = order.Id, PriceBookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); oiList.add(oi); } insert oiList; Test.startTest(); PageReference page = new PageReference('/apex/OrderPdf?id=' + order.Id); System.Test.setCurrentPage(page); OrderPdfController controller = new OrderPdfController(); controller.init2(); Test.stopTest(); } @isTest static void test_init2_3() { List rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; List rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer NDT']; Account buyer = new Account( Name = '*', FacilityName__c = 'buyer', PostCode__c = '123456', RecordTypeId = rectDealer[0].Id, ProductSegment__c = 'NDT' ); Account delivery = new Account( Name = '*', FacilityName__c = 'delivery', PostCode__c = '123456', ProductSegment__c = 'NDT' ); Account user = new Account( Name = '*', FacilityName__c = 'user', PostCode__c = '123456', RecordTypeId = rectIE[0].Id, ProductSegment__c = 'NDT' ); insert new List {buyer, delivery, user}; Contact delivery_contact = new Contact( LastName = 'delivery_contact', AccountId = delivery.Id ); Contact contact = new Contact( LastName = 'contact', AccountId = user.Id ); insert new List {delivery_contact, contact}; Id pricebookId = Test.getStandardPricebookId(); Pricebook2 pricebook = new Pricebook2( Name = 'NDT', ProductSegment__c = 'NDT', TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', MachineParts__c = 'Machine', isActive = true, CurrencyIsoCode = 'USD' ); insert pricebook; Product2 product1 = new Product2(); product1.Name = 'product1'; product1.ProductCode = 'product1'; product1.Product_ECCode__c = 'product1'; insert product1; PricebookEntry standardPrice1 = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = product1.Id, UnitPrice = 0, IsActive = true, CurrencyIsoCode = 'USD' ); insert standardPrice1; PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id); entry1.UnitPrice = 0; entry1.IsActive = true; entry1.UseStandardPrice = false; entry1.CurrencyIsoCode = 'USD'; insert entry1; Opportunity opp = new Opportunity( Name = 'test opp', AccountId = user.Id, StageName = 'Phase3', CurrencyIsoCode = 'USD', ProductSegment__c = 'NDT', CloseDate = Date.today(), NewInquiryDate__c = Date.today().addDays(-2), ExpectedOrderDate__c = Date.today().addDays(2), TradeType__c = 'Tax Exemption', SalesChannel__c = 'direct', Machine_Parts__c = 'Machine', Dealer__c = buyer.Id, SpecialDeliveryAddress__c = delivery.Id, SpecialDeliveryContact__c = delivery_contact.Id, Pricebook2Id = pricebook.Id ); insert opp; OpportunityLineItem oli = new OpportunityLineItem( OpportunityId = opp.Id, PricebookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); insert oli; OpportunityContactRole ocr = new OpportunityContactRole( Role = 'End user', OpportunityId = opp.Id, ContactId = contact.Id, IsPrimary = true ); insert ocr; List rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract']; Order order = new Order( Name = 'order', CurrencyIsoCode = 'USD', AccountId = user.Id, SpecialDeliveryAccount__c = user.Id, SpecialDeliveryAccount_D__c = user.Id, SpecialDeliveryContact2_D__c = contact.Id, OpportunityId = opp.Id, EffectiveDate = Date.today(), ProductListApproveStatus__c = 'Draft',RecordTypeId=rectOdr[0].Id, Status = 'Draft', Pricebook2Id = pricebook.Id, EndUser__c = contact.Id, EndUserD__c = contact.Id ); insert order; List oiList = new List(); for (Integer i = 0; i < 1; i++) { OrderItem oi = new OrderItem( OrderId = order.Id, PriceBookEntryId = entry1.Id, Quantity = 1, UnitPrice = 10 ); oiList.add(oi); } insert oiList; Test.startTest(); PageReference page = new PageReference('/apex/OrderContract?id=' + order.Id); System.Test.setCurrentPage(page); OrderPdfController controller = new OrderPdfController(); controller.init(); controller.saveBtn(); controller.printBtn(); Test.stopTest(); } }