Skip to content

Commit 55235cb

Browse files
pradeep.rauthanpradeep.rauthan
authored andcommitted
MC-42704: Admin Checkout validation bug
-Integration test has been written.
1 parent f06da40 commit 55235cb

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* this fixture update customer_address `input_validation` to `alphanum-with-spaces` for `street` field.
4+
*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
$attributeCode = 'street';
9+
$entityType = \Magento\Customer\Model\Metadata\AddressMetadata::ENTITY_TYPE_ADDRESS;
10+
11+
//@codingStandardsIgnoreFile
12+
/** @var \Magento\Customer\Model\Attribute $model */
13+
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
14+
$model->loadByCode($entityType, $attributeCode);
15+
16+
$validationRules = array_replace_recursive($model->getValidationRules(),['input_validation'=>'alphanum-with-spaces']);
17+
$model->setValidationRules($validationRules);
18+
19+
$model->save();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Registry $registry */
8+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
9+
$registry->unregister('isSecureArea');
10+
$registry->register('isSecureArea', true);
11+
12+
$attributeCode = 'street';
13+
$entityType = \Magento\Customer\Model\Metadata\AddressMetadata::ENTITY_TYPE_ADDRESS;
14+
//@codingStandardsIgnoreFile
15+
/** @var \Magento\Customer\Model\Attribute $model */
16+
$model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Attribute::class);
17+
$model->loadByCode($entityType, $attributeCode);
18+
$validationRules = $model->getValidationRules();
19+
20+
if(!empty($validationRules['input_validation'])){
21+
if(in_array('alphanum-with-spaces', $validationRules)){
22+
unset($validationRules['input_validation']);
23+
$model->setValidationRules($validationRules);
24+
$model->save();
25+
}
26+
}
27+
28+
$registry->unregister('isSecureArea');
29+
$registry->register('isSecureArea', false);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Customer default address fixture with entity_id = 2,
4+
* this fixture also inherit other fixtures for creating simple product, a customer with entity_id=1 and default customer address.
5+
* It also call a new fixture to update customer address `input_validation` rule for `street` field.
6+
*
7+
* Copyright © Magento, Inc. All rights reserved.
8+
* See COPYING.txt for license details.
9+
*/
10+
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
12+
13+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple.php');
14+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
15+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_address.php');
16+
17+
/** @var \Magento\Customer\Model\Address $customerAddress */
18+
$customerAddress = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
19+
->create(\Magento\Customer\Model\Address::class);
20+
/** @var CustomerRegistry $customerRegistry */
21+
$customerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
22+
->get(CustomerRegistry::class);
23+
$customerAddress->isObjectNew(true);
24+
$customerAddress->setData(
25+
[
26+
'entity_id' => 2,
27+
'attribute_set_id' => 2,
28+
'telephone' => 3234676,
29+
'postcode' => 47676,
30+
'country_id' => 'US',
31+
'city' => 'CityX',
32+
'street' => ['Black str 48'],
33+
'lastname' => 'Smith',
34+
'firstname' => 'John',
35+
'parent_id' => 1,
36+
'region_id' => 1,
37+
]
38+
)->setCustomerId(
39+
1
40+
);
41+
42+
$customerAddress->save();
43+
$customerRegistry->remove($customerAddress->getCustomerId());
44+
45+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_address_attribute_update.php');

dev/tests/integration/testsuite/Magento/Sales/Model/AdminOrder/CreateTest.php

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,9 @@ private function preparePreconditionsForCreateOrder(
732732
/** Unset fake IDs for default billing and shipping customer addresses */
733733
/** @var Customer $customer */
734734
$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+
}
736738
} else {
737739
/**
738740
* Customer ID must be set to session to pass \Magento\Sales\Model\AdminOrder\Create::_validate()
@@ -840,4 +842,94 @@ private function getValidAddressData()
840842
'vat_id' => ''
841843
];
842844
}
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+
}
843935
}

0 commit comments

Comments
 (0)