@@ -732,7 +732,9 @@ private function preparePreconditionsForCreateOrder(
732
732
/** Unset fake IDs for default billing and shipping customer addresses */
733
733
/** @var Customer $customer */
734
734
$ customer = $ this ->objectManager ->create (Customer::class);
735
- $ customer ->load ($ customerIdFromFixture )->setDefaultBilling (null )->setDefaultShipping (null )->save ();
735
+ if (empty ($ orderData ['checkForDefaultStreet ' ])){
736
+ $ customer ->load ($ customerIdFromFixture )->setDefaultBilling (null )->setDefaultShipping (null )->save ();
737
+ }
736
738
} else {
737
739
/**
738
740
* Customer ID must be set to session to pass \Magento\Sales\Model\AdminOrder\Create::_validate()
@@ -840,4 +842,94 @@ private function getValidAddressData()
840
842
'vat_id ' => ''
841
843
];
842
844
}
845
+
846
+ /**
847
+ * @magentoDataFixture Magento/Customer/_files/customer.php
848
+ * @magentoDataFixture Magento/Customer/_files/customer_address_attribute_update.php
849
+ * @magentoDbIsolation disabled
850
+ */
851
+ public function testSetBillingAddressStreetValidationErrors ()
852
+ {
853
+ $ customerIdFromFixture = 1 ;
854
+ /** @var SessionQuote $session */
855
+ $ session = $ this ->objectManager ->create (SessionQuote::class);
856
+ $ session ->setCustomerId ($ customerIdFromFixture );
857
+ $ invalidAddressData = array_merge ($ this ->getValidAddressData (), ['street ' => [0 => 'Whit`e ' , 1 => 'Lane ' ]]);
858
+ /**
859
+ * Note that validation errors are collected during setBillingAddress() call in the internal class variable,
860
+ * but they are not set to message manager at this step.
861
+ * They are set to message manager only during createOrder() call.
862
+ */
863
+ $ this ->model ->setIsValidate (true )->setBillingAddress ($ invalidAddressData );
864
+ try {
865
+ $ this ->model ->createOrder ();
866
+ $ this ->fail ('Validation errors are expected to lead to exception during createOrder() call. ' );
867
+ } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
868
+ /** createOrder is expected to throw exception with empty message when validation error occurs */
869
+ }
870
+ $ errorMessages = [];
871
+ /** @var $validationError \Magento\Framework\Message\Error */
872
+ foreach ($ this ->messageManager ->getMessages ()->getItems () as $ validationError ) {
873
+ $ errorMessages [] = $ validationError ->getText ();
874
+ }
875
+ self ::assertTrue (
876
+ in_array ('Billing Address: "Street Address" contains non-alphabetic or non-numeric characters. ' , $ errorMessages ),
877
+ 'Expected validation message is absent. '
878
+ );
879
+ self ::assertTrue (
880
+ in_array ('Shipping Address: "Street Address" contains non-alphabetic or non-numeric characters. ' , $ errorMessages ),
881
+ 'Expected validation message is absent. '
882
+ );
883
+ }
884
+
885
+ /**
886
+ * If the current street address for a customer differs with the default one (saved in customer address book)
887
+ * then the updated validation rule (`input_validation`) should applied on current one while placing a new order.
888
+ *
889
+ * @magentoDataFixture Magento/Customer/_files/customer_address_street_attribute.php
890
+ * @magentoDbIsolation disabled
891
+ * @magentoAppIsolation enabled
892
+ */
893
+ public function testCreateOrderExistingCustomerWhenDefaultAddressDiffersWithNew ()
894
+ {
895
+ $ productIdFromFixture = 1 ;
896
+ $ customerIdFromFixture = 1 ;
897
+ $ customerEmailFromFixture =
'[email protected] ' ;
898
+ $ shippingMethod = 'freeshipping_freeshipping ' ;
899
+ $ paymentMethod = 'checkmo ' ;
900
+ $ shippingAddressAsBilling = 1 ;
901
+ $ invalidAddressData = array_merge ($ this ->getValidAddressData (), ['street ' => [0 => 'White ' , 1 => 'Lane ' ]]);
902
+
903
+ // Optionally, to bypass default customer address validation, just set `customer_address_id` to `null` in billingAddress.
904
+ $ address = array_merge ($ invalidAddressData , ['save_in_address_book ' => '1 ' , 'customer_address_id ' => 1 ]);
905
+ $ orderData = [
906
+ 'currency ' => 'USD ' ,
907
+ 'billing_address ' => $ address ,
908
+ 'shipping_method ' => $ shippingMethod ,
909
+ 'comment ' => ['customer_note ' => '' ],
910
+ 'send_confirmation ' => false ,
911
+ 'checkForDefaultStreet ' => true ,
912
+ ];
913
+ $ paymentData = ['method ' => $ paymentMethod ];
914
+
915
+ $ this ->preparePreconditionsForCreateOrder (
916
+ $ productIdFromFixture ,
917
+ $ customerEmailFromFixture ,
918
+ $ shippingMethod ,
919
+ $ shippingAddressAsBilling ,
920
+ $ paymentData ,
921
+ $ orderData ,
922
+ $ paymentMethod ,
923
+ $ customerIdFromFixture
924
+ );
925
+ $ this ->model ->setBillingAddress ($ orderData ['billing_address ' ]);
926
+ try {
927
+ $ order =$ this ->model ->createOrder ();
928
+ $ orderData = $ order ->getData ();
929
+ self ::assertNotEmpty ($ orderData ['increment_id ' ], 'Order increment ID is empty. ' );
930
+ } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
931
+ /** createOrder is expected to throw exception with empty message when validation error occurs */
932
+ self ::assertEquals ('Validation is failed. ' , $ e ->getRawMessage ());
933
+ }
934
+ }
843
935
}
0 commit comments