Skip to content
13 changes: 13 additions & 0 deletions app/code/Magento/Customer/Test/Mftf/Data/CustomerData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
<data key="website_id">0</data>
<requiredEntity type="address">US_Address_TX</requiredEntity>
</entity>
<entity name="Simple_US_Customer_Incorrect_Name" type="customer">
<data key="group_id">1</data>
<data key="default_billing">true</data>
<data key="default_shipping">true</data>
<data key="email" unique="prefix">John.Doe@example.com</data>
<data key="firstname">LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum</data>
<data key="lastname">Doe</data>
<data key="fullname">John Doe</data>
<data key="password">pwdTest123!</data>
<data key="store_id">0</data>
<data key="website_id">0</data>
<requiredEntity type="address">US_Address_TX</requiredEntity>
</entity>
<entity name="Simple_Customer_Without_Address" type="customer">
<data key="group_id">1</data>
<data key="email" unique="prefix">John.Doe@example.com</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,88 @@
<?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="fillWrongCustomerAddress" after="fillCustomerEmail">
<argument name="customer" value="Simple_US_Customer_Incorrect_Name"/>
<argument name="address" value="US_Address_TX"/>
</actionGroup>
<!-- Select shipping -->
<actionGroup ref="orderSelectFlatRateShipping" stepKey="selectFlatRateShipping"
after="fillWrongCustomerAddress"/>

<!--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="clickSubmitOrderWrong"
after="seeCorrectGrandTotal"/>
<see selector="{{AdminOrderFormBillingAddressSection.firstNameError}}"
userInput="Please enter less or equal than 255 symbols." stepKey="firstNameError"
after="clickSubmitOrderWrong"/>

<!--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>

<!-- Select shipping -->
<click selector="{{AdminOrderFormPaymentSection.getShippingMethods}}" stepKey="clickShipping" after="fillCustomerAddress"/>
<click selector="{{AdminOrderFormPaymentSection.getShippingMethods}}" stepKey="selectShipping" after="clickShipping"/>

<!--Submit Order and verify information-->
<click selector="{{AdminOrderFormActionSection.SubmitOrder}}" stepKey="clickSubmitOrder" after="selectShipping"/>
<seeInCurrentUrl url="{{AdminOrderDetailsPage.url}}" stepKey="seeViewOrderPage" after="clickSubmitOrder"/>
<see selector="{{AdminOrderDetailsMessagesSection.successMessage}}" userInput="You created the order." stepKey="seeSuccessMessage" after="seeViewOrderPage"/>
</test>
</tests>