Skip to content

Commit 24d21b6

Browse files
author
Lysenko Olexandr
authored
Merge pull request #2822 from magento-chaika/chaika_pr
[Chaika] Bugfixes
2 parents b2a65ae + 5d6f5d4 commit 24d21b6

File tree

18 files changed

+438
-159
lines changed

18 files changed

+438
-159
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function imagePreprocessing($data)
279279
continue;
280280
}
281281

282-
$data[$attributeCode] = false;
282+
$data[$attributeCode] = '';
283283
}
284284

285285
return $data;

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category;
77

8-
use Magento\Catalog\Controller\Adminhtml\Category\Save as Model;
9-
108
/**
119
* Class SaveTest
1210
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -463,18 +461,35 @@ public function dataProviderExecute()
463461
*/
464462
public function imagePreprocessingDataProvider()
465463
{
464+
$dataWithImage = [
465+
'image' => 'path.jpg',
466+
'name' => 'category',
467+
'description' => '',
468+
'parent' => 0
469+
];
470+
$expectedSameAsDataWithImage = $dataWithImage;
471+
472+
$dataWithoutImage = [
473+
'name' => 'category',
474+
'description' => '',
475+
'parent' => 0
476+
];
477+
$expectedIfDataWithoutImage = $dataWithoutImage;
478+
$expectedIfDataWithoutImage['image'] = '';
479+
466480
return [
467-
[['attribute1' => null, 'attribute2' => 123]],
468-
[['attribute2' => 123]]
481+
'categoryPostData contains image' => [$dataWithImage, $expectedSameAsDataWithImage],
482+
'categoryPostData doesn\'t contain image' => [$dataWithoutImage, $expectedIfDataWithoutImage],
469483
];
470484
}
471485

472486
/**
473487
* @dataProvider imagePreprocessingDataProvider
474488
*
475489
* @param array $data
490+
* @param array $expected
476491
*/
477-
public function testImagePreprocessingWithoutValue($data)
492+
public function testImagePreprocessing($data, $expected)
478493
{
479494
$eavConfig = $this->createPartialMock(\Magento\Eav\Model\Config::class, ['getEntityType']);
480495

@@ -484,68 +499,30 @@ public function testImagePreprocessingWithoutValue($data)
484499

485500
$collection = new \Magento\Framework\DataObject(['attribute_collection' => [
486501
new \Magento\Framework\DataObject([
487-
'attribute_code' => 'attribute1',
502+
'attribute_code' => 'image',
488503
'backend' => $imageBackendModel
489504
]),
490505
new \Magento\Framework\DataObject([
491-
'attribute_code' => 'attribute2',
506+
'attribute_code' => 'name',
492507
'backend' => new \Magento\Framework\DataObject()
493-
])
494-
]]);
495-
496-
$eavConfig->expects($this->once())
497-
->method('getEntityType')
498-
->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
499-
->will($this->returnValue($collection));
500-
501-
$model = $this->objectManager->getObject(\Magento\Catalog\Controller\Adminhtml\Category\Save::class, [
502-
'eavConfig' => $eavConfig
503-
]);
504-
505-
$result = $model->imagePreprocessing($data);
506-
507-
$this->assertEquals([
508-
'attribute1' => false,
509-
'attribute2' => 123
510-
], $result);
511-
}
512-
513-
public function testImagePreprocessingWithValue()
514-
{
515-
$eavConfig = $this->createPartialMock(\Magento\Eav\Model\Config::class, ['getEntityType']);
516-
517-
$imageBackendModel = $this->objectManager->getObject(
518-
\Magento\Catalog\Model\Category\Attribute\Backend\Image::class
519-
);
520-
521-
$collection = new \Magento\Framework\DataObject(['attribute_collection' => [
522-
new \Magento\Framework\DataObject([
523-
'attribute_code' => 'attribute1',
524-
'backend' => $imageBackendModel
525508
]),
526509
new \Magento\Framework\DataObject([
527-
'attribute_code' => 'attribute2',
510+
'attribute_code' => 'level',
528511
'backend' => new \Magento\Framework\DataObject()
529-
])
512+
]),
530513
]]);
531514

532515
$eavConfig->expects($this->once())
533516
->method('getEntityType')
534517
->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
535518
->will($this->returnValue($collection));
536519

537-
$model = $this->objectManager->getObject(Model::class, [
520+
$model = $this->objectManager->getObject(\Magento\Catalog\Controller\Adminhtml\Category\Save::class, [
538521
'eavConfig' => $eavConfig
539522
]);
540523

541-
$result = $model->imagePreprocessing([
542-
'attribute1' => 'somevalue',
543-
'attribute2' => null
544-
]);
524+
$result = $model->imagePreprocessing($data);
545525

546-
$this->assertEquals([
547-
'attribute1' => 'somevalue',
548-
'attribute2' => null
549-
], $result);
526+
$this->assertEquals($expected, $result);
550527
}
551528
}

