Skip to content

Commit 79cb787

Browse files
committed
MC-19398: Changing Attribute Set while editing disabled Product makes it enabled
- Fixing static issues and Unit tests
1 parent 54244f7 commit 79cb787

File tree

2 files changed

+94
-38
lines changed
  • app/code/Magento/Catalog

2 files changed

+94
-38
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/GeneralTest.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function setUp()
3333
parent::setUp();
3434

3535
$this->attributeRepositoryMock = $this->getMockBuilder(AttributeRepositoryInterface::class)
36-
->getMockForAbstractClass();
36+
->getMockForAbstractClass();
3737

3838
$arrayManager = $this->objectManager->getObject(ArrayManager::class);
3939

@@ -52,35 +52,42 @@ protected function setUp()
5252
*/
5353
protected function createModel()
5454
{
55-
return $this->objectManager->getObject(General::class, [
55+
return $this->objectManager->getObject(
56+
General::class,
57+
[
5658
'locator' => $this->locatorMock,
5759
'arrayManager' => $this->arrayManagerMock,
58-
]);
60+
]
61+
);
5962
}
6063

6164
public function testModifyMeta()
6265
{
6366
$this->arrayManagerMock->expects($this->any())
6467
->method('merge')
6568
->willReturnArgument(2);
66-
$this->assertNotEmpty($this->getModel()->modifyMeta([
67-
'first_panel_code' => [
69+
$this->assertNotEmpty(
70+
$this->getModel()->modifyMeta(
71+
[
72+
'first_panel_code' => [
6873
'arguments' => [
6974
'data' => [
7075
'config' => [
7176
'label' => 'Test label',
7277
]
7378
],
7479
]
75-
]
76-
]));
80+
]
81+
]
82+
)
83+
);
7784
}
7885

7986
/**
80-
* @param array $data
81-
* @param int $defaultStatusValue
82-
* @param array $expectedResult
83-
* @throws \Magento\Framework\Exception\NoSuchEntityException
87+
* @param array $data
88+
* @param int $defaultStatusValue
89+
* @param array $expectedResult
90+
* @throws \Magento\Framework\Exception\NoSuchEntityException
8491
* @dataProvider modifyDataDataProvider
8592
*/
8693
public function testModifyDataNewProduct(array $data, int $defaultStatusValue, array $expectedResult)
@@ -100,6 +107,52 @@ public function testModifyDataNewProduct(array $data, int $defaultStatusValue, a
100107
$this->assertSame($expectedResult, $this->generalModifier->modifyData($data));
101108
}
102109

