Skip to content
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

[Backport] Missed form validation in Admin Order Address Edit route sales/order/address #20841

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function _addAttributesToForm($attributes, \Magento\Framework\Data\For
[
'name' => $attribute->getAttributeCode(),
'label' => __($attribute->getStoreLabel()),
'class' => $attribute->getFrontendClass(),
'class' => $this->getValidationClasses($attribute),
'required' => $attribute->isRequired()
]
);
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>
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@
<field name="default_shipping" xsi:type="string">Yes</field>
</dataset>

<dataset name="US_address_TX_Wrong_Validation">
<field name="firstname" xsi:type="string">LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum</field>
<field name="lastname" xsi:type="string">Doe</field>
<field name="email" xsi:type="string">John.Doe%isolation%@example.com</field>
<field name="company" xsi:type="string">Magento %isolation%</field>
<field name="street" xsi:type="string">7700 W. Parmer Lane Bldg. D</field>
<field name="city" xsi:type="string">Austin</field>
<field name="region_id" xsi:type="string">Texas</field>
<field name="postcode" xsi:type="string">78729</field>
<field name="country_id" xsi:type="string">United States</field>
<field name="telephone" xsi:type="string">512-691-4400</field>
<field name="default_billing" xsi:type="string">Yes</field>
<field name="default_shipping" xsi:type="string">Yes</field>
</dataset>

<dataset name="US_address_NY">
<field name="firstname" xsi:type="string">John</field>
<field name="lastname" xsi:type="string">Doe</field>
Expand Down