app/code/Magento/Customer/Controller/Address/FormPost.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,17 @@ public function execute()
197197
try {
198198
$address = $this->_extractAddress();
199199
$this->_addressRepository->save($address);
200-
$this->messageManager->addSuccess(__('You saved the address.'));
200+
$this->messageManager->addSuccessMessage(__('You saved the address.'));
201201
$url = $this->_buildUrl('*/*/index', ['_secure' => true]);
202202
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
203203
} catch (InputException $e) {
204-
$this->messageManager->addError($e->getMessage());
204+
$this->messageManager->addErrorMessage($e->getMessage());
205205
foreach ($e->getErrors() as $error) {
206-
$this->messageManager->addError($error->getMessage());
206+
$this->messageManager->addErrorMessage($error->getMessage());
207207
}
208208
} catch (\Exception $e) {
209209
$redirectUrl = $this->_buildUrl('*/*/index');
210-
$this->messageManager->addException($e, __('We can\'t save the address.'));
210+
$this->messageManager->addExceptionMessage($e, __('We can\'t save the address.'));
211211
}
212212

213213
$url = $redirectUrl;

app/code/Magento/Customer/Model/Metadata/Form/Text.php

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Customer\Model\Metadata\Form;
910

11+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
1012
use Magento\Framework\Api\ArrayObjectSearch;
1113

1214
class Text extends AbstractData
@@ -19,7 +21,7 @@ class Text extends AbstractData
1921
/**
2022
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
2123
* @param \Psr\Log\LoggerInterface $logger
22-
* @param \Magento\Customer\Api\Data\AttributeMetadataInterface $attribute
24+
* @param AttributeMetadataInterface $attribute
2325
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
2426
* @param string $value
2527
* @param string $entityTypeCode
@@ -29,7 +31,7 @@ class Text extends AbstractData
2931
public function __construct(
3032
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
3133
\Psr\Log\LoggerInterface $logger,
32-
\Magento\Customer\Api\Data\AttributeMetadataInterface $attribute,
34+
AttributeMetadataInterface $attribute,
3335
\Magento\Framework\Locale\ResolverInterface $localeResolver,
3436
$value,
3537
$entityTypeCode,
@@ -72,26 +74,7 @@ public function validateValue($value)
7274
return true;
7375
}
7476

75-
// validate length
76-
$length = $this->_string->strlen(trim($value));
77-
78-
$validateRules = $attribute->getValidationRules();
79-
80-
$minTextLength = ArrayObjectSearch::getArrayElementByName(
81-
$validateRules,
82-
'min_text_length'
83-
);
84-
if ($minTextLength !== null && $length < $minTextLength) {
85-
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
86-
}
87-
88-
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
89-
$validateRules,
90-
'max_text_length'
91-
);
92-
if ($maxTextLength !== null && $length > $maxTextLength) {
93-
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
94-
}
77+
$errors = $this->validateLength($value, $attribute, $errors);
9578

9679
$result = $this->_validateInputRule($value);
9780
if ($result !== true) {
@@ -127,4 +110,42 @@ public function outputValue($format = \Magento\Customer\Model\Metadata\ElementFa
127110
{
128111
return $this->_applyOutputFilter($this->_value);
129112
}
113+
114+
/**
115+
* Length validation
116+
*
117+
* @param mixed $value
118+
* @param AttributeMetadataInterface $attribute
119+
* @param array $errors
120+
* @return array
121+
*/
122+
protected function validateLength($value, AttributeMetadataInterface $attribute, array $errors): array
123+
{
124+
// validate length
125+
$label = __($attribute->getStoreLabel());
126+
127+
$length = $this->_string->strlen(trim($value));
128+
129+
$validateRules = $attribute->getValidationRules();
130+
131+
if (!empty(ArrayObjectSearch::getArrayElementByName($validateRules, 'input_validation'))) {
132+
$minTextLength = ArrayObjectSearch::getArrayElementByName(
133+
$validateRules,
134+
'min_text_length'
135+
);
136+
if ($minTextLength !== null && $length < $minTextLength) {
137+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $minTextLength);
138+
}
139+
140+
$maxTextLength = ArrayObjectSearch::getArrayElementByName(
141+
$validateRules,
142+
'max_text_length'
143+
);
144+
if ($maxTextLength !== null && $length > $maxTextLength) {
145+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $maxTextLength);
146+
}
147+
}
148+
149+
return $errors;
150+
}
130151
}