110+
/**
111+
* Verify the product attribute status set owhen editing existing product
112+
*
113+
* @throws \Magento\Framework\Exception\NoSuchEntityException
114+
*/
115+
public function testModifyDataExistingProduct()
116+
{
117+
$data = [];
118+
$modelId = 1;
119+
$defaultStatusValue = 1;
120+
$expectedResult = [
121+
'enabledProductStatus' => [
122+
General::DATA_SOURCE_DEFAULT => [
123+
ProductAttributeInterface::CODE_STATUS => 1,
124+
],
125+
],
126+
'disabledProductStatus' => [
127+
General::DATA_SOURCE_DEFAULT => [
128+
ProductAttributeInterface::CODE_STATUS => 2,
129+
],
130+
],
131+
];
132+
$enabledProductStatus = 1;
133+
$disabledProductStatus = 2;
134+
$attributeMock = $this->getMockBuilder(AttributeInterface::class)
135+
->getMockForAbstractClass();
136+
$attributeMock
137+
->method('getDefaultValue')
138+
->willReturn($defaultStatusValue);
139+
$this->attributeRepositoryMock
140+
->method('get')
141+
->with(
142+
ProductAttributeInterface::ENTITY_TYPE_CODE,
143+
ProductAttributeInterface::CODE_STATUS
144+
)
145+
->willReturn($attributeMock);
146+
$this->productMock->expects($this->any())
147+
->method('getId')
148+
->willReturn($modelId);
149+
$this->productMock->expects($this->any())
150+
->method('getStatus')
151+
->willReturnOnConsecutiveCalls($enabledProductStatus, $disabledProductStatus);
152+
$this->assertSame($expectedResult['enabledProductStatus'], current($this->generalModifier->modifyData($data)));
153+
$this->assertSame($expectedResult['disabledProductStatus'], current($this->generalModifier->modifyData($data)));
154+
}
155+
103156
/**
104157
* @return array
105158
*/

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/General.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
class General extends AbstractModifier
2222
{
2323
/**
24-
* @var LocatorInterface
24+
* @var LocatorInterface
2525
* @since 101.0.0
2626
*/
2727
protected $locator;
2828

2929
/**
30-
* @var ArrayManager
30+
* @var ArrayManager
3131
* @since 101.0.0
3232
*/
3333
protected $arrayManager;
@@ -43,8 +43,8 @@ class General extends AbstractModifier
4343
private $attributeRepository;
4444

4545
/**
46-
* @param LocatorInterface $locator
47-
* @param ArrayManager $arrayManager
46+
* @param LocatorInterface $locator
47+
* @param ArrayManager $arrayManager
4848
* @param AttributeRepositoryInterface|null $attributeRepository
4949
*/
5050
public function __construct(
@@ -61,18 +61,21 @@ public function __construct(
6161
/**
6262
* Customize number fields for advanced price and weight fields.
6363
*
64-
* @param array $data
64+
* @param array $data
6565
* @return array
6666
* @throws \Magento\Framework\Exception\NoSuchEntityException
67-
* @since 101.0.0
67+
* @since 101.0.0
6868
*/
6969
public function modifyData(array $data)
7070
{
7171
$data = $this->customizeWeightFormat($data);
7272
$data = $this->customizeAdvancedPriceFormat($data);
7373
$modelId = $this->locator->getProduct()->getId();
7474

75-
if (!isset($data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS])) {
75+
$productStatus = $this->locator->getProduct()->getStatus();
76+
if ((isset($productStatus) && !empty($productStatus)) && (isset($modelId)) && !empty($modelId)) {
77+
$data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS] = $productStatus;
78+
} elseif (!isset($data[$modelId][static::DATA_SOURCE_DEFAULT][ProductAttributeInterface::CODE_STATUS])) {
7679
$attributeStatus = $this->attributeRepository->get(
7780
ProductAttributeInterface::ENTITY_TYPE_CODE,
7881
ProductAttributeInterface::CODE_STATUS
@@ -87,9 +90,9 @@ public function modifyData(array $data)
8790
/**
8891
* Customizing weight fields
8992
*
90-
* @param array $data
93+
* @param array $data
9194
* @return array
92-
* @since 101.0.0
95+
* @since 101.0.0
9396
*/
9497
protected function customizeWeightFormat(array $data)
9598
{
@@ -112,9 +115,9 @@ protected function customizeWeightFormat(array $data)
112115
/**
113116
* Customizing number fields for advanced price
114117
*
115-
* @param array $data
118+
* @param array $data
116119
* @return array
117-
* @since 101.0.0
120+
* @since 101.0.0
118121
*/
119122
protected function customizeAdvancedPriceFormat(array $data)
120123
{
@@ -136,9 +139,9 @@ protected function customizeAdvancedPriceFormat(array $data)
136139
/**
137140
* Customize product form fields.
138141
*
139-
* @param array $meta
142+
* @param array $meta
140143
* @return array
141-
* @since 101.0.0
144+
* @since 101.0.0
142145
*/
143146
public function modifyMeta(array $meta)
144147
{
@@ -154,9 +157,9 @@ public function modifyMeta(array $meta)
154157
/**
155158
* Disable collapsible and set empty label
156159
*
157-
* @param array $meta
160+
* @param array $meta
158161
* @return array
159-
* @since 101.0.0
162+
* @since 101.0.0
160163
*/
161164
protected function prepareFirstPanel(array $meta)
162165
{
@@ -177,9 +180,9 @@ protected function prepareFirstPanel(array $meta)
177180
/**
178181
* Customize Status field
179182
*
180-
* @param array $meta
183+
* @param array $meta
181184
* @return array
182-
* @since 101.0.0
185+
* @since 101.0.0
183186
*/
184187
protected function customizeStatusField(array $meta)
185188
{
@@ -203,9 +206,9 @@ protected function customizeStatusField(array $meta)
203206
/**
204207
* Customize Weight filed
205208
*
206-
* @param array $meta
209+
* @param array $meta
207210
* @return array
208-
* @since 101.0.0
211+
* @since 101.0.0
209212
*/
210213
protected function customizeWeightField(array $meta)
211214
{
@@ -277,9 +280,9 @@ protected function customizeWeightField(array $meta)
277280
/**
278281
* Customize "Set Product as New" date fields
279282
*
280-
* @param array $meta
283+
* @param array $meta
281284
* @return array
282-
* @since 101.0.0
285+
* @since 101.0.0
283286
*/
284287
protected function customizeNewDateRangeField(array $meta)
285288
{
@@ -335,9 +338,9 @@ protected function customizeNewDateRangeField(array $meta)
335338
/**
336339
* Add links for fields depends of product name
337340
*
338-
* @param array $meta
341+
* @param array $meta
339342
* @return array
340-
* @since 101.0.0
343+
* @since 101.0.0
341344
*/
342345
protected function customizeNameListeners(array $meta)
343346
{
@@ -409,9 +412,9 @@ private function getLocaleCurrency()
409412
/**
410413
* Format price according to the locale of the currency
411414
*
412-
* @param mixed $value
415+
* @param mixed $value
413416
* @return string
414-
* @since 101.0.0
417+
* @since 101.0.0
415418
*/
416419
protected function formatPrice($value)
417420
{
@@ -429,9 +432,9 @@ protected function formatPrice($value)
429432
/**
430433
* Format number according to the locale of the currency and precision of input
431434
*
432-
* @param mixed $value
435+
* @param mixed $value
433436
* @return string
434-
* @since 101.0.0
437+
* @since 101.0.0
435438
*/
436439
protected function formatNumber($value)
437440
{

0 commit comments

Comments
 (0)