Skip to content

Commit 910d913

Browse files
author
Sergii Kovalenko
committed
Merge branch 'MAGETWO-42994' into BUGS
2 parents 62408e3 + 49dd4f9 commit 910d913

File tree

7 files changed

+156
-49
lines changed

7 files changed

+156
-49
lines changed

app/code/Magento/Customer/Helper/Address.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper
7171
/** @var \Magento\Store\Model\StoreManagerInterface */
7272
protected $_storeManager;
7373

74-
/** @var CustomerMetadataInterface */
74+
/**
75+
* @var CustomerMetadataInterface
76+
*
77+
* @deprecated
78+
*/
7579
protected $_customerMetadataService;
7680

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

253-
/** @var $customerAttribute AttributeMetadataInterface */
254-
$customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode);
255-
$class .= $customerAttribute &&
256-
$customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : '';
257-
$class = implode(' ', array_unique(array_filter(explode(' ', $class))));
258-
}
251+
$class = $attribute ? $attribute->getFrontendClass() : '';
259252
} catch (NoSuchEntityException $e) {
260253
// the attribute does not exist so just return an empty string
261254
}

app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,39 +147,33 @@ public function testGetConfigCanShowConfig()
147147
$this->assertTrue($this->helper->canShowConfig('key2'));
148148
}
149149

150-
/**
151-
* @param $attrCode
152-
* @param $attrClass
153-
* @param $customAttrClass
154-
* @param $result
155-
* @dataProvider getAttributeValidationClassDataProvider
156-
*/
157-
public function testGetAttributeValidationClass($attrCode, $attrClass, $customAttrClass, $result)
150+
public function testGetAttributeValidationClass()
158151
{
159-
$attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock();
160-
$attributeMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($attrClass));
152+
$attributeCode = 'attr_code';
153+
$attributeClass = 'Attribute_Class';
161154

162-
$customAttrMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')->getMock();
163-
$customAttrMock->expects($this->any())->method('isVisible')->will($this->returnValue(true));
164-
$customAttrMock->expects($this->any())->method('getFrontendClass')->will($this->returnValue($customAttrClass));
165-
166-
$this->customerMetadataService->expects($this->any())
167-
->method('getAttributeMetadata')
168-
->will($this->returnValue($customAttrMock));
155+
$attributeMock = $this->getMockBuilder('Magento\Customer\Api\Data\AttributeMetadataInterface')
156+
->getMockForAbstractClass();
157+
$attributeMock->expects($this->once())
158+
->method('getFrontendClass')
159+
->willReturn($attributeClass);
169160

170161
$this->addressMetadataService->expects($this->any())
171162
->method('getAttributeMetadata')
172-
->will($this->returnValue($attributeMock));
163+
->willReturn($attributeMock);
173164

174-
$this->assertEquals($result, $this->helper->getAttributeValidationClass($attrCode));
165+
$this->assertEquals($attributeClass, $this->helper->getAttributeValidationClass($attributeCode));
175166
}
176167

177-
public function getAttributeValidationClassDataProvider()
168+
public function testGetAttributeValidationClassWithNoAttribute()
178169
{
179-
return [
180-
['attr_code', 'Attribute_Class', '', 'Attribute_Class'],
181-
['firstname', 'Attribute_Class', 'Attribute2_Class', 'Attribute2_Class'],
182-
];
170+
$attrCode = 'attr_code';
171+
172+
$this->addressMetadataService->expects($this->any())
173+
->method('getAttributeMetadata')
174+
->willReturn(null);
175+
176+
$this->assertEquals('', $this->helper->getAttributeValidationClass($attrCode));
183177
}
184178