app/code/Magento/Customer/Test/Unit/Controller/Address/FormPostTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public function testExecute(
549549
->willReturnSelf();
550550

551551
$this->messageManager->expects($this->once())
552-
->method('addSuccess')
552+
->method('addSuccessMessage')
553553
->with(__('You saved the address.'))
554554
->willReturnSelf();
555555

@@ -640,7 +640,7 @@ public function testExecuteInputException()
640640
->willThrowException(new InputException(__('InputException')));
641641

642642
$this->messageManager->expects($this->once())
643-
->method('addError')
643+
->method('addErrorMessage')
644644
->with('InputException')
645645
->willReturnSelf();
646646

@@ -703,7 +703,7 @@ public function testExecuteException()
703703
->willThrowException($exception);
704704

705705
$this->messageManager->expects($this->once())
706-
->method('addException')
706+
->method('addExceptionMessage')
707707
->with($exception, __('We can\'t save the address.'))
708708
->willReturnSelf();
709709

app/code/Magento/Customer/Test/Unit/Model/Metadata/Form/TextTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Copyright © Magento, Inc. All rights reserved.
66
* See COPYING.txt for license details.
77
*/
8+
89
namespace Magento\Customer\Test\Unit\Model\Metadata\Form;
910

11+
use Magento\Customer\Api\Data\ValidationRuleInterface;
1012
use Magento\Customer\Model\Metadata\Form\Text;
1113

1214
class TextTest extends AbstractFormTestCase
@@ -111,7 +113,7 @@ public function validateValueRequiredDataProvider()
111113
*/
112114
public function testValidateValueLength($value, $expected)
113115
{
114-
$minTextLengthRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
116+
$minTextLengthRule = $this->getMockBuilder(ValidationRuleInterface::class)
115117
->disableOriginalConstructor()
116118
->setMethods(['getName', 'getValue'])
117119
->getMockForAbstractClass();
@@ -122,7 +124,7 @@ public function testValidateValueLength($value, $expected)
122124
->method('getValue')
123125
->will($this->returnValue(4));
124126

125-
$maxTextLengthRule = $this->getMockBuilder(\Magento\Customer\Api\Data\ValidationRuleInterface::class)
127+
$maxTextLengthRule = $this->getMockBuilder(ValidationRuleInterface::class)
126128
->disableOriginalConstructor()
127129
->setMethods(['getName', 'getValue'])
128130
->getMockForAbstractClass();
@@ -133,7 +135,19 @@ public function testValidateValueLength($value, $expected)
133135
->method('getValue')
134136
->will($this->returnValue(8));
135137

138+
$inputValidationRule = $this->getMockBuilder(ValidationRuleInterface::class)
139+
->disableOriginalConstructor()
140+
->setMethods(['getName', 'getValue'])
141+
->getMockForAbstractClass();
142+
$inputValidationRule->expects($this->any())
143+
->method('getName')
144+
->will($this->returnValue('input_validation'));
145+
$inputValidationRule->expects($this->any())
146+
->method('getValue')
147+
->will($this->returnValue('other'));
148+
136149
$validationRules = [
150+
'input_validation' => $inputValidationRule,
137151
'min_text_length' => $minTextLengthRule,
138152
'max_text_length' => $maxTextLengthRule,
139153
];

app/code/Magento/Eav/Model/Attribute/Data/Text.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Eav\Model\Attribute\Data;
78

89
use Magento\Framework\App\RequestInterface;
@@ -76,19 +77,9 @@ public function validateValue($value)
7677
return true;
7778
}
7879

79-
// validate length
80-
$length = $this->_string->strlen(trim($value));
81-
82-
$validateRules = $attribute->getValidateRules();
83-
if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
84-
$label = __($attribute->getStoreLabel());
85-
$v = $validateRules['min_text_length'];
86-
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
87-
}
88-
if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
89-
$label = __($attribute->getStoreLabel());
90-
$v = $validateRules['max_text_length'];
91-
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
80+
$result = $this->validateLength($attribute, $value);
81+
if (count($result) !== 0) {
82+
$errors = array_merge($errors, $result);
9283
}
9384

9485
$result = $this->_validateInputRule($value);
@@ -142,4 +133,33 @@ public function outputValue($format = \Magento\Eav\Model\AttributeDataFactory::O
142133

143134
return $value;
144135
}
136+
137+
/**
138+
* Validates value length by attribute rules
139+
*
140+
* @param \Magento\Eav\Model\Attribute $attribute
141+
* @param string $value
142+
* @return array errors
143+
*/
144+
private function validateLength(\Magento\Eav\Model\Attribute $attribute, $value): array
145+
{
146+
$errors = [];
147+
$length = $this->_string->strlen(trim($value));
148+
$validateRules = $attribute->getValidateRules();
149+
150+
if (!empty($validateRules['input_validation'])) {
151+
if (!empty($validateRules['min_text_length']) && $length < $validateRules['min_text_length']) {
152+
$label = __($attribute->getStoreLabel());
153+
$v = $validateRules['min_text_length'];
154+
$errors[] = __('"%1" length must be equal or greater than %2 characters.', $label, $v);
155+
}
156+
if (!empty($validateRules['max_text_length']) && $length > $validateRules['max_text_length']) {
157+
$label = __($attribute->getStoreLabel());
158+
$v = $validateRules['max_text_length'];
159+
$errors[] = __('"%1" length must be equal or less than %2 characters.', $label, $v);
160+
}
161+
}
162+
163+
return $errors;
164+
}
145165
}

0 commit comments

Comments
 (0)