Skip to content

Commit 0621dfd

Browse files
Merge remote-tracking branch 'origin/MC-35608' into 2.4-develop-pr36
2 parents be63f6b + 5420c8d commit 0621dfd

File tree

2 files changed

+108
-27
lines changed

2 files changed

+108
-27
lines changed

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Controller\Adminhtml\Product\Attribute;
89

10+
use Magento\Backend\App\Action\Context;
911
use Magento\Catalog\Controller\Adminhtml\Product\Attribute as AttributeAction;
12+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
13+
use Magento\Eav\Model\Entity\Attribute\Set;
1014
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
1115
use Magento\Framework\App\Action\HttpGetActionInterface;
1216
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1317
use Magento\Framework\App\ObjectManager;
18+
use Magento\Framework\Cache\FrontendInterface;
19+
use Magento\Framework\Controller\Result\JsonFactory;
20+
use Magento\Framework\Controller\ResultInterface;
1421
use Magento\Framework\DataObject;
1522
use Magento\Framework\Escaper;
23+
use Magento\Framework\Registry;
1624
use Magento\Framework\Serialize\Serializer\FormData;
25+
use Magento\Framework\View\LayoutFactory;
26+
use Magento\Framework\View\Result\PageFactory;
1727

1828
/**
1929
* Product attribute validate controller.
@@ -25,12 +35,12 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
2535
const DEFAULT_MESSAGE_KEY = 'message';
2636

2737
/**
28-
* @var \Magento\Framework\Controller\Result\JsonFactory
38+
* @var JsonFactory
2939
*/
3040
protected $resultJsonFactory;
3141

3242
/**
33-
* @var \Magento\Framework\View\LayoutFactory
43+
* @var LayoutFactory
3444
*/
3545
protected $layoutFactory;
3646

