Skip to content

Commit 0c12792

Browse files
[Magento Community Engineering] Community Contributions - 2.2-develop
- merged latest code from mainline branch
2 parents 562e989 + 947ed0d commit 0c12792

File tree

24 files changed

+479
-102
lines changed

24 files changed

+479
-102
lines changed

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/cc-form.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ define(
7979
*/
8080
onError: function (response) {
8181
braintree.showError($t('Payment ' + this.getTitle() + ' can\'t be initialized'));
82-
this.isPlaceOrderActionAllowed(true);
8382
throw response.message;
8483
},
8584

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/hosted-fields.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ define([
156156
*/
157157
placeOrderClick: function () {
158158
if (this.validateCardType()) {
159-
this.isPlaceOrderActionAllowed(false);
160159
$(this.getSelector('submit')).trigger('click');
161160
}
162161
},

app/code/Magento/Catalog/Controller/Adminhtml/Product/Attribute/Save.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,6 @@ public function execute()
189189
? $model->getAttributeCode()
190190
: $this->getRequest()->getParam('attribute_code');
191191
$attributeCode = $attributeCode ?: $this->generateCode($this->getRequest()->getParam('frontend_label')[0]);
192-
if (strlen($attributeCode) > 0) {
193-
$validatorAttrCode = new \Zend_Validate_Regex(
194-
['pattern' => '/^[a-z\x{600}-\x{6FF}][a-z\x{600}-\x{6FF}_0-9]{0,30}$/u']
195-
);
196-
if (!$validatorAttrCode->isValid($attributeCode)) {
197-
$this->messageManager->addErrorMessage(
198-
__(
199-
'Attribute code "%1" is invalid. Please use only letters (a-z), ' .
200-
'numbers (0-9) or underscore(_) in this field, first character should be a letter.',
201-
$attributeCode
202-
)
203-
);
204-
205-
return $this->returnResult(
206-
'catalog/*/edit',
207-
['attribute_id' => $attributeId, '_current' => true],
208-
['error' => true]
209-
);
210-
}
211-
}
212192
$data['attribute_code'] = $attributeCode;
213193

214194
//validate frontend_input

app/code/Magento/Checkout/view/frontend/web/js/view/payment/default.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,25 @@ define([
133133
event.preventDefault();
134134
}
135135

136-
if (this.validate() && additionalValidators.validate()) {
136+
if (this.validate() &&
137+
additionalValidators.validate() &&
138+
this.isPlaceOrderActionAllowed() === true
139+
) {
137140
this.isPlaceOrderActionAllowed(false);
138141

139142
this.getPlaceOrderDeferredObject()
140-
.fail(
141-
function () {
142-
self.isPlaceOrderActionAllowed(true);
143-
}
144-
).done(
143+
.done(
145144
function () {
146145
self.afterPlaceOrder();
147146

148147
if (self.redirectAfterPlaceOrder) {
149148
redirectOnSuccessAction.execute();
150149
}
151150
}
151+
).always(
152+
function () {
153+
self.isPlaceOrderActionAllowed(true);
154+
}
152155
);
153156

154157
return true;

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,6 @@ public function beforeSave()
238238
);
239239
}
240240

241-
/**
242-
* Check for maximum attribute_code length
243-
*/
244-
if (isset(
245-
$this->_data['attribute_code']
246-
) && !\Zend_Validate::is(
247-
$this->_data['attribute_code'],
248-
'StringLength',
249-
['max' => self::ATTRIBUTE_CODE_MAX_LENGTH]
250-
)
251-
) {
252-
throw new LocalizedException(
253-
__('An attribute code must not be more than %1 characters.', self::ATTRIBUTE_CODE_MAX_LENGTH)
254-
);
255-
}
256-
257241
$defaultValue = $this->getDefaultValue();
258242
$hasDefaultValue = (string)$defaultValue != '';
259243

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Model\Validator\Attribute;
9+
10+
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
11+
use Magento\Eav\Model\Entity\Attribute;
12+
use Magento\Framework\Validator\AbstractValidator;
13+
use Zend_Validate;
14+
use Zend_Validate_Exception;
15+
16+
class Code extends AbstractValidator
17+
{
18+
/**
19+
* Validation pattern for attribute code
20+
*/
21+
const VALIDATION_RULE_PATTERN = '/^[a-zA-Z]+[a-zA-Z0-9_]*$/u';
22+
23+
/**
24+
* Returns true if and only if $value meets the validation requirements
25+
*
26+
* If $value fails validation, then this method returns false, and
27+
* getMessages() will return an array of messages that explain why the
28+
* validation failed.
29+
*
30+
* @param string $attributeCode
31+
* @return boolean
32+
* @throws Zend_Validate_Exception If validation of $attributeCode is impossible
33+
*/
34+
public function isValid($attributeCode): bool
35+
{
36+
$errorMessages = [];
37+
/**
38+
* Check attribute_code for allowed characters
39+
*/
40+
if (trim($attributeCode)
41+
&& !preg_match(self::VALIDATION_RULE_PATTERN, trim($attributeCode))
42+
) {
43+
$errorMessages[] = __(
44+
'Attribute code "%1" is invalid. Please use only letters (a-z or A-Z), ' .
45+
'numbers (0-9) or underscore (_) in this field, and the first character should be a letter.',
46+
$attributeCode
47+
);
48+
}
49+
50+
/**
51+
* Check attribute_code for allowed length
52+
*/
53+
$minLength = Attribute::ATTRIBUTE_CODE_MIN_LENGTH;
54+
$maxLength = Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
55+
$isAllowedLength = Zend_Validate::is(
56+
trim($attributeCode),
57+
'StringLength',
58+
['min' => $minLength, 'max' => $maxLength]
59+
);
60+
if (!$isAllowedLength) {
61+
$errorMessages[] = __(
62+
'An attribute code must not be less than %1 and more than %2 characters.',
63+
$minLength,
64+
$maxLength
65+
);
66+
}
67+
68+
/**
69+
* Check attribute_code for prohibited prefix
70+
*/
71+
if (strpos($attributeCode, AbstractModifier::CONTAINER_PREFIX) === 0) {
72+
$errorMessages[] = __(
73+
'"%1" prefix is reserved by the system and cannot be used in attribute code names.',
74+
AbstractModifier::CONTAINER_PREFIX
75+
);
76+
}
77+
78+
$this->_addMessages($errorMessages);
79+
80+
return !$this->hasMessages();
81+
}
82+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Plugin\Model\Entity;
9+
10+
use Magento\Eav\Model\Entity\Attribute as EavEntityAttribute;
11+
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
12+
use Magento\Framework\Exception\LocalizedException;
13+
14+
class Attribute
15+
{
16+
/**
17+
* @var AttributeCodeValidator
18+
*/
19+
private $attributeCodeValidator;
20+
21+
/**
22+
* @param AttributeCodeValidator $attributeCodeValidator
23+
*/
24+
public function __construct(AttributeCodeValidator $attributeCodeValidator)
25+
{
26+
$this->attributeCodeValidator = $attributeCodeValidator;
27+
}
28+
29+
/**
30+
* @param EavEntityAttribute $subject
31+
* @throws \Zend_Validate_Exception
32+
* @throws LocalizedException
33+
*/
34+
public function beforeSave(EavEntityAttribute $subject)
35+
{
36+
$attributeCode = $subject->getData('attribute_code')
37+
?? $subject->getData('frontend_label')[0];
38+
39+
if (!$this->attributeCodeValidator->isValid($attributeCode)) {
40+
throw new LocalizedException(current($this->attributeCodeValidator->getMessages()));
41+
}
42+
}
43+
}

app/code/Magento/Eav/Setup/EavSetup.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Eav\Setup;
77

8+
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
89
use Magento\Eav\Model\Entity\Setup\Context;
910
use Magento\Eav\Model\Entity\Setup\PropertyMapperInterface;
1011
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
@@ -782,13 +783,18 @@ private function _validateAttributeData($data)
782783
);
783784

784785
if (!$isAllowedLength) {
785-
$errorMessage = __(
786+
throw new LocalizedException(__(
786787
'An attribute code must not be less than %1 and more than %2 characters.',
787788
$minLength,
788789
$maxLength
789-
);
790+
));
791+
}
790792

791-
throw new LocalizedException($errorMessage);
793+
if (strpos($attributeCode, AbstractModifier::CONTAINER_PREFIX) === 0) {
794+
throw new LocalizedException(__(
795+
'"%1" prefix is reserved by the system and cannot be used in attribute code names.',
796+
AbstractModifier::CONTAINER_PREFIX
797+
));
792798
}
793799

794800
return true;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Test\Unit\Model\Validator\Attribute;
9+
10+
use Magento\Eav\Model\Validator\Attribute\Code;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class CodeTest extends TestCase
14+
{
15+
/**
16+
* Testing \Magento\Eav\Model\Validator\Attribute\Code::isValid
17+
*
18+
* @dataProvider isValidDataProvider
19+
* @param string $attributeCode
20+
* @param bool $expected
21+
* @throws \Zend_Validate_Exception
22+
*/
23+
public function testIsValid(string $attributeCode, bool $expected)
24+
{
25+
$validator = new Code();
26+
$this->assertEquals($expected, $validator->isValid($attributeCode));
27+
}
28+
29+
/**
30+
* Data provider for testIsValid
31+
*
32+
* @return array
33+
*/
34+
public function isValidDataProvider(): array
35+
{
36+
return [
37+
[
38+
'Attribute_code',
39+
true
40+
], [
41+
'attribute_1',
42+
true
43+
],[
44+
'Attribute_1',
45+
true
46+
], [
47+
'_attribute_code',
48+
false
49+
], [
50+
'attribute.code',
51+
false
52+
], [
53+
'1attribute_code',
54+
false
55+
], [
56+
'more_than_60_chars_more_than_60_chars_more_than_60_chars_more',
57+
false
58+
], [
59+
'container_attribute',
60+
false,
61+
],
62+
];
63+
}
64+
}

app/code/Magento/Eav/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</arguments>
5454
</type>
5555
<type name="Magento\Eav\Model\Entity\Attribute">
56+
<plugin name="eav_code_validator" type="Magento\Eav\Plugin\Model\Entity\Attribute" />
5657
<arguments>
5758
<argument name="reservedAttributeList" xsi:type="object">Magento\Catalog\Model\Product\ReservedAttributeList\Proxy</argument>
5859
</arguments>

0 commit comments

Comments
 (0)