185179
/**

app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ public function getClass()
155155
if ($inputRuleClass) {
156156
$out[] = $inputRuleClass;
157157
}
158-
if (!empty($out)) {
159-
$out = implode(' ', $out);
160-
} else {
161-
$out = '';
158+
159+
$textLengthValidateClasses = $this->getTextLengthValidateClasses();
160+
if (!empty($textLengthValidateClasses)) {
161+
$out[] = implode(' ', $textLengthValidateClasses);
162162
}
163+
164+
$out = !empty($out) ? trim(implode(' ', $out)) : '';
163165
return $out;
164166
}
165167

@@ -197,6 +199,31 @@ protected function _getInputValidateClass()
197199
return $class;
198200
}
199201

202+
/**
203+
* Retrieve validation classes by min_text_length and max_text_length rules
204+
*
205+
* @return array
206+
*/
207+
private function getTextLengthValidateClasses()
208+
{
209+
$classes = [];
210+
211+
if ($this->_getInputValidateClass()) {
212+
$validateRules = $this->getAttribute()->getValidateRules();
213+
if (!empty($validateRules['min_text_length'])) {
214+
$classes[] = 'minimum-length-' . $validateRules['min_text_length'];
215+
}
216+
if (!empty($validateRules['max_text_length'])) {
217+
$classes[] = 'maximum-length-' . $validateRules['max_text_length'];
218+
}
219+
if (!empty($classes)) {
220+
$classes[] = 'validate-length';
221+
}
222+
}
223+
224+
return $classes;
225+
}
226+
200227
/**
201228
* Reireive config field
202229
*
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Frontend;
7+
8+
use Magento\Eav\Model\Entity\Attribute\Frontend\DefaultFrontend;
9+
10+
class DefaultFrontendTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var DefaultFrontend
14+
*/
15+
protected $model;
16+
17+
/**
18+
* @var \Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory|\PHPUnit_Framework_MockObject_MockObject
19+
*/
20+
protected $booleanFactory;
21+
22+
protected function setUp()
23+
{
24+
$this->booleanFactory = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory')
25+
->disableOriginalConstructor()
26+
->getMock();
27+
28+
$this->model = new DefaultFrontend(
29+
$this->booleanFactory
30+
);
31+
}
32+
33+
public function testGetClassEmpty()
34+
{
35+
$attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
36+
->disableOriginalConstructor()
37+
->setMethods([
38+
'getIsRequired',
39+
'getFrontendClass',
40+
'getValidateRules',
41+
])
42+
->getMock();
43+
$attributeMock->expects($this->once())
44+
->method('getIsRequired')
45+
->willReturn(false);
46+
$attributeMock->expects($this->once())
47+
->method('getFrontendClass')
48+
->willReturn('');
49+
$attributeMock->expects($this->exactly(2))
50+
->method('getValidateRules')
51+
->willReturn('');
52+
53+
$this->model->setAttribute($attributeMock);
54+
$this->assertEmpty($this->model->getClass());
55+
}
56+
57+
public function testGetClass()
58+
{
59+
$attributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\AbstractAttribute')
60+
->disableOriginalConstructor()
61+
->setMethods([
62+
'getIsRequired',
63+
'getFrontendClass',
64+
'getValidateRules',
65+
])
66+
->getMock();
67+
$attributeMock->expects($this->once())
68+
->method('getIsRequired')
69+
->willReturn(true);
70+
$attributeMock->expects($this->once())
71+
->method('getFrontendClass')
72+
->willReturn('');
73+
$attributeMock->expects($this->exactly(3))
74+
->method('getValidateRules')
75+
->willReturn([
76+
'input_validation' => 'alphanumeric',
77+
'min_text_length' => 1,
78+
'max_text_length' => 2,
79+
]);
80+
81+
$this->model->setAttribute($attributeMock);
82+
$result = $this->model->getClass();
83+
84+
$this->assertContains('validate-alphanum', $result);
85+
$this->assertContains('minimum-length-1', $result);
86+
$this->assertContains('maximum-length-2', $result);
87+
$this->assertContains('validate-length', $result);
88+
}
89+
}

dev/tests/api-functional/testsuite/Magento/Customer/Api/CustomerMetadataTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getAttributeMetadataDataProvider()
7474
AttributeMetadata::REQUIRED => true,
7575
AttributeMetadata::DATA_MODEL => '',
7676
AttributeMetadata::OPTIONS => [],
77-
AttributeMetadata::FRONTEND_CLASS => ' required-entry',
77+
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
7878
AttributeMetadata::USER_DEFINED => false,
7979
AttributeMetadata::SORT_ORDER => 40,
8080
AttributeMetadata::FRONTEND_LABEL => 'First Name',
@@ -134,7 +134,7 @@ public function getAttributeMetadataDataProvider()
134134
AttributeMetadata::OPTIONS => [
135135
['label' => 'Main Website', 'value' => '1'],
136136
],
137-
AttributeMetadata::FRONTEND_CLASS => ' required-entry',
137+
AttributeMetadata::FRONTEND_CLASS => 'required-entry',
138138
AttributeMetadata::USER_DEFINED => false,
139139
AttributeMetadata::SORT_ORDER => 10,
140140
AttributeMetadata::FRONTEND_LABEL => 'Associate to Website',

dev/tests/integration/testsuite/Magento/Customer/Helper/AddressTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function getAttributeValidationClass()
3030
{
3131
return [
3232
['bad-code', ''],
33-
['city', ' required-entry'],
33+
['city', 'required-entry'],
3434
['company', ''],
35-
['country_id', ' required-entry'],
35+
['country_id', 'required-entry'],
3636
['fax', ''],
3737
['firstname', 'required-entry'],
3838
['lastname', 'required-entry'],

lib/web/mage/validation.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,18 +1136,22 @@
11361136
$.each(elm.className.split(' '), function (index, name) {
11371137
if (name.match(reMax) && result) {
11381138
length = name.split('-')[2];
1139-
validator.attrLength = length;
11401139
result = (v.length <= length);
1140+
validator.validateMessage = $.mage.__(
1141+
"Please enter less or equal than %1 symbols."
1142+
).replace('%1', length);
11411143
}
1142-
if (name.match(reMin) && result && $.mage.isEmpty(v)) {
1144+
if (name.match(reMin) && result && !$.mage.isEmpty(v)) {
11431145
length = name.split('-')[2];
11441146
result = v.length >= length;
1147+
validator.validateMessage = $.mage.__(
1148+
"Please enter more or equal than %1 symbols."
1149+
).replace('%1', length);
11451150
}
11461151
});
11471152
return result;
11481153
}, function () {
1149-
return $.mage.__("Maximum length of this field must be equal or less than %1 symbols.")
1150-
.replace('%1', this.attrLength);
1154+
return this.validateMessage;
11511155
}
11521156
],
11531157
'required-entry': [

0 commit comments

Comments
 (0)