Skip to content

Missed form validation in Admin Order Address Edit route sales/order/address #20840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 16, 2019
17 changes: 17 additions & 0 deletions app/code/Magento/Customer/Test/Mftf/Data/AddressData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@
<data key="default_shipping">Yes</data>
<requiredEntity type="region">RegionTX</requiredEntity>
</entity>
<entity name="US_address_TX_Wrong_Validation" type="address">
<data key="firstname">LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum</data>
<data key="lastname">Doe</data>
<data key="company">Magento</data>
<array key="street">
<item>7700 West Parmer Lane</item>
</array>
<data key="city">Austin</data>
<data key="state">Texas</data>
<data key="country_id">US</data>
<data key="country">United States</data>
<data key="postcode">78729</data>
<data key="telephone">512-345-6789</data>
<data key="default_billing">Yes</data>
<data key="default_shipping">Yes</data>
<requiredEntity type="region">RegionTX</requiredEntity>
</entity>
<entity name="US_Address_TX_Default_Billing" type="address">
<data key="firstname">John</data>
<data key="lastname">Doe</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ protected function _addAttributesToForm($attributes, \Magento\Framework\Data\For
[
'name' => $attribute->getAttributeCode(),
'label' => __($attribute->getStoreLabel()),
'class' => $attribute->getFrontendClass(),
'required' => $attribute->isRequired()
'class' => $this->getValidationClasses($attribute),
'required' => $attribute->isRequired(),
]
);
if ($inputType == 'multiline') {
Expand Down Expand Up @@ -227,4 +227,58 @@ public function getFormValues()
{
return [];
}

/**
* Retrieve frontend classes according validation rules
*
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
*
* @return string
*/
private function getValidationClasses(\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute) : string
{
$out = [];
$out[] = $attribute->getFrontendClass();

$textClasses = $this->getTextLengthValidateClasses($attribute);
if (!empty($textClasses)) {
$out = array_merge($out, $textClasses);
}

$out = !empty($out) ? implode(' ', array_unique(array_filter($out))) : '';
return $out;
}

/**
* Retrieve validation classes by min_text_length and max_text_length rules
*
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
*
* @return array
*/
private function getTextLengthValidateClasses(\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute) : array
{
$classes = [];

$validateRules = $attribute->getValidationRules();
if(!empty($validateRules)) {
foreach ($validateRules as $rule) {
switch ($rule->getName()) {
case 'min_text_length' :
$classes[] = 'minimum-length-' . $rule->getValue();
break;

case 'max_text_length' :
$classes[] = 'maximum-length-' . $rule->getValue();
break;
}
}

if (!empty($classes)) {
$classes[] = 'validate-length';
}
}

return $classes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminSubmitsOrderWithAndWithoutFieldsValidationTest">
<annotations>
<features value="Sales"/>
<stories value="Create orders"/>
<title value="Fields validation is required to create an order from Admin Panel"/>
<description value="Admin should not be able to submit orders without invalid address fields"/>
<group value="sales"/>
</annotations>
<before>
<createData entity="_defaultCategory" stepKey="createCategory"/>
<createData entity="_defaultProduct" stepKey="createSimpleProduct">
<requiredEntity createDataKey="createCategory"/>
</createData>
</before>
<after>
<deleteData createDataKey="createSimpleProduct" stepKey="deleteProduct"/>
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
<amOnPage url="admin/admin/auth/logout/" stepKey="amOnLogoutPage"/>
</after>
<!--Create order via Admin-->
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<comment userInput="Admin creates order" stepKey="adminCreateOrderComment"/>
<!--<actionGroup ref="navigateToNewOrderPageNewCustomer" stepKey="navigateToNewOrderPage"/>-->
<amOnPage url="{{AdminOrdersPage.url}}" stepKey="navigateToOrderIndexPage"/>
<waitForPageLoad stepKey="waitForIndexPageLoad"/>
<see selector="{{AdminHeaderSection.pageTitle}}" userInput="Orders" stepKey="seeIndexPageTitle"/>
<click selector="{{AdminOrdersGridSection.createNewOrder}}" stepKey="clickCreateNewOrder"/>
<click selector="{{AdminOrderFormActionSection.CreateNewCustomer}}" stepKey="clickCreateCustomer"/>
<see selector="{{AdminHeaderSection.pageTitle}}" userInput="Create New Order" stepKey="seeNewOrderPageTitle"/>

<!--Check if order can be submitted without the required fields including email address-->
<actionGroup ref="checkRequiredFieldsNewOrderForm" stepKey="checkRequiredFieldsNewOrder" after="seeNewOrderPageTitle"/>
<scrollToTopOfPage stepKey="scrollToTopOfOrderFormPage" after="checkRequiredFieldsNewOrder"/>
<actionGroup ref="addSimpleProductToOrder" stepKey="addSimpleProductToOrder" after="scrollToTopOfOrderFormPage">
<argument name="product" value="_defaultProduct"/>
</actionGroup>

<!--Fill customer group and customer email-->
<selectOption selector="{{AdminOrderFormAccountSection.group}}" userInput="{{GeneralCustomerGroup.code}}" stepKey="selectCustomerGroup" after="addSimpleProductToOrder"/>
<fillField selector="{{AdminOrderFormAccountSection.email}}" userInput="{{Simple_US_Customer.email}}" stepKey="fillCustomerEmail" after="selectCustomerGroup"/>

<!--Fill wrong customer address information-->
<actionGroup ref="fillOrderCustomerInformation" stepKey="fillCustomerAddress" after="fillCustomerEmail">
<argument name="customer" value="Simple_US_Customer"/>
<argument name="address" value="US_address_TX_Wrong_Validation"/>
</actionGroup>
<!-- Select shipping -->
<actionGroup ref="orderSelectFlatRateShipping" stepKey="selectFlatRateShipping" after="fillCustomerAddress"/>

<!--Verify totals on Order page-->
<see selector="{{AdminOrderFormTotalSection.total('Subtotal')}}" userInput="${{AdminOrderSimpleProduct.subtotal}}" stepKey="seeOrderSubTotal" after="selectFlatRateShipping"/>
<see selector="{{AdminOrderFormTotalSection.total('Shipping')}}" userInput="${{AdminOrderSimpleProduct.shipping}}" stepKey="seeOrderShipping" after="seeOrderSubTotal"/>
<scrollTo selector="{{AdminOrderFormTotalSection.grandTotal}}" stepKey="scrollToOrderGrandTotal"/>
<see selector="{{AdminOrderFormTotalSection.grandTotal}}" userInput="${{AdminOrderSimpleProduct.grandTotal}}" stepKey="seeCorrectGrandTotal" after="scrollToOrderGrandTotal"/>

<!--Submit Order and verify information-->
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="clickSubmitOrder" after="seeCorrectGrandTotal"/>
<see selector="{{AdminOrderFormBillingAddressSection.firstNameError}}" userInput="Please enter less or equal than 255 symbols." stepKey="firstNameError" after="clickSubmitOrder"/>

<!--Fill correct customer address information-->
<actionGroup ref="fillOrderCustomerInformation" stepKey="fillCustomerAddress" after="firstNameError">
<argument name="customer" value="Simple_US_Customer"/>
<argument name="address" value="US_address_TX"/>
</actionGroup>
<!--Submit Order and verify information-->
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="clickSubmitOrder" after="fillCustomerAddress"/>
<seeInCurrentUrl url="{{AdminOrderDetailsPage.url}}" stepKey="seeViewOrderPage" after="clickSubmitOrder"/>
<see selector="{{AdminOrderDetailsMessagesSection.successMessage}}" userInput="You created the order." stepKey="seeSuccessMessage" after="seeViewOrderPage"/>
</test>
</tests>