@@ -57,25 +67,25 @@ class Validate extends AttributeAction implements HttpGetActionInterface, HttpPo
5767
/**
5868
* Constructor
5969
*
60-
* @param \Magento\Backend\App\Action\Context $context
61-
* @param \Magento\Framework\Cache\FrontendInterface $attributeLabelCache
62-
* @param \Magento\Framework\Registry $coreRegistry
63-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
64-
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
65-
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
70+
* @param Context $context
71+
* @param FrontendInterface $attributeLabelCache
72+
* @param Registry $coreRegistry
73+
* @param PageFactory $resultPageFactory
74+
* @param JsonFactory $resultJsonFactory
75+
* @param LayoutFactory $layoutFactory
6676
* @param array $multipleAttributeList
6777
* @param FormData|null $formDataSerializer
6878
* @param AttributeCodeValidator|null $attributeCodeValidator
6979
* @param Escaper $escaper
7080
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7181
*/
7282
public function __construct(
73-
\Magento\Backend\App\Action\Context $context,
74-
\Magento\Framework\Cache\FrontendInterface $attributeLabelCache,
75-
\Magento\Framework\Registry $coreRegistry,
76-
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
77-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
78-
\Magento\Framework\View\LayoutFactory $layoutFactory,
83+
Context $context,
84+
FrontendInterface $attributeLabelCache,
85+
Registry $coreRegistry,
86+
PageFactory $resultPageFactory,
87+
JsonFactory $resultJsonFactory,
88+
LayoutFactory $layoutFactory,
7989
array $multipleAttributeList = [],
8090
FormData $formDataSerializer = null,
8191
AttributeCodeValidator $attributeCodeValidator = null,
@@ -96,7 +106,7 @@ public function __construct(
96106
/**
97107
* @inheritdoc
98108
*
99-
* @return \Magento\Framework\Controller\ResultInterface
109+
* @return ResultInterface
100110
* @SuppressWarnings(PHPMD.NPathComplexity)
101111
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
102112
*/
@@ -118,14 +128,22 @@ public function execute()
118128

119129
$attributeCode = $this->getRequest()->getParam('attribute_code');
120130
$frontendLabel = $this->getRequest()->getParam('frontend_label');
121-
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
122131
$attributeId = $this->getRequest()->getParam('attribute_id');
123-
$attribute = $this->_objectManager->create(
124-
\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
125-
)->loadByCode(
126-
$this->_entityTypeId,
127-
$attributeCode
128-
);
132+
133+
if ($attributeId) {
134+
$attribute = $this->_objectManager->create(
135+
Attribute::class
136+
)->load($attributeId);
137+
$attributeCode = $attribute->getAttributeCode();
138+
} else {
139+
$attributeCode = $attributeCode ?: $this->generateCode($frontendLabel[0]);
140+
$attribute = $this->_objectManager->create(
141+
Attribute::class
142+
)->loadByCode(
143+
$this->_entityTypeId,
144+
$attributeCode
145+
);
146+
}
129147

130148
if ($attribute->getId() && !$attributeId || $attributeCode === 'product_type' || $attributeCode === 'type_id') {
131149
$message = strlen($this->getRequest()->getParam('attribute_code'))
@@ -145,8 +163,8 @@ public function execute()
145163

146164
if ($this->getRequest()->has('new_attribute_set_name')) {
147165
$setName = $this->getRequest()->getParam('new_attribute_set_name');
148-
/** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */
149-
$attributeSet = $this->_objectManager->create(\Magento\Eav\Model\Entity\Attribute\Set::class);
166+
/** @var $attributeSet Set */
167+
$attributeSet = $this->_objectManager->create(Set::class);
150168
$attributeSet->setEntityTypeId($this->_entityTypeId)->load($setName, 'attribute_set_name');
151169
if ($attributeSet->getId()) {
152170
$setName = $this->escaper->escapeHtml($setName);
@@ -252,7 +270,7 @@ private function checkUniqueOption(DataObject $response, array $options = null)
252270
private function checkEmptyOption(DataObject $response, array $optionsForCheck = null)
253271
{
254272
foreach ($optionsForCheck as $optionValues) {
255-
if (isset($optionValues[0]) && trim($optionValues[0]) == '') {
273+
if (isset($optionValues[0]) && trim((string)$optionValues[0]) == '') {
256274
$this->setMessageToResponse($response, [__("The value of Admin scope can't be empty.")]);
257275
$response->setError(true);
258276
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Attribute/ValidateTest.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testExecute()
153153
->willReturnMap(
154154
[
155155
[Attribute::class, [], $this->attributeMock],
156-
[\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock]
156+
[AttributeSet::class, [], $this->attributeSetMock]
157157
]
158158
);
159159
$this->attributeMock->expects($this->once())
@@ -188,6 +188,69 @@ public function testExecute()
188188
$this->assertInstanceOf(ResultJson::class, $this->getModel()->execute());
189189
}
190190

191+
/**
192+
* Test that editing existing attribute loads attribute by id
193+
*
194+
* @return void
195+
* @throws NotFoundException
196+
*/
197+
public function testExecuteEditExisting(): void
198+
{
199+
$serializedOptions = '{"key":"value"}';
200+
$this->requestMock->expects($this->any())
201+
->method('getParam')
202+
->willReturnMap(
203+
[
204+
['frontend_label', null, 'test_frontend_label'],
205+
['attribute_id', null, 10],
206+
['attribute_code', null, 'test_attribute_code'],
207+
['new_attribute_set_name', null, 'test_attribute_set_name'],
208+
['serialized_options', '[]', $serializedOptions],
209+
]
210+
);
211+
$this->objectManagerMock->expects($this->exactly(2))
212+
->method('create')
213+
->willReturnMap(
214+
[
215+
[Attribute::class, [], $this->attributeMock],
216+
[AttributeSet::class, [], $this->attributeSetMock]
217+
]
218+
);
219+
$this->attributeMock->expects($this->once())
220+
->method('load')
221+
->willReturnSelf();
222+
$this->attributeMock->expects($this->once())
223+
->method('getAttributeCode')
224+
->willReturn('test_attribute_code');
225+
226+
$this->attributeCodeValidatorMock->expects($this->once())
227+
->method('isValid')
228+
->with('test_attribute_code')
229+
->willReturn(true);
230+
231+
$this->requestMock->expects($this->once())
232+
->method('has')
233+
->with('new_attribute_set_name')
234+
->willReturn(true);
235+
$this->attributeSetMock->expects($this->once())
236+
->method('setEntityTypeId')
237+
->willReturnSelf();
238+
$this->attributeSetMock->expects($this->once())
239+
->method('load')
240+
->willReturnSelf();
241+
$this->attributeSetMock->expects($this->once())
242+
->method('getId')
243+
->willReturn(false);
244+
$this->resultJsonFactoryMock->expects($this->once())
245+
->method('create')
246+
->willReturn($this->resultJson);
247+
$this->resultJson->expects($this->once())
248+
->method('setJsonData')
249+
->willReturnSelf();
250+
251+
$this->assertInstanceOf(ResultJson::class, $this->getModel()->execute());
252+
}
253+
191254
/**
192255
* @dataProvider provideUniqueData
193256
* @param array $options
@@ -605,7 +668,7 @@ public function testExecuteWithOptionsDataError()
605668
->willReturnMap(
606669
[
607670
[Attribute::class, [], $this->attributeMock],
608-
[\Magento\Eav\Model\Entity\Attribute\Set::class, [], $this->attributeSetMock]
671+
[AttributeSet::class, [], $this->attributeSetMock]
609672
]
610673
);
611674

0 commit comments

Comments
 (0)