Skip to content

Commit

Permalink
MAGETWO-42994: Customer custom attribute is not validated on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Semenov committed Jul 20, 2016
1 parent f553937 commit 6ce2b5f
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 45 deletions.
19 changes: 6 additions & 13 deletions app/code/Magento/Customer/Helper/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper
/** @var \Magento\Store\Model\StoreManagerInterface */
protected $_storeManager;

/** @var CustomerMetadataInterface */
/**
* @var CustomerMetadataInterface
*
* @deprecated
*/
protected $_customerMetadataService;

/** @var AddressMetadataInterface */
Expand Down Expand Up @@ -243,19 +247,8 @@ public function getAttributeValidationClass($attributeCode)
$attribute = isset($this->_attributes[$attributeCode])
? $this->_attributes[$attributeCode]
: $this->_addressMetadataService->getAttributeMetadata($attributeCode);
$class = $attribute ? $attribute->getFrontendClass() : '';
if (in_array($attributeCode, ['firstname', 'middlename', 'lastname', 'prefix', 'suffix', 'taxvat'])) {
if ($class && !$attribute->isVisible()) {
// address attribute is not visible thus its validation rules are not applied
$class = '';
}

/** @var $customerAttribute AttributeMetadataInterface */
$customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode);
$class .= $customerAttribute &&
$customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : '';
$class = implode(' ', array_unique(array_filter(explode(' ', $class))));
}
$class = $attribute ? $attribute->getFrontendClass() : '';
} catch (NoSuchEntityException $e) {
// the attribute does not exist so just return an empty string
}
Expand Down
42 changes: 18 additions & 24 deletions app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,39 +147,33 @@ public function testGetConfigCanShowConfig()
$this->assertTrue($this->helper->canShowConfig('key2'));
}

/**
* @param $attrCode
* @param $attrClass
* @param $customAttrClass
* @param $result
* @dataProvider getAttributeValidationClassDataProvider
*/
public function testGetAttributeValidationClass($attrCode, $attrClass, $customAttrClass, $result)
public function testGetAttributeValidationClass()
{
$attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock();
$attributeMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($attrClass));
$attributeCode = 'attr_code';
$attributeClass = 'Attribute_Class';

$customAttrMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock();
$customAttrMock->expects($this->any())->method('isVisible')->will($this->returnValue(true));
$customAttrMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($customAttrClass));

$this->customerMetadataService->expects($this->any())
->method('getAttributeMetadata')
->will($this->returnValue($customAttrMock));
$attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')
->getMockForAbstractClass();
$attributeMock->expects($this->once())
->method('getFrontendClass')
->willReturn($attributeClass);

$this->addressMetadataService->expects($this->any())
->method('getAttributeMetadata')
->will($this->returnValue($attributeMock));
->willReturn($attributeMock);

$this->assertEquals($result, $this->helper->getAttributeValidationClass($attrCode));
$this->assertEquals($attributeClass, $this->helper->getAttributeValidationClass($attributeCode));
}

public function getAttributeValidationClassDataProvider()
public function testGetAttributeValidationClassWithNoAttribute()
{
return [
['attr_code', 'Attribute_Class', '', 'Attribute_Class'],
['firstname', 'Attribute_Class', 'Attribute2_Class', 'Attribute2_Class'],
];
$attrCode = 'attr_code';

$this->addressMetadataService->expects($this->any())
->method('getAttributeMetadata')
->willReturn(null);

$this->assertEquals('', $this->helper->getAttributeValidationClass($attrCode));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,13 @@ public function getClass()
if ($inputRuleClass) {
$out[] = $inputRuleClass;
}
if (!empty($out)) {
$out = implode(' ', $out);
} else {
$out = '';

$textLengthValidateClasses = $this->getTextLengthValidateClasses();
if (!empty($textLengthValidateClasses)) {
$out[] = implode(' ', $textLengthValidateClasses);
}

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

Expand Down Expand Up @@ -197,6 +199,29 @@ protected function _getInputValidateClass()
return $class;
}

/**
* Retrieve validation classes by min_text_length and max_text_length rules
*
* @return array
*/
private function getTextLengthValidateClasses()
{
$classes = [];

$validateRules = $this->getAttribute()->getValidateRules();
if (!empty($validateRules['min_text_length'])) {
$classes[] = 'minimum-length-' . $validateRules['min_text_length'];
}
if (!empty($validateRules['max_text_length'])) {
$classes[] = 'maximum-length-' . $validateRules['max_text_length'];
}
if (!empty($classes)) {
$classes[] = 'validate-length';
}

return $classes;
}

/**
* Reireive config field
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Frontend;

use Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend;

class DefaultFrontendTest extends \PHPUnit_Framework_TestCase
{
/**
* @var DefaultFrontend
*/
protected $model;

/**
* @var \Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $booleanFactory;

protected function setUp()
{
$this->booleanFactory = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory')
->disableOriginalConstructor()
->getMock();

$this->model = new DefaultFrontend(
$this->booleanFactory
);
}

public function testGetClassEmpty()
{
/** @var \PHPUnit_Framework_MockObject_MockObject $attributeMock */
$attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
->disableOriginalConstructor()
->setMethods([
'getIsRequired',
'getFrontendClass',
'getValidateRules',
])
->getMock();
$attributeMock->expects($this->once())
->method('getIsRequired')
->willReturn(false);
$attributeMock->expects($this->once())
->method('getFrontendClass')
->willReturn('');
$attributeMock->expects($this->exactly(2))
->method('getValidateRules')
->willReturn('');

$this->model->setAttribute($attributeMock);
$this->assertEmpty($this->model->getClass());
}

public function testGetClass()
{
/** @var \PHPUnit_Framework_MockObject_MockObject $attributeMock */
$attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
->disableOriginalConstructor()
->setMethods([
'getIsRequired',
'getFrontendClass',
'getValidateRules',
])
->getMock();
$attributeMock->expects($this->once())
->method('getIsRequired')
->willReturn(true);
$attributeMock->expects($this->once())
->method('getFrontendClass')
->willReturn('');
$attributeMock->expects($this->exactly(2))
->method('getValidateRules')
->willReturn([
'input_validation' => 'alphanumeric',
'min_text_length' => 1,
'max_text_length' => 2,
]);

$this->model->setAttribute($attributeMock);
$result = $this->model->getClass();

$this->assertContains('validate-alphanum', $result);
$this->assertContains('minimum-length-1', $result);
$this->assertContains('maximum-length-2', $result);
$this->assertContains('validate-length', $result);
}
}
12 changes: 8 additions & 4 deletions lib/web/mage/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,18 +1137,22 @@
$.each(elm.className.split(' '), function (index, name) {
if (name.match(reMax) && result) {
length = name.split('-')[2];
validator.attrLength = length;
result = (v.length <= length);
validator.validateMessage = $.mage.__(
"Please enter less or equal than %1 symbols."
).replace('%1', length);
}
if (name.match(reMin) && result && $.mage.isEmpty(v)) {
if (name.match(reMin) && result && !$.mage.isEmpty(v)) {
length = name.split('-')[2];
result = v.length >= length;
validator.validateMessage = $.mage.__(
"Please enter more or equal than %1 symbols."
).replace('%1', length);
}
});
return result;
}, function () {
return $.mage.__("Maximum length of this field must be equal or less than %1 symbols.")
.replace('%1', this.attrLength);
return this.validateMessage;
}
],
'required-entry': [
Expand Down

0 comments on commit 6ce2b5f

Please sign in to